blob: f06975d3a2f516fbb0e09abf6f5524d225d1b7dd [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 :
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200469 * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100470 */
471};
472
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100473const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100474 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
475 [HTTP_METH_GET] = { "GET", 3 },
476 [HTTP_METH_HEAD] = { "HEAD", 4 },
477 [HTTP_METH_POST] = { "POST", 4 },
478 [HTTP_METH_PUT] = { "PUT", 3 },
479 [HTTP_METH_DELETE] = { "DELETE", 6 },
480 [HTTP_METH_TRACE] = { "TRACE", 5 },
481 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
482};
483
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100484/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200485 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100486 * RFC2616 for those chars.
487 */
488
489const char http_is_spht[256] = {
490 [' '] = 1, ['\t'] = 1,
491};
492
493const char http_is_crlf[256] = {
494 ['\r'] = 1, ['\n'] = 1,
495};
496
497const char http_is_lws[256] = {
498 [' '] = 1, ['\t'] = 1,
499 ['\r'] = 1, ['\n'] = 1,
500};
501
502const char http_is_sep[256] = {
503 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
504 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
505 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
506 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
507 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
508};
509
510const char http_is_ctl[256] = {
511 [0 ... 31] = 1,
512 [127] = 1,
513};
514
515/*
516 * A token is any ASCII char that is neither a separator nor a CTL char.
517 * Do not overwrite values in assignment since gcc-2.95 will not handle
518 * them correctly. Instead, define every non-CTL char's status.
519 */
520const char http_is_token[256] = {
521 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
522 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
523 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
524 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
525 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
526 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
527 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
528 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
529 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
530 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
531 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
532 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
533 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
534 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
535 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
536 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
537 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
538 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
539 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
540 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
541 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
542 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
543 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
544 ['|'] = 1, ['}'] = 0, ['~'] = 1,
545};
546
547
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100548/*
549 * An http ver_token is any ASCII which can be found in an HTTP version,
550 * which includes 'H', 'T', 'P', '/', '.' and any digit.
551 */
552const char http_is_ver_token[256] = {
553 ['.'] = 1, ['/'] = 1,
554 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
555 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
Thierry FOURNIER63d692c2015-02-28 19:03:56 +0100556 ['H'] = 1, ['P'] = 1, ['R'] = 1, ['S'] = 1, ['T'] = 1,
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100557};
558
559
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100560/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100561 * Adds a header and its CRLF at the tail of the message's buffer, just before
562 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100563 * The header is also automatically added to the index <hdr_idx>, and the end
564 * of headers is automatically adjusted. The number of bytes added is returned
565 * on success, otherwise <0 is returned indicating an error.
566 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100567int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100568{
569 int bytes, len;
570
571 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200572 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100573 if (!bytes)
574 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100575 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100576 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
577}
578
579/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100580 * Adds a header and its CRLF at the tail of the message's buffer, just before
581 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100582 * the buffer is only opened and the space reserved, but nothing is copied.
583 * The header is also automatically added to the index <hdr_idx>, and the end
584 * of headers is automatically adjusted. The number of bytes added is returned
585 * on success, otherwise <0 is returned indicating an error.
586 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100587int http_header_add_tail2(struct http_msg *msg,
588 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100589{
590 int bytes;
591
Willy Tarreau9b28e032012-10-12 23:49:43 +0200592 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100593 if (!bytes)
594 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100595 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100596 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
597}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200598
599/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100600 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
601 * If so, returns the position of the first non-space character relative to
602 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
603 * to return a pointer to the place after the first space. Returns 0 if the
604 * header name does not match. Checks are case-insensitive.
605 */
606int http_header_match2(const char *hdr, const char *end,
607 const char *name, int len)
608{
609 const char *val;
610
611 if (hdr + len >= end)
612 return 0;
613 if (hdr[len] != ':')
614 return 0;
615 if (strncasecmp(hdr, name, len) != 0)
616 return 0;
617 val = hdr + len + 1;
618 while (val < end && HTTP_IS_SPHT(*val))
619 val++;
620 if ((val >= end) && (len + 2 <= end - hdr))
621 return len + 2; /* we may replace starting from second space */
622 return val - hdr;
623}
624
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200625/* Find the first or next occurrence of header <name> in message buffer <sol>
626 * using headers index <idx>, and return it in the <ctx> structure. This
627 * structure holds everything necessary to use the header and find next
628 * occurrence. If its <idx> member is 0, the header is searched from the
629 * beginning. Otherwise, the next occurrence is returned. The function returns
630 * 1 when it finds a value, and 0 when there is no more. It is very similar to
631 * http_find_header2() except that it is designed to work with full-line headers
632 * whose comma is not a delimiter but is part of the syntax. As a special case,
633 * if ctx->val is NULL when searching for a new values of a header, the current
634 * header is rescanned. This allows rescanning after a header deletion.
635 */
636int http_find_full_header2(const char *name, int len,
637 char *sol, struct hdr_idx *idx,
638 struct hdr_ctx *ctx)
639{
640 char *eol, *sov;
641 int cur_idx, old_idx;
642
643 cur_idx = ctx->idx;
644 if (cur_idx) {
645 /* We have previously returned a header, let's search another one */
646 sol = ctx->line;
647 eol = sol + idx->v[cur_idx].len;
648 goto next_hdr;
649 }
650
651 /* first request for this header */
652 sol += hdr_idx_first_pos(idx);
653 old_idx = 0;
654 cur_idx = hdr_idx_first_idx(idx);
655 while (cur_idx) {
656 eol = sol + idx->v[cur_idx].len;
657
658 if (len == 0) {
659 /* No argument was passed, we want any header.
660 * To achieve this, we simply build a fake request. */
661 while (sol + len < eol && sol[len] != ':')
662 len++;
663 name = sol;
664 }
665
666 if ((len < eol - sol) &&
667 (sol[len] == ':') &&
668 (strncasecmp(sol, name, len) == 0)) {
669 ctx->del = len;
670 sov = sol + len + 1;
671 while (sov < eol && http_is_lws[(unsigned char)*sov])
672 sov++;
673
674 ctx->line = sol;
675 ctx->prev = old_idx;
676 ctx->idx = cur_idx;
677 ctx->val = sov - sol;
678 ctx->tws = 0;
679 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
680 eol--;
681 ctx->tws++;
682 }
683 ctx->vlen = eol - sov;
684 return 1;
685 }
686 next_hdr:
687 sol = eol + idx->v[cur_idx].cr + 1;
688 old_idx = cur_idx;
689 cur_idx = idx->v[cur_idx].next;
690 }
691 return 0;
692}
693
Willy Tarreauc90dc232015-02-20 13:51:36 +0100694/* Find the first or next header field in message buffer <sol> using headers
695 * index <idx>, and return it in the <ctx> structure. This structure holds
696 * everything necessary to use the header and find next occurrence. If its
697 * <idx> member is 0, the first header is retrieved. Otherwise, the next
698 * occurrence is returned. The function returns 1 when it finds a value, and
699 * 0 when there is no more. It is equivalent to http_find_full_header2() with
700 * no header name.
701 */
702int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
703{
704 char *eol, *sov;
705 int cur_idx, old_idx;
706 int len;
707
708 cur_idx = ctx->idx;
709 if (cur_idx) {
710 /* We have previously returned a header, let's search another one */
711 sol = ctx->line;
712 eol = sol + idx->v[cur_idx].len;
713 goto next_hdr;
714 }
715
716 /* first request for this header */
717 sol += hdr_idx_first_pos(idx);
718 old_idx = 0;
719 cur_idx = hdr_idx_first_idx(idx);
720 while (cur_idx) {
721 eol = sol + idx->v[cur_idx].len;
722
723 len = 0;
724 while (1) {
725 if (len >= eol - sol)
726 goto next_hdr;
727 if (sol[len] == ':')
728 break;
729 len++;
730 }
731
732 ctx->del = len;
733 sov = sol + len + 1;
734 while (sov < eol && http_is_lws[(unsigned char)*sov])
735 sov++;
736
737 ctx->line = sol;
738 ctx->prev = old_idx;
739 ctx->idx = cur_idx;
740 ctx->val = sov - sol;
741 ctx->tws = 0;
742
743 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
744 eol--;
745 ctx->tws++;
746 }
747 ctx->vlen = eol - sov;
748 return 1;
749
750 next_hdr:
751 sol = eol + idx->v[cur_idx].cr + 1;
752 old_idx = cur_idx;
753 cur_idx = idx->v[cur_idx].next;
754 }
755 return 0;
756}
757
Willy Tarreau68085d82010-01-18 14:54:04 +0100758/* Find the end of the header value contained between <s> and <e>. See RFC2616,
759 * par 2.2 for more information. Note that it requires a valid header to return
760 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200761 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100762char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200763{
764 int quoted, qdpair;
765
766 quoted = qdpair = 0;
767 for (; s < e; s++) {
768 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200769 else if (quoted) {
770 if (*s == '\\') qdpair = 1;
771 else if (*s == '"') quoted = 0;
772 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200773 else if (*s == '"') quoted = 1;
774 else if (*s == ',') return s;
775 }
776 return s;
777}
778
779/* Find the first or next occurrence of header <name> in message buffer <sol>
780 * using headers index <idx>, and return it in the <ctx> structure. This
781 * structure holds everything necessary to use the header and find next
782 * occurrence. If its <idx> member is 0, the header is searched from the
783 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100784 * 1 when it finds a value, and 0 when there is no more. It is designed to work
785 * with headers defined as comma-separated lists. As a special case, if ctx->val
786 * is NULL when searching for a new values of a header, the current header is
787 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200788 */
789int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100790 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200791 struct hdr_ctx *ctx)
792{
Willy Tarreau68085d82010-01-18 14:54:04 +0100793 char *eol, *sov;
794 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200795
Willy Tarreau68085d82010-01-18 14:54:04 +0100796 cur_idx = ctx->idx;
797 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200798 /* We have previously returned a value, let's search
799 * another one on the same line.
800 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200801 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200802 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100803 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200804 eol = sol + idx->v[cur_idx].len;
805
806 if (sov >= eol)
807 /* no more values in this header */
808 goto next_hdr;
809
Willy Tarreau68085d82010-01-18 14:54:04 +0100810 /* values remaining for this header, skip the comma but save it
811 * for later use (eg: for header deletion).
812 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200813 sov++;
814 while (sov < eol && http_is_lws[(unsigned char)*sov])
815 sov++;
816
817 goto return_hdr;
818 }
819
820 /* first request for this header */
821 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100822 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200823 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200824 while (cur_idx) {
825 eol = sol + idx->v[cur_idx].len;
826
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200827 if (len == 0) {
828 /* No argument was passed, we want any header.
829 * To achieve this, we simply build a fake request. */
830 while (sol + len < eol && sol[len] != ':')
831 len++;
832 name = sol;
833 }
834
Willy Tarreau33a7e692007-06-10 19:45:56 +0200835 if ((len < eol - sol) &&
836 (sol[len] == ':') &&
837 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100838 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200839 sov = sol + len + 1;
840 while (sov < eol && http_is_lws[(unsigned char)*sov])
841 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100842
Willy Tarreau33a7e692007-06-10 19:45:56 +0200843 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100844 ctx->prev = old_idx;
845 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200846 ctx->idx = cur_idx;
847 ctx->val = sov - sol;
848
849 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200850 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200851 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200852 eol--;
853 ctx->tws++;
854 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200855 ctx->vlen = eol - sov;
856 return 1;
857 }
858 next_hdr:
859 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100860 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200861 cur_idx = idx->v[cur_idx].next;
862 }
863 return 0;
864}
865
866int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100867 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200868 struct hdr_ctx *ctx)
869{
870 return http_find_header2(name, strlen(name), sol, idx, ctx);
871}
872
Willy Tarreau68085d82010-01-18 14:54:04 +0100873/* Remove one value of a header. This only works on a <ctx> returned by one of
874 * the http_find_header functions. The value is removed, as well as surrounding
875 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100876 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100877 * message <msg>. The new index is returned. If it is zero, it means there is
878 * no more header, so any processing may stop. The ctx is always left in a form
879 * that can be handled by http_find_header2() to find next occurrence.
880 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100881int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100882{
883 int cur_idx = ctx->idx;
884 char *sol = ctx->line;
885 struct hdr_idx_elem *hdr;
886 int delta, skip_comma;
887
888 if (!cur_idx)
889 return 0;
890
891 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200892 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100893 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200894 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100895 http_msg_move_end(msg, delta);
896 idx->used--;
897 hdr->len = 0; /* unused entry */
898 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100899 if (idx->tail == ctx->idx)
900 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100901 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100902 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100903 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200904 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100905 return ctx->idx;
906 }
907
908 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200909 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
910 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100911 */
912
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200913 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200914 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200915 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100916 NULL, 0);
917 hdr->len += delta;
918 http_msg_move_end(msg, delta);
919 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200920 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100921 return ctx->idx;
922}
923
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100924/* This function handles a server error at the stream interface level. The
925 * stream interface is assumed to be already in a closed state. An optional
926 * message is copied into the input buffer, and an HTTP status code stored.
927 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100928 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200929 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200930static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100931 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200932{
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100933 channel_auto_read(si_oc(si));
934 channel_abort(si_oc(si));
935 channel_auto_close(si_oc(si));
936 channel_erase(si_oc(si));
937 channel_auto_close(si_ic(si));
938 channel_auto_read(si_ic(si));
Willy Tarreau0f772532006-12-23 20:51:41 +0100939 if (status > 0 && msg) {
Willy Tarreaueee5b512015-04-03 23:46:31 +0200940 s->txn->status = status;
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100941 bo_inject(si_ic(si), msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200942 }
Willy Tarreaue7dff022015-04-03 01:14:29 +0200943 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200944 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200945 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200946 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200947}
948
Willy Tarreau87b09662015-04-03 00:22:06 +0200949/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100950 * and message.
951 */
952
Willy Tarreau87b09662015-04-03 00:22:06 +0200953struct chunk *http_error_message(struct stream *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100954{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200955 if (s->be->errmsg[msgnum].str)
956 return &s->be->errmsg[msgnum];
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200957 else if (strm_fe(s)->errmsg[msgnum].str)
958 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100959 else
960 return &http_err_chunks[msgnum];
961}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200962
Willy Tarreau53b6c742006-12-17 13:37:46 +0100963/*
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200964 * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown
965 * ones.
Willy Tarreau53b6c742006-12-17 13:37:46 +0100966 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100967enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100968{
969 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100970 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100971
972 m = ((unsigned)*str - 'A');
973
974 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100975 for (h = http_methods[m]; h->len > 0; h++) {
976 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100977 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100978 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100979 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100980 };
Willy Tarreau53b6c742006-12-17 13:37:46 +0100981 }
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200982 return HTTP_METH_OTHER;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100983}
984
Willy Tarreau21d2af32008-02-14 20:25:24 +0100985/* Parse the URI from the given transaction (which is assumed to be in request
986 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
987 * It is returned otherwise.
988 */
Thierry FOURNIER3c331782015-09-17 19:33:35 +0200989char *http_get_path(struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +0100990{
991 char *ptr, *end;
992
Willy Tarreau9b28e032012-10-12 23:49:43 +0200993 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100994 end = ptr + txn->req.sl.rq.u_l;
995
996 if (ptr >= end)
997 return NULL;
998
999 /* RFC2616, par. 5.1.2 :
1000 * Request-URI = "*" | absuri | abspath | authority
1001 */
1002
1003 if (*ptr == '*')
1004 return NULL;
1005
1006 if (isalpha((unsigned char)*ptr)) {
1007 /* this is a scheme as described by RFC3986, par. 3.1 */
1008 ptr++;
1009 while (ptr < end &&
1010 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
1011 ptr++;
1012 /* skip '://' */
1013 if (ptr == end || *ptr++ != ':')
1014 return NULL;
1015 if (ptr == end || *ptr++ != '/')
1016 return NULL;
1017 if (ptr == end || *ptr++ != '/')
1018 return NULL;
1019 }
1020 /* skip [user[:passwd]@]host[:[port]] */
1021
1022 while (ptr < end && *ptr != '/')
1023 ptr++;
1024
1025 if (ptr == end)
1026 return NULL;
1027
1028 /* OK, we got the '/' ! */
1029 return ptr;
1030}
1031
William Lallemand65ad6e12014-01-31 15:08:02 +01001032/* Parse the URI from the given string and look for the "/" beginning the PATH.
1033 * If not found, return NULL. It is returned otherwise.
1034 */
1035static char *
1036http_get_path_from_string(char *str)
1037{
1038 char *ptr = str;
1039
1040 /* RFC2616, par. 5.1.2 :
1041 * Request-URI = "*" | absuri | abspath | authority
1042 */
1043
1044 if (*ptr == '*')
1045 return NULL;
1046
1047 if (isalpha((unsigned char)*ptr)) {
1048 /* this is a scheme as described by RFC3986, par. 3.1 */
1049 ptr++;
1050 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
1051 ptr++;
1052 /* skip '://' */
1053 if (*ptr == '\0' || *ptr++ != ':')
1054 return NULL;
1055 if (*ptr == '\0' || *ptr++ != '/')
1056 return NULL;
1057 if (*ptr == '\0' || *ptr++ != '/')
1058 return NULL;
1059 }
1060 /* skip [user[:passwd]@]host[:[port]] */
1061
1062 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
1063 ptr++;
1064
1065 if (*ptr == '\0' || *ptr == ' ')
1066 return NULL;
1067
1068 /* OK, we got the '/' ! */
1069 return ptr;
1070}
1071
Willy Tarreau71241ab2012-12-27 11:30:54 +01001072/* Returns a 302 for a redirectable request that reaches a server working in
1073 * in redirect mode. This may only be called just after the stream interface
1074 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
1075 * follow normal proxy processing. NOTE: this function is designed to support
1076 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001077 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001078void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001079{
1080 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001081 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001082 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001083 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001084
1085 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001086 trash.len = strlen(HTTP_302);
1087 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001088
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001089 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001090
Willy Tarreauefb453c2008-10-26 20:49:47 +01001091 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001092 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001093 return;
1094
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001095 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +01001096 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001097 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
1098 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001099 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001100
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001101 /* 3: add the request URI. Since it was already forwarded, we need
1102 * to temporarily rewind the buffer.
1103 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001104 txn = s->txn;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001105 b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001106
Willy Tarreauefb453c2008-10-26 20:49:47 +01001107 path = http_get_path(txn);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001108 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 +02001109
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001110 b_adv(s->req.buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001111
Willy Tarreauefb453c2008-10-26 20:49:47 +01001112 if (!path)
1113 return;
1114
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001115 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001116 return;
1117
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001118 memcpy(trash.str + trash.len, path, len);
1119 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001120
1121 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001122 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
1123 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001124 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001125 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
1126 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001127 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001128
1129 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001130 si_shutr(si);
1131 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001132 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001133 si->state = SI_ST_CLO;
1134
1135 /* send the message */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001136 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001137
1138 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001139 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001140 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001141}
1142
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001143/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001144 * that the server side is closed. Note that err_type is actually a
1145 * bitmask, where almost only aborts may be cumulated with other
1146 * values. We consider that aborted operations are more important
1147 * than timeouts or errors due to the fact that nobody else in the
1148 * logs might explain incomplete retries. All others should avoid
1149 * being cumulated. It should normally not be possible to have multiple
1150 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001151 * Note that connection errors appearing on the second request of a keep-alive
1152 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001153 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001154void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001155{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001156 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001157
1158 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001159 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001160 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001161 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001162 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001163 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001164 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001165 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001166 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001167 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001168 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001169 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001170 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001171 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001172 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001173 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001174 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001175 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001176 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
1177 503, (s->flags & SF_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001178 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001179 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001180 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001181 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001182 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001183 else /* SI_ET_CONN_OTHER and others */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001184 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001185 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001186}
1187
Willy Tarreau42250582007-04-01 01:30:43 +02001188extern const char sess_term_cond[8];
1189extern const char sess_fin_state[8];
1190extern const char *monthname[12];
Willy Tarreau63986c72015-04-03 22:55:33 +02001191struct pool_head *pool2_http_txn;
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001192struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001193struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001194struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001195
Willy Tarreau117f59e2007-03-04 18:17:17 +01001196/*
1197 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001198 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001199 */
1200void capture_headers(char *som, struct hdr_idx *idx,
1201 char **cap, struct cap_hdr *cap_hdr)
1202{
1203 char *eol, *sol, *col, *sov;
1204 int cur_idx;
1205 struct cap_hdr *h;
1206 int len;
1207
1208 sol = som + hdr_idx_first_pos(idx);
1209 cur_idx = hdr_idx_first_idx(idx);
1210
1211 while (cur_idx) {
1212 eol = sol + idx->v[cur_idx].len;
1213
1214 col = sol;
1215 while (col < eol && *col != ':')
1216 col++;
1217
1218 sov = col + 1;
1219 while (sov < eol && http_is_lws[(unsigned char)*sov])
1220 sov++;
1221
1222 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001223 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001224 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1225 if (cap[h->index] == NULL)
1226 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001227 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001228
1229 if (cap[h->index] == NULL) {
1230 Alert("HTTP capture : out of memory.\n");
1231 continue;
1232 }
1233
1234 len = eol - sov;
1235 if (len > h->len)
1236 len = h->len;
1237
1238 memcpy(cap[h->index], sov, len);
1239 cap[h->index][len]=0;
1240 }
1241 }
1242 sol = eol + idx->v[cur_idx].cr + 1;
1243 cur_idx = idx->v[cur_idx].next;
1244 }
1245}
1246
1247
Willy Tarreau42250582007-04-01 01:30:43 +02001248/* either we find an LF at <ptr> or we jump to <bad>.
1249 */
1250#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1251
1252/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1253 * otherwise to <http_msg_ood> with <state> set to <st>.
1254 */
1255#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1256 ptr++; \
1257 if (likely(ptr < end)) \
1258 goto good; \
1259 else { \
1260 state = (st); \
1261 goto http_msg_ood; \
1262 } \
1263 } while (0)
1264
1265
Willy Tarreaubaaee002006-06-26 02:48:02 +02001266/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001267 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001268 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1269 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1270 * will give undefined results.
1271 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1272 * and that msg->sol points to the beginning of the response.
1273 * If a complete line is found (which implies that at least one CR or LF is
1274 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1275 * returned indicating an incomplete line (which does not mean that parts have
1276 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1277 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1278 * upon next call.
1279 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001280 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001281 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1282 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001283 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001284 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001285const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001286 enum ht_state state, const char *ptr, const char *end,
1287 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001288{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001289 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001290
Willy Tarreau8973c702007-01-21 23:58:29 +01001291 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001292 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001293 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001294 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001295 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1296
1297 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001298 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001299 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1300 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001301 state = HTTP_MSG_ERROR;
1302 break;
1303
Willy Tarreau8973c702007-01-21 23:58:29 +01001304 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001305 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001306 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001307 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001308 goto http_msg_rpcode;
1309 }
1310 if (likely(HTTP_IS_SPHT(*ptr)))
1311 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1312 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001313 state = HTTP_MSG_ERROR;
1314 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001315
Willy Tarreau8973c702007-01-21 23:58:29 +01001316 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001317 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001318 if (likely(!HTTP_IS_LWS(*ptr)))
1319 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1320
1321 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001322 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001323 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1324 }
1325
1326 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001327 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001328 http_msg_rsp_reason:
1329 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001330 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001331 msg->sl.st.r_l = 0;
1332 goto http_msg_rpline_eol;
1333
Willy Tarreau8973c702007-01-21 23:58:29 +01001334 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001335 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001336 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001337 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001338 goto http_msg_rpreason;
1339 }
1340 if (likely(HTTP_IS_SPHT(*ptr)))
1341 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1342 /* so it's a CR/LF, so there is no reason phrase */
1343 goto http_msg_rsp_reason;
1344
Willy Tarreau8973c702007-01-21 23:58:29 +01001345 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001346 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001347 if (likely(!HTTP_IS_CRLF(*ptr)))
1348 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001349 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001350 http_msg_rpline_eol:
1351 /* We have seen the end of line. Note that we do not
1352 * necessarily have the \n yet, but at least we know that we
1353 * have EITHER \r OR \n, otherwise the response would not be
1354 * complete. We can then record the response length and return
1355 * to the caller which will be able to register it.
1356 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001357 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001358 return ptr;
1359
Willy Tarreau8973c702007-01-21 23:58:29 +01001360 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001361#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001362 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1363 exit(1);
1364#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001365 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001366 }
1367
1368 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001369 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001370 if (ret_state)
1371 *ret_state = state;
1372 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001373 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001374 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001375}
1376
Willy Tarreau8973c702007-01-21 23:58:29 +01001377/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001378 * This function parses a request line between <ptr> and <end>, starting with
1379 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1380 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1381 * will give undefined results.
1382 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1383 * and that msg->sol points to the beginning of the request.
1384 * If a complete line is found (which implies that at least one CR or LF is
1385 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1386 * returned indicating an incomplete line (which does not mean that parts have
1387 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1388 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1389 * upon next call.
1390 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001391 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001392 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1393 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001394 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001395 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001396const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001397 enum ht_state state, const char *ptr, const char *end,
1398 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001399{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001400 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001401
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001402 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001403 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001404 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001405 if (likely(HTTP_IS_TOKEN(*ptr)))
1406 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001407
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001408 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001409 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001410 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1411 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001412
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001413 if (likely(HTTP_IS_CRLF(*ptr))) {
1414 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001415 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001416 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001417 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001418 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001419 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001420 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001421 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001422 msg->sl.rq.v_l = 0;
1423 goto http_msg_rqline_eol;
1424 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001425 state = HTTP_MSG_ERROR;
1426 break;
1427
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001428 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001429 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001430 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001431 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001432 goto http_msg_rquri;
1433 }
1434 if (likely(HTTP_IS_SPHT(*ptr)))
1435 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1436 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1437 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001438
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001439 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001440 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001441 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001442 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001443
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001444 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001445 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001446 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1447 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001448
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001449 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001450 /* non-ASCII chars are forbidden unless option
1451 * accept-invalid-http-request is enabled in the frontend.
1452 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001453 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001454 if (msg->err_pos < -1)
1455 goto invalid_char;
1456 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001457 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001458 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1459 }
1460
1461 if (likely(HTTP_IS_CRLF(*ptr))) {
1462 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1463 goto http_msg_req09_uri_e;
1464 }
1465
1466 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001467 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001468 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001469 state = HTTP_MSG_ERROR;
1470 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001471
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001472 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001473 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001474 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001475 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001476 goto http_msg_rqver;
1477 }
1478 if (likely(HTTP_IS_SPHT(*ptr)))
1479 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1480 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1481 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001482
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001483 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001484 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001485 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001486 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001487
1488 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001489 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001490 http_msg_rqline_eol:
1491 /* We have seen the end of line. Note that we do not
1492 * necessarily have the \n yet, but at least we know that we
1493 * have EITHER \r OR \n, otherwise the request would not be
1494 * complete. We can then record the request length and return
1495 * to the caller which will be able to register it.
1496 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001497 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001498 return ptr;
1499 }
1500
1501 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001502 state = HTTP_MSG_ERROR;
1503 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001504
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001505 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001506#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001507 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1508 exit(1);
1509#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001510 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001511 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001512
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001513 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001514 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001515 if (ret_state)
1516 *ret_state = state;
1517 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001518 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001519 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001520}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001521
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001522/*
1523 * Returns the data from Authorization header. Function may be called more
1524 * than once so data is stored in txn->auth_data. When no header is found
1525 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001526 * searching again for something we are unable to find anyway. However, if
1527 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +02001528 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001529 */
1530
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001531/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1532 * set according to global.tune.bufsize.
1533 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001534char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001535
1536int
Willy Tarreau87b09662015-04-03 00:22:06 +02001537get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001538{
1539
Willy Tarreaueee5b512015-04-03 23:46:31 +02001540 struct http_txn *txn = s->txn;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001541 struct chunk auth_method;
1542 struct hdr_ctx ctx;
1543 char *h, *p;
1544 int len;
1545
1546#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +02001547 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001548#endif
1549
1550 if (txn->auth.method == HTTP_AUTH_WRONG)
1551 return 0;
1552
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001553 txn->auth.method = HTTP_AUTH_WRONG;
1554
1555 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001556
1557 if (txn->flags & TX_USE_PX_CONN) {
1558 h = "Proxy-Authorization";
1559 len = strlen(h);
1560 } else {
1561 h = "Authorization";
1562 len = strlen(h);
1563 }
1564
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001565 if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001566 return 0;
1567
1568 h = ctx.line + ctx.val;
1569
1570 p = memchr(h, ' ', ctx.vlen);
1571 if (!p || p == h)
1572 return 0;
1573
1574 chunk_initlen(&auth_method, h, 0, p-h);
1575 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1576
1577 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1578
1579 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001580 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001581
1582 if (len < 0)
1583 return 0;
1584
1585
1586 get_http_auth_buff[len] = '\0';
1587
1588 p = strchr(get_http_auth_buff, ':');
1589
1590 if (!p)
1591 return 0;
1592
1593 txn->auth.user = get_http_auth_buff;
1594 *p = '\0';
1595 txn->auth.pass = p+1;
1596
1597 txn->auth.method = HTTP_AUTH_BASIC;
1598 return 1;
1599 }
1600
1601 return 0;
1602}
1603
Willy Tarreau58f10d72006-12-04 02:26:12 +01001604
Willy Tarreau8973c702007-01-21 23:58:29 +01001605/*
1606 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001607 * depending on the initial msg->msg_state. The caller is responsible for
1608 * ensuring that the message does not wrap. The function can be preempted
1609 * everywhere when data are missing and recalled at the exact same location
1610 * with no information loss. The message may even be realigned between two
1611 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001612 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001613 * fields. Note that msg->sol will be initialized after completing the first
1614 * state, so that none of the msg pointers has to be initialized prior to the
1615 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001616 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001617void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001618{
Willy Tarreau3770f232013-12-07 00:01:53 +01001619 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001620 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001621 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001622
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001623 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001624 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001625 ptr = buf->p + msg->next;
1626 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001627
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001628 if (unlikely(ptr >= end))
1629 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001630
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001631 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001632 /*
1633 * First, states that are specific to the response only.
1634 * We check them first so that request and headers are
1635 * closer to each other (accessed more often).
1636 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001637 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001638 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001639 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001640 /* we have a start of message, but we have to check
1641 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001642 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001643 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001644 if (unlikely(ptr != buf->p)) {
1645 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001646 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001647 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001648 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001649 }
Willy Tarreau26927362012-05-18 23:22:52 +02001650 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001651 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001652 hdr_idx_init(idx);
1653 state = HTTP_MSG_RPVER;
1654 goto http_msg_rpver;
1655 }
1656
1657 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1658 goto http_msg_invalid;
1659
1660 if (unlikely(*ptr == '\n'))
1661 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1662 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1663 /* stop here */
1664
Willy Tarreau8973c702007-01-21 23:58:29 +01001665 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001666 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001667 EXPECT_LF_HERE(ptr, http_msg_invalid);
1668 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1669 /* stop here */
1670
Willy Tarreau8973c702007-01-21 23:58:29 +01001671 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001672 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001673 case HTTP_MSG_RPVER_SP:
1674 case HTTP_MSG_RPCODE:
1675 case HTTP_MSG_RPCODE_SP:
1676 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001677 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001678 state, ptr, end,
1679 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001680 if (unlikely(!ptr))
1681 return;
1682
1683 /* we have a full response and we know that we have either a CR
1684 * or an LF at <ptr>.
1685 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001686 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1687
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001688 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001689 if (likely(*ptr == '\r'))
1690 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1691 goto http_msg_rpline_end;
1692
Willy Tarreau8973c702007-01-21 23:58:29 +01001693 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001694 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001695 /* msg->sol must point to the first of CR or LF. */
1696 EXPECT_LF_HERE(ptr, http_msg_invalid);
1697 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1698 /* stop here */
1699
1700 /*
1701 * Second, states that are specific to the request only
1702 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001703 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001704 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001705 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001706 /* we have a start of message, but we have to check
1707 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001708 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001709 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001710 if (likely(ptr != buf->p)) {
1711 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001712 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001713 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001714 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001715 }
Willy Tarreau26927362012-05-18 23:22:52 +02001716 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001717 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001718 state = HTTP_MSG_RQMETH;
1719 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001720 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001721
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001722 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1723 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001724
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001725 if (unlikely(*ptr == '\n'))
1726 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1727 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001728 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001729
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001730 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001731 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001732 EXPECT_LF_HERE(ptr, http_msg_invalid);
1733 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001734 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001735
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001736 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001737 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001738 case HTTP_MSG_RQMETH_SP:
1739 case HTTP_MSG_RQURI:
1740 case HTTP_MSG_RQURI_SP:
1741 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001742 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001743 state, ptr, end,
1744 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001745 if (unlikely(!ptr))
1746 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001747
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001748 /* we have a full request and we know that we have either a CR
1749 * or an LF at <ptr>.
1750 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001751 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001752
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001753 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001754 if (likely(*ptr == '\r'))
1755 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001756 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001757
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001758 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001759 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001760 /* check for HTTP/0.9 request : no version information available.
1761 * msg->sol must point to the first of CR or LF.
1762 */
1763 if (unlikely(msg->sl.rq.v_l == 0))
1764 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001765
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001766 EXPECT_LF_HERE(ptr, http_msg_invalid);
1767 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001768 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001769
Willy Tarreau8973c702007-01-21 23:58:29 +01001770 /*
1771 * Common states below
1772 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001773 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001774 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001775 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001776 if (likely(!HTTP_IS_CRLF(*ptr))) {
1777 goto http_msg_hdr_name;
1778 }
1779
1780 if (likely(*ptr == '\r'))
1781 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1782 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001783
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001784 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001785 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001786 /* assumes msg->sol points to the first char */
1787 if (likely(HTTP_IS_TOKEN(*ptr)))
1788 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001789
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001790 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001791 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001792
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001793 if (likely(msg->err_pos < -1) || *ptr == '\n')
1794 goto http_msg_invalid;
1795
1796 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001797 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001798
1799 /* and we still accept this non-token character */
1800 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001801
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001802 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001803 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001804 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001805 if (likely(HTTP_IS_SPHT(*ptr)))
1806 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001807
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001808 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001809 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001810
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001811 if (likely(!HTTP_IS_CRLF(*ptr))) {
1812 goto http_msg_hdr_val;
1813 }
1814
1815 if (likely(*ptr == '\r'))
1816 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1817 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001818
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001819 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001820 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001821 EXPECT_LF_HERE(ptr, http_msg_invalid);
1822 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001823
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001824 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001825 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001826 if (likely(HTTP_IS_SPHT(*ptr))) {
1827 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001828 for (; buf->p + msg->sov < ptr; msg->sov++)
1829 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001830 goto http_msg_hdr_l1_sp;
1831 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001832 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001833 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001834 goto http_msg_complete_header;
1835
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001836 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001837 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001838 /* assumes msg->sol points to the first char, and msg->sov
1839 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001840 */
1841 if (likely(!HTTP_IS_CRLF(*ptr)))
1842 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001843
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001844 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001845 /* Note: we could also copy eol into ->eoh so that we have the
1846 * real header end in case it ends with lots of LWS, but is this
1847 * really needed ?
1848 */
1849 if (likely(*ptr == '\r'))
1850 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1851 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001852
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001853 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001854 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001855 EXPECT_LF_HERE(ptr, http_msg_invalid);
1856 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001857
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001858 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001859 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001860 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1861 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001862 for (; buf->p + msg->eol < ptr; msg->eol++)
1863 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001864 goto http_msg_hdr_val;
1865 }
1866 http_msg_complete_header:
1867 /*
1868 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001869 * Assumes msg->sol points to the first char, msg->sov points
1870 * to the first character of the value and msg->eol to the
1871 * first CR or LF so we know how the line ends. We insert last
1872 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001873 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001874 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001875 idx, idx->tail) < 0))
1876 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001877
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001878 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001879 if (likely(!HTTP_IS_CRLF(*ptr))) {
1880 goto http_msg_hdr_name;
1881 }
1882
1883 if (likely(*ptr == '\r'))
1884 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1885 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001886
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001887 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001888 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01001889 /* Assumes msg->sol points to the first of either CR or LF.
1890 * Sets ->sov and ->next to the total header length, ->eoh to
1891 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
1892 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001893 EXPECT_LF_HERE(ptr, http_msg_invalid);
1894 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001895 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001896 msg->eoh = msg->sol;
1897 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01001898 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001899 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001900 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001901
1902 case HTTP_MSG_ERROR:
1903 /* this may only happen if we call http_msg_analyser() twice with an error */
1904 break;
1905
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001906 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001907#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001908 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1909 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001910#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001911 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001912 }
1913 http_msg_ood:
1914 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001915 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001916 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001917 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001918
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001919 http_msg_invalid:
1920 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001921 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001922 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001923 return;
1924}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001925
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001926/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1927 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1928 * nothing is done and 1 is returned.
1929 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001930static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001931{
1932 int delta;
1933 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001934 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001935
1936 if (msg->sl.rq.v_l != 0)
1937 return 1;
1938
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001939 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1940 if (txn->meth != HTTP_METH_GET)
1941 return 0;
1942
Willy Tarreau9b28e032012-10-12 23:49:43 +02001943 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001944 delta = 0;
1945
1946 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001947 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1948 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001949 }
1950 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001951 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001952 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001953 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001954 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001955 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001956 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001957 NULL, NULL);
1958 if (unlikely(!cur_end))
1959 return 0;
1960
1961 /* we have a full HTTP/1.0 request now and we know that
1962 * we have either a CR or an LF at <ptr>.
1963 */
1964 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1965 return 1;
1966}
1967
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001968/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001969 * and "keep-alive" values. If we already know that some headers may safely
1970 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001971 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1972 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001973 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001974 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1975 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1976 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001977 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001978 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001979void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001980{
Willy Tarreau5b154472009-12-21 20:11:07 +01001981 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001982 const char *hdr_val = "Connection";
1983 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001984
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001985 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001986 return;
1987
Willy Tarreau88d349d2010-01-25 12:15:43 +01001988 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1989 hdr_val = "Proxy-Connection";
1990 hdr_len = 16;
1991 }
1992
Willy Tarreau5b154472009-12-21 20:11:07 +01001993 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001994 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001995 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001996 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1997 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001998 if (to_del & 2)
1999 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002000 else
2001 txn->flags |= TX_CON_KAL_SET;
2002 }
2003 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2004 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002005 if (to_del & 1)
2006 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002007 else
2008 txn->flags |= TX_CON_CLO_SET;
2009 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01002010 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
2011 txn->flags |= TX_HDR_CONN_UPG;
2012 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002013 }
2014
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002015 txn->flags |= TX_HDR_CONN_PRS;
2016 return;
2017}
Willy Tarreau5b154472009-12-21 20:11:07 +01002018
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002019/* Apply desired changes on the Connection: header. Values may be removed and/or
2020 * added depending on the <wanted> flags, which are exclusively composed of
2021 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
2022 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
2023 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002024void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002025{
2026 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002027 const char *hdr_val = "Connection";
2028 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002029
2030 ctx.idx = 0;
2031
Willy Tarreau88d349d2010-01-25 12:15:43 +01002032
2033 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2034 hdr_val = "Proxy-Connection";
2035 hdr_len = 16;
2036 }
2037
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002038 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002039 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002040 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2041 if (wanted & TX_CON_KAL_SET)
2042 txn->flags |= TX_CON_KAL_SET;
2043 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002044 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01002045 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002046 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2047 if (wanted & TX_CON_CLO_SET)
2048 txn->flags |= TX_CON_CLO_SET;
2049 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002050 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002051 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002052 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002053
2054 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
2055 return;
2056
2057 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
2058 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002059 hdr_val = "Connection: close";
2060 hdr_len = 17;
2061 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2062 hdr_val = "Proxy-Connection: close";
2063 hdr_len = 23;
2064 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002065 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002066 }
2067
2068 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
2069 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002070 hdr_val = "Connection: keep-alive";
2071 hdr_len = 22;
2072 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2073 hdr_val = "Proxy-Connection: keep-alive";
2074 hdr_len = 28;
2075 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002076 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002077 }
2078 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01002079}
2080
Willy Tarreauc24715e2014-04-17 15:21:20 +02002081/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to
2082 * the first byte of data after the chunk size, so that we know we can forward
2083 * exactly msg->next bytes. msg->sol contains the exact number of bytes forming
2084 * the chunk size. That way it is always possible to differentiate between the
2085 * start of the body and the start of the data.
Willy Tarreau115acb92009-12-26 13:56:06 +01002086 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002087 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002088 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002089static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002090{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002091 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002092 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002093 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002094 const char *end = buf->data + buf->size;
2095 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01002096 unsigned int chunk = 0;
2097
2098 /* The chunk size is in the following form, though we are only
2099 * interested in the size and CRLF :
2100 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2101 */
2102 while (1) {
2103 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002104 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002105 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002106 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002107 if (c < 0) /* not a hex digit anymore */
2108 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02002109 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002110 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01002111 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002112 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002113 chunk = (chunk << 4) + c;
2114 }
2115
Willy Tarreaud98cf932009-12-27 22:54:55 +01002116 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02002117 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002118 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002119
2120 while (http_is_spht[(unsigned char)*ptr]) {
2121 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002122 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02002123 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01002124 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002125 }
2126
Willy Tarreaud98cf932009-12-27 22:54:55 +01002127 /* Up to there, we know that at least one byte is present at *ptr. Check
2128 * for the end of chunk size.
2129 */
2130 while (1) {
2131 if (likely(HTTP_IS_CRLF(*ptr))) {
2132 /* we now have a CR or an LF at ptr */
2133 if (likely(*ptr == '\r')) {
2134 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002135 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002136 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002137 return 0;
2138 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002139
Willy Tarreaud98cf932009-12-27 22:54:55 +01002140 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002141 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002142 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002143 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002144 /* done */
2145 break;
2146 }
2147 else if (*ptr == ';') {
2148 /* chunk extension, ends at next CRLF */
2149 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002150 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002151 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002152 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002153
2154 while (!HTTP_IS_CRLF(*ptr)) {
2155 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002156 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002157 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002158 return 0;
2159 }
2160 /* we have a CRLF now, loop above */
2161 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002162 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002163 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002164 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002165 }
2166
Willy Tarreaud98cf932009-12-27 22:54:55 +01002167 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreauc24715e2014-04-17 15:21:20 +02002168 * which may or may not be present. We save that into ->next,
2169 * and the number of bytes parsed into msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002170 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002171 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002172 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002173 msg->sol += buf->size;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002174 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01002175 msg->chunk_len = chunk;
2176 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002177 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002178 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002179 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002180 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002181 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002182}
2183
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002184/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002185 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002186 * the trailers is found, it is automatically scheduled to be forwarded,
2187 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2188 * If not enough data are available, the function does not change anything
Willy Tarreauc24715e2014-04-17 15:21:20 +02002189 * except maybe msg->next if it could parse some lines, and returns zero.
2190 * If a parse error is encountered, the function returns < 0 and does not
2191 * change anything except maybe msg->next. Note that the message must
2192 * already be in HTTP_MSG_TRAILERS state before calling this function,
Willy Tarreau638cd022010-01-03 07:42:04 +01002193 * which implies that all non-trailers data have already been scheduled for
Willy Tarreauc24715e2014-04-17 15:21:20 +02002194 * forwarding, and that msg->next exactly matches the length of trailers
2195 * already parsed and not forwarded. It is also important to note that this
2196 * function is designed to be able to parse wrapped headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002197 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002198static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002199{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002200 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002201
Willy Tarreaua458b672012-03-05 11:17:50 +01002202 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002203 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002204 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002205 const char *ptr = b_ptr(buf, msg->next);
2206 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002207 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002208
2209 /* scan current line and stop at LF or CRLF */
2210 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002211 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002212 return 0;
2213
2214 if (*ptr == '\n') {
2215 if (!p1)
2216 p1 = ptr;
2217 p2 = ptr;
2218 break;
2219 }
2220
2221 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002222 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002223 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002224 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002225 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002226 p1 = ptr;
2227 }
2228
2229 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002230 if (ptr >= buf->data + buf->size)
2231 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002232 }
2233
2234 /* after LF; point to beginning of next line */
2235 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002236 if (p2 >= buf->data + buf->size)
2237 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002238
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002239 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002240 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002241 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002242
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002243 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002244 /* LF/CRLF at beginning of line => end of trailers at p2.
2245 * Everything was scheduled for forwarding, there's nothing
2246 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002247 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002248 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002249 msg->msg_state = HTTP_MSG_DONE;
2250 return 1;
2251 }
2252 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002253 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002254 }
2255}
2256
Willy Tarreauc24715e2014-04-17 15:21:20 +02002257/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF
2258 * or a possible LF alone at the end of a chunk. It automatically adjusts
2259 * msg->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002260 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002261 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2262 * not enough data are available, the function does not change anything and
2263 * returns zero. If a parse error is encountered, the function returns < 0 and
2264 * does not change anything. Note: this function is designed to parse wrapped
2265 * CRLF at the end of the buffer.
2266 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002267static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002268{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002269 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002270 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002271 int bytes;
2272
2273 /* NB: we'll check data availabilty at the end. It's not a
2274 * problem because whatever we match first will be checked
2275 * against the correct length.
2276 */
2277 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002278 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002279 if (*ptr == '\r') {
2280 bytes++;
2281 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002282 if (ptr >= buf->data + buf->size)
2283 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002284 }
2285
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002286 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002287 return 0;
2288
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002289 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002290 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002291 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002292 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002293
2294 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002295 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002296 ptr = buf->data;
Willy Tarreauc24715e2014-04-17 15:21:20 +02002297 /* Advance ->next to allow the CRLF to be forwarded */
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002298 msg->next += bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002299 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2300 return 1;
2301}
Willy Tarreau5b154472009-12-21 20:11:07 +01002302
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002303/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2304 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2305 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2306 * Unparsable qvalues return 1000 as "q=1.000".
2307 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002308int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002309{
2310 int q = 1000;
2311
Willy Tarreau506c69a2014-07-08 00:59:48 +02002312 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002313 goto out;
2314 q = (*qvalue++ - '0') * 1000;
2315
2316 if (*qvalue++ != '.')
2317 goto out;
2318
Willy Tarreau506c69a2014-07-08 00:59:48 +02002319 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002320 goto out;
2321 q += (*qvalue++ - '0') * 100;
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') * 10;
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') * 1;
2330 out:
2331 if (q > 1000)
2332 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002333 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002334 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002335 return q;
2336}
William Lallemand82fe75c2012-10-23 10:25:10 +02002337
2338/*
2339 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002340 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002341int select_compression_request_header(struct stream *s, struct buffer *req)
William Lallemand82fe75c2012-10-23 10:25:10 +02002342{
Willy Tarreaueee5b512015-04-03 23:46:31 +02002343 struct http_txn *txn = s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002344 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002345 struct hdr_ctx ctx;
2346 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002347 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002348
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002349 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2350 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002351 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2352 */
2353 ctx.idx = 0;
2354 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2355 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002356 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2357 (ctx.vlen < 31 ||
2358 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2359 ctx.line[ctx.val + 30] < '6' ||
2360 (ctx.line[ctx.val + 30] == '6' &&
2361 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2362 s->comp_algo = NULL;
2363 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002364 }
2365
William Lallemand82fe75c2012-10-23 10:25:10 +02002366 /* search for the algo in the backend in priority or the frontend */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002367 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 +01002368 int best_q = 0;
2369
William Lallemand82fe75c2012-10-23 10:25:10 +02002370 ctx.idx = 0;
2371 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002372 const char *qval;
2373 int q;
2374 int toklen;
2375
2376 /* try to isolate the token from the optional q-value */
2377 toklen = 0;
2378 while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)])
2379 toklen++;
2380
2381 qval = ctx.line + ctx.val + toklen;
2382 while (1) {
2383 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2384 qval++;
2385
2386 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
2387 qval = NULL;
2388 break;
2389 }
2390 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002391
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002392 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2393 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002394
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002395 if (qval >= ctx.line + ctx.val + ctx.vlen) {
2396 qval = NULL;
2397 break;
William Lallemand82fe75c2012-10-23 10:25:10 +02002398 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002399 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
2400 break;
2401
2402 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
2403 qval++;
2404 }
2405
2406 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002407 q = qval ? parse_qvalue(qval + 2, NULL) : 1000;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002408
2409 if (q <= best_q)
2410 continue;
2411
2412 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
2413 if (*(ctx.line + ctx.val) == '*' ||
Willy Tarreau615105e2015-03-28 16:40:46 +01002414 word_match(ctx.line + ctx.val, toklen, comp_algo->ua_name, comp_algo->ua_name_len)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002415 s->comp_algo = comp_algo;
2416 best_q = q;
2417 break;
2418 }
2419 }
2420 }
2421 }
2422
2423 /* remove all occurrences of the header when "compression offload" is set */
2424 if (s->comp_algo) {
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002425 if ((s->be->comp && s->be->comp->offload) || (strm_fe(s)->comp && strm_fe(s)->comp->offload)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002426 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2427 ctx.idx = 0;
2428 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2429 http_remove_header2(msg, &txn->hdr_idx, &ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +02002430 }
2431 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002432 return 1;
William Lallemand82fe75c2012-10-23 10:25:10 +02002433 }
2434
2435 /* identity is implicit does not require headers */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002436 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 +02002437 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
Willy Tarreau615105e2015-03-28 16:40:46 +01002438 if (comp_algo->cfg_name_len == 8 && memcmp(comp_algo->cfg_name, "identity", 8) == 0) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002439 s->comp_algo = comp_algo;
2440 return 1;
2441 }
2442 }
2443 }
2444
2445 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002446 return 0;
2447}
2448
2449/*
2450 * Selects a comression algorithm depending of the server response.
2451 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002452int select_compression_response_header(struct stream *s, struct buffer *res)
William Lallemand82fe75c2012-10-23 10:25:10 +02002453{
Willy Tarreaueee5b512015-04-03 23:46:31 +02002454 struct http_txn *txn = s->txn;
William Lallemand82fe75c2012-10-23 10:25:10 +02002455 struct http_msg *msg = &txn->rsp;
2456 struct hdr_ctx ctx;
2457 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002458
2459 /* no common compression algorithm was found in request header */
2460 if (s->comp_algo == NULL)
2461 goto fail;
2462
2463 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002464 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002465 goto fail;
2466
Jesse Hathaway2468d4e2015-03-06 20:16:15 +00002467 /* compress 200,201,202,203 responses only */
2468 if ((txn->status != 200) &&
2469 (txn->status != 201) &&
2470 (txn->status != 202) &&
2471 (txn->status != 203))
William Lallemandd3002612012-11-26 14:34:47 +01002472 goto fail;
2473
William Lallemand82fe75c2012-10-23 10:25:10 +02002474 /* Content-Length is null */
2475 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2476 goto fail;
2477
2478 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002479 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002480 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2481 goto fail;
2482
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002483 /* no compression when Cache-Control: no-transform is present in the message */
2484 ctx.idx = 0;
2485 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2486 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2487 goto fail;
2488 }
2489
William Lallemand82fe75c2012-10-23 10:25:10 +02002490 comp_type = NULL;
2491
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002492 /* we don't want to compress multipart content-types, nor content-types that are
2493 * not listed in the "compression type" directive if any. If no content-type was
2494 * found but configuration requires one, we don't compress either. Backend has
2495 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002496 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002497 ctx.idx = 0;
2498 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2499 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2500 goto fail;
2501
2502 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002503 (strm_fe(s)->comp && (comp_type = strm_fe(s)->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002504 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002505 if (ctx.vlen >= comp_type->name_len &&
2506 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002507 /* this Content-Type should be compressed */
2508 break;
2509 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002510 /* this Content-Type should not be compressed */
2511 if (comp_type == NULL)
2512 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002513 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002514 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002515 else { /* no content-type header */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002516 if ((s->be->comp && s->be->comp->types) || (strm_fe(s)->comp && strm_fe(s)->comp->types))
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002517 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002518 }
2519
William Lallemandd85f9172012-11-09 17:05:39 +01002520 /* limit compression rate */
2521 if (global.comp_rate_lim > 0)
2522 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2523 goto fail;
2524
William Lallemand072a2bf2012-11-20 17:01:01 +01002525 /* limit cpu usage */
2526 if (idle_pct < compress_min_idle)
2527 goto fail;
2528
William Lallemand4c49fae2012-11-07 15:00:23 +01002529 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002530 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002531 goto fail;
2532
Willy Tarreaue7dff022015-04-03 01:14:29 +02002533 s->flags |= SF_COMP_READY;
William Lallemandec3e3892012-11-12 17:02:18 +01002534
William Lallemand82fe75c2012-10-23 10:25:10 +02002535 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002536 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002537 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2538 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2539
2540 /* add Transfer-Encoding header */
2541 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2542 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2543
2544 /*
2545 * Add Content-Encoding header when it's not identity encoding.
2546 * RFC 2616 : Identity encoding: This content-coding is used only in the
2547 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2548 * header.
2549 */
Willy Tarreau615105e2015-03-28 16:40:46 +01002550 if (s->comp_algo->cfg_name_len != 8 || memcmp(s->comp_algo->cfg_name, "identity", 8) != 0) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002551 trash.len = 18;
2552 memcpy(trash.str, "Content-Encoding: ", trash.len);
Willy Tarreau615105e2015-03-28 16:40:46 +01002553 memcpy(trash.str + trash.len, s->comp_algo->ua_name, s->comp_algo->ua_name_len);
2554 trash.len += s->comp_algo->ua_name_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002555 trash.str[trash.len] = '\0';
2556 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002557 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002558 return 1;
2559
2560fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002561 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002562 return 0;
2563}
2564
Willy Tarreau87b09662015-04-03 00:22:06 +02002565void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002566{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002567 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002568 int tmp = TX_CON_WANT_KAL;
2569
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002570 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2571 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002572 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2573 tmp = TX_CON_WANT_TUN;
2574
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002575 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002576 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2577 tmp = TX_CON_WANT_TUN;
2578 }
2579
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002580 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002581 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2582 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002583 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002584 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2585 tmp = TX_CON_WANT_CLO;
2586 else
2587 tmp = TX_CON_WANT_SCL;
2588 }
2589
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002590 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002591 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2592 tmp = TX_CON_WANT_CLO;
2593
2594 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2595 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2596
2597 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2598 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2599 /* parse the Connection header and possibly clean it */
2600 int to_del = 0;
2601 if ((msg->flags & HTTP_MSGF_VER_11) ||
2602 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002603 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002604 to_del |= 2; /* remove "keep-alive" */
2605 if (!(msg->flags & HTTP_MSGF_VER_11))
2606 to_del |= 1; /* remove "close" */
2607 http_parse_connection_header(txn, msg, to_del);
2608 }
2609
2610 /* check if client or config asks for explicit close in KAL/SCL */
2611 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2612 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2613 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2614 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2615 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002616 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002617 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2618}
William Lallemand82fe75c2012-10-23 10:25:10 +02002619
Willy Tarreaud787e662009-07-07 10:14:51 +02002620/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2621 * processing can continue on next analysers, or zero if it either needs more
2622 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002623 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02002624 * when it has nothing left to do, and may remove any analyser when it wants to
2625 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002626 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002627int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002628{
Willy Tarreau59234e92008-11-30 23:51:27 +01002629 /*
2630 * We will parse the partial (or complete) lines.
2631 * We will check the request syntax, and also join multi-line
2632 * headers. An index of all the lines will be elaborated while
2633 * parsing.
2634 *
2635 * For the parsing, we use a 28 states FSM.
2636 *
2637 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002638 * req->buf->p = beginning of request
2639 * req->buf->p + msg->eoh = end of processed headers / start of current one
2640 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002641 * msg->eol = end of current header or line (LF or CRLF)
2642 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002643 *
2644 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02002645 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002646 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2647 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002648 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002649
Willy Tarreau59234e92008-11-30 23:51:27 +01002650 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002651 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002652 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01002653 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002654 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002655
Willy Tarreau87b09662015-04-03 00:22:06 +02002656 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 +01002657 now_ms, __FUNCTION__,
2658 s,
2659 req,
2660 req->rex, req->wex,
2661 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002662 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002663 req->analysers);
2664
Willy Tarreau52a0c602009-08-16 22:45:38 +02002665 /* we're speaking HTTP here, so let's speak HTTP to the client */
2666 s->srv_error = http_return_srv_error;
2667
Willy Tarreau83e3af02009-12-28 17:39:57 +01002668 /* There's a protected area at the end of the buffer for rewriting
2669 * purposes. We don't want to start to parse the request if the
2670 * protected area is affected, because we may have to move processed
2671 * data later, which is much more complicated.
2672 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002673 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002674 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01002675 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002676 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002677 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002678 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002679 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002680 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002681 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002682 return 0;
2683 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002684 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2685 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2686 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002687 }
2688
Willy Tarreau065e8332010-01-08 00:30:20 +01002689 /* Note that we have the same problem with the response ; we
2690 * may want to send a redirect, error or anything which requires
2691 * some spare space. So we'll ensure that we have at least
2692 * maxrewrite bytes available in the response buffer before
2693 * processing that one. This will only affect pipelined
2694 * keep-alive requests.
2695 */
2696 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002697 unlikely(!channel_is_rewritable(&s->res) ||
2698 bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) ||
2699 bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) {
2700 if (s->res.buf->o) {
2701 if (s->res.flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002702 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002703 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002704 channel_dont_connect(req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002705 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
2706 s->res.flags |= CF_WAKE_WRITE;
2707 s->res.analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002708 return 0;
2709 }
2710 }
2711
Willy Tarreau9b28e032012-10-12 23:49:43 +02002712 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002713 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002714 }
2715
Willy Tarreau59234e92008-11-30 23:51:27 +01002716 /* 1: we might have to print this header in debug mode */
2717 if (unlikely((global.mode & MODE_DEBUG) &&
2718 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02002719 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002720 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002721
Willy Tarreau9b28e032012-10-12 23:49:43 +02002722 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002723 /* this is a bit complex : in case of error on the request line,
2724 * we know that rq.l is still zero, so we display only the part
2725 * up to the end of the line (truncated by debug_hdr).
2726 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002727 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002728 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002729
Willy Tarreau59234e92008-11-30 23:51:27 +01002730 sol += hdr_idx_first_pos(&txn->hdr_idx);
2731 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002732
Willy Tarreau59234e92008-11-30 23:51:27 +01002733 while (cur_idx) {
2734 eol = sol + txn->hdr_idx.v[cur_idx].len;
2735 debug_hdr("clihdr", s, sol, eol);
2736 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2737 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002738 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002739 }
2740
Willy Tarreau58f10d72006-12-04 02:26:12 +01002741
Willy Tarreau59234e92008-11-30 23:51:27 +01002742 /*
2743 * Now we quickly check if we have found a full valid request.
2744 * If not so, we check the FD and buffer states before leaving.
2745 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002746 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002747 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02002748 * on a keep-alive stream, if we encounter and error, close, t/o,
2749 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002750 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02002751 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002752 * Last, we may increase some tracked counters' http request errors on
2753 * the cases that are deliberately the client's fault. For instance,
2754 * a timeout or connection reset is not counted as an error. However
2755 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002756 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002757
Willy Tarreau655dce92009-11-08 13:10:58 +01002758 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002759 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002760 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002761 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002762 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002763 stream_inc_http_req_ctr(s);
2764 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002765 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002766 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002767 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002768
Willy Tarreau59234e92008-11-30 23:51:27 +01002769 /* 1: Since we are in header mode, if there's no space
2770 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02002771 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01002772 * must terminate it now.
2773 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002774 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002775 /* FIXME: check if URI is set and return Status
2776 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002777 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002778 stream_inc_http_req_ctr(s);
2779 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002780 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002781 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002782 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002783 goto return_bad_req;
2784 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002785
Willy Tarreau59234e92008-11-30 23:51:27 +01002786 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002787 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002788 if (!(s->flags & SF_ERR_MASK))
2789 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002790
Willy Tarreaufcffa692010-01-10 14:21:19 +01002791 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002792 goto failed_keep_alive;
2793
Willy Tarreau0f228a02015-05-01 15:37:53 +02002794 if (sess->fe->options & PR_O_IGNORE_PRB)
2795 goto failed_keep_alive;
2796
Willy Tarreau59234e92008-11-30 23:51:27 +01002797 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002798 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002799 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002800 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002801 }
2802
Willy Tarreaudc979f22012-12-04 10:39:01 +01002803 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01002804 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002805 msg->msg_state = HTTP_MSG_ERROR;
2806 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002807
Willy Tarreau87b09662015-04-03 00:22:06 +02002808 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002809 proxy_inc_fe_req_ctr(sess->fe);
2810 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002811 if (sess->listener->counters)
2812 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002813
Willy Tarreaue7dff022015-04-03 01:14:29 +02002814 if (!(s->flags & SF_FINST_MASK))
2815 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002816 return 0;
2817 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002818
Willy Tarreau59234e92008-11-30 23:51:27 +01002819 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002820 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002821 if (!(s->flags & SF_ERR_MASK))
2822 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002823
Willy Tarreaufcffa692010-01-10 14:21:19 +01002824 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002825 goto failed_keep_alive;
2826
Willy Tarreau0f228a02015-05-01 15:37:53 +02002827 if (sess->fe->options & PR_O_IGNORE_PRB)
2828 goto failed_keep_alive;
2829
Willy Tarreau59234e92008-11-30 23:51:27 +01002830 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002831 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002832 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002833 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002834 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002835 txn->status = 408;
Willy Tarreau350f4872014-11-28 14:42:25 +01002836 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002837 msg->msg_state = HTTP_MSG_ERROR;
2838 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002839
Willy Tarreau87b09662015-04-03 00:22:06 +02002840 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002841 proxy_inc_fe_req_ctr(sess->fe);
2842 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002843 if (sess->listener->counters)
2844 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002845
Willy Tarreaue7dff022015-04-03 01:14:29 +02002846 if (!(s->flags & SF_FINST_MASK))
2847 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002848 return 0;
2849 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002850
Willy Tarreau59234e92008-11-30 23:51:27 +01002851 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002852 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002853 if (!(s->flags & SF_ERR_MASK))
2854 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002855
Willy Tarreaufcffa692010-01-10 14:21:19 +01002856 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002857 goto failed_keep_alive;
2858
Willy Tarreau0f228a02015-05-01 15:37:53 +02002859 if (sess->fe->options & PR_O_IGNORE_PRB)
2860 goto failed_keep_alive;
2861
Willy Tarreau4076a152009-04-02 15:18:36 +02002862 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002863 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002864 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01002865 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002866 msg->msg_state = HTTP_MSG_ERROR;
2867 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002868
Willy Tarreau87b09662015-04-03 00:22:06 +02002869 stream_inc_http_err_ctr(s);
2870 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002871 proxy_inc_fe_req_ctr(sess->fe);
2872 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002873 if (sess->listener->counters)
2874 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002875
Willy Tarreaue7dff022015-04-03 01:14:29 +02002876 if (!(s->flags & SF_FINST_MASK))
2877 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002878 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002879 }
2880
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002881 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002882 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002883 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002884#ifdef TCP_QUICKACK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002885 if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i &&
2886 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002887 /* We need more data, we have to re-enable quick-ack in case we
2888 * previously disabled it, otherwise we might cause the client
2889 * to delay next data.
2890 */
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002891 setsockopt(__objt_conn(sess->origin)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002892 }
2893#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002894
Willy Tarreaufcffa692010-01-10 14:21:19 +01002895 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2896 /* If the client starts to talk, let's fall back to
2897 * request timeout processing.
2898 */
2899 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002900 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002901 }
2902
Willy Tarreau59234e92008-11-30 23:51:27 +01002903 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002904 if (!tick_isset(req->analyse_exp)) {
2905 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2906 (txn->flags & TX_WAIT_NEXT_RQ) &&
2907 tick_isset(s->be->timeout.httpka))
2908 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2909 else
2910 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2911 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002912
Willy Tarreau59234e92008-11-30 23:51:27 +01002913 /* we're not ready yet */
2914 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002915
2916 failed_keep_alive:
2917 /* Here we process low-level errors for keep-alive requests. In
2918 * short, if the request is not the first one and it experiences
2919 * a timeout, read error or shutdown, we just silently close so
2920 * that the client can try again.
2921 */
2922 txn->status = 0;
2923 msg->msg_state = HTTP_MSG_RQBEFORE;
2924 req->analysers = 0;
2925 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002926 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002927 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau350f4872014-11-28 14:42:25 +01002928 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002929 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002930 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002931
Willy Tarreaud787e662009-07-07 10:14:51 +02002932 /* OK now we have a complete HTTP request with indexed headers. Let's
2933 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002934 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002935 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002936 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002937 * byte after the last LF. msg->sov points to the first byte of data.
2938 * msg->eol cannot be trusted because it may have been left uninitialized
2939 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002940 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002941
Willy Tarreau87b09662015-04-03 00:22:06 +02002942 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002943 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002944
Willy Tarreaub16a5742010-01-10 14:46:16 +01002945 if (txn->flags & TX_WAIT_NEXT_RQ) {
2946 /* kill the pending keep-alive timeout */
2947 txn->flags &= ~TX_WAIT_NEXT_RQ;
2948 req->analyse_exp = TICK_ETERNITY;
2949 }
2950
2951
Willy Tarreaud787e662009-07-07 10:14:51 +02002952 /* Maybe we found in invalid header name while we were configured not
2953 * to block on that, so we have to capture it now.
2954 */
2955 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002956 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002957
Willy Tarreau59234e92008-11-30 23:51:27 +01002958 /*
2959 * 1: identify the method
2960 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002961 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002962
2963 /* we can make use of server redirect on GET and HEAD */
2964 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002965 s->flags |= SF_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002966
Willy Tarreau59234e92008-11-30 23:51:27 +01002967 /*
2968 * 2: check if the URI matches the monitor_uri.
2969 * We have to do this for every request which gets in, because
2970 * the monitor-uri is defined by the frontend.
2971 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002972 if (unlikely((sess->fe->monitor_uri_len != 0) &&
2973 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002974 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002975 sess->fe->monitor_uri,
2976 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002977 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002978 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002979 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002980 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002981
Willy Tarreaue7dff022015-04-03 01:14:29 +02002982 s->flags |= SF_MONITOR;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002983 sess->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002984
Willy Tarreau59234e92008-11-30 23:51:27 +01002985 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002986 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002987 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002988
Willy Tarreau59234e92008-11-30 23:51:27 +01002989 ret = acl_pass(ret);
2990 if (cond->pol == ACL_COND_UNLESS)
2991 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002992
Willy Tarreau59234e92008-11-30 23:51:27 +01002993 if (ret) {
2994 /* we fail this request, let's return 503 service unavail */
2995 txn->status = 503;
Willy Tarreau350f4872014-11-28 14:42:25 +01002996 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_503));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002997 if (!(s->flags & SF_ERR_MASK))
2998 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002999 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01003000 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003001 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01003002
Willy Tarreau59234e92008-11-30 23:51:27 +01003003 /* nothing to fail, let's reply normaly */
3004 txn->status = 200;
Willy Tarreau350f4872014-11-28 14:42:25 +01003005 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_200));
Willy Tarreaue7dff022015-04-03 01:14:29 +02003006 if (!(s->flags & SF_ERR_MASK))
3007 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01003008 goto return_prx_cond;
3009 }
3010
3011 /*
3012 * 3: Maybe we have to copy the original REQURI for the logs ?
3013 * Note: we cannot log anymore if the request has been
3014 * classified as invalid.
3015 */
3016 if (unlikely(s->logs.logwait & LW_REQ)) {
3017 /* we have a complete HTTP request that we must log */
3018 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
3019 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003020
Willy Tarreau59234e92008-11-30 23:51:27 +01003021 if (urilen >= REQURI_LEN)
3022 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02003023 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01003024 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003025
Willy Tarreaud79a3b22012-12-28 09:40:16 +01003026 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01003027 s->do_log(s);
3028 } else {
3029 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01003030 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003031 }
Willy Tarreau06619262006-12-17 08:37:22 +01003032
Willy Tarreau91852eb2015-05-01 13:26:00 +02003033 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
3034 * exactly one digit "." one digit. This check may be disabled using
3035 * option accept-invalid-http-request.
3036 */
3037 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
3038 if (msg->sl.rq.v_l != 8) {
3039 msg->err_pos = msg->sl.rq.v;
3040 goto return_bad_req;
3041 }
3042
3043 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
3044 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
3045 req->buf->p[msg->sl.rq.v + 6] != '.' ||
3046 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
3047 msg->err_pos = msg->sl.rq.v + 4;
3048 goto return_bad_req;
3049 }
3050 }
Willy Tarreau13317662015-05-01 13:47:08 +02003051 else {
3052 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
3053 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
3054 goto return_bad_req;
3055 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02003056
Willy Tarreau5b154472009-12-21 20:11:07 +01003057 /* ... and check if the request is HTTP/1.1 or above */
3058 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003059 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
3060 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
3061 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003062 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01003063
3064 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01003065 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 +01003066
Willy Tarreau88d349d2010-01-25 12:15:43 +01003067 /* if the frontend has "option http-use-proxy-header", we'll check if
3068 * we have what looks like a proxied connection instead of a connection,
3069 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
3070 * Note that this is *not* RFC-compliant, however browsers and proxies
3071 * happen to do that despite being non-standard :-(
3072 * We consider that a request not beginning with either '/' or '*' is
3073 * a proxied connection, which covers both "scheme://location" and
3074 * CONNECT ip:port.
3075 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003076 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003077 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01003078 txn->flags |= TX_USE_PX_CONN;
3079
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003080 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003081 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003082
Willy Tarreau59234e92008-11-30 23:51:27 +01003083 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02003084 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02003085 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02003086 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02003087
Willy Tarreau557f1992015-05-01 10:05:17 +02003088 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
3089 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003090 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003091 * The length of a message body is determined by one of the following
3092 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02003093 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003094 * 1. Any response to a HEAD request and any response with a 1xx
3095 * (Informational), 204 (No Content), or 304 (Not Modified) status
3096 * code is always terminated by the first empty line after the
3097 * header fields, regardless of the header fields present in the
3098 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003099 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003100 * 2. Any 2xx (Successful) response to a CONNECT request implies that
3101 * the connection will become a tunnel immediately after the empty
3102 * line that concludes the header fields. A client MUST ignore any
3103 * Content-Length or Transfer-Encoding header fields received in
3104 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003105 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003106 * 3. If a Transfer-Encoding header field is present and the chunked
3107 * transfer coding (Section 4.1) is the final encoding, the message
3108 * body length is determined by reading and decoding the chunked
3109 * data until the transfer coding indicates the data is complete.
3110 *
3111 * If a Transfer-Encoding header field is present in a response and
3112 * the chunked transfer coding is not the final encoding, the
3113 * message body length is determined by reading the connection until
3114 * it is closed by the server. If a Transfer-Encoding header field
3115 * is present in a request and the chunked transfer coding is not
3116 * the final encoding, the message body length cannot be determined
3117 * reliably; the server MUST respond with the 400 (Bad Request)
3118 * status code and then close the connection.
3119 *
3120 * If a message is received with both a Transfer-Encoding and a
3121 * Content-Length header field, the Transfer-Encoding overrides the
3122 * Content-Length. Such a message might indicate an attempt to
3123 * perform request smuggling (Section 9.5) or response splitting
3124 * (Section 9.4) and ought to be handled as an error. A sender MUST
3125 * remove the received Content-Length field prior to forwarding such
3126 * a message downstream.
3127 *
3128 * 4. If a message is received without Transfer-Encoding and with
3129 * either multiple Content-Length header fields having differing
3130 * field-values or a single Content-Length header field having an
3131 * invalid value, then the message framing is invalid and the
3132 * recipient MUST treat it as an unrecoverable error. If this is a
3133 * request message, the server MUST respond with a 400 (Bad Request)
3134 * status code and then close the connection. If this is a response
3135 * message received by a proxy, the proxy MUST close the connection
3136 * to the server, discard the received response, and send a 502 (Bad
3137 * Gateway) response to the client. If this is a response message
3138 * received by a user agent, the user agent MUST close the
3139 * connection to the server and discard the received response.
3140 *
3141 * 5. If a valid Content-Length header field is present without
3142 * Transfer-Encoding, its decimal value defines the expected message
3143 * body length in octets. If the sender closes the connection or
3144 * the recipient times out before the indicated number of octets are
3145 * received, the recipient MUST consider the message to be
3146 * incomplete and close the connection.
3147 *
3148 * 6. If this is a request message and none of the above are true, then
3149 * the message body length is zero (no message body is present).
3150 *
3151 * 7. Otherwise, this is a response message without a declared message
3152 * body length, so the message body length is determined by the
3153 * number of octets received prior to the server closing the
3154 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003155 */
3156
Willy Tarreau32b47f42009-10-18 20:55:02 +02003157 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003158 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau4979d5c2015-05-01 10:06:30 +02003159 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003160 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003161 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
3162 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02003163 /* chunked not last, return badreq */
3164 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003165 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003166 }
3167
Willy Tarreau1c913912015-04-30 10:57:51 +02003168 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02003169 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02003170 if (msg->flags & HTTP_MSGF_TE_CHNK) {
3171 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
3172 http_remove_header2(msg, &txn->hdr_idx, &ctx);
3173 }
Willy Tarreau34dfc602015-05-01 10:09:49 +02003174 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02003175 signed long long cl;
3176
Willy Tarreauad14f752011-09-02 20:33:27 +02003177 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003178 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003179 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003180 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003181
Willy Tarreauad14f752011-09-02 20:33:27 +02003182 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003183 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003184 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02003185 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003186
Willy Tarreauad14f752011-09-02 20:33:27 +02003187 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003188 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003189 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003190 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003191
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003192 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003193 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003194 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02003195 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003196
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003197 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003198 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003199 }
3200
Willy Tarreau34dfc602015-05-01 10:09:49 +02003201 /* even bodyless requests have a known length */
3202 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003203
Willy Tarreau179085c2014-04-28 16:48:56 +02003204 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3205 * only change if both the request and the config reference something else.
3206 * Option httpclose by itself sets tunnel mode where headers are mangled.
3207 * However, if another mode is set, it will affect it (eg: server-close/
3208 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3209 * if FE and BE have the same settings (common). The method consists in
3210 * checking if options changed between the two calls (implying that either
3211 * one is non-null, or one of them is non-null and we are there for the first
3212 * time.
3213 */
3214 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003215 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02003216 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003217
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02003218 /* we may have to wait for the request's body */
3219 if ((s->be->options & PR_O_WREQ_BODY) &&
3220 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
3221 req->analysers |= AN_REQ_HTTP_BODY;
3222
Willy Tarreaud787e662009-07-07 10:14:51 +02003223 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003224 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003225 req->analyse_exp = TICK_ETERNITY;
3226 return 1;
3227
3228 return_bad_req:
3229 /* We centralize bad requests processing here */
3230 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3231 /* we detected a parsing error. We want to archive this request
3232 * in the dedicated proxy area for later troubleshooting.
3233 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003234 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003235 }
3236
3237 txn->req.msg_state = HTTP_MSG_ERROR;
3238 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01003239 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003240
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003241 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003242 if (sess->listener->counters)
3243 sess->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003244
3245 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003246 if (!(s->flags & SF_ERR_MASK))
3247 s->flags |= SF_ERR_PRXCOND;
3248 if (!(s->flags & SF_FINST_MASK))
3249 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02003250
3251 req->analysers = 0;
3252 req->analyse_exp = TICK_ETERNITY;
3253 return 0;
3254}
3255
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003256
Willy Tarreau347a35d2013-11-22 17:51:09 +01003257/* This function prepares an applet to handle the stats. It can deal with the
3258 * "100-continue" expectation, check that admin rules are met for POST requests,
3259 * and program a response message if something was unexpected. It cannot fail
3260 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003261 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003262 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02003263 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003264 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003265int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003266{
3267 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01003268 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02003269 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003270 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003271 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003272 struct uri_auth *uri_auth = s->be->uri_auth;
3273 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003274 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003275
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003276 appctx = si_appctx(si);
3277 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3278 appctx->st1 = appctx->st2 = 0;
3279 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3280 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003281 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003282 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003283
3284 uri = msg->chn->buf->p + msg->sl.rq.u;
3285 lookup = uri + uri_auth->uri_len;
3286
3287 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3288 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003289 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003290 break;
3291 }
3292 }
3293
3294 if (uri_auth->refresh) {
3295 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3296 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003297 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003298 break;
3299 }
3300 }
3301 }
3302
3303 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3304 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003305 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003306 break;
3307 }
3308 }
3309
3310 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3311 if (memcmp(h, ";st=", 4) == 0) {
3312 int i;
3313 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003314 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003315 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3316 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003317 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003318 break;
3319 }
3320 }
3321 break;
3322 }
3323 }
3324
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003325 appctx->ctx.stats.scope_str = 0;
3326 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003327 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3328 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3329 int itx = 0;
3330 const char *h2;
3331 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3332 const char *err;
3333
3334 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3335 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003336 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003337 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3338 itx++;
3339 h++;
3340 }
3341
3342 if (itx > STAT_SCOPE_TXT_MAXLEN)
3343 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003344 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003345
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003346 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003347 memcpy(scope_txt, h2, itx);
3348 scope_txt[itx] = '\0';
3349 err = invalid_char(scope_txt);
3350 if (err) {
3351 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003352 appctx->ctx.stats.scope_str = 0;
3353 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003354 }
3355 break;
3356 }
3357 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003358
3359 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003360 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003361 int ret = 1;
3362
3363 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003364 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 +01003365 ret = acl_pass(ret);
3366 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3367 ret = !ret;
3368 }
3369
3370 if (ret) {
3371 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003372 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003373 break;
3374 }
3375 }
3376
3377 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003378 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003379 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003380 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003381 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
3382 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003383 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003384 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003385 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003386 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3387 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003388 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003389 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003390 else {
3391 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003392 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003393 }
3394
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003395 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003396 return 1;
3397}
3398
Lukas Tribus67db8df2013-06-23 17:37:13 +02003399/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3400 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3401 */
Thierry FOURNIER7fe75e02015-03-16 12:03:44 +01003402void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003403{
3404#ifdef IP_TOS
3405 if (from.ss_family == AF_INET)
3406 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3407#endif
3408#ifdef IPV6_TCLASS
3409 if (from.ss_family == AF_INET6) {
3410 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3411 /* v4-mapped addresses need IP_TOS */
3412 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3413 else
3414 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3415 }
3416#endif
3417}
3418
Willy Tarreau87b09662015-04-03 00:22:06 +02003419int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003420 const char* name, unsigned int name_len,
3421 const char *str, struct my_regex *re,
3422 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06003423{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003424 struct hdr_ctx ctx;
3425 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003426 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003427 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3428 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003429 struct chunk *output = get_trash_chunk();
3430
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003431 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06003432
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003433 /* Choose the header browsing function. */
3434 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003435 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003436 http_find_hdr_func = http_find_header2;
3437 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003438 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003439 http_find_hdr_func = http_find_full_header2;
3440 break;
3441 default: /* impossible */
3442 return -1;
3443 }
3444
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003445 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
3446 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06003447 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003448 char *val = ctx.line + ctx.val;
3449 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003450
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003451 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
3452 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003453
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003454 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003455 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003456 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003457
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003458 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003459
3460 hdr->len += delta;
3461 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003462
3463 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003464 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003465 }
3466
3467 return 0;
3468}
3469
Willy Tarreau87b09662015-04-03 00:22:06 +02003470static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003471 const char* name, unsigned int name_len,
3472 struct list *fmt, struct my_regex *re,
3473 int action)
3474{
3475 struct chunk *replace = get_trash_chunk();
3476
3477 replace->len = build_logline(s, replace->str, replace->size, fmt);
3478 if (replace->len >= replace->size - 1)
3479 return -1;
3480
3481 return http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
3482}
3483
Willy Tarreau87b09662015-04-03 00:22:06 +02003484/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003485 * transaction <txn>. Returns the verdict of the first rule that prevents
3486 * further processing of the request (auth, deny, ...), and defaults to
3487 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3488 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
3489 * on txn->flags if it encounters a tarpit rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003490 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003491enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003492http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003493{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003494 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003495 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003496 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003497 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003498 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003499 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02003500 int act_flags = 0;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003501
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003502 /* If "the current_rule_list" match the executed rule list, we are in
3503 * resume condition. If a resume is needed it is always in the action
3504 * and never in the ACL or converters. In this case, we initialise the
3505 * current rule, and go to the action execution point.
3506 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003507 if (s->current_rule) {
3508 rule = s->current_rule;
3509 s->current_rule = NULL;
3510 if (s->current_rule_list == rules)
3511 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003512 }
3513 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003514
Willy Tarreauff011f22011-01-06 17:51:27 +01003515 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003516
Willy Tarreau96257ec2012-12-27 10:46:37 +01003517 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003518 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003519 int ret;
3520
Willy Tarreau192252e2015-04-04 01:47:55 +02003521 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003522 ret = acl_pass(ret);
3523
Willy Tarreauff011f22011-01-06 17:51:27 +01003524 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003525 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003526
3527 if (!ret) /* condition not matched */
3528 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003529 }
3530
Willy Tarreauacc98002015-09-27 23:34:39 +02003531 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003532resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003533 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003534 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003535 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003536
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003537 case ACT_ACTION_DENY:
CJ Ess108b1dd2015-04-07 12:03:37 -04003538 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003539 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003540
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003541 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01003542 txn->flags |= TX_CLTARPIT;
CJ Ess108b1dd2015-04-07 12:03:37 -04003543 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003544 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003545
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003546 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003547 /* Auth might be performed on regular http-req rules as well as on stats */
3548 auth_realm = rule->arg.auth.realm;
3549 if (!auth_realm) {
3550 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3551 auth_realm = STATS_DEFAULT_REALM;
3552 else
3553 auth_realm = px->id;
3554 }
3555 /* send 401/407 depending on whether we use a proxy or not. We still
3556 * count one error, because normal browsing won't significantly
3557 * increase the counter but brute force attempts will.
3558 */
3559 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3560 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
3561 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02003562 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003563 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003564
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003565 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003566 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3567 return HTTP_RULE_RES_BADREQ;
3568 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003569
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003570 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003571 s->task->nice = rule->arg.nice;
3572 break;
3573
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003574 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003575 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003576 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003577 break;
3578
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003579 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003580#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003581 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003582 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003583#endif
3584 break;
3585
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003586 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003587 s->logs.level = rule->arg.loglevel;
3588 break;
3589
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003590 case ACT_HTTP_REPLACE_HDR:
3591 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003592 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3593 rule->arg.hdr_add.name_len,
3594 &rule->arg.hdr_add.fmt,
3595 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003596 return HTTP_RULE_RES_BADREQ;
3597 break;
3598
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003599 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003600 ctx.idx = 0;
3601 /* remove all occurrences of the header */
3602 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3603 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3604 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003605 }
Willy Tarreau85603282015-01-21 20:39:27 +01003606 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003607
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003608 case ACT_HTTP_SET_HDR:
3609 case ACT_HTTP_ADD_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003610 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3611 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3612 trash.len = rule->arg.hdr_add.name_len;
3613 trash.str[trash.len++] = ':';
3614 trash.str[trash.len++] = ' ';
3615 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 +01003616
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003617 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003618 /* remove all occurrences of the header */
3619 ctx.idx = 0;
3620 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3621 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3622 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3623 }
3624 }
3625
Willy Tarreau96257ec2012-12-27 10:46:37 +01003626 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3627 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003628
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003629 case ACT_HTTP_DEL_ACL:
3630 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003631 struct pat_ref *ref;
3632 char *key;
3633 int len;
3634
3635 /* collect reference */
3636 ref = pat_ref_lookup(rule->arg.map.ref);
3637 if (!ref)
3638 continue;
3639
3640 /* collect key */
3641 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3642 key = trash.str;
3643 key[len] = '\0';
3644
3645 /* perform update */
3646 /* returned code: 1=ok, 0=ko */
3647 pat_ref_delete(ref, key);
3648
3649 break;
3650 }
3651
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003652 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003653 struct pat_ref *ref;
3654 char *key;
3655 struct chunk *trash_key;
3656 int len;
3657
3658 trash_key = get_trash_chunk();
3659
3660 /* collect reference */
3661 ref = pat_ref_lookup(rule->arg.map.ref);
3662 if (!ref)
3663 continue;
3664
3665 /* collect key */
3666 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3667 key = trash_key->str;
3668 key[len] = '\0';
3669
3670 /* perform update */
3671 /* add entry only if it does not already exist */
3672 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003673 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003674
3675 break;
3676 }
3677
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003678 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003679 struct pat_ref *ref;
3680 char *key, *value;
3681 struct chunk *trash_key, *trash_value;
3682 int len;
3683
3684 trash_key = get_trash_chunk();
3685 trash_value = get_trash_chunk();
3686
3687 /* collect reference */
3688 ref = pat_ref_lookup(rule->arg.map.ref);
3689 if (!ref)
3690 continue;
3691
3692 /* collect key */
3693 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3694 key = trash_key->str;
3695 key[len] = '\0';
3696
3697 /* collect value */
3698 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3699 value = trash_value->str;
3700 value[len] = '\0';
3701
3702 /* perform update */
3703 if (pat_ref_find_elt(ref, key) != NULL)
3704 /* update entry if it exists */
3705 pat_ref_set(ref, key, value, NULL);
3706 else
3707 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003708 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003709
3710 break;
3711 }
William Lallemand73025dd2014-04-24 14:38:37 +02003712
Thierry FOURNIER42148732015-09-02 17:17:33 +02003713 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02003714 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
3715 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003716
Willy Tarreauacc98002015-09-27 23:34:39 +02003717 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003718 case ACT_RET_ERR:
3719 case ACT_RET_CONT:
3720 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003721 case ACT_RET_STOP:
3722 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003723 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003724 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003725 return HTTP_RULE_RES_YIELD;
3726 }
William Lallemand73025dd2014-04-24 14:38:37 +02003727 break;
3728
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003729 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02003730 /* Note: only the first valid tracking parameter of each
3731 * applies.
3732 */
3733
3734 if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) {
3735 struct stktable *t;
3736 struct stksess *ts;
3737 struct stktable_key *key;
3738 void *ptr;
3739
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02003740 t = rule->arg.trk_ctr.table.t;
3741 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 +02003742
3743 if (key && (ts = stktable_get_entry(t, key))) {
Willy Tarreau87b09662015-04-03 00:22:06 +02003744 stream_track_stkctr(&s->stkctr[http_req_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02003745
3746 /* let's count a new HTTP request as it's the first time we do it */
3747 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3748 if (ptr)
3749 stktable_data_cast(ptr, http_req_cnt)++;
3750
3751 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3752 if (ptr)
3753 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3754 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3755
3756 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003757 if (sess->fe != s->be)
Willy Tarreau09448f72014-06-25 18:12:15 +02003758 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3759 }
3760 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003761 break;
3762
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003763 case ACT_HTTP_REQ_SET_SRC:
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003764 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
3765 struct sample *smp;
3766
Thierry FOURNIERa002dc92015-07-31 08:50:51 +02003767 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 +02003768
3769 if (smp) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003770 if (smp->data.type == SMP_T_IPV4) {
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003771 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_family = AF_INET;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003772 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003773 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003774 } else if (smp->data.type == SMP_T_IPV6) {
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003775 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_family = AF_INET6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003776 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 +02003777 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = 0;
3778 }
3779 }
3780 }
3781 break;
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003782
3783 /* other flags exists, but normaly, they never be matched. */
3784 default:
3785 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003786 }
3787 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003788
3789 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003790 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003791}
3792
Willy Tarreau71241ab2012-12-27 11:30:54 +01003793
Willy Tarreau51d861a2015-05-22 17:30:48 +02003794/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3795 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3796 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3797 * is returned, the process can continue the evaluation of next rule list. If
3798 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3799 * is returned, it means the operation could not be processed and a server error
3800 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3801 * deny rule. If *YIELD is returned, the caller must call again the function
3802 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003803 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003804static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003805http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003806{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003807 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003808 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003809 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003810 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003811 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02003812 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003813
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003814 /* If "the current_rule_list" match the executed rule list, we are in
3815 * resume condition. If a resume is needed it is always in the action
3816 * and never in the ACL or converters. In this case, we initialise the
3817 * current rule, and go to the action execution point.
3818 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003819 if (s->current_rule) {
3820 rule = s->current_rule;
3821 s->current_rule = NULL;
3822 if (s->current_rule_list == rules)
3823 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003824 }
3825 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003826
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003827 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003828
3829 /* check optional condition */
3830 if (rule->cond) {
3831 int ret;
3832
Willy Tarreau192252e2015-04-04 01:47:55 +02003833 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003834 ret = acl_pass(ret);
3835
3836 if (rule->cond->pol == ACL_COND_UNLESS)
3837 ret = !ret;
3838
3839 if (!ret) /* condition not matched */
3840 continue;
3841 }
3842
Willy Tarreauacc98002015-09-27 23:34:39 +02003843 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003844resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003845 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003846 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003847 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003848
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003849 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003850 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003851 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003852
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003853 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003854 s->task->nice = rule->arg.nice;
3855 break;
3856
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003857 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003858 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003859 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003860 break;
3861
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003862 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003863#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003864 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003865 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003866#endif
3867 break;
3868
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003869 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003870 s->logs.level = rule->arg.loglevel;
3871 break;
3872
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003873 case ACT_HTTP_REPLACE_HDR:
3874 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003875 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3876 rule->arg.hdr_add.name_len,
3877 &rule->arg.hdr_add.fmt,
3878 &rule->arg.hdr_add.re, rule->action))
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003879 return HTTP_RULE_RES_STOP; /* note: we should report an error here */
Sasha Pachev218f0642014-06-16 12:05:59 -06003880 break;
3881
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003882 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003883 ctx.idx = 0;
3884 /* remove all occurrences of the header */
3885 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3886 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3887 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3888 }
Willy Tarreau85603282015-01-21 20:39:27 +01003889 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003890
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003891 case ACT_HTTP_SET_HDR:
3892 case ACT_HTTP_ADD_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003893 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3894 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3895 trash.len = rule->arg.hdr_add.name_len;
3896 trash.str[trash.len++] = ':';
3897 trash.str[trash.len++] = ' ';
3898 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 +01003899
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003900 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003901 /* remove all occurrences of the header */
3902 ctx.idx = 0;
3903 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3904 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3905 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3906 }
3907 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003908 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3909 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003910
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003911 case ACT_HTTP_DEL_ACL:
3912 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003913 struct pat_ref *ref;
3914 char *key;
3915 int len;
3916
3917 /* collect reference */
3918 ref = pat_ref_lookup(rule->arg.map.ref);
3919 if (!ref)
3920 continue;
3921
3922 /* collect key */
3923 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3924 key = trash.str;
3925 key[len] = '\0';
3926
3927 /* perform update */
3928 /* returned code: 1=ok, 0=ko */
3929 pat_ref_delete(ref, key);
3930
3931 break;
3932 }
3933
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003934 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003935 struct pat_ref *ref;
3936 char *key;
3937 struct chunk *trash_key;
3938 int len;
3939
3940 trash_key = get_trash_chunk();
3941
3942 /* collect reference */
3943 ref = pat_ref_lookup(rule->arg.map.ref);
3944 if (!ref)
3945 continue;
3946
3947 /* collect key */
3948 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3949 key = trash_key->str;
3950 key[len] = '\0';
3951
3952 /* perform update */
3953 /* check if the entry already exists */
3954 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003955 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003956
3957 break;
3958 }
3959
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003960 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003961 struct pat_ref *ref;
3962 char *key, *value;
3963 struct chunk *trash_key, *trash_value;
3964 int len;
3965
3966 trash_key = get_trash_chunk();
3967 trash_value = get_trash_chunk();
3968
3969 /* collect reference */
3970 ref = pat_ref_lookup(rule->arg.map.ref);
3971 if (!ref)
3972 continue;
3973
3974 /* collect key */
3975 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3976 key = trash_key->str;
3977 key[len] = '\0';
3978
3979 /* collect value */
3980 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3981 value = trash_value->str;
3982 value[len] = '\0';
3983
3984 /* perform update */
3985 if (pat_ref_find_elt(ref, key) != NULL)
3986 /* update entry if it exists */
3987 pat_ref_set(ref, key, value, NULL);
3988 else
3989 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003990 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003991
3992 break;
3993 }
William Lallemand73025dd2014-04-24 14:38:37 +02003994
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003995 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02003996 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3997 return HTTP_RULE_RES_BADREQ;
3998 return HTTP_RULE_RES_DONE;
3999
Thierry FOURNIER42148732015-09-02 17:17:33 +02004000 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02004001 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
4002 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02004003
Willy Tarreauacc98002015-09-27 23:34:39 +02004004 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004005 case ACT_RET_ERR:
4006 case ACT_RET_CONT:
4007 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02004008 case ACT_RET_STOP:
4009 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004010 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02004011 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004012 return HTTP_RULE_RES_YIELD;
4013 }
William Lallemand73025dd2014-04-24 14:38:37 +02004014 break;
4015
Thierry FOURNIER22e49012015-08-05 19:13:48 +02004016 /* other flags exists, but normaly, they never be matched. */
4017 default:
4018 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004019 }
4020 }
4021
4022 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01004023 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004024}
4025
4026
Willy Tarreau71241ab2012-12-27 11:30:54 +01004027/* Perform an HTTP redirect based on the information in <rule>. The function
4028 * returns non-zero on success, or zero in case of a, irrecoverable error such
4029 * as too large a request to build a valid response.
4030 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004031static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01004032{
Willy Tarreaub329a312015-05-22 16:27:37 +02004033 struct http_msg *req = &txn->req;
4034 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004035 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004036 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004037
4038 /* build redirect message */
4039 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04004040 case 308:
4041 msg_fmt = HTTP_308;
4042 break;
4043 case 307:
4044 msg_fmt = HTTP_307;
4045 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004046 case 303:
4047 msg_fmt = HTTP_303;
4048 break;
4049 case 301:
4050 msg_fmt = HTTP_301;
4051 break;
4052 case 302:
4053 default:
4054 msg_fmt = HTTP_302;
4055 break;
4056 }
4057
4058 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
4059 return 0;
4060
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004061 location = trash.str + trash.len;
4062
Willy Tarreau71241ab2012-12-27 11:30:54 +01004063 switch(rule->type) {
4064 case REDIRECT_TYPE_SCHEME: {
4065 const char *path;
4066 const char *host;
4067 struct hdr_ctx ctx;
4068 int pathlen;
4069 int hostlen;
4070
4071 host = "";
4072 hostlen = 0;
4073 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02004074 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004075 host = ctx.line + ctx.val;
4076 hostlen = ctx.vlen;
4077 }
4078
4079 path = http_get_path(txn);
4080 /* build message using path */
4081 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004082 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004083 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4084 int qs = 0;
4085 while (qs < pathlen) {
4086 if (path[qs] == '?') {
4087 pathlen = qs;
4088 break;
4089 }
4090 qs++;
4091 }
4092 }
4093 } else {
4094 path = "/";
4095 pathlen = 1;
4096 }
4097
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004098 if (rule->rdr_str) { /* this is an old "redirect" rule */
4099 /* check if we can add scheme + "://" + host + path */
4100 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
4101 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004102
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004103 /* add scheme */
4104 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4105 trash.len += rule->rdr_len;
4106 }
4107 else {
4108 /* add scheme with executing log format */
4109 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004110
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004111 /* check if we can add scheme + "://" + host + path */
4112 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
4113 return 0;
4114 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004115 /* add "://" */
4116 memcpy(trash.str + trash.len, "://", 3);
4117 trash.len += 3;
4118
4119 /* add host */
4120 memcpy(trash.str + trash.len, host, hostlen);
4121 trash.len += hostlen;
4122
4123 /* add path */
4124 memcpy(trash.str + trash.len, path, pathlen);
4125 trash.len += pathlen;
4126
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004127 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004128 if (trash.len && trash.str[trash.len - 1] != '/' &&
4129 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4130 if (trash.len > trash.size - 5)
4131 return 0;
4132 trash.str[trash.len] = '/';
4133 trash.len++;
4134 }
4135
4136 break;
4137 }
4138 case REDIRECT_TYPE_PREFIX: {
4139 const char *path;
4140 int pathlen;
4141
4142 path = http_get_path(txn);
4143 /* build message using path */
4144 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004145 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004146 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4147 int qs = 0;
4148 while (qs < pathlen) {
4149 if (path[qs] == '?') {
4150 pathlen = qs;
4151 break;
4152 }
4153 qs++;
4154 }
4155 }
4156 } else {
4157 path = "/";
4158 pathlen = 1;
4159 }
4160
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004161 if (rule->rdr_str) { /* this is an old "redirect" rule */
4162 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
4163 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004164
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004165 /* add prefix. Note that if prefix == "/", we don't want to
4166 * add anything, otherwise it makes it hard for the user to
4167 * configure a self-redirection.
4168 */
4169 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
4170 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4171 trash.len += rule->rdr_len;
4172 }
4173 }
4174 else {
4175 /* add prefix with executing log format */
4176 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
4177
4178 /* Check length */
4179 if (trash.len + pathlen > trash.size - 4)
4180 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004181 }
4182
4183 /* add path */
4184 memcpy(trash.str + trash.len, path, pathlen);
4185 trash.len += pathlen;
4186
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004187 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004188 if (trash.len && trash.str[trash.len - 1] != '/' &&
4189 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4190 if (trash.len > trash.size - 5)
4191 return 0;
4192 trash.str[trash.len] = '/';
4193 trash.len++;
4194 }
4195
4196 break;
4197 }
4198 case REDIRECT_TYPE_LOCATION:
4199 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004200 if (rule->rdr_str) { /* this is an old "redirect" rule */
4201 if (trash.len + rule->rdr_len > trash.size - 4)
4202 return 0;
4203
4204 /* add location */
4205 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4206 trash.len += rule->rdr_len;
4207 }
4208 else {
4209 /* add location with executing log format */
4210 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004211
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004212 /* Check left length */
4213 if (trash.len > trash.size - 4)
4214 return 0;
4215 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004216 break;
4217 }
4218
4219 if (rule->cookie_len) {
4220 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
4221 trash.len += 14;
4222 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
4223 trash.len += rule->cookie_len;
4224 memcpy(trash.str + trash.len, "\r\n", 2);
4225 trash.len += 2;
4226 }
4227
4228 /* add end of headers and the keep-alive/close status.
4229 * We may choose to set keep-alive if the Location begins
4230 * with a slash, because the client will come back to the
4231 * same server.
4232 */
4233 txn->status = rule->code;
4234 /* let's log the request time */
4235 s->logs.tv_request = now;
4236
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004237 if (*location == '/' &&
Willy Tarreaub329a312015-05-22 16:27:37 +02004238 (req->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau2de8a502015-05-28 17:23:54 +02004239 ((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004240 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4241 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4242 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02004243 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004244 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4245 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
4246 trash.len += 30;
4247 } else {
4248 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
4249 trash.len += 24;
4250 }
4251 }
4252 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
4253 trash.len += 4;
Willy Tarreaub329a312015-05-22 16:27:37 +02004254 bo_inject(res->chn, trash.str, trash.len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004255 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02004256 bi_fast_delete(req->chn->buf, req->sov);
4257 req->next -= req->sov;
4258 req->sov = 0;
4259 s->req.analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004260 s->res.analysers = AN_RES_HTTP_XFER_BODY;
Willy Tarreaub329a312015-05-22 16:27:37 +02004261 req->msg_state = HTTP_MSG_CLOSED;
4262 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02004263 /* Trim any possible response */
4264 res->chn->buf->i = 0;
4265 res->next = res->sov = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004266 } else {
4267 /* keep-alive not possible */
4268 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4269 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4270 trash.len += 29;
4271 } else {
4272 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
4273 trash.len += 23;
4274 }
Willy Tarreau350f4872014-11-28 14:42:25 +01004275 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreaub329a312015-05-22 16:27:37 +02004276 req->chn->analysers = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004277 }
4278
Willy Tarreaue7dff022015-04-03 01:14:29 +02004279 if (!(s->flags & SF_ERR_MASK))
4280 s->flags |= SF_ERR_LOCAL;
4281 if (!(s->flags & SF_FINST_MASK))
4282 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004283
4284 return 1;
4285}
4286
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004287/* This stream analyser runs all HTTP request processing which is common to
4288 * frontends and backends, which means blocking ACLs, filters, connection-close,
4289 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004290 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004291 * either needs more data or wants to immediately abort the request (eg: deny,
4292 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004293 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004294int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004295{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004296 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004297 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02004298 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004299 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004300 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004301 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02004302
Willy Tarreau655dce92009-11-08 13:10:58 +01004303 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004304 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004305 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004306 }
4307
Willy Tarreau87b09662015-04-03 00:22:06 +02004308 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 +02004309 now_ms, __FUNCTION__,
4310 s,
4311 req,
4312 req->rex, req->wex,
4313 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004314 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004315 req->analysers);
4316
Willy Tarreau65410832014-04-28 21:25:43 +02004317 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02004318 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02004319
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004320 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004321 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004322 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Willy Tarreau51425942010-02-01 10:40:19 +01004323
Willy Tarreau0b748332014-04-29 00:13:29 +02004324 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004325 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4326 goto return_prx_yield;
4327
Willy Tarreau0b748332014-04-29 00:13:29 +02004328 case HTTP_RULE_RES_CONT:
4329 case HTTP_RULE_RES_STOP: /* nothing to do */
4330 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004331
Willy Tarreau0b748332014-04-29 00:13:29 +02004332 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4333 if (txn->flags & TX_CLTARPIT)
4334 goto tarpit;
4335 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004336
Willy Tarreau0b748332014-04-29 00:13:29 +02004337 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4338 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004339
Willy Tarreau0b748332014-04-29 00:13:29 +02004340 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004341 goto done;
4342
Willy Tarreau0b748332014-04-29 00:13:29 +02004343 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4344 goto return_bad_req;
4345 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004346 }
4347
Willy Tarreau52542592014-04-28 18:33:26 +02004348 /* OK at this stage, we know that the request was accepted according to
4349 * the http-request rules, we can check for the stats. Note that the
4350 * URI is detected *before* the req* rules in order not to be affected
4351 * by a possible reqrep, while they are processed *after* so that a
4352 * reqdeny can still block them. This clearly needs to change in 1.6!
4353 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004354 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004355 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004356 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004357 txn->status = 500;
4358 s->logs.tv_request = now;
Willy Tarreau350f4872014-11-28 14:42:25 +01004359 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004360
Willy Tarreaue7dff022015-04-03 01:14:29 +02004361 if (!(s->flags & SF_ERR_MASK))
4362 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02004363 goto return_prx_cond;
4364 }
4365
4366 /* parse the whole stats request and extract the relevant information */
4367 http_handle_stats(s, req);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004368 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Willy Tarreau0b748332014-04-29 00:13:29 +02004369 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004370
Willy Tarreau0b748332014-04-29 00:13:29 +02004371 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4372 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004373
Willy Tarreau0b748332014-04-29 00:13:29 +02004374 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4375 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004376 }
4377
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004378 /* evaluate the req* rules except reqadd */
4379 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004380 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004381 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004382
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004383 if (txn->flags & TX_CLDENY)
4384 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004385
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004386 if (txn->flags & TX_CLTARPIT)
4387 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004388 }
Willy Tarreau06619262006-12-17 08:37:22 +01004389
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004390 /* add request headers from the rule sets in the same order */
4391 list_for_each_entry(wl, &px->req_add, list) {
4392 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004393 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004394 ret = acl_pass(ret);
4395 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4396 ret = !ret;
4397 if (!ret)
4398 continue;
4399 }
4400
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004401 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004402 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004403 }
4404
Willy Tarreau52542592014-04-28 18:33:26 +02004405
4406 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004407 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004408 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004409 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
4410 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004411
Willy Tarreaue7dff022015-04-03 01:14:29 +02004412 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
4413 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
4414 if (!(s->flags & SF_FINST_MASK))
4415 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004416
Willy Tarreau70730dd2014-04-24 18:06:27 +02004417 /* we may want to compress the stats page */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004418 if (sess->fe->comp || s->be->comp)
Willy Tarreau70730dd2014-04-24 18:06:27 +02004419 select_compression_request_header(s, req->buf);
4420
4421 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreau5506e3f2014-11-20 22:23:10 +01004422 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004423 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004424 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004425
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004426 /* check whether we have some ACLs set to redirect this request */
4427 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004428 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004429 int ret;
4430
Willy Tarreau192252e2015-04-04 01:47:55 +02004431 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004432 ret = acl_pass(ret);
4433 if (rule->cond->pol == ACL_COND_UNLESS)
4434 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004435 if (!ret)
4436 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004437 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004438 if (!http_apply_redirect_rule(rule, s, txn))
4439 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004440 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004441 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004442
Willy Tarreau2be39392010-01-03 17:24:51 +01004443 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4444 * If this happens, then the data will not come immediately, so we must
4445 * send all what we have without waiting. Note that due to the small gain
4446 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004447 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004448 * itself once used.
4449 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004450 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004451
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004452 done: /* done with this analyser, continue with next ones that the calling
4453 * points will have set, if any.
4454 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004455 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004456 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4457 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004458 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004459
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004460 tarpit:
4461 /* When a connection is tarpitted, we use the tarpit timeout,
4462 * which may be the same as the connect timeout if unspecified.
4463 * If unset, then set it to zero because we really want it to
4464 * eventually expire. We build the tarpit as an analyser.
4465 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004466 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004467
4468 /* wipe the request out so that we can drop the connection early
4469 * if the client closes first.
4470 */
4471 channel_dont_connect(req);
4472 req->analysers = 0; /* remove switching rules etc... */
4473 req->analysers |= AN_REQ_HTTP_TARPIT;
4474 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4475 if (!req->analyse_exp)
4476 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02004477 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004478 sess->fe->fe_counters.denied_req++;
4479 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004480 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004481 if (sess->listener->counters)
4482 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004483 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004484
4485 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004486 txn->flags |= TX_CLDENY;
CJ Ess108b1dd2015-04-07 12:03:37 -04004487 txn->status = http_err_codes[txn->rule_deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004488 s->logs.tv_request = now;
CJ Ess108b1dd2015-04-07 12:03:37 -04004489 stream_int_retnclose(&s->si[0], http_error_message(s, txn->rule_deny_status));
Willy Tarreau87b09662015-04-03 00:22:06 +02004490 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004491 sess->fe->fe_counters.denied_req++;
4492 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004493 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004494 if (sess->listener->counters)
4495 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004496 goto return_prx_cond;
4497
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004498 return_bad_req:
4499 /* We centralize bad requests processing here */
4500 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4501 /* we detected a parsing error. We want to archive this request
4502 * in the dedicated proxy area for later troubleshooting.
4503 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004504 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004505 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004506
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004507 txn->req.msg_state = HTTP_MSG_ERROR;
4508 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01004509 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004510
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004511 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004512 if (sess->listener->counters)
4513 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004514
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004515 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02004516 if (!(s->flags & SF_ERR_MASK))
4517 s->flags |= SF_ERR_PRXCOND;
4518 if (!(s->flags & SF_FINST_MASK))
4519 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004520
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004521 req->analysers = 0;
4522 req->analyse_exp = TICK_ETERNITY;
4523 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004524
4525 return_prx_yield:
4526 channel_dont_connect(req);
4527 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004528}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004529
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004530/* This function performs all the processing enabled for the current request.
4531 * It returns 1 if the processing can continue on next analysers, or zero if it
4532 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004533 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004534 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004535int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004536{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004537 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004538 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004539 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02004540 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004541
Willy Tarreau655dce92009-11-08 13:10:58 +01004542 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004543 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004544 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004545 return 0;
4546 }
4547
Willy Tarreau87b09662015-04-03 00:22:06 +02004548 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 +02004549 now_ms, __FUNCTION__,
4550 s,
4551 req,
4552 req->rex, req->wex,
4553 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004554 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004555 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004556
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004557 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02004558 select_compression_request_header(s, req->buf);
4559
Willy Tarreau59234e92008-11-30 23:51:27 +01004560 /*
4561 * Right now, we know that we have processed the entire headers
4562 * and that unwanted requests have been filtered out. We can do
4563 * whatever we want with the remaining request. Also, now we
4564 * may have separate values for ->fe, ->be.
4565 */
Willy Tarreau06619262006-12-17 08:37:22 +01004566
Willy Tarreau59234e92008-11-30 23:51:27 +01004567 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004568 * If HTTP PROXY is set we simply get remote server address parsing
4569 * incoming request. Note that this requires that a connection is
4570 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004571 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004572 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004573 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004574 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004575
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004576 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau973a5422015-08-05 21:47:23 +02004577 if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004578 txn->req.msg_state = HTTP_MSG_ERROR;
4579 txn->status = 500;
4580 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004581 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004582
Willy Tarreaue7dff022015-04-03 01:14:29 +02004583 if (!(s->flags & SF_ERR_MASK))
4584 s->flags |= SF_ERR_RESOURCE;
4585 if (!(s->flags & SF_FINST_MASK))
4586 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004587
4588 return 0;
4589 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004590
4591 path = http_get_path(txn);
4592 url2sa(req->buf->p + msg->sl.rq.u,
4593 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004594 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004595 /* if the path was found, we have to remove everything between
4596 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4597 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4598 * u_l characters by a single "/".
4599 */
4600 if (path) {
4601 char *cur_ptr = req->buf->p;
4602 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4603 int delta;
4604
4605 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4606 http_msg_move_end(&txn->req, delta);
4607 cur_end += delta;
4608 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4609 goto return_bad_req;
4610 }
4611 else {
4612 char *cur_ptr = req->buf->p;
4613 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4614 int delta;
4615
4616 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4617 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4618 http_msg_move_end(&txn->req, delta);
4619 cur_end += delta;
4620 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4621 goto return_bad_req;
4622 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004623 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004624
Willy Tarreau59234e92008-11-30 23:51:27 +01004625 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004626 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004627 * Note that doing so might move headers in the request, but
4628 * the fields will stay coherent and the URI will not move.
4629 * This should only be performed in the backend.
4630 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02004631 if ((s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004632 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4633 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004634
William Lallemanda73203e2012-03-12 12:48:57 +01004635 /* add unique-id if "header-unique-id" is specified */
4636
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004637 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004638 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4639 goto return_bad_req;
4640 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004641 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004642 }
William Lallemanda73203e2012-03-12 12:48:57 +01004643
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004644 if (sess->fe->header_unique_id && s->unique_id) {
4645 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004646 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004647 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004648 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004649 goto return_bad_req;
4650 }
4651
Cyril Bontéb21570a2009-11-29 20:04:48 +01004652 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004653 * 9: add X-Forwarded-For if either the frontend or the backend
4654 * asks for it.
4655 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004656 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004657 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004658 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
4659 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
4660 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004661 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004662 /* The header is set to be added only if none is present
4663 * and we found it, so don't do anything.
4664 */
4665 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004666 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004667 /* Add an X-Forwarded-For header unless the source IP is
4668 * in the 'except' network range.
4669 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004670 if ((!sess->fe->except_mask.s_addr ||
4671 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
4672 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01004673 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004674 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004675 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004676 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004677 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004678 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004679
4680 /* Note: we rely on the backend to get the header name to be used for
4681 * x-forwarded-for, because the header is really meant for the backends.
4682 * However, if the backend did not specify any option, we have to rely
4683 * on the frontend's header name.
4684 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004685 if (s->be->fwdfor_hdr_len) {
4686 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004687 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004688 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004689 len = sess->fe->fwdfor_hdr_len;
4690 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004691 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004692 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 +01004693
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004694 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004695 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004696 }
4697 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004698 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004699 /* FIXME: for the sake of completeness, we should also support
4700 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004701 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004702 int len;
4703 char pn[INET6_ADDRSTRLEN];
4704 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004705 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004706 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004707
Willy Tarreau59234e92008-11-30 23:51:27 +01004708 /* Note: we rely on the backend to get the header name to be used for
4709 * x-forwarded-for, because the header is really meant for the backends.
4710 * However, if the backend did not specify any option, we have to rely
4711 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004712 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004713 if (s->be->fwdfor_hdr_len) {
4714 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004715 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004716 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004717 len = sess->fe->fwdfor_hdr_len;
4718 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004719 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004720 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004721
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004722 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004723 goto return_bad_req;
4724 }
4725 }
4726
4727 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004728 * 10: add X-Original-To if either the frontend or the backend
4729 * asks for it.
4730 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004731 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004732
4733 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004734 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004735 /* Add an X-Original-To header unless the destination IP is
4736 * in the 'except' network range.
4737 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004738 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004739
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004740 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004741 ((!sess->fe->except_mask_to.s_addr ||
4742 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
4743 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004744 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004745 (((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 +02004746 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004747 int len;
4748 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004749 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004750
4751 /* Note: we rely on the backend to get the header name to be used for
4752 * x-original-to, because the header is really meant for the backends.
4753 * However, if the backend did not specify any option, we have to rely
4754 * on the frontend's header name.
4755 */
4756 if (s->be->orgto_hdr_len) {
4757 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004758 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004759 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004760 len = sess->fe->orgto_hdr_len;
4761 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004762 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004763 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 +02004764
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004765 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004766 goto return_bad_req;
4767 }
4768 }
4769 }
4770
Willy Tarreau50fc7772012-11-11 22:19:57 +01004771 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4772 * If an "Upgrade" token is found, the header is left untouched in order not to have
4773 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4774 * "Upgrade" is present in the Connection header.
4775 */
4776 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4777 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
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 Tarreaubbf0b372010-01-18 16:54:40 +01004780 unsigned int want_flags = 0;
4781
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004782 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004783 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
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_CLO_SET;
4788 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004789 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004790 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004791 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004792 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004793 want_flags |= TX_CON_KAL_SET;
4794 }
4795
4796 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004797 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004798 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004799
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004800
Willy Tarreau522d6c02009-12-06 18:49:18 +01004801 /* If we have no server assigned yet and we're balancing on url_param
4802 * with a POST request, we may be interested in checking the body for
4803 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004804 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004805 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02004806 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004807 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004808 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004809 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004810 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004811
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004812 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004813 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004814#ifdef TCP_QUICKACK
4815 /* We expect some data from the client. Unless we know for sure
4816 * we already have a full request, we have to re-enable quick-ack
4817 * in case we previously disabled it, otherwise we might cause
4818 * the client to delay further data.
4819 */
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004820 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004821 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004822 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004823 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004824 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004825#endif
4826 }
Willy Tarreau03945942009-12-22 16:50:27 +01004827
Willy Tarreau59234e92008-11-30 23:51:27 +01004828 /*************************************************************
4829 * OK, that's finished for the headers. We have done what we *
4830 * could. Let's switch to the DATA state. *
4831 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004832 req->analyse_exp = TICK_ETERNITY;
4833 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004834
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004835 /* if the server closes the connection, we want to immediately react
4836 * and close the socket to save packets and syscalls.
4837 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004838 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
Willy Tarreau350f4872014-11-28 14:42:25 +01004839 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004840
Willy Tarreau59234e92008-11-30 23:51:27 +01004841 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004842 /* OK let's go on with the BODY now */
4843 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004844
Willy Tarreau59234e92008-11-30 23:51:27 +01004845 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004846 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004847 /* we detected a parsing error. We want to archive this request
4848 * in the dedicated proxy area for later troubleshooting.
4849 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004850 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004851 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004852
Willy Tarreau59234e92008-11-30 23:51:27 +01004853 txn->req.msg_state = HTTP_MSG_ERROR;
4854 txn->status = 400;
4855 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004856 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004857
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004858 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004859 if (sess->listener->counters)
4860 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004861
Willy Tarreaue7dff022015-04-03 01:14:29 +02004862 if (!(s->flags & SF_ERR_MASK))
4863 s->flags |= SF_ERR_PRXCOND;
4864 if (!(s->flags & SF_FINST_MASK))
4865 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004866 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004867}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004868
Willy Tarreau60b85b02008-11-30 23:28:40 +01004869/* This function is an analyser which processes the HTTP tarpit. It always
4870 * returns zero, at the beginning because it prevents any other processing
4871 * from occurring, and at the end because it terminates the request.
4872 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004873int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004874{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004875 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004876
4877 /* This connection is being tarpitted. The CLIENT side has
4878 * already set the connect expiration date to the right
4879 * timeout. We just have to check that the client is still
4880 * there and that the timeout has not expired.
4881 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004882 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004883 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004884 !tick_is_expired(req->analyse_exp, now_ms))
4885 return 0;
4886
4887 /* We will set the queue timer to the time spent, just for
4888 * logging purposes. We fake a 500 server error, so that the
4889 * attacker will not suspect his connection has been tarpitted.
4890 * It will not cause trouble to the logs because we can exclude
4891 * the tarpitted connections by filtering on the 'PT' status flags.
4892 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004893 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4894
4895 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004896 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau350f4872014-11-28 14:42:25 +01004897 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004898
4899 req->analysers = 0;
4900 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004901
Willy Tarreaue7dff022015-04-03 01:14:29 +02004902 if (!(s->flags & SF_ERR_MASK))
4903 s->flags |= SF_ERR_PRXCOND;
4904 if (!(s->flags & SF_FINST_MASK))
4905 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004906 return 0;
4907}
4908
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004909/* This function is an analyser which waits for the HTTP request body. It waits
4910 * for either the buffer to be full, or the full advertised contents to have
4911 * reached the buffer. It must only be called after the standard HTTP request
4912 * processing has occurred, because it expects the request to be parsed and will
4913 * look for the Expect header. It may send a 100-Continue interim response. It
4914 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4915 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4916 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004917 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004918int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004919{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004920 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004921 struct http_txn *txn = s->txn;
4922 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004923
4924 /* We have to parse the HTTP request body to find any required data.
4925 * "balance url_param check_post" should have been the only way to get
4926 * into this. We were brought here after HTTP header analysis, so all
4927 * related structures are ready.
4928 */
4929
Willy Tarreau890988f2014-04-10 11:59:33 +02004930 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4931 /* This is the first call */
4932 if (msg->msg_state < HTTP_MSG_BODY)
4933 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004934
Willy Tarreau890988f2014-04-10 11:59:33 +02004935 if (msg->msg_state < HTTP_MSG_100_SENT) {
4936 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4937 * send an HTTP/1.1 100 Continue intermediate response.
4938 */
4939 if (msg->flags & HTTP_MSGF_VER_11) {
4940 struct hdr_ctx ctx;
4941 ctx.idx = 0;
4942 /* Expect is allowed in 1.1, look for it */
4943 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4944 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004945 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau890988f2014-04-10 11:59:33 +02004946 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004947 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004948 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004949 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004950
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004951 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004952 * req->buf->p still points to the beginning of the message. We
4953 * must save the body in msg->next because it survives buffer
4954 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004955 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004956 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004957
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004958 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004959 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4960 else
4961 msg->msg_state = HTTP_MSG_DATA;
4962 }
4963
Willy Tarreau890988f2014-04-10 11:59:33 +02004964 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4965 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004966 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004967 goto missing_data;
4968
4969 /* OK we have everything we need now */
4970 goto http_end;
4971 }
4972
4973 /* OK here we're parsing a chunked-encoded message */
4974
Willy Tarreau522d6c02009-12-06 18:49:18 +01004975 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004976 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004977 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004978 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004979 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004980 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004981
Willy Tarreau115acb92009-12-26 13:56:06 +01004982 if (!ret)
4983 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004984 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004985 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004986 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004987 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004988 }
4989
Willy Tarreaud98cf932009-12-27 22:54:55 +01004990 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02004991 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4992 * for at least a whole chunk or the whole content length bytes after
4993 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004994 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004995 if (msg->msg_state == HTTP_MSG_TRAILERS)
4996 goto http_end;
4997
Willy Tarreaue115b492015-05-01 23:05:14 +02004998 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004999 goto http_end;
5000
5001 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02005002 /* we get here if we need to wait for more data. If the buffer is full,
5003 * we have the maximum we can expect.
5004 */
5005 if (buffer_full(req->buf, global.tune.maxrewrite))
5006 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01005007
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005008 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01005009 txn->status = 408;
Willy Tarreau350f4872014-11-28 14:42:25 +01005010 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02005011
Willy Tarreaue7dff022015-04-03 01:14:29 +02005012 if (!(s->flags & SF_ERR_MASK))
5013 s->flags |= SF_ERR_CLITO;
5014 if (!(s->flags & SF_FINST_MASK))
5015 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005016 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01005017 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01005018
5019 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02005020 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01005021 /* Not enough data. We'll re-use the http-request
5022 * timeout here. Ideally, we should set the timeout
5023 * relative to the accept() date. We just set the
5024 * request timeout once at the beginning of the
5025 * request.
5026 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005027 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01005028 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02005029 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01005030 return 0;
5031 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01005032
5033 http_end:
5034 /* The situation will not evolve, so let's give up on the analysis. */
5035 s->logs.tv_request = now; /* update the request timer to reflect full request */
5036 req->analysers &= ~an_bit;
5037 req->analyse_exp = TICK_ETERNITY;
5038 return 1;
5039
5040 return_bad_req: /* let's centralize all bad requests */
5041 txn->req.msg_state = HTTP_MSG_ERROR;
5042 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01005043 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01005044
Willy Tarreaue7dff022015-04-03 01:14:29 +02005045 if (!(s->flags & SF_ERR_MASK))
5046 s->flags |= SF_ERR_PRXCOND;
5047 if (!(s->flags & SF_FINST_MASK))
5048 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005049
Willy Tarreau522d6c02009-12-06 18:49:18 +01005050 return_err_msg:
5051 req->analysers = 0;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005052 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005053 if (sess->listener->counters)
5054 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005055 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01005056}
5057
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005058/* send a server's name with an outgoing request over an established connection.
5059 * Note: this function is designed to be called once the request has been scheduled
5060 * for being forwarded. This is the reason why it rewinds the buffer before
5061 * proceeding.
5062 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01005063int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05005064
5065 struct hdr_ctx ctx;
5066
Mark Lamourinec2247f02012-01-04 13:02:01 -05005067 char *hdr_name = be->server_id_hdr_name;
5068 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02005069 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005070 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005071 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005072
William Lallemandd9e90662012-01-30 17:27:17 +01005073 ctx.idx = 0;
5074
Willy Tarreau211cdec2014-04-17 20:18:08 +02005075 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005076 if (old_o) {
5077 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005078 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02005079 txn->req.next += old_o;
5080 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005081 }
5082
Willy Tarreau9b28e032012-10-12 23:49:43 +02005083 old_i = chn->buf->i;
5084 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 -05005085 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005086 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005087 }
5088
5089 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005090 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005091 memcpy(hdr_val, hdr_name, hdr_name_len);
5092 hdr_val += hdr_name_len;
5093 *hdr_val++ = ':';
5094 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005095 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
5096 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005097
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005098 if (old_o) {
5099 /* If this was a forwarded request, we must readjust the amount of
5100 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02005101 * variations. Note that the current state is >= HTTP_MSG_BODY,
5102 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005103 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02005104 old_o += chn->buf->i - old_i;
5105 b_adv(chn->buf, old_o);
5106 txn->req.next -= old_o;
5107 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005108 }
5109
Mark Lamourinec2247f02012-01-04 13:02:01 -05005110 return 0;
5111}
5112
Willy Tarreau610ecce2010-01-04 21:15:02 +01005113/* Terminate current transaction and prepare a new one. This is very tricky
5114 * right now but it works.
5115 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005116void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005117{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005118 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005119 struct proxy *fe = strm_fe(s);
Willy Tarreau323a2d92015-08-04 19:00:17 +02005120 struct connection *srv_conn;
5121 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02005122 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01005123
Willy Tarreau610ecce2010-01-04 21:15:02 +01005124 /* FIXME: We need a more portable way of releasing a backend's and a
5125 * server's connections. We need a safer way to reinitialize buffer
5126 * flags. We also need a more accurate method for computing per-request
5127 * data.
5128 */
Willy Tarreau323a2d92015-08-04 19:00:17 +02005129 srv_conn = objt_conn(s->si[1].end);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005130
Willy Tarreau4213a112013-12-15 10:25:42 +01005131 /* unless we're doing keep-alive, we want to quickly close the connection
5132 * to the server.
5133 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005134 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005135 !si_conn_ready(&s->si[1])) {
5136 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
5137 si_shutr(&s->si[1]);
5138 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01005139 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005140
Willy Tarreaue7dff022015-04-03 01:14:29 +02005141 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005142 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01005143 if (unlikely(s->srv_conn))
5144 sess_change_server(s, NULL);
5145 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005146
5147 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02005148 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005149
Willy Tarreaueee5b512015-04-03 23:46:31 +02005150 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005151 int n;
5152
Willy Tarreaueee5b512015-04-03 23:46:31 +02005153 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005154 if (n < 1 || n > 5)
5155 n = 0;
5156
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005157 if (fe->mode == PR_MODE_HTTP) {
5158 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005159 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005160 fe->fe_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005161 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02005162 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005163 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005164 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005165 s->be->be_counters.p.http.cum_req++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005166 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005167 s->be->be_counters.p.http.comp_rsp++;
5168 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005169 }
5170
5171 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005172 s->logs.bytes_in -= s->req.buf->i;
5173 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005174
5175 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005176 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005177 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005178 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005179 s->do_log(s);
5180 }
5181
Willy Tarreaud713bcc2014-06-25 15:36:04 +02005182 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02005183 stream_stop_content_counters(s);
5184 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02005185
Willy Tarreau610ecce2010-01-04 21:15:02 +01005186 s->logs.accept_date = date; /* user-visible date for logging */
5187 s->logs.tv_accept = now; /* corrected date for internal use */
5188 tv_zero(&s->logs.tv_request);
5189 s->logs.t_queue = -1;
5190 s->logs.t_connect = -1;
5191 s->logs.t_data = -1;
5192 s->logs.t_close = 0;
5193 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
5194 s->logs.srv_queue_size = 0; /* we will get this number soon */
5195
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005196 s->logs.bytes_in = s->req.total = s->req.buf->i;
5197 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005198
5199 if (s->pend_pos)
5200 pendconn_free(s->pend_pos);
5201
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005202 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005203 if (s->flags & SF_CURR_SESS) {
5204 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005205 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005206 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005207 if (may_dequeue_tasks(objt_server(s->target), s->be))
5208 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01005209 }
5210
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005211 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005212
Willy Tarreau4213a112013-12-15 10:25:42 +01005213 /* only release our endpoint if we don't intend to reuse the
5214 * connection.
5215 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005216 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005217 !si_conn_ready(&s->si[1])) {
5218 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02005219 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005220 }
5221
Willy Tarreau350f4872014-11-28 14:42:25 +01005222 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
5223 s->si[1].err_type = SI_ET_NONE;
5224 s->si[1].conn_retries = 0; /* used for logging too */
5225 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02005226 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 +01005227 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);
5228 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 +02005229 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
5230 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
5231 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005232
Willy Tarreaueee5b512015-04-03 23:46:31 +02005233 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005234 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02005235 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005236
5237 if (prev_status == 401 || prev_status == 407) {
5238 /* In HTTP keep-alive mode, if we receive a 401, we still have
5239 * a chance of being able to send the visitor again to the same
5240 * server over the same connection. This is required by some
5241 * broken protocols such as NTLM, and anyway whenever there is
5242 * an opportunity for sending the challenge to the proper place,
5243 * it's better to do it (at least it helps with debugging).
5244 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005245 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02005246 if (srv_conn)
5247 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01005248 }
5249
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005250 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005251 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005252
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005253 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005254 s->req.flags |= CF_NEVER_WAIT;
5255 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005256 }
5257
Willy Tarreau610ecce2010-01-04 21:15:02 +01005258 /* if the request buffer is not empty, it means we're
5259 * about to process another request, so send pending
5260 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005261 * Just don't do this if the buffer is close to be full,
5262 * because the request will wait for it to flush a little
5263 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005264 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005265 if (s->req.buf->i) {
5266 if (s->res.buf->o &&
5267 !buffer_full(s->res.buf, global.tune.maxrewrite) &&
5268 bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)
5269 s->res.flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005270 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005271
5272 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005273 channel_auto_read(&s->req);
5274 channel_auto_close(&s->req);
5275 channel_auto_read(&s->res);
5276 channel_auto_close(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005277
Willy Tarreau27375622013-12-17 00:00:28 +01005278 /* we're in keep-alive with an idle connection, monitor it */
Willy Tarreau4320eaa2015-08-05 11:08:30 +02005279 if (srv_conn) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02005280 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005281 if (!srv)
5282 si_idle_conn(&s->si[1], NULL);
5283 else if ((srv_conn->flags & CO_FL_PRIVATE) ||
5284 ((s->be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR))
5285 si_idle_conn(&s->si[1], &srv->priv_conns);
Willy Tarreau449d74a2015-08-05 17:16:33 +02005286 else if (prev_flags & TX_NOT_FIRST)
5287 /* note: we check the request, not the connection, but
5288 * this is valid for strategies SAFE and AGGR, and in
5289 * case of ALWS, we don't care anyway.
5290 */
5291 si_idle_conn(&s->si[1], &srv->safe_conns);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005292 else
5293 si_idle_conn(&s->si[1], &srv->idle_conns);
Willy Tarreau4320eaa2015-08-05 11:08:30 +02005294 }
Willy Tarreau27375622013-12-17 00:00:28 +01005295
Willy Tarreauc29d0cd2015-09-23 12:18:14 +02005296 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005297 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005298}
5299
5300
5301/* This function updates the request state machine according to the response
5302 * state machine and buffer flags. It returns 1 if it changes anything (flag
5303 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5304 * it is only used to find when a request/response couple is complete. Both
5305 * this function and its equivalent should loop until both return zero. It
5306 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5307 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005308int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005309{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005310 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005311 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005312 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005313 unsigned int old_state = txn->req.msg_state;
5314
Willy Tarreau610ecce2010-01-04 21:15:02 +01005315 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5316 return 0;
5317
5318 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005319 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005320 * We can shut the read side unless we want to abort_on_close,
5321 * or we have a POST request. The issue with POST requests is
5322 * that some browsers still send a CRLF after the request, and
5323 * this CRLF must be read so that it does not remain in the kernel
5324 * buffers, otherwise a close could cause an RST on some systems
5325 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005326 * Note that if we're using keep-alive on the client side, we'd
5327 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005328 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005329 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005330 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005331 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5332 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5333 !(s->be->options & PR_O_ABRT_CLOSE) &&
5334 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005335 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005336
Willy Tarreau40f151a2012-12-20 12:10:09 +01005337 /* if the server closes the connection, we want to immediately react
5338 * and close the socket to save packets and syscalls.
5339 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005340 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005341
Willy Tarreau610ecce2010-01-04 21:15:02 +01005342 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5343 goto wait_other_side;
5344
5345 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5346 /* The server has not finished to respond, so we
5347 * don't want to move in order not to upset it.
5348 */
5349 goto wait_other_side;
5350 }
5351
5352 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5353 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005354 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005355 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005356 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005357 goto wait_other_side;
5358 }
5359
5360 /* When we get here, it means that both the request and the
5361 * response have finished receiving. Depending on the connection
5362 * mode, we'll have to wait for the last bytes to leave in either
5363 * direction, and sometimes for a close to be effective.
5364 */
5365
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005366 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5367 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005368 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5369 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005370 }
5371 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5372 /* Option forceclose is set, or either side wants to close,
5373 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005374 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005375 * once both states are CLOSED.
5376 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005377 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5378 channel_shutr_now(chn);
5379 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005380 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005381 }
5382 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005383 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5384 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005385 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005386 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5387 channel_auto_read(chn);
5388 txn->req.msg_state = HTTP_MSG_TUNNEL;
5389 chn->flags |= CF_NEVER_WAIT;
5390 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005391 }
5392
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005393 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005394 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005395 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005396
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005397 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005398 txn->req.msg_state = HTTP_MSG_CLOSING;
5399 goto http_msg_closing;
5400 }
5401 else {
5402 txn->req.msg_state = HTTP_MSG_CLOSED;
5403 goto http_msg_closed;
5404 }
5405 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005406 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005407 }
5408
5409 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5410 http_msg_closing:
5411 /* nothing else to forward, just waiting for the output buffer
5412 * to be empty and for the shutw_now to take effect.
5413 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005414 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005415 txn->req.msg_state = HTTP_MSG_CLOSED;
5416 goto http_msg_closed;
5417 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005418 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005419 txn->req.msg_state = HTTP_MSG_ERROR;
5420 goto wait_other_side;
5421 }
5422 }
5423
5424 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5425 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005426 /* see above in MSG_DONE why we only do this in these states */
5427 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5428 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5429 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005430 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005431 goto wait_other_side;
5432 }
5433
5434 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005435 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005436}
5437
5438
5439/* This function updates the response state machine according to the request
5440 * state machine and buffer flags. It returns 1 if it changes anything (flag
5441 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5442 * it is only used to find when a request/response couple is complete. Both
5443 * this function and its equivalent should loop until both return zero. It
5444 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5445 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005446int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005447{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005448 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005449 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005450 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005451 unsigned int old_state = txn->rsp.msg_state;
5452
Willy Tarreau610ecce2010-01-04 21:15:02 +01005453 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5454 return 0;
5455
5456 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5457 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005458 * still monitor the server connection for a possible close
5459 * while the request is being uploaded, so we don't disable
5460 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005461 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005462 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005463
5464 if (txn->req.msg_state == HTTP_MSG_ERROR)
5465 goto wait_other_side;
5466
5467 if (txn->req.msg_state < HTTP_MSG_DONE) {
5468 /* The client seems to still be sending data, probably
5469 * because we got an error response during an upload.
5470 * We have the choice of either breaking the connection
5471 * or letting it pass through. Let's do the later.
5472 */
5473 goto wait_other_side;
5474 }
5475
5476 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5477 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005478 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005479 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005480 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005481 goto wait_other_side;
5482 }
5483
5484 /* When we get here, it means that both the request and the
5485 * response have finished receiving. Depending on the connection
5486 * mode, we'll have to wait for the last bytes to leave in either
5487 * direction, and sometimes for a close to be effective.
5488 */
5489
5490 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5491 /* Server-close mode : shut read and wait for the request
5492 * side to close its output buffer. The caller will detect
5493 * when we're in DONE and the other is in CLOSED and will
5494 * catch that for the final cleanup.
5495 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005496 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5497 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005498 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005499 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5500 /* Option forceclose is set, or either side wants to close,
5501 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005502 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005503 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005504 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005505 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5506 channel_shutr_now(chn);
5507 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005508 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005509 }
5510 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005511 /* The last possible modes are keep-alive and tunnel. Tunnel will
5512 * need to forward remaining data. Keep-alive will need to monitor
5513 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005514 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005515 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005516 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005517 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5518 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005519 }
5520
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005521 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005522 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005523 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005524 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5525 goto http_msg_closing;
5526 }
5527 else {
5528 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5529 goto http_msg_closed;
5530 }
5531 }
5532 goto wait_other_side;
5533 }
5534
5535 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5536 http_msg_closing:
5537 /* nothing else to forward, just waiting for the output buffer
5538 * to be empty and for the shutw_now to take effect.
5539 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005540 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005541 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5542 goto http_msg_closed;
5543 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005544 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005545 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005546 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005547 if (objt_server(s->target))
5548 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005549 goto wait_other_side;
5550 }
5551 }
5552
5553 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5554 http_msg_closed:
5555 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005556 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005557 channel_auto_close(chn);
5558 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005559 goto wait_other_side;
5560 }
5561
5562 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005563 /* We force the response to leave immediately if we're waiting for the
5564 * other side, since there is no pending shutdown to push it out.
5565 */
5566 if (!channel_is_empty(chn))
5567 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005568 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005569}
5570
5571
5572/* Resync the request and response state machines. Return 1 if either state
5573 * changes.
5574 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005575int http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005576{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005577 struct http_txn *txn = s->txn;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005578 int old_req_state = txn->req.msg_state;
5579 int old_res_state = txn->rsp.msg_state;
5580
Willy Tarreau610ecce2010-01-04 21:15:02 +01005581 http_sync_req_state(s);
5582 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005583 if (!http_sync_res_state(s))
5584 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005585 if (!http_sync_req_state(s))
5586 break;
5587 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005588
Willy Tarreau610ecce2010-01-04 21:15:02 +01005589 /* OK, both state machines agree on a compatible state.
5590 * There are a few cases we're interested in :
5591 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5592 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5593 * directions, so let's simply disable both analysers.
5594 * - HTTP_MSG_CLOSED on the response only means we must abort the
5595 * request.
5596 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5597 * with server-close mode means we've completed one request and we
5598 * must re-initialize the server connection.
5599 */
5600
5601 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5602 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5603 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5604 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005605 s->req.analysers = 0;
5606 channel_auto_close(&s->req);
5607 channel_auto_read(&s->req);
5608 s->res.analysers = 0;
5609 channel_auto_close(&s->res);
5610 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005611 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005612 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005613 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005614 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005615 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005616 s->res.analysers = 0;
5617 channel_auto_close(&s->res);
5618 channel_auto_read(&s->res);
5619 s->req.analysers = 0;
5620 channel_abort(&s->req);
5621 channel_auto_close(&s->req);
5622 channel_auto_read(&s->req);
5623 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005624 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005625 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5626 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005627 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005628 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5629 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5630 /* server-close/keep-alive: terminate this transaction,
5631 * possibly killing the server connection and reinitialize
5632 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005633 */
5634 http_end_txn_clean_session(s);
5635 }
5636
Willy Tarreau610ecce2010-01-04 21:15:02 +01005637 return txn->req.msg_state != old_req_state ||
5638 txn->rsp.msg_state != old_res_state;
5639}
5640
Willy Tarreaud98cf932009-12-27 22:54:55 +01005641/* This function is an analyser which forwards request body (including chunk
5642 * sizes if any). It is called as soon as we must forward, even if we forward
5643 * zero byte. The only situation where it must not be called is when we're in
5644 * tunnel mode and we want to forward till the close. It's used both to forward
5645 * remaining data and to resync after end of body. It expects the msg_state to
5646 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005647 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005648 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005649 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005650 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005651int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005652{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005653 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005654 struct http_txn *txn = s->txn;
5655 struct http_msg *msg = &s->txn->req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005656
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005657 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5658 return 0;
5659
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005660 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005661 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005662 /* Output closed while we were sending data. We must abort and
5663 * wake the other side up.
5664 */
5665 msg->msg_state = HTTP_MSG_ERROR;
5666 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005667 return 1;
5668 }
5669
Willy Tarreaud98cf932009-12-27 22:54:55 +01005670 /* Note that we don't have to send 100-continue back because we don't
5671 * need the data to complete our job, and it's up to the server to
5672 * decide whether to return 100, 417 or anything else in return of
5673 * an "Expect: 100-continue" header.
5674 */
5675
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005676 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005677 /* we have msg->sov which points to the first byte of message
5678 * body, and req->buf.p still points to the beginning of the
5679 * message. We forward the headers now, as we don't need them
5680 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005681 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005682 b_adv(req->buf, msg->sov);
5683 msg->next -= msg->sov;
5684 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005685
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005686 /* The previous analysers guarantee that the state is somewhere
5687 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5688 * msg->next are always correct.
5689 */
5690 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5691 if (msg->flags & HTTP_MSGF_TE_CHNK)
5692 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5693 else
5694 msg->msg_state = HTTP_MSG_DATA;
5695 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005696 }
5697
Willy Tarreau7ba23542014-04-17 21:50:00 +02005698 /* Some post-connect processing might want us to refrain from starting to
5699 * forward data. Currently, the only reason for this is "balance url_param"
5700 * whichs need to parse/process the request after we've enabled forwarding.
5701 */
5702 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005703 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005704 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005705 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005706 goto missing_data;
5707 }
5708 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5709 }
5710
Willy Tarreau80a92c02014-03-12 10:41:13 +01005711 /* in most states, we should abort in case of early close */
5712 channel_auto_close(req);
5713
Willy Tarreauefdf0942014-04-24 20:08:57 +02005714 if (req->to_forward) {
5715 /* We can't process the buffer's contents yet */
5716 req->flags |= CF_WAKE_WRITE;
5717 goto missing_data;
5718 }
5719
Willy Tarreaud98cf932009-12-27 22:54:55 +01005720 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005721 if (msg->msg_state == HTTP_MSG_DATA) {
5722 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005723 /* we may have some pending data starting at req->buf->p */
5724 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005725 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005726 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005727 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005728 msg->next += msg->chunk_len;
5729 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005730
5731 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005732 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005733 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005734 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005735 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005736 }
5737 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005738 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005739 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005740 * TRAILERS state.
5741 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005742 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005743
Willy Tarreau54d23df2012-10-25 19:04:45 +02005744 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005745 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005746 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005747 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005748 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005749 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005750 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005751 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005752 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005753 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005754 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005755 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005756 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005757
5758 if (ret == 0)
5759 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005760 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005761 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005762 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005763 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005764 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005765 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005766 /* we're in MSG_CHUNK_SIZE now */
5767 }
5768 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005769 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005770
5771 if (ret == 0)
5772 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005773 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005774 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005775 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005776 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005777 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005778 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005779 /* we're in HTTP_MSG_DONE now */
5780 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005781 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005782 int old_state = msg->msg_state;
5783
Willy Tarreau610ecce2010-01-04 21:15:02 +01005784 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005785
5786 /* we may have some pending data starting at req->buf->p
5787 * such as last chunk of data or trailers.
5788 */
5789 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005790 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005791 msg->sov -= msg->next;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005792 msg->next = 0;
5793
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005794 /* we don't want to forward closes on DONE except in
5795 * tunnel mode.
5796 */
5797 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005798 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005799 if (http_resync_states(s)) {
5800 /* some state changes occurred, maybe the analyser
5801 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005802 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005803 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005804 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005805 /* request errors are most likely due to
5806 * the server aborting the transfer.
5807 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005808 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005809 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005810 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005811 http_capture_bad_message(&sess->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005812 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005813 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005814 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005815 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005816
5817 /* If "option abortonclose" is set on the backend, we
5818 * want to monitor the client's connection and forward
5819 * any shutdown notification to the server, which will
5820 * decide whether to close or to go on processing the
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005821 * request. We only do that in tunnel mode, and not in
5822 * other modes since it can be abused to exhaust source
5823 * ports.
Willy Tarreau5c54c712010-07-17 08:02:58 +02005824 */
5825 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005826 channel_auto_read(req);
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005827 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
5828 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
5829 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005830 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005831 }
Willy Tarreaueee5b512015-04-03 23:46:31 +02005832 else if (s->txn->meth == HTTP_METH_POST) {
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005833 /* POST requests may require to read extra CRLF
5834 * sent by broken browsers and which could cause
5835 * an RST to be sent upon close on some systems
5836 * (eg: Linux).
5837 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005838 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005839 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005840
Willy Tarreau610ecce2010-01-04 21:15:02 +01005841 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005842 }
5843 }
5844
Willy Tarreaud98cf932009-12-27 22:54:55 +01005845 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005846 /* we may have some pending data starting at req->buf->p */
5847 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005848 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005849 msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i);
5850
Willy Tarreaubed410e2014-04-22 08:19:34 +02005851 msg->next = 0;
5852 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5853
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005854 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005855 if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005856 if (!(s->flags & SF_ERR_MASK))
5857 s->flags |= SF_ERR_CLICL;
5858 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005859 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005860 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005861 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005862 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005863 }
5864
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005865 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005866 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005867 if (objt_server(s->target))
5868 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005869
5870 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005871 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005872
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005873 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005874 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005875 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005876
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005877 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005878 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005879 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005880 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005881 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005882
Willy Tarreau5c620922011-05-11 19:56:11 +02005883 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005884 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005885 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005886 * modes are already handled by the stream sock layer. We must not do
5887 * this in content-length mode because it could present the MSG_MORE
5888 * flag with the last block of forwarded data, which would cause an
5889 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005890 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005891 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005892 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005893
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005894 return 0;
5895
Willy Tarreaud98cf932009-12-27 22:54:55 +01005896 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005897 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005898 if (sess->listener->counters)
5899 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005900
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005901 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005902 /* we may have some pending data starting at req->buf->p */
5903 b_adv(req->buf, msg->next);
5904 msg->next = 0;
5905
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005906 txn->req.msg_state = HTTP_MSG_ERROR;
5907 if (txn->status) {
5908 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005909 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005910 } else {
5911 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01005912 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005913 }
5914 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005915 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005916
Willy Tarreaue7dff022015-04-03 01:14:29 +02005917 if (!(s->flags & SF_ERR_MASK))
5918 s->flags |= SF_ERR_PRXCOND;
5919 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005920 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005921 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005922 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005923 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005924 }
5925 return 0;
5926
5927 aborted_xfer:
5928 txn->req.msg_state = HTTP_MSG_ERROR;
5929 if (txn->status) {
5930 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005931 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005932 } else {
5933 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005934 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005935 }
5936 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005937 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005938
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005939 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005940 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005941 if (objt_server(s->target))
5942 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005943
Willy Tarreaue7dff022015-04-03 01:14:29 +02005944 if (!(s->flags & SF_ERR_MASK))
5945 s->flags |= SF_ERR_SRVCL;
5946 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005947 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005948 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005949 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005950 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005951 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005952 return 0;
5953}
5954
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005955/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5956 * processing can continue on next analysers, or zero if it either needs more
5957 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005958 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005959 * when it has nothing left to do, and may remove any analyser when it wants to
5960 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005961 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005962int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005963{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005964 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005965 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005966 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005967 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005968 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005969 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005970 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005971
Willy Tarreau87b09662015-04-03 00:22:06 +02005972 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 +02005973 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005974 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005975 rep,
5976 rep->rex, rep->wex,
5977 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005978 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005979 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005980
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005981 /*
5982 * Now parse the partial (or complete) lines.
5983 * We will check the response syntax, and also join multi-line
5984 * headers. An index of all the lines will be elaborated while
5985 * parsing.
5986 *
5987 * For the parsing, we use a 28 states FSM.
5988 *
5989 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005990 * rep->buf->p = beginning of response
5991 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5992 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005993 * msg->eol = end of current header or line (LF or CRLF)
5994 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005995 */
5996
Willy Tarreau628c40c2014-04-24 19:11:26 +02005997 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005998 /* There's a protected area at the end of the buffer for rewriting
5999 * purposes. We don't want to start to parse the request if the
6000 * protected area is affected, because we may have to move processed
6001 * data later, which is much more complicated.
6002 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006003 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01006004 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02006005 /* some data has still not left the buffer, wake us once that's done */
6006 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
6007 goto abort_response;
6008 channel_dont_close(rep);
6009 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01006010 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02006011 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01006012 }
6013
Willy Tarreau379357a2013-06-08 12:55:46 +02006014 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
6015 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
6016 buffer_slow_realign(rep->buf);
6017
Willy Tarreau9b28e032012-10-12 23:49:43 +02006018 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01006019 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01006020 }
6021
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006022 /* 1: we might have to print this header in debug mode */
6023 if (unlikely((global.mode & MODE_DEBUG) &&
6024 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02006025 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006026 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006027
Willy Tarreau9b28e032012-10-12 23:49:43 +02006028 sol = rep->buf->p;
6029 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006030 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006031
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006032 sol += hdr_idx_first_pos(&txn->hdr_idx);
6033 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006034
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006035 while (cur_idx) {
6036 eol = sol + txn->hdr_idx.v[cur_idx].len;
6037 debug_hdr("srvhdr", s, sol, eol);
6038 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
6039 cur_idx = txn->hdr_idx.v[cur_idx].next;
6040 }
6041 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006042
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006043 /*
6044 * Now we quickly check if we have found a full valid response.
6045 * If not so, we check the FD and buffer states before leaving.
6046 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01006047 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006048 * responses are checked first.
6049 *
6050 * Depending on whether the client is still there or not, we
6051 * may send an error response back or not. Note that normally
6052 * we should only check for HTTP status there, and check I/O
6053 * errors somewhere else.
6054 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006055
Willy Tarreau655dce92009-11-08 13:10:58 +01006056 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006057 /* Invalid response */
6058 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6059 /* we detected a parsing error. We want to archive this response
6060 * in the dedicated proxy area for later troubleshooting.
6061 */
6062 hdr_response_bad:
6063 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006064 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006065
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006066 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006067 if (objt_server(s->target)) {
6068 objt_server(s->target)->counters.failed_resp++;
6069 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006070 }
Willy Tarreau64648412010-03-05 10:41:54 +01006071 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006072 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006073 rep->analysers = 0;
6074 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006075 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006076 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006077 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006078
Willy Tarreaue7dff022015-04-03 01:14:29 +02006079 if (!(s->flags & SF_ERR_MASK))
6080 s->flags |= SF_ERR_PRXCOND;
6081 if (!(s->flags & SF_FINST_MASK))
6082 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006083
6084 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006085 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006086
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006087 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006088 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02006089 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02006090 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006091 goto hdr_response_bad;
6092 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006093
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006094 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006095 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006096 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006097 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006098 else if (txn->flags & TX_NOT_FIRST)
6099 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02006100
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006101 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006102 if (objt_server(s->target)) {
6103 objt_server(s->target)->counters.failed_resp++;
6104 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006105 }
Willy Tarreau461f6622008-08-15 23:43:19 +02006106
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006107 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006108 rep->analysers = 0;
6109 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006110 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006111 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006112 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02006113
Willy Tarreaue7dff022015-04-03 01:14:29 +02006114 if (!(s->flags & SF_ERR_MASK))
6115 s->flags |= SF_ERR_SRVCL;
6116 if (!(s->flags & SF_FINST_MASK))
6117 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02006118 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006119 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006120
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02006121 /* read timeout : return a 504 to the client. */
6122 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006123 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006124 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006125 else if (txn->flags & TX_NOT_FIRST)
6126 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006127
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006128 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006129 if (objt_server(s->target)) {
6130 objt_server(s->target)->counters.failed_resp++;
6131 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006132 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006133
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006134 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006135 rep->analysers = 0;
6136 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01006137 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006138 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006139 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02006140
Willy Tarreaue7dff022015-04-03 01:14:29 +02006141 if (!(s->flags & SF_ERR_MASK))
6142 s->flags |= SF_ERR_SRVTO;
6143 if (!(s->flags & SF_FINST_MASK))
6144 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006145 return 0;
6146 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02006147
Willy Tarreauf003d372012-11-26 13:35:37 +01006148 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006149 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006150 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01006151 s->be->be_counters.cli_aborts++;
6152 if (objt_server(s->target))
6153 objt_server(s->target)->counters.cli_aborts++;
6154
6155 rep->analysers = 0;
6156 channel_auto_close(rep);
6157
6158 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01006159 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006160 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreauf003d372012-11-26 13:35:37 +01006161
Willy Tarreaue7dff022015-04-03 01:14:29 +02006162 if (!(s->flags & SF_ERR_MASK))
6163 s->flags |= SF_ERR_CLICL;
6164 if (!(s->flags & SF_FINST_MASK))
6165 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01006166
Willy Tarreau87b09662015-04-03 00:22:06 +02006167 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01006168 return 0;
6169 }
6170
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006171 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006172 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006173 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006174 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006175 else if (txn->flags & TX_NOT_FIRST)
6176 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006177
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006178 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006179 if (objt_server(s->target)) {
6180 objt_server(s->target)->counters.failed_resp++;
6181 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006182 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006183
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006184 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006185 rep->analysers = 0;
6186 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006187 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006188 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006189 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01006190
Willy Tarreaue7dff022015-04-03 01:14:29 +02006191 if (!(s->flags & SF_ERR_MASK))
6192 s->flags |= SF_ERR_SRVCL;
6193 if (!(s->flags & SF_FINST_MASK))
6194 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006195 return 0;
6196 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006197
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006198 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006199 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006200 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006201 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006202 else if (txn->flags & TX_NOT_FIRST)
6203 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006204
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006205 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006206 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006207 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006208
Willy Tarreaue7dff022015-04-03 01:14:29 +02006209 if (!(s->flags & SF_ERR_MASK))
6210 s->flags |= SF_ERR_CLICL;
6211 if (!(s->flags & SF_FINST_MASK))
6212 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006213
Willy Tarreau87b09662015-04-03 00:22:06 +02006214 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006215 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006216 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006217
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006218 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01006219 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006220 return 0;
6221 }
6222
6223 /* More interesting part now : we know that we have a complete
6224 * response which at least looks like HTTP. We have an indicator
6225 * of each header's length, so we can parse them quickly.
6226 */
6227
6228 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006229 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006230
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006231 /*
6232 * 1: get the status code
6233 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006234 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006235 if (n < 1 || n > 5)
6236 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02006237 /* when the client triggers a 4xx from the server, it's most often due
6238 * to a missing object or permission. These events should be tracked
6239 * because if they happen often, it may indicate a brute force or a
6240 * vulnerability scan.
6241 */
6242 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02006243 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02006244
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006245 if (objt_server(s->target))
6246 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006247
Willy Tarreau91852eb2015-05-01 13:26:00 +02006248 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
6249 * exactly one digit "." one digit. This check may be disabled using
6250 * option accept-invalid-http-response.
6251 */
6252 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
6253 if (msg->sl.st.v_l != 8) {
6254 msg->err_pos = 0;
6255 goto hdr_response_bad;
6256 }
6257
6258 if (rep->buf->p[4] != '/' ||
6259 !isdigit((unsigned char)rep->buf->p[5]) ||
6260 rep->buf->p[6] != '.' ||
6261 !isdigit((unsigned char)rep->buf->p[7])) {
6262 msg->err_pos = 4;
6263 goto hdr_response_bad;
6264 }
6265 }
6266
Willy Tarreau5b154472009-12-21 20:11:07 +01006267 /* check if the response is HTTP/1.1 or above */
6268 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006269 ((rep->buf->p[5] > '1') ||
6270 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006271 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006272
6273 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006274 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 +01006275
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006276 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006277 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006278
Willy Tarreau9b28e032012-10-12 23:49:43 +02006279 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006280
Willy Tarreau39650402010-03-15 19:44:39 +01006281 /* Adjust server's health based on status code. Note: status codes 501
6282 * and 505 are triggered on demand by client request, so we must not
6283 * count them as server failures.
6284 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006285 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006286 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006287 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006288 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006289 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006290 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006291
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006292 /*
6293 * 2: check for cacheability.
6294 */
6295
6296 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006297 case 100:
6298 /*
6299 * We may be facing a 100-continue response, in which case this
6300 * is not the right response, and we're waiting for the next one.
6301 * Let's allow this response to go to the client and wait for the
6302 * next one.
6303 */
6304 hdr_idx_init(&txn->hdr_idx);
6305 msg->next -= channel_forward(rep, msg->next);
6306 msg->msg_state = HTTP_MSG_RPBEFORE;
6307 txn->status = 0;
6308 s->logs.t_data = -1; /* was not a response yet */
6309 goto next_one;
6310
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006311 case 200:
6312 case 203:
6313 case 206:
6314 case 300:
6315 case 301:
6316 case 410:
6317 /* RFC2616 @13.4:
6318 * "A response received with a status code of
6319 * 200, 203, 206, 300, 301 or 410 MAY be stored
6320 * by a cache (...) unless a cache-control
6321 * directive prohibits caching."
6322 *
6323 * RFC2616 @9.5: POST method :
6324 * "Responses to this method are not cacheable,
6325 * unless the response includes appropriate
6326 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006327 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006328 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006329 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006330 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6331 break;
6332 default:
6333 break;
6334 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006335
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006336 /*
6337 * 3: we may need to capture headers
6338 */
6339 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006340 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006341 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006342 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006343
Willy Tarreau557f1992015-05-01 10:05:17 +02006344 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6345 * by RFC7230#3.3.3 :
6346 *
6347 * The length of a message body is determined by one of the following
6348 * (in order of precedence):
6349 *
6350 * 1. Any response to a HEAD request and any response with a 1xx
6351 * (Informational), 204 (No Content), or 304 (Not Modified) status
6352 * code is always terminated by the first empty line after the
6353 * header fields, regardless of the header fields present in the
6354 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006355 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006356 * 2. Any 2xx (Successful) response to a CONNECT request implies that
6357 * the connection will become a tunnel immediately after the empty
6358 * line that concludes the header fields. A client MUST ignore any
6359 * Content-Length or Transfer-Encoding header fields received in
6360 * such a message.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006361 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006362 * 3. If a Transfer-Encoding header field is present and the chunked
6363 * transfer coding (Section 4.1) is the final encoding, the message
6364 * body length is determined by reading and decoding the chunked
6365 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006366 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006367 * If a Transfer-Encoding header field is present in a response and
6368 * the chunked transfer coding is not the final encoding, the
6369 * message body length is determined by reading the connection until
6370 * it is closed by the server. If a Transfer-Encoding header field
6371 * is present in a request and the chunked transfer coding is not
6372 * the final encoding, the message body length cannot be determined
6373 * reliably; the server MUST respond with the 400 (Bad Request)
6374 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006375 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006376 * If a message is received with both a Transfer-Encoding and a
6377 * Content-Length header field, the Transfer-Encoding overrides the
6378 * Content-Length. Such a message might indicate an attempt to
6379 * perform request smuggling (Section 9.5) or response splitting
6380 * (Section 9.4) and ought to be handled as an error. A sender MUST
6381 * remove the received Content-Length field prior to forwarding such
6382 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006383 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006384 * 4. If a message is received without Transfer-Encoding and with
6385 * either multiple Content-Length header fields having differing
6386 * field-values or a single Content-Length header field having an
6387 * invalid value, then the message framing is invalid and the
6388 * recipient MUST treat it as an unrecoverable error. If this is a
6389 * request message, the server MUST respond with a 400 (Bad Request)
6390 * status code and then close the connection. If this is a response
6391 * message received by a proxy, the proxy MUST close the connection
6392 * to the server, discard the received response, and send a 502 (Bad
6393 * Gateway) response to the client. If this is a response message
6394 * received by a user agent, the user agent MUST close the
6395 * connection to the server and discard the received response.
6396 *
6397 * 5. If a valid Content-Length header field is present without
6398 * Transfer-Encoding, its decimal value defines the expected message
6399 * body length in octets. If the sender closes the connection or
6400 * the recipient times out before the indicated number of octets are
6401 * received, the recipient MUST consider the message to be
6402 * incomplete and close the connection.
6403 *
6404 * 6. If this is a request message and none of the above are true, then
6405 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006406 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006407 * 7. Otherwise, this is a response message without a declared message
6408 * body length, so the message body length is determined by the
6409 * number of octets received prior to the server closing the
6410 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006411 */
6412
6413 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006414 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006415 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006416 * FIXME: should we parse anyway and return an error on chunked encoding ?
6417 */
6418 if (txn->meth == HTTP_METH_HEAD ||
6419 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006420 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006421 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01006422 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006423 goto skip_content_length;
6424 }
6425
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006426 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006427 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02006428 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006429 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006430 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6431 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006432 /* bad transfer-encoding (chunked followed by something else) */
6433 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006434 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006435 break;
6436 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006437 }
6438
Willy Tarreau1c913912015-04-30 10:57:51 +02006439 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006440 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006441 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02006442 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6443 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6444 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006445 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006446 signed long long cl;
6447
Willy Tarreauad14f752011-09-02 20:33:27 +02006448 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006449 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006450 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006451 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006452
Willy Tarreauad14f752011-09-02 20:33:27 +02006453 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006454 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006455 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006456 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006457
Willy Tarreauad14f752011-09-02 20:33:27 +02006458 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006459 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006460 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006461 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006462
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006463 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006464 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006465 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006466 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006467
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006468 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006469 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006470 }
6471
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006472 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02006473 select_compression_response_header(s, rep->buf);
6474
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006475skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006476 /* Now we have to check if we need to modify the Connection header.
6477 * This is more difficult on the response than it is on the request,
6478 * because we can have two different HTTP versions and we don't know
6479 * how the client will interprete a response. For instance, let's say
6480 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6481 * HTTP/1.1 response without any header. Maybe it will bound itself to
6482 * HTTP/1.0 because it only knows about it, and will consider the lack
6483 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6484 * the lack of header as a keep-alive. Thus we will use two flags
6485 * indicating how a request MAY be understood by the client. In case
6486 * of multiple possibilities, we'll fix the header to be explicit. If
6487 * ambiguous cases such as both close and keepalive are seen, then we
6488 * will fall back to explicit close. Note that we won't take risks with
6489 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006490 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006491 */
6492
Willy Tarreaudc008c52010-02-01 16:20:08 +01006493 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6494 txn->status == 101)) {
6495 /* Either we've established an explicit tunnel, or we're
6496 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006497 * to understand the next protocols. We have to switch to tunnel
6498 * mode, so that we transfer the request and responses then let
6499 * this protocol pass unmodified. When we later implement specific
6500 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006501 * header which contains information about that protocol for
6502 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006503 */
6504 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6505 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006506 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6507 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
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 Tarreau60466522010-01-18 19:08:45 +01006510 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006511
Willy Tarreau70dffda2014-01-30 03:07:23 +01006512 /* this situation happens when combining pretend-keepalive with httpclose. */
6513 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006514 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006515 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006516 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6517
Willy Tarreau60466522010-01-18 19:08:45 +01006518 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006519 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006520 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6521 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006522
Willy Tarreau60466522010-01-18 19:08:45 +01006523 /* now adjust header transformations depending on current state */
6524 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6525 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6526 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006527 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006528 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006529 }
Willy Tarreau60466522010-01-18 19:08:45 +01006530 else { /* SCL / KAL */
6531 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006532 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006533 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006534 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006535
Willy Tarreau60466522010-01-18 19:08:45 +01006536 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006537 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006538
Willy Tarreau60466522010-01-18 19:08:45 +01006539 /* Some keep-alive responses are converted to Server-close if
6540 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006541 */
Willy Tarreau60466522010-01-18 19:08:45 +01006542 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6543 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006544 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006545 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006546 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006547 }
6548
Willy Tarreau7959a552013-09-23 16:44:27 +02006549 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006550 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006551
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006552 /* end of job, return OK */
6553 rep->analysers &= ~an_bit;
6554 rep->analyse_exp = TICK_ETERNITY;
6555 channel_auto_close(rep);
6556 return 1;
6557
6558 abort_keep_alive:
6559 /* A keep-alive request to the server failed on a network error.
6560 * The client is required to retry. We need to close without returning
6561 * any other information so that the client retries.
6562 */
6563 txn->status = 0;
6564 rep->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006565 s->req.analysers = 0;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006566 channel_auto_close(rep);
6567 s->logs.logwait = 0;
6568 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006569 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006570 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006571 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006572 return 0;
6573}
6574
6575/* This function performs all the processing enabled for the current response.
6576 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006577 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006578 * other functions. It works like process_request (see indications above).
6579 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006580int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006581{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006582 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006583 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006584 struct http_msg *msg = &txn->rsp;
6585 struct proxy *cur_proxy;
6586 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006587 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006588
Willy Tarreau87b09662015-04-03 00:22:06 +02006589 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 +02006590 now_ms, __FUNCTION__,
6591 s,
6592 rep,
6593 rep->rex, rep->wex,
6594 rep->flags,
6595 rep->buf->i,
6596 rep->analysers);
6597
6598 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6599 return 0;
6600
Willy Tarreau70730dd2014-04-24 18:06:27 +02006601 /* The stats applet needs to adjust the Connection header but we don't
6602 * apply any filter there.
6603 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006604 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6605 rep->analysers &= ~an_bit;
6606 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006607 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006608 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006609
Willy Tarreau58975672014-04-24 21:13:57 +02006610 /*
6611 * We will have to evaluate the filters.
6612 * As opposed to version 1.2, now they will be evaluated in the
6613 * filters order and not in the header order. This means that
6614 * each filter has to be validated among all headers.
6615 *
6616 * Filters are tried with ->be first, then with ->fe if it is
6617 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006618 *
6619 * Maybe we are in resume condiion. In this case I choose the
6620 * "struct proxy" which contains the rule list matching the resume
6621 * pointer. If none of theses "struct proxy" match, I initialise
6622 * the process with the first one.
6623 *
6624 * In fact, I check only correspondance betwwen the current list
6625 * pointer and the ->fe rule list. If it doesn't match, I initialize
6626 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006627 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006628 if (s->current_rule_list == &sess->fe->http_res_rules)
6629 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006630 else
6631 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006632 while (1) {
6633 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006634
Willy Tarreau58975672014-04-24 21:13:57 +02006635 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02006636 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02006637 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006638
Willy Tarreau51d861a2015-05-22 17:30:48 +02006639 if (ret == HTTP_RULE_RES_BADREQ)
6640 goto return_srv_prx_502;
6641
6642 if (ret == HTTP_RULE_RES_DONE) {
6643 rep->analysers &= ~an_bit;
6644 rep->analyse_exp = TICK_ETERNITY;
6645 return 1;
6646 }
6647 }
6648
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006649 /* we need to be called again. */
6650 if (ret == HTTP_RULE_RES_YIELD) {
6651 channel_dont_close(rep);
6652 return 0;
6653 }
6654
Willy Tarreau58975672014-04-24 21:13:57 +02006655 /* try headers filters */
6656 if (rule_set->rsp_exp != NULL) {
6657 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6658 return_bad_resp:
6659 if (objt_server(s->target)) {
6660 objt_server(s->target)->counters.failed_resp++;
6661 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006662 }
Willy Tarreau58975672014-04-24 21:13:57 +02006663 s->be->be_counters.failed_resp++;
6664 return_srv_prx_502:
6665 rep->analysers = 0;
6666 txn->status = 502;
6667 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006668 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006669 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006670 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006671 if (!(s->flags & SF_ERR_MASK))
6672 s->flags |= SF_ERR_PRXCOND;
6673 if (!(s->flags & SF_FINST_MASK))
6674 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006675 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006676 }
Willy Tarreau58975672014-04-24 21:13:57 +02006677 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006678
Willy Tarreau58975672014-04-24 21:13:57 +02006679 /* has the response been denied ? */
6680 if (txn->flags & TX_SVDENY) {
6681 if (objt_server(s->target))
6682 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006683
Willy Tarreau58975672014-04-24 21:13:57 +02006684 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006685 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006686 if (sess->listener->counters)
6687 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006688
Willy Tarreau58975672014-04-24 21:13:57 +02006689 goto return_srv_prx_502;
6690 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006691
Willy Tarreau58975672014-04-24 21:13:57 +02006692 /* add response headers from the rule sets in the same order */
6693 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006694 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006695 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006696 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006697 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02006698 ret = acl_pass(ret);
6699 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6700 ret = !ret;
6701 if (!ret)
6702 continue;
6703 }
6704 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6705 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006706 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006707
Willy Tarreau58975672014-04-24 21:13:57 +02006708 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006709 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006710 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006711 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006712 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006713
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006714 /* After this point, this anayzer can't return yield, so we can
6715 * remove the bit corresponding to this analyzer from the list.
6716 *
6717 * Note that the intermediate returns and goto found previously
6718 * reset the analyzers.
6719 */
6720 rep->analysers &= ~an_bit;
6721 rep->analyse_exp = TICK_ETERNITY;
6722
Willy Tarreau58975672014-04-24 21:13:57 +02006723 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006724 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006725 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006726
Willy Tarreau58975672014-04-24 21:13:57 +02006727 /*
6728 * Now check for a server cookie.
6729 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006730 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02006731 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006732
Willy Tarreau58975672014-04-24 21:13:57 +02006733 /*
6734 * Check for cache-control or pragma headers if required.
6735 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006736 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 +02006737 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006738
Willy Tarreau58975672014-04-24 21:13:57 +02006739 /*
6740 * Add server cookie in the response if needed
6741 */
6742 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6743 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006744 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006745 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6746 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6747 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6748 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6749 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006750 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006751 /* the server is known, it's not the one the client requested, or the
6752 * cookie's last seen date needs to be refreshed. We have to
6753 * insert a set-cookie here, except if we want to insert only on POST
6754 * requests and this one isn't. Note that servers which don't have cookies
6755 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006756 */
Willy Tarreau58975672014-04-24 21:13:57 +02006757 if (!objt_server(s->target)->cookie) {
6758 chunk_printf(&trash,
6759 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6760 s->be->cookie_name);
6761 }
6762 else {
6763 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006764
Willy Tarreau58975672014-04-24 21:13:57 +02006765 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6766 /* emit last_date, which is mandatory */
6767 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6768 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6769 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006770
Willy Tarreau58975672014-04-24 21:13:57 +02006771 if (s->be->cookie_maxlife) {
6772 /* emit first_date, which is either the original one or
6773 * the current date.
6774 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006775 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006776 s30tob64(txn->cookie_first_date ?
6777 txn->cookie_first_date >> 2 :
6778 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006779 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006780 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006781 }
Willy Tarreau58975672014-04-24 21:13:57 +02006782 chunk_appendf(&trash, "; path=/");
6783 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006784
Willy Tarreau58975672014-04-24 21:13:57 +02006785 if (s->be->cookie_domain)
6786 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006787
Willy Tarreau58975672014-04-24 21:13:57 +02006788 if (s->be->ck_opts & PR_CK_HTTPONLY)
6789 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006790
Willy Tarreau58975672014-04-24 21:13:57 +02006791 if (s->be->ck_opts & PR_CK_SECURE)
6792 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006793
Willy Tarreau58975672014-04-24 21:13:57 +02006794 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6795 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006796
Willy Tarreau58975672014-04-24 21:13:57 +02006797 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006798 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006799 /* the server did not change, only the date was updated */
6800 txn->flags |= TX_SCK_UPDATED;
6801 else
6802 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006803
Willy Tarreau58975672014-04-24 21:13:57 +02006804 /* Here, we will tell an eventual cache on the client side that we don't
6805 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6806 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6807 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006808 */
Willy Tarreau58975672014-04-24 21:13:57 +02006809 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006810
Willy Tarreau58975672014-04-24 21:13:57 +02006811 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006812
Willy Tarreau58975672014-04-24 21:13:57 +02006813 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6814 "Cache-control: private", 22) < 0))
6815 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006816 }
Willy Tarreau58975672014-04-24 21:13:57 +02006817 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006818
Willy Tarreau58975672014-04-24 21:13:57 +02006819 /*
6820 * Check if result will be cacheable with a cookie.
6821 * We'll block the response if security checks have caught
6822 * nasty things such as a cacheable cookie.
6823 */
6824 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6825 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6826 (s->be->options & PR_O_CHK_CACHE)) {
6827 /* we're in presence of a cacheable response containing
6828 * a set-cookie header. We'll block it as requested by
6829 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006830 */
Willy Tarreau58975672014-04-24 21:13:57 +02006831 if (objt_server(s->target))
6832 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006833
Willy Tarreau58975672014-04-24 21:13:57 +02006834 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006835 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006836 if (sess->listener->counters)
6837 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006838
Willy Tarreau58975672014-04-24 21:13:57 +02006839 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6840 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6841 send_log(s->be, LOG_ALERT,
6842 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6843 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6844 goto return_srv_prx_502;
6845 }
Willy Tarreau03945942009-12-22 16:50:27 +01006846
Willy Tarreau70730dd2014-04-24 18:06:27 +02006847 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006848 /*
6849 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6850 * If an "Upgrade" token is found, the header is left untouched in order
6851 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006852 * if anything but "Upgrade" is present in the Connection header. We don't
6853 * want to touch any 101 response either since it's switching to another
6854 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006855 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006856 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006857 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006858 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006859 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6860 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006861
Willy Tarreau58975672014-04-24 21:13:57 +02006862 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6863 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6864 /* we want a keep-alive response here. Keep-alive header
6865 * required if either side is not 1.1.
6866 */
6867 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6868 want_flags |= TX_CON_KAL_SET;
6869 }
6870 else {
6871 /* we want a close response here. Close header required if
6872 * the server is 1.1, regardless of the client.
6873 */
6874 if (msg->flags & HTTP_MSGF_VER_11)
6875 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006876 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006877
Willy Tarreau58975672014-04-24 21:13:57 +02006878 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6879 http_change_connection_header(txn, msg, want_flags);
6880 }
6881
6882 skip_header_mangling:
6883 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6884 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6885 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006886
Willy Tarreau58975672014-04-24 21:13:57 +02006887 /* if the user wants to log as soon as possible, without counting
6888 * bytes from the server, then this is the right moment. We have
6889 * to temporarily assign bytes_out to log what we currently have.
6890 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006891 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006892 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6893 s->logs.bytes_out = txn->rsp.eoh;
6894 s->do_log(s);
6895 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006896 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006897 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006898}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006899
Willy Tarreaud98cf932009-12-27 22:54:55 +01006900/* This function is an analyser which forwards response body (including chunk
6901 * sizes if any). It is called as soon as we must forward, even if we forward
6902 * zero byte. The only situation where it must not be called is when we're in
6903 * tunnel mode and we want to forward till the close. It's used both to forward
6904 * remaining data and to resync after end of body. It expects the msg_state to
6905 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006906 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006907 *
6908 * It is capable of compressing response data both in content-length mode and
6909 * in chunked mode. The state machines follows different flows depending on
6910 * whether content-length and chunked modes are used, since there are no
6911 * trailers in content-length :
6912 *
6913 * chk-mode cl-mode
6914 * ,----- BODY -----.
6915 * / \
6916 * V size > 0 V chk-mode
6917 * .--> SIZE -------------> DATA -------------> CRLF
6918 * | | size == 0 | last byte |
6919 * | v final crlf v inspected |
6920 * | TRAILERS -----------> DONE |
6921 * | |
6922 * `----------------------------------------------'
6923 *
6924 * Compression only happens in the DATA state, and must be flushed in final
6925 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6926 * is performed at once on final states for all bytes parsed, or when leaving
6927 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006928 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006929int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006930{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006931 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006932 struct http_txn *txn = s->txn;
6933 struct http_msg *msg = &s->txn->rsp;
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006934 static struct buffer *tmpbuf = &buf_empty;
William Lallemand82fe75c2012-10-23 10:25:10 +02006935 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006936 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006937
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006938 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6939 return 0;
6940
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006941 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006942 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006943 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006944 /* Output closed while we were sending data. We must abort and
6945 * wake the other side up.
6946 */
6947 msg->msg_state = HTTP_MSG_ERROR;
6948 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006949 return 1;
6950 }
6951
Willy Tarreau4fe41902010-06-07 22:27:41 +02006952 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006953 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006954
Willy Tarreaubb2e6692014-07-10 19:06:10 +02006955 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006956 /* we have msg->sov which points to the first byte of message
6957 * body, and res->buf.p still points to the beginning of the
6958 * message. We forward the headers now, as we don't need them
6959 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006960 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006961 b_adv(res->buf, msg->sov);
6962 msg->next -= msg->sov;
6963 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006964
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006965 /* The previous analysers guarantee that the state is somewhere
6966 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6967 * msg->next are always correct.
6968 */
6969 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6970 if (msg->flags & HTTP_MSGF_TE_CHNK)
6971 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6972 else
6973 msg->msg_state = HTTP_MSG_DATA;
6974 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006975 }
6976
Willy Tarreauefdf0942014-04-24 20:08:57 +02006977 if (res->to_forward) {
6978 /* We can't process the buffer's contents yet */
6979 res->flags |= CF_WAKE_WRITE;
6980 goto missing_data;
6981 }
6982
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006983 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6984 /* We need a compression buffer in the DATA state to put the
6985 * output of compressed data, and in CRLF state to let the
6986 * TRAILERS state finish the job of removing the trailing CRLF.
6987 */
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006988 if (unlikely(!tmpbuf->size)) {
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006989 /* this is the first time we need the compression buffer */
Willy Tarreaue583ea52014-11-24 11:30:16 +01006990 if (b_alloc(&tmpbuf) == NULL)
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006991 goto aborted_xfer; /* no memory */
6992 }
6993
6994 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006995 if (ret < 0) {
6996 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006997 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006998 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006999 compressing = 1;
7000 }
7001
Willy Tarreaud98cf932009-12-27 22:54:55 +01007002 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007003 switch (msg->msg_state - HTTP_MSG_DATA) {
7004 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02007005 /* we may have some pending data starting at res->buf->p */
7006 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02007007 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01007008 if (ret < 0)
7009 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02007010
Willy Tarreaud5a67832014-04-21 10:54:27 +02007011 if (msg->chunk_len) {
7012 /* input empty or output full */
7013 if (res->buf->i > msg->next)
7014 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02007015 goto missing_data;
7016 }
William Lallemandbf3ae612012-11-19 12:35:37 +01007017 }
Willy Tarreauc623c172014-04-18 09:53:50 +02007018 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02007019 if (msg->chunk_len > res->buf->i - msg->next) {
7020 /* output full */
7021 res->flags |= CF_WAKE_WRITE;
7022 goto missing_data;
7023 }
Willy Tarreauc623c172014-04-18 09:53:50 +02007024 msg->next += msg->chunk_len;
7025 msg->chunk_len = 0;
7026 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01007027
7028 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01007029 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02007030 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01007031 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01007032 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007033 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01007034 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007035 /* fall through for HTTP_MSG_CHUNK_CRLF */
7036
7037 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
7038 /* we want the CRLF after the data */
7039
7040 ret = http_skip_chunk_crlf(msg);
7041 if (ret == 0)
7042 goto missing_data;
7043 else if (ret < 0) {
7044 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007045 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, sess->fe);
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007046 goto return_bad_res;
7047 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007048 /* we're in MSG_CHUNK_SIZE now, fall through */
7049
7050 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01007051 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02007052 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01007053 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01007054 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01007055
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007056 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02007057 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01007058 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007059 else if (ret < 0) {
7060 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007061 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007062 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007063 }
Willy Tarreau0161d622013-04-02 01:26:55 +02007064 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007065 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01007066
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007067 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02007068 if (unlikely(compressing)) {
7069 /* we need to flush output contents before syncing FSMs */
7070 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
7071 compressing = 0;
7072 }
7073
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007074 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007075 if (ret == 0)
7076 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007077 else if (ret < 0) {
7078 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007079 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007080 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007081 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02007082 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007083
7084 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01007085 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02007086 if (unlikely(compressing)) {
7087 /* we need to flush output contents before syncing FSMs */
7088 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
7089 compressing = 0;
7090 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007091
Willy Tarreauc623c172014-04-18 09:53:50 +02007092 /* we may have some pending data starting at res->buf->p
7093 * such as a last chunk of data or trailers.
7094 */
7095 b_adv(res->buf, msg->next);
7096 msg->next = 0;
7097
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007098 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02007099 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007100 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
7101 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02007102 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02007103
Willy Tarreau610ecce2010-01-04 21:15:02 +01007104 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01007105 /* some state changes occurred, maybe the analyser
7106 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01007107 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007108 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007109 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007110 /* response errors are most likely due to
7111 * the client aborting the transfer.
7112 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007113 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007114 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007115 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007116 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, sess->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01007117 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007118 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01007119 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01007120 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01007121 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007122 }
7123 }
7124
Willy Tarreaud98cf932009-12-27 22:54:55 +01007125 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02007126 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02007127 if (unlikely(compressing)) {
7128 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02007129 compressing = 0;
7130 }
Willy Tarreauf003d372012-11-26 13:35:37 +01007131
Willy Tarreauc623c172014-04-18 09:53:50 +02007132 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
7133 b_adv(res->buf, msg->next);
7134 msg->next = 0;
7135 msg->chunk_len -= channel_forward(res, msg->chunk_len);
7136 }
7137
Willy Tarreauf003d372012-11-26 13:35:37 +01007138 if (res->flags & CF_SHUTW)
7139 goto aborted_xfer;
7140
7141 /* stop waiting for data if the input is closed before the end. If the
7142 * client side was already closed, it means that the client has aborted,
7143 * so we don't want to count this as a server abort. Otherwise it's a
7144 * server abort.
7145 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007146 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007147 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01007148 goto aborted_xfer;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007149 if (!(s->flags & SF_ERR_MASK))
7150 s->flags |= SF_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007151 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007152 if (objt_server(s->target))
7153 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007154 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01007155 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01007156
Willy Tarreau40dba092010-03-04 18:14:51 +01007157 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007158 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01007159 goto return_bad_res;
7160
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007161 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007162 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007163 * Similarly, with keep-alive on the client side, we don't want to forward a
7164 * close.
7165 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02007166 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007167 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
7168 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02007169 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007170
Willy Tarreau5c620922011-05-11 19:56:11 +02007171 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007172 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02007173 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01007174 * modes are already handled by the stream sock layer. We must not do
7175 * this in content-length mode because it could present the MSG_MORE
7176 * flag with the last block of forwarded data, which would cause an
7177 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02007178 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02007179 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007180 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02007181
Willy Tarreau87b09662015-04-03 00:22:06 +02007182 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01007183 return 0;
7184
Willy Tarreau40dba092010-03-04 18:14:51 +01007185 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007186 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007187 if (objt_server(s->target))
7188 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007189
7190 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02007191 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02007192 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02007193 compressing = 0;
7194 }
7195
Willy Tarreauc623c172014-04-18 09:53:50 +02007196 /* we may have some pending data starting at res->buf->p */
7197 if (s->comp_algo == NULL) {
7198 b_adv(res->buf, msg->next);
7199 msg->next = 0;
7200 }
7201
Willy Tarreaud98cf932009-12-27 22:54:55 +01007202 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01007203 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01007204 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007205 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007206 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007207 if (objt_server(s->target))
7208 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007209
Willy Tarreaue7dff022015-04-03 01:14:29 +02007210 if (!(s->flags & SF_ERR_MASK))
7211 s->flags |= SF_ERR_PRXCOND;
7212 if (!(s->flags & SF_FINST_MASK))
7213 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007214 return 0;
7215
7216 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02007217 if (unlikely(compressing)) {
7218 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
7219 compressing = 0;
7220 }
7221
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007222 txn->rsp.msg_state = HTTP_MSG_ERROR;
7223 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01007224 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007225 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007226 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007227
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007228 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007229 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007230 if (objt_server(s->target))
7231 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007232
Willy Tarreaue7dff022015-04-03 01:14:29 +02007233 if (!(s->flags & SF_ERR_MASK))
7234 s->flags |= SF_ERR_CLICL;
7235 if (!(s->flags & SF_FINST_MASK))
7236 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007237 return 0;
7238}
7239
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007240/* Iterate the same filter through all request headers.
7241 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007242 * Since it can manage the switch to another backend, it updates the per-proxy
7243 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007244 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007245int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007246{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007247 char *cur_ptr, *cur_end, *cur_next;
7248 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007249 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007250 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007251 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01007252
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007253 last_hdr = 0;
7254
Willy Tarreau9b28e032012-10-12 23:49:43 +02007255 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007256 old_idx = 0;
7257
7258 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007259 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007260 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007261 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007262 (exp->action == ACT_ALLOW ||
7263 exp->action == ACT_DENY ||
7264 exp->action == ACT_TARPIT))
7265 return 0;
7266
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007267 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007268 if (!cur_idx)
7269 break;
7270
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007271 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007272 cur_ptr = cur_next;
7273 cur_end = cur_ptr + cur_hdr->len;
7274 cur_next = cur_end + cur_hdr->cr + 1;
7275
7276 /* Now we have one header between cur_ptr and cur_end,
7277 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007278 */
7279
Willy Tarreau15a53a42015-01-21 13:39:42 +01007280 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007281 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007282 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007283 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007284 last_hdr = 1;
7285 break;
7286
7287 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007288 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007289 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007290 break;
7291
7292 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007293 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007294 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007295 break;
7296
7297 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007298 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7299 if (trash.len < 0)
7300 return -1;
7301
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007302 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007303 /* FIXME: if the user adds a newline in the replacement, the
7304 * index will not be recalculated for now, and the new line
7305 * will not be counted as a new header.
7306 */
7307
7308 cur_end += delta;
7309 cur_next += delta;
7310 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007311 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007312 break;
7313
7314 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007315 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007316 cur_next += delta;
7317
Willy Tarreaufa355d42009-11-29 18:12:29 +01007318 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007319 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7320 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007321 cur_hdr->len = 0;
7322 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007323 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007324 break;
7325
7326 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007327 }
7328
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007329 /* keep the link from this header to next one in case of later
7330 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007331 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007332 old_idx = cur_idx;
7333 }
7334 return 0;
7335}
7336
7337
7338/* Apply the filter to the request line.
7339 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7340 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007341 * Since it can manage the switch to another backend, it updates the per-proxy
7342 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007343 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007344int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007345{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007346 char *cur_ptr, *cur_end;
7347 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007348 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007349 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007350
Willy Tarreau3d300592007-03-18 18:34:41 +01007351 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007352 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007353 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007354 (exp->action == ACT_ALLOW ||
7355 exp->action == ACT_DENY ||
7356 exp->action == ACT_TARPIT))
7357 return 0;
7358 else if (exp->action == ACT_REMOVE)
7359 return 0;
7360
7361 done = 0;
7362
Willy Tarreau9b28e032012-10-12 23:49:43 +02007363 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007364 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007365
7366 /* Now we have the request line between cur_ptr and cur_end */
7367
Willy Tarreau15a53a42015-01-21 13:39:42 +01007368 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007369 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007370 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007371 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007372 done = 1;
7373 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007374
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007375 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007376 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007377 done = 1;
7378 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007379
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007380 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007381 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007382 done = 1;
7383 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007384
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007385 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007386 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7387 if (trash.len < 0)
7388 return -1;
7389
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007390 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007391 /* FIXME: if the user adds a newline in the replacement, the
7392 * index will not be recalculated for now, and the new line
7393 * will not be counted as a new header.
7394 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007395
Willy Tarreaufa355d42009-11-29 18:12:29 +01007396 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007397 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007398 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007399 HTTP_MSG_RQMETH,
7400 cur_ptr, cur_end + 1,
7401 NULL, NULL);
7402 if (unlikely(!cur_end))
7403 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007404
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007405 /* we have a full request and we know that we have either a CR
7406 * or an LF at <ptr>.
7407 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007408 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7409 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007410 /* there is no point trying this regex on headers */
7411 return 1;
7412 }
7413 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007414 return done;
7415}
Willy Tarreau97de6242006-12-27 17:18:38 +01007416
Willy Tarreau58f10d72006-12-04 02:26:12 +01007417
Willy Tarreau58f10d72006-12-04 02:26:12 +01007418
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007419/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007420 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007421 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007422 * unparsable request. Since it can manage the switch to another backend, it
7423 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007424 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007425int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007426{
Willy Tarreau192252e2015-04-04 01:47:55 +02007427 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007428 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007429 struct hdr_exp *exp;
7430
7431 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007432 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007433
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007434 /*
7435 * The interleaving of transformations and verdicts
7436 * makes it difficult to decide to continue or stop
7437 * the evaluation.
7438 */
7439
Willy Tarreau6c123b12010-01-28 20:22:06 +01007440 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7441 break;
7442
Willy Tarreau3d300592007-03-18 18:34:41 +01007443 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007444 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007445 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007446 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007447
7448 /* if this filter had a condition, evaluate it now and skip to
7449 * next filter if the condition does not match.
7450 */
7451 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007452 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007453 ret = acl_pass(ret);
7454 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7455 ret = !ret;
7456
7457 if (!ret)
7458 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007459 }
7460
7461 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007462 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007463 if (unlikely(ret < 0))
7464 return -1;
7465
7466 if (likely(ret == 0)) {
7467 /* The filter did not match the request, it can be
7468 * iterated through all headers.
7469 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007470 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7471 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007472 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007473 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007474 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007475}
7476
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007477
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007478/* Find the end of a cookie value contained between <s> and <e>. It works the
7479 * same way as with headers above except that the semi-colon also ends a token.
7480 * See RFC2965 for more information. Note that it requires a valid header to
7481 * return a valid result.
7482 */
7483char *find_cookie_value_end(char *s, const char *e)
7484{
7485 int quoted, qdpair;
7486
7487 quoted = qdpair = 0;
7488 for (; s < e; s++) {
7489 if (qdpair) qdpair = 0;
7490 else if (quoted) {
7491 if (*s == '\\') qdpair = 1;
7492 else if (*s == '"') quoted = 0;
7493 }
7494 else if (*s == '"') quoted = 1;
7495 else if (*s == ',' || *s == ';') return s;
7496 }
7497 return s;
7498}
7499
7500/* Delete a value in a header between delimiters <from> and <next> in buffer
7501 * <buf>. The number of characters displaced is returned, and the pointer to
7502 * the first delimiter is updated if required. The function tries as much as
7503 * possible to respect the following principles :
7504 * - replace <from> delimiter by the <next> one unless <from> points to a
7505 * colon, in which case <next> is simply removed
7506 * - set exactly one space character after the new first delimiter, unless
7507 * there are not enough characters in the block being moved to do so.
7508 * - remove unneeded spaces before the previous delimiter and after the new
7509 * one.
7510 *
7511 * It is the caller's responsibility to ensure that :
7512 * - <from> points to a valid delimiter or the colon ;
7513 * - <next> points to a valid delimiter or the final CR/LF ;
7514 * - there are non-space chars before <from> ;
7515 * - there is a CR/LF at or after <next>.
7516 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007517int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007518{
7519 char *prev = *from;
7520
7521 if (*prev == ':') {
7522 /* We're removing the first value, preserve the colon and add a
7523 * space if possible.
7524 */
7525 if (!http_is_crlf[(unsigned char)*next])
7526 next++;
7527 prev++;
7528 if (prev < next)
7529 *prev++ = ' ';
7530
7531 while (http_is_spht[(unsigned char)*next])
7532 next++;
7533 } else {
7534 /* Remove useless spaces before the old delimiter. */
7535 while (http_is_spht[(unsigned char)*(prev-1)])
7536 prev--;
7537 *from = prev;
7538
7539 /* copy the delimiter and if possible a space if we're
7540 * not at the end of the line.
7541 */
7542 if (!http_is_crlf[(unsigned char)*next]) {
7543 *prev++ = *next++;
7544 if (prev + 1 < next)
7545 *prev++ = ' ';
7546 while (http_is_spht[(unsigned char)*next])
7547 next++;
7548 }
7549 }
7550 return buffer_replace2(buf, prev, next, NULL, 0);
7551}
7552
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007553/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007554 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007555 * desirable to call it only when needed. This code is quite complex because
7556 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7557 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007558 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007559void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007560{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007561 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007562 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007563 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007564 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007565 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7566 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007567
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007568 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007569 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007570 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007571
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007572 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007573 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007574 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007575
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007576 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007577 hdr_beg = hdr_next;
7578 hdr_end = hdr_beg + cur_hdr->len;
7579 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007580
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007581 /* We have one full header between hdr_beg and hdr_end, and the
7582 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007583 * "Cookie:" headers.
7584 */
7585
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007586 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007587 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007588 old_idx = cur_idx;
7589 continue;
7590 }
7591
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007592 del_from = NULL; /* nothing to be deleted */
7593 preserve_hdr = 0; /* assume we may kill the whole header */
7594
Willy Tarreau58f10d72006-12-04 02:26:12 +01007595 /* Now look for cookies. Conforming to RFC2109, we have to support
7596 * attributes whose name begin with a '$', and associate them with
7597 * the right cookie, if we want to delete this cookie.
7598 * So there are 3 cases for each cookie read :
7599 * 1) it's a special attribute, beginning with a '$' : ignore it.
7600 * 2) it's a server id cookie that we *MAY* want to delete : save
7601 * some pointers on it (last semi-colon, beginning of cookie...)
7602 * 3) it's an application cookie : we *MAY* have to delete a previous
7603 * "special" cookie.
7604 * At the end of loop, if a "special" cookie remains, we may have to
7605 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007606 * *MUST* delete it.
7607 *
7608 * Note: RFC2965 is unclear about the processing of spaces around
7609 * the equal sign in the ATTR=VALUE form. A careful inspection of
7610 * the RFC explicitly allows spaces before it, and not within the
7611 * tokens (attrs or values). An inspection of RFC2109 allows that
7612 * too but section 10.1.3 lets one think that spaces may be allowed
7613 * after the equal sign too, resulting in some (rare) buggy
7614 * implementations trying to do that. So let's do what servers do.
7615 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7616 * allowed quoted strings in values, with any possible character
7617 * after a backslash, including control chars and delimitors, which
7618 * causes parsing to become ambiguous. Browsers also allow spaces
7619 * within values even without quotes.
7620 *
7621 * We have to keep multiple pointers in order to support cookie
7622 * removal at the beginning, middle or end of header without
7623 * corrupting the header. All of these headers are valid :
7624 *
7625 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7626 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7627 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7628 * | | | | | | | | |
7629 * | | | | | | | | hdr_end <--+
7630 * | | | | | | | +--> next
7631 * | | | | | | +----> val_end
7632 * | | | | | +-----------> val_beg
7633 * | | | | +--------------> equal
7634 * | | | +----------------> att_end
7635 * | | +---------------------> att_beg
7636 * | +--------------------------> prev
7637 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007638 */
7639
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007640 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7641 /* Iterate through all cookies on this line */
7642
7643 /* find att_beg */
7644 att_beg = prev + 1;
7645 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7646 att_beg++;
7647
7648 /* find att_end : this is the first character after the last non
7649 * space before the equal. It may be equal to hdr_end.
7650 */
7651 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007652
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007653 while (equal < hdr_end) {
7654 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007655 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007656 if (http_is_spht[(unsigned char)*equal++])
7657 continue;
7658 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007659 }
7660
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007661 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7662 * is between <att_beg> and <equal>, both may be identical.
7663 */
7664
7665 /* look for end of cookie if there is an equal sign */
7666 if (equal < hdr_end && *equal == '=') {
7667 /* look for the beginning of the value */
7668 val_beg = equal + 1;
7669 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7670 val_beg++;
7671
7672 /* find the end of the value, respecting quotes */
7673 next = find_cookie_value_end(val_beg, hdr_end);
7674
7675 /* make val_end point to the first white space or delimitor after the value */
7676 val_end = next;
7677 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7678 val_end--;
7679 } else {
7680 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007681 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007682
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007683 /* We have nothing to do with attributes beginning with '$'. However,
7684 * they will automatically be removed if a header before them is removed,
7685 * since they're supposed to be linked together.
7686 */
7687 if (*att_beg == '$')
7688 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007689
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007690 /* Ignore cookies with no equal sign */
7691 if (equal == next) {
7692 /* This is not our cookie, so we must preserve it. But if we already
7693 * scheduled another cookie for removal, we cannot remove the
7694 * complete header, but we can remove the previous block itself.
7695 */
7696 preserve_hdr = 1;
7697 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007698 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007699 val_end += delta;
7700 next += delta;
7701 hdr_end += delta;
7702 hdr_next += delta;
7703 cur_hdr->len += delta;
7704 http_msg_move_end(&txn->req, delta);
7705 prev = del_from;
7706 del_from = NULL;
7707 }
7708 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007709 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007710
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007711 /* if there are spaces around the equal sign, we need to
7712 * strip them otherwise we'll get trouble for cookie captures,
7713 * or even for rewrites. Since this happens extremely rarely,
7714 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007715 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007716 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7717 int stripped_before = 0;
7718 int stripped_after = 0;
7719
7720 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007721 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007722 equal += stripped_before;
7723 val_beg += stripped_before;
7724 }
7725
7726 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007727 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007728 val_beg += stripped_after;
7729 stripped_before += stripped_after;
7730 }
7731
7732 val_end += stripped_before;
7733 next += stripped_before;
7734 hdr_end += stripped_before;
7735 hdr_next += stripped_before;
7736 cur_hdr->len += stripped_before;
7737 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007738 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007739 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007740
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007741 /* First, let's see if we want to capture this cookie. We check
7742 * that we don't already have a client side cookie, because we
7743 * can only capture one. Also as an optimisation, we ignore
7744 * cookies shorter than the declared name.
7745 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007746 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7747 (val_end - att_beg >= sess->fe->capture_namelen) &&
7748 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007749 int log_len = val_end - att_beg;
7750
7751 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7752 Alert("HTTP logging : out of memory.\n");
7753 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007754 if (log_len > sess->fe->capture_len)
7755 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007756 memcpy(txn->cli_cookie, att_beg, log_len);
7757 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007758 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007759 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007760
Willy Tarreaubca99692010-10-06 19:25:55 +02007761 /* Persistence cookies in passive, rewrite or insert mode have the
7762 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007763 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007764 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007765 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007766 * For cookies in prefix mode, the form is :
7767 *
7768 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007769 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007770 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7771 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7772 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007773 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007774
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007775 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7776 * have the server ID between val_beg and delim, and the original cookie between
7777 * delim+1 and val_end. Otherwise, delim==val_end :
7778 *
7779 * Cookie: NAME=SRV; # in all but prefix modes
7780 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7781 * | || || | |+-> next
7782 * | || || | +--> val_end
7783 * | || || +---------> delim
7784 * | || |+------------> val_beg
7785 * | || +-------------> att_end = equal
7786 * | |+-----------------> att_beg
7787 * | +------------------> prev
7788 * +-------------------------> hdr_beg
7789 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007790
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007791 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007792 for (delim = val_beg; delim < val_end; delim++)
7793 if (*delim == COOKIE_DELIM)
7794 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007795 } else {
7796 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007797 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007798 /* Now check if the cookie contains a date field, which would
7799 * appear after a vertical bar ('|') just after the server name
7800 * and before the delimiter.
7801 */
7802 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7803 if (vbar1) {
7804 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007805 * right is the last seen date. It is a base64 encoded
7806 * 30-bit value representing the UNIX date since the
7807 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007808 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007809 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007810 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007811 if (val_end - vbar1 >= 5) {
7812 val = b64tos30(vbar1);
7813 if (val > 0)
7814 txn->cookie_last_date = val << 2;
7815 }
7816 /* look for a second vertical bar */
7817 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7818 if (vbar1 && (val_end - vbar1 > 5)) {
7819 val = b64tos30(vbar1 + 1);
7820 if (val > 0)
7821 txn->cookie_first_date = val << 2;
7822 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007823 }
7824 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007825
Willy Tarreauf64d1412010-10-07 20:06:11 +02007826 /* if the cookie has an expiration date and the proxy wants to check
7827 * it, then we do that now. We first check if the cookie is too old,
7828 * then only if it has expired. We detect strict overflow because the
7829 * time resolution here is not great (4 seconds). Cookies with dates
7830 * in the future are ignored if their offset is beyond one day. This
7831 * allows an admin to fix timezone issues without expiring everyone
7832 * and at the same time avoids keeping unwanted side effects for too
7833 * long.
7834 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007835 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7836 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007837 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007838 txn->flags &= ~TX_CK_MASK;
7839 txn->flags |= TX_CK_OLD;
7840 delim = val_beg; // let's pretend we have not found the cookie
7841 txn->cookie_first_date = 0;
7842 txn->cookie_last_date = 0;
7843 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007844 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7845 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007846 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007847 txn->flags &= ~TX_CK_MASK;
7848 txn->flags |= TX_CK_EXPIRED;
7849 delim = val_beg; // let's pretend we have not found the cookie
7850 txn->cookie_first_date = 0;
7851 txn->cookie_last_date = 0;
7852 }
7853
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007854 /* Here, we'll look for the first running server which supports the cookie.
7855 * This allows to share a same cookie between several servers, for example
7856 * to dedicate backup servers to specific servers only.
7857 * However, to prevent clients from sticking to cookie-less backup server
7858 * when they have incidentely learned an empty cookie, we simply ignore
7859 * empty cookies and mark them as invalid.
7860 * The same behaviour is applied when persistence must be ignored.
7861 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007862 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007863 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007864
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007865 while (srv) {
7866 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7867 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007868 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007869 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007870 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007871 /* we found the server and we can use it */
7872 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007873 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007874 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007875 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007876 break;
7877 } else {
7878 /* we found a server, but it's down,
7879 * mark it as such and go on in case
7880 * another one is available.
7881 */
7882 txn->flags &= ~TX_CK_MASK;
7883 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007884 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007885 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007886 srv = srv->next;
7887 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007888
Willy Tarreauf64d1412010-10-07 20:06:11 +02007889 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007890 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007891 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007892 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007893 txn->flags |= TX_CK_UNUSED;
7894 else
7895 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007896 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007897
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007898 /* depending on the cookie mode, we may have to either :
7899 * - delete the complete cookie if we're in insert+indirect mode, so that
7900 * the server never sees it ;
7901 * - remove the server id from the cookie value, and tag the cookie as an
7902 * application cookie so that it does not get accidentely removed later,
7903 * if we're in cookie prefix mode
7904 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007905 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007906 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007907
Willy Tarreau9b28e032012-10-12 23:49:43 +02007908 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007909 val_end += delta;
7910 next += delta;
7911 hdr_end += delta;
7912 hdr_next += delta;
7913 cur_hdr->len += delta;
7914 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007915
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007916 del_from = NULL;
7917 preserve_hdr = 1; /* we want to keep this cookie */
7918 }
7919 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007920 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007921 del_from = prev;
7922 }
7923 } else {
7924 /* This is not our cookie, so we must preserve it. But if we already
7925 * scheduled another cookie for removal, we cannot remove the
7926 * complete header, but we can remove the previous block itself.
7927 */
7928 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007929
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007930 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007931 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007932 if (att_beg >= del_from)
7933 att_beg += delta;
7934 if (att_end >= del_from)
7935 att_end += delta;
7936 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007937 val_end += delta;
7938 next += delta;
7939 hdr_end += delta;
7940 hdr_next += delta;
7941 cur_hdr->len += delta;
7942 http_msg_move_end(&txn->req, delta);
7943 prev = del_from;
7944 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007945 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007946 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007947
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007948 /* continue with next cookie on this header line */
7949 att_beg = next;
7950 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007951
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007952 /* There are no more cookies on this line.
7953 * We may still have one (or several) marked for deletion at the
7954 * end of the line. We must do this now in two ways :
7955 * - if some cookies must be preserved, we only delete from the
7956 * mark to the end of line ;
7957 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007958 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007959 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007960 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007961 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007962 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007963 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007964 cur_hdr->len += delta;
7965 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007966 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007967
7968 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007969 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7970 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007971 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007972 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007973 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007974 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007975 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007976 }
7977
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007978 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007979 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007980 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007981}
7982
7983
Willy Tarreaua15645d2007-03-18 16:22:39 +01007984/* Iterate the same filter through all response headers contained in <rtr>.
7985 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7986 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007987int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007988{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007989 char *cur_ptr, *cur_end, *cur_next;
7990 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007991 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007992 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007993 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007994
7995 last_hdr = 0;
7996
Willy Tarreau9b28e032012-10-12 23:49:43 +02007997 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007998 old_idx = 0;
7999
8000 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008001 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008002 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008003 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008004 (exp->action == ACT_ALLOW ||
8005 exp->action == ACT_DENY))
8006 return 0;
8007
8008 cur_idx = txn->hdr_idx.v[old_idx].next;
8009 if (!cur_idx)
8010 break;
8011
8012 cur_hdr = &txn->hdr_idx.v[cur_idx];
8013 cur_ptr = cur_next;
8014 cur_end = cur_ptr + cur_hdr->len;
8015 cur_next = cur_end + cur_hdr->cr + 1;
8016
8017 /* Now we have one header between cur_ptr and cur_end,
8018 * and the next header starts at cur_next.
8019 */
8020
Willy Tarreau15a53a42015-01-21 13:39:42 +01008021 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008022 switch (exp->action) {
8023 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008024 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008025 last_hdr = 1;
8026 break;
8027
8028 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008029 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008030 last_hdr = 1;
8031 break;
8032
8033 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008034 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8035 if (trash.len < 0)
8036 return -1;
8037
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008038 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008039 /* FIXME: if the user adds a newline in the replacement, the
8040 * index will not be recalculated for now, and the new line
8041 * will not be counted as a new header.
8042 */
8043
8044 cur_end += delta;
8045 cur_next += delta;
8046 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008047 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008048 break;
8049
8050 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02008051 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008052 cur_next += delta;
8053
Willy Tarreaufa355d42009-11-29 18:12:29 +01008054 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008055 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8056 txn->hdr_idx.used--;
8057 cur_hdr->len = 0;
8058 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01008059 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008060 break;
8061
8062 }
8063 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008064
8065 /* keep the link from this header to next one in case of later
8066 * removal of next header.
8067 */
8068 old_idx = cur_idx;
8069 }
8070 return 0;
8071}
8072
8073
8074/* Apply the filter to the status line in the response buffer <rtr>.
8075 * Returns 0 if nothing has been done, 1 if the filter has been applied,
8076 * or -1 if a replacement resulted in an invalid status line.
8077 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008078int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008079{
Willy Tarreaua15645d2007-03-18 16:22:39 +01008080 char *cur_ptr, *cur_end;
8081 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008082 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008083 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008084
8085
Willy Tarreau3d300592007-03-18 18:34:41 +01008086 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008087 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008088 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008089 (exp->action == ACT_ALLOW ||
8090 exp->action == ACT_DENY))
8091 return 0;
8092 else if (exp->action == ACT_REMOVE)
8093 return 0;
8094
8095 done = 0;
8096
Willy Tarreau9b28e032012-10-12 23:49:43 +02008097 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008098 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008099
8100 /* Now we have the status line between cur_ptr and cur_end */
8101
Willy Tarreau15a53a42015-01-21 13:39:42 +01008102 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008103 switch (exp->action) {
8104 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008105 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008106 done = 1;
8107 break;
8108
8109 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008110 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008111 done = 1;
8112 break;
8113
8114 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008115 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8116 if (trash.len < 0)
8117 return -1;
8118
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008119 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008120 /* FIXME: if the user adds a newline in the replacement, the
8121 * index will not be recalculated for now, and the new line
8122 * will not be counted as a new header.
8123 */
8124
Willy Tarreaufa355d42009-11-29 18:12:29 +01008125 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008126 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008127 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02008128 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01008129 cur_ptr, cur_end + 1,
8130 NULL, NULL);
8131 if (unlikely(!cur_end))
8132 return -1;
8133
8134 /* we have a full respnse and we know that we have either a CR
8135 * or an LF at <ptr>.
8136 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008137 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008138 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01008139 /* there is no point trying this regex on headers */
8140 return 1;
8141 }
8142 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008143 return done;
8144}
8145
8146
8147
8148/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008149 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008150 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
8151 * unparsable response.
8152 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008153int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008154{
Willy Tarreau192252e2015-04-04 01:47:55 +02008155 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008156 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008157 struct hdr_exp *exp;
8158
8159 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008160 int ret;
8161
8162 /*
8163 * The interleaving of transformations and verdicts
8164 * makes it difficult to decide to continue or stop
8165 * the evaluation.
8166 */
8167
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008168 if (txn->flags & TX_SVDENY)
8169 break;
8170
Willy Tarreau3d300592007-03-18 18:34:41 +01008171 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008172 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
8173 exp->action == ACT_PASS)) {
8174 exp = exp->next;
8175 continue;
8176 }
8177
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008178 /* if this filter had a condition, evaluate it now and skip to
8179 * next filter if the condition does not match.
8180 */
8181 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02008182 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008183 ret = acl_pass(ret);
8184 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8185 ret = !ret;
8186 if (!ret)
8187 continue;
8188 }
8189
Willy Tarreaua15645d2007-03-18 16:22:39 +01008190 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008191 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008192 if (unlikely(ret < 0))
8193 return -1;
8194
8195 if (likely(ret == 0)) {
8196 /* The filter did not match the response, it can be
8197 * iterated through all headers.
8198 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008199 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8200 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008201 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008202 }
8203 return 0;
8204}
8205
8206
Willy Tarreaua15645d2007-03-18 16:22:39 +01008207/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008208 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008209 * desirable to call it only when needed. This function is also used when we
8210 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008211 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008212void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008213{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008214 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008215 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01008216 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008217 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008218 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008219 char *hdr_beg, *hdr_end, *hdr_next;
8220 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008221
Willy Tarreaua15645d2007-03-18 16:22:39 +01008222 /* Iterate through the headers.
8223 * we start with the start line.
8224 */
8225 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008226 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008227
8228 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8229 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008230 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008231
8232 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008233 hdr_beg = hdr_next;
8234 hdr_end = hdr_beg + cur_hdr->len;
8235 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008236
Willy Tarreau24581ba2010-08-31 22:39:35 +02008237 /* We have one full header between hdr_beg and hdr_end, and the
8238 * next header starts at hdr_next. We're only interested in
8239 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008240 */
8241
Willy Tarreau24581ba2010-08-31 22:39:35 +02008242 is_cookie2 = 0;
8243 prev = hdr_beg + 10;
8244 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008245 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008246 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8247 if (!val) {
8248 old_idx = cur_idx;
8249 continue;
8250 }
8251 is_cookie2 = 1;
8252 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008253 }
8254
Willy Tarreau24581ba2010-08-31 22:39:35 +02008255 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8256 * <prev> points to the colon.
8257 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008258 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008259
Willy Tarreau24581ba2010-08-31 22:39:35 +02008260 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8261 * check-cache is enabled) and we are not interested in checking
8262 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008263 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02008264 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008265 return;
8266
Willy Tarreau24581ba2010-08-31 22:39:35 +02008267 /* OK so now we know we have to process this response cookie.
8268 * The format of the Set-Cookie header is slightly different
8269 * from the format of the Cookie header in that it does not
8270 * support the comma as a cookie delimiter (thus the header
8271 * cannot be folded) because the Expires attribute described in
8272 * the original Netscape's spec may contain an unquoted date
8273 * with a comma inside. We have to live with this because
8274 * many browsers don't support Max-Age and some browsers don't
8275 * support quoted strings. However the Set-Cookie2 header is
8276 * clean.
8277 *
8278 * We have to keep multiple pointers in order to support cookie
8279 * removal at the beginning, middle or end of header without
8280 * corrupting the header (in case of set-cookie2). A special
8281 * pointer, <scav> points to the beginning of the set-cookie-av
8282 * fields after the first semi-colon. The <next> pointer points
8283 * either to the end of line (set-cookie) or next unquoted comma
8284 * (set-cookie2). All of these headers are valid :
8285 *
8286 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8287 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8288 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8289 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8290 * | | | | | | | | | |
8291 * | | | | | | | | +-> next hdr_end <--+
8292 * | | | | | | | +------------> scav
8293 * | | | | | | +--------------> val_end
8294 * | | | | | +--------------------> val_beg
8295 * | | | | +----------------------> equal
8296 * | | | +------------------------> att_end
8297 * | | +----------------------------> att_beg
8298 * | +------------------------------> prev
8299 * +-----------------------------------------> hdr_beg
8300 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008301
Willy Tarreau24581ba2010-08-31 22:39:35 +02008302 for (; prev < hdr_end; prev = next) {
8303 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008304
Willy Tarreau24581ba2010-08-31 22:39:35 +02008305 /* find att_beg */
8306 att_beg = prev + 1;
8307 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8308 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008309
Willy Tarreau24581ba2010-08-31 22:39:35 +02008310 /* find att_end : this is the first character after the last non
8311 * space before the equal. It may be equal to hdr_end.
8312 */
8313 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008314
Willy Tarreau24581ba2010-08-31 22:39:35 +02008315 while (equal < hdr_end) {
8316 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8317 break;
8318 if (http_is_spht[(unsigned char)*equal++])
8319 continue;
8320 att_end = equal;
8321 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008322
Willy Tarreau24581ba2010-08-31 22:39:35 +02008323 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8324 * is between <att_beg> and <equal>, both may be identical.
8325 */
8326
8327 /* look for end of cookie if there is an equal sign */
8328 if (equal < hdr_end && *equal == '=') {
8329 /* look for the beginning of the value */
8330 val_beg = equal + 1;
8331 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8332 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008333
Willy Tarreau24581ba2010-08-31 22:39:35 +02008334 /* find the end of the value, respecting quotes */
8335 next = find_cookie_value_end(val_beg, hdr_end);
8336
8337 /* make val_end point to the first white space or delimitor after the value */
8338 val_end = next;
8339 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8340 val_end--;
8341 } else {
8342 /* <equal> points to next comma, semi-colon or EOL */
8343 val_beg = val_end = next = equal;
8344 }
8345
8346 if (next < hdr_end) {
8347 /* Set-Cookie2 supports multiple cookies, and <next> points to
8348 * a colon or semi-colon before the end. So skip all attr-value
8349 * pairs and look for the next comma. For Set-Cookie, since
8350 * commas are permitted in values, skip to the end.
8351 */
8352 if (is_cookie2)
8353 next = find_hdr_value_end(next, hdr_end);
8354 else
8355 next = hdr_end;
8356 }
8357
8358 /* Now everything is as on the diagram above */
8359
8360 /* Ignore cookies with no equal sign */
8361 if (equal == val_end)
8362 continue;
8363
8364 /* If there are spaces around the equal sign, we need to
8365 * strip them otherwise we'll get trouble for cookie captures,
8366 * or even for rewrites. Since this happens extremely rarely,
8367 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008368 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008369 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8370 int stripped_before = 0;
8371 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008372
Willy Tarreau24581ba2010-08-31 22:39:35 +02008373 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008374 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008375 equal += stripped_before;
8376 val_beg += stripped_before;
8377 }
8378
8379 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008380 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008381 val_beg += stripped_after;
8382 stripped_before += stripped_after;
8383 }
8384
8385 val_end += stripped_before;
8386 next += stripped_before;
8387 hdr_end += stripped_before;
8388 hdr_next += stripped_before;
8389 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008390 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008391 }
8392
8393 /* First, let's see if we want to capture this cookie. We check
8394 * that we don't already have a server side cookie, because we
8395 * can only capture one. Also as an optimisation, we ignore
8396 * cookies shorter than the declared name.
8397 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008398 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008399 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008400 (val_end - att_beg >= sess->fe->capture_namelen) &&
8401 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008402 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008403 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008404 Alert("HTTP logging : out of memory.\n");
8405 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008406 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008407 if (log_len > sess->fe->capture_len)
8408 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008409 memcpy(txn->srv_cookie, att_beg, log_len);
8410 txn->srv_cookie[log_len] = 0;
8411 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008412 }
8413
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008414 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008415 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008416 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008417 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8418 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008419 /* assume passive cookie by default */
8420 txn->flags &= ~TX_SCK_MASK;
8421 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008422
8423 /* If the cookie is in insert mode on a known server, we'll delete
8424 * this occurrence because we'll insert another one later.
8425 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008426 * a direct access.
8427 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008428 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008429 /* The "preserve" flag was set, we don't want to touch the
8430 * server's cookie.
8431 */
8432 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008433 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008434 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008435 /* this cookie must be deleted */
8436 if (*prev == ':' && next == hdr_end) {
8437 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008438 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008439 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8440 txn->hdr_idx.used--;
8441 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008442 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008443 hdr_next += delta;
8444 http_msg_move_end(&txn->rsp, delta);
8445 /* note: while both invalid now, <next> and <hdr_end>
8446 * are still equal, so the for() will stop as expected.
8447 */
8448 } else {
8449 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008450 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008451 next = prev;
8452 hdr_end += delta;
8453 hdr_next += delta;
8454 cur_hdr->len += delta;
8455 http_msg_move_end(&txn->rsp, delta);
8456 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008457 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008458 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008459 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008460 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008461 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008462 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008463 * with this server since we know it.
8464 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008465 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008466 next += delta;
8467 hdr_end += delta;
8468 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008469 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008470 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008471
Willy Tarreauf1348312010-10-07 15:54:11 +02008472 txn->flags &= ~TX_SCK_MASK;
8473 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008474 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008475 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008476 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008477 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008478 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008479 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008480 next += delta;
8481 hdr_end += delta;
8482 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008483 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008484 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008485
Willy Tarreau827aee92011-03-10 16:55:02 +01008486 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008487 txn->flags &= ~TX_SCK_MASK;
8488 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008489 }
8490 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008491 /* that's done for this cookie, check the next one on the same
8492 * line when next != hdr_end (only if is_cookie2).
8493 */
8494 }
8495 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008496 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008497 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008498}
8499
8500
Willy Tarreaua15645d2007-03-18 16:22:39 +01008501/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008502 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008503 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008504void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008505{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008506 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008507 char *p1, *p2;
8508
8509 char *cur_ptr, *cur_end, *cur_next;
8510 int cur_idx;
8511
Willy Tarreau5df51872007-11-25 16:20:08 +01008512 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008513 return;
8514
8515 /* Iterate through the headers.
8516 * we start with the start line.
8517 */
8518 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008519 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008520
8521 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8522 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008523 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008524
8525 cur_hdr = &txn->hdr_idx.v[cur_idx];
8526 cur_ptr = cur_next;
8527 cur_end = cur_ptr + cur_hdr->len;
8528 cur_next = cur_end + cur_hdr->cr + 1;
8529
8530 /* We have one full header between cur_ptr and cur_end, and the
8531 * next header starts at cur_next. We're only interested in
8532 * "Cookie:" headers.
8533 */
8534
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008535 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8536 if (val) {
8537 if ((cur_end - (cur_ptr + val) >= 8) &&
8538 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8539 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8540 return;
8541 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008542 }
8543
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008544 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8545 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008546 continue;
8547
8548 /* OK, right now we know we have a cache-control header at cur_ptr */
8549
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008550 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008551
8552 if (p1 >= cur_end) /* no more info */
8553 continue;
8554
8555 /* p1 is at the beginning of the value */
8556 p2 = p1;
8557
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008558 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008559 p2++;
8560
8561 /* we have a complete value between p1 and p2 */
8562 if (p2 < cur_end && *p2 == '=') {
8563 /* we have something of the form no-cache="set-cookie" */
8564 if ((cur_end - p1 >= 21) &&
8565 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8566 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008567 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008568 continue;
8569 }
8570
8571 /* OK, so we know that either p2 points to the end of string or to a comma */
8572 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008573 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008574 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8575 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8576 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008577 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008578 return;
8579 }
8580
8581 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008582 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008583 continue;
8584 }
8585 }
8586}
8587
Willy Tarreau58f10d72006-12-04 02:26:12 +01008588
Willy Tarreaub2513902006-12-17 14:52:38 +01008589/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008590 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008591 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008592 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008593 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008594 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008595 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008596 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008597 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008598int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008599{
8600 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008601 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008602 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008603
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008604 if (!uri_auth)
8605 return 0;
8606
Cyril Bonté70be45d2010-10-12 00:14:35 +02008607 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008608 return 0;
8609
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008610 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008611 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008612 return 0;
8613
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008614 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008615 return 0;
8616
Willy Tarreaub2513902006-12-17 14:52:38 +01008617 return 1;
8618}
8619
Willy Tarreau4076a152009-04-02 15:18:36 +02008620/*
8621 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008622 * By default it tries to report the error position as msg->err_pos. However if
8623 * this one is not set, it will then report msg->next, which is the last known
8624 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008625 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008626 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008627void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008628 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008629 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008630{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008631 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008632 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008633 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008634
Willy Tarreau9b28e032012-10-12 23:49:43 +02008635 es->len = MIN(chn->buf->i, sizeof(es->buf));
8636 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008637 len1 = MIN(len1, es->len);
8638 len2 = es->len - len1; /* remaining data if buffer wraps */
8639
Willy Tarreau9b28e032012-10-12 23:49:43 +02008640 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008641 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008642 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008643
Willy Tarreau4076a152009-04-02 15:18:36 +02008644 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008645 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008646 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008647 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008648
Willy Tarreau4076a152009-04-02 15:18:36 +02008649 es->when = date; // user-visible date
8650 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008651 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008652 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008653 if (objt_conn(sess->origin))
8654 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008655 else
8656 memset(&es->src, 0, sizeof(es->src));
8657
Willy Tarreau078272e2010-12-12 12:46:33 +01008658 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008659 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008660 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008661 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008662 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008663 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008664 es->b_out = chn->buf->o;
8665 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008666 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008667 es->m_clen = msg->chunk_len;
8668 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008669}
Willy Tarreaub2513902006-12-17 14:52:38 +01008670
Willy Tarreau294c4732011-12-16 21:35:50 +01008671/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8672 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8673 * performed over the whole headers. Otherwise it must contain a valid header
8674 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8675 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8676 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8677 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008678 * -1. The value fetch stops at commas, so this function is suited for use with
8679 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008680 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008681 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008682unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008683 struct hdr_idx *idx, int occ,
8684 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008685{
Willy Tarreau294c4732011-12-16 21:35:50 +01008686 struct hdr_ctx local_ctx;
8687 char *ptr_hist[MAX_HDR_HISTORY];
8688 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008689 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008690 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008691
Willy Tarreau294c4732011-12-16 21:35:50 +01008692 if (!ctx) {
8693 local_ctx.idx = 0;
8694 ctx = &local_ctx;
8695 }
8696
Willy Tarreaubce70882009-09-07 11:51:47 +02008697 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008698 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008699 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008700 occ--;
8701 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008702 *vptr = ctx->line + ctx->val;
8703 *vlen = ctx->vlen;
8704 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008705 }
8706 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008707 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008708 }
8709
8710 /* negative occurrence, we scan all the list then walk back */
8711 if (-occ > MAX_HDR_HISTORY)
8712 return 0;
8713
Willy Tarreau294c4732011-12-16 21:35:50 +01008714 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008715 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008716 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8717 len_hist[hist_ptr] = ctx->vlen;
8718 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008719 hist_ptr = 0;
8720 found++;
8721 }
8722 if (-occ > found)
8723 return 0;
8724 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008725 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8726 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8727 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008728 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008729 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008730 if (hist_ptr >= MAX_HDR_HISTORY)
8731 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008732 *vptr = ptr_hist[hist_ptr];
8733 *vlen = len_hist[hist_ptr];
8734 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008735}
8736
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008737/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8738 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8739 * performed over the whole headers. Otherwise it must contain a valid header
8740 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8741 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8742 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8743 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8744 * -1. This function differs from http_get_hdr() in that it only returns full
8745 * line header values and does not stop at commas.
8746 * The return value is 0 if nothing was found, or non-zero otherwise.
8747 */
8748unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8749 struct hdr_idx *idx, int occ,
8750 struct hdr_ctx *ctx, char **vptr, int *vlen)
8751{
8752 struct hdr_ctx local_ctx;
8753 char *ptr_hist[MAX_HDR_HISTORY];
8754 int len_hist[MAX_HDR_HISTORY];
8755 unsigned int hist_ptr;
8756 int found;
8757
8758 if (!ctx) {
8759 local_ctx.idx = 0;
8760 ctx = &local_ctx;
8761 }
8762
8763 if (occ >= 0) {
8764 /* search from the beginning */
8765 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8766 occ--;
8767 if (occ <= 0) {
8768 *vptr = ctx->line + ctx->val;
8769 *vlen = ctx->vlen;
8770 return 1;
8771 }
8772 }
8773 return 0;
8774 }
8775
8776 /* negative occurrence, we scan all the list then walk back */
8777 if (-occ > MAX_HDR_HISTORY)
8778 return 0;
8779
8780 found = hist_ptr = 0;
8781 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8782 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8783 len_hist[hist_ptr] = ctx->vlen;
8784 if (++hist_ptr >= MAX_HDR_HISTORY)
8785 hist_ptr = 0;
8786 found++;
8787 }
8788 if (-occ > found)
8789 return 0;
8790 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8791 * find occurrence -occ, so we have to check [hist_ptr+occ].
8792 */
8793 hist_ptr += occ;
8794 if (hist_ptr >= MAX_HDR_HISTORY)
8795 hist_ptr -= MAX_HDR_HISTORY;
8796 *vptr = ptr_hist[hist_ptr];
8797 *vlen = len_hist[hist_ptr];
8798 return 1;
8799}
8800
Willy Tarreaubaaee002006-06-26 02:48:02 +02008801/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008802 * Print a debug line with a header. Always stop at the first CR or LF char,
8803 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8804 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008805 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008806void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008807{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008808 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008809 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008810
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008811 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008812 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008813 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008814 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 +02008815
8816 for (max = 0; start + max < end; max++)
8817 if (start[max] == '\r' || start[max] == '\n')
8818 break;
8819
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008820 UBOUND(max, trash.size - trash.len - 3);
8821 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8822 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008823 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008824}
8825
Willy Tarreaueee5b512015-04-03 23:46:31 +02008826
8827/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8828 * The hdr_idx is allocated as well. In case of allocation failure, everything
8829 * allocated is freed and NULL is returned. Otherwise the new transaction is
8830 * assigned to the stream and returned.
8831 */
8832struct http_txn *http_alloc_txn(struct stream *s)
8833{
8834 struct http_txn *txn = s->txn;
8835
8836 if (txn)
8837 return txn;
8838
8839 txn = pool_alloc2(pool2_http_txn);
8840 if (!txn)
8841 return txn;
8842
8843 txn->hdr_idx.size = global.tune.max_http_hdr;
8844 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8845 if (!txn->hdr_idx.v) {
8846 pool_free2(pool2_http_txn, txn);
8847 return NULL;
8848 }
8849
8850 s->txn = txn;
8851 return txn;
8852}
8853
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008854void http_txn_reset_req(struct http_txn *txn)
8855{
8856 txn->req.flags = 0;
8857 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8858 txn->req.next = 0;
8859 txn->req.chunk_len = 0LL;
8860 txn->req.body_len = 0LL;
8861 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8862}
8863
8864void http_txn_reset_res(struct http_txn *txn)
8865{
8866 txn->rsp.flags = 0;
8867 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8868 txn->rsp.next = 0;
8869 txn->rsp.chunk_len = 0LL;
8870 txn->rsp.body_len = 0LL;
8871 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8872}
8873
Willy Tarreau0937bc42009-12-22 15:03:09 +01008874/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008875 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008876 * the required fields are properly allocated and that we only need to (re)init
8877 * them. This should be used before processing any new request.
8878 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008879void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008880{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008881 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008882 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008883
8884 txn->flags = 0;
8885 txn->status = -1;
8886
Willy Tarreauf64d1412010-10-07 20:06:11 +02008887 txn->cookie_first_date = 0;
8888 txn->cookie_last_date = 0;
8889
Willy Tarreaueee5b512015-04-03 23:46:31 +02008890 txn->srv_cookie = NULL;
8891 txn->cli_cookie = NULL;
8892 txn->uri = NULL;
8893
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008894 http_txn_reset_req(txn);
8895 http_txn_reset_res(txn);
8896
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008897 txn->req.chn = &s->req;
8898 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008899
8900 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008901
8902 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8903 if (fe->options2 & PR_O2_REQBUG_OK)
8904 txn->req.err_pos = -1; /* let buggy requests pass */
8905
Willy Tarreau0937bc42009-12-22 15:03:09 +01008906 if (txn->hdr_idx.v)
8907 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008908
8909 vars_init(&s->vars_txn, SCOPE_TXN);
8910 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008911}
8912
8913/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008914void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008915{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008916 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008917 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008918
Willy Tarreau75195602014-03-11 15:48:55 +01008919 /* release any possible compression context */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008920 if (s->flags & SF_COMP_READY)
Willy Tarreau75195602014-03-11 15:48:55 +01008921 s->comp_algo->end(&s->comp_ctx);
8922 s->comp_algo = NULL;
Willy Tarreaue7dff022015-04-03 01:14:29 +02008923 s->flags &= ~SF_COMP_READY;
Willy Tarreau75195602014-03-11 15:48:55 +01008924
Willy Tarreau0937bc42009-12-22 15:03:09 +01008925 /* these ones will have been dynamically allocated */
8926 pool_free2(pool2_requri, txn->uri);
8927 pool_free2(pool2_capture, txn->cli_cookie);
8928 pool_free2(pool2_capture, txn->srv_cookie);
William Lallemanda73203e2012-03-12 12:48:57 +01008929 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008930
William Lallemanda73203e2012-03-12 12:48:57 +01008931 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008932 txn->uri = NULL;
8933 txn->srv_cookie = NULL;
8934 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008935
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008936 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008937 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008938 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008939 pool_free2(h->pool, s->req_cap[h->index]);
8940 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008941 }
8942
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008943 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008944 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008945 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008946 pool_free2(h->pool, s->res_cap[h->index]);
8947 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008948 }
8949
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008950 vars_prune(&s->vars_txn, s);
8951 vars_prune(&s->vars_reqres, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008952}
8953
8954/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008955void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008956{
8957 http_end_txn(s);
8958 http_init_txn(s);
8959
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008960 /* reinitialise the current rule list pointer to NULL. We are sure that
8961 * any rulelist match the NULL pointer.
8962 */
8963 s->current_rule_list = NULL;
8964
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008965 s->be = strm_fe(s);
8966 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008967 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008968 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008969 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008970 /* re-init store persistence */
8971 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008972 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008973
Willy Tarreau0937bc42009-12-22 15:03:09 +01008974 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008975
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008976 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008977
Willy Tarreau739cfba2010-01-25 23:11:14 +01008978 /* We must trim any excess data from the response buffer, because we
8979 * may have blocked an invalid response from a server that we don't
8980 * want to accidentely forward once we disable the analysers, nor do
8981 * we want those data to come along with next response. A typical
8982 * example of such data would be from a buggy server responding to
8983 * a HEAD with some data, or sending more than the advertised
8984 * content-length.
8985 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008986 if (unlikely(s->res.buf->i))
8987 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008988
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008989 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008990 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008991
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008992 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008993 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008994
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008995 s->req.rex = TICK_ETERNITY;
8996 s->req.wex = TICK_ETERNITY;
8997 s->req.analyse_exp = TICK_ETERNITY;
8998 s->res.rex = TICK_ETERNITY;
8999 s->res.wex = TICK_ETERNITY;
9000 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009001}
Willy Tarreau58f10d72006-12-04 02:26:12 +01009002
Sasha Pachev218f0642014-06-16 12:05:59 -06009003void free_http_res_rules(struct list *r)
9004{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009005 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06009006
9007 list_for_each_entry_safe(pr, tr, r, list) {
9008 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009009 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06009010 free(pr);
9011 }
9012}
9013
9014void free_http_req_rules(struct list *r)
9015{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009016 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01009017
9018 list_for_each_entry_safe(pr, tr, r, list) {
9019 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009020 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009021 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01009022
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009023 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01009024 free(pr);
9025 }
9026}
9027
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009028/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009029struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01009030{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009031 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009032 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009033 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009034 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01009035
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009036 rule = (struct act_rule*)calloc(1, sizeof(struct act_rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01009037 if (!rule) {
9038 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009039 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009040 }
9041
CJ Ess108b1dd2015-04-07 12:03:37 -04009042 rule->deny_status = HTTP_ERR_403;
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009043 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009044 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01009045 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02009046 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04009047 int code;
9048 int hc;
9049
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009050 rule->action = ACT_ACTION_DENY;
Willy Tarreauff011f22011-01-06 17:51:27 +01009051 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04009052 if (strcmp(args[cur_arg], "deny_status") == 0) {
9053 cur_arg++;
9054 if (!args[cur_arg]) {
9055 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
9056 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9057 goto out_err;
9058 }
9059
9060 code = atol(args[cur_arg]);
9061 cur_arg++;
9062 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
9063 if (http_err_codes[hc] == code) {
9064 rule->deny_status = hc;
9065 break;
9066 }
9067 }
9068
9069 if (hc >= HTTP_ERR_SIZE) {
9070 Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n",
9071 file, linenum, code);
9072 }
9073 }
Willy Tarreauccbcc372012-12-27 12:37:57 +01009074 } else if (!strcmp(args[0], "tarpit")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009075 rule->action = ACT_HTTP_REQ_TARPIT;
Willy Tarreauccbcc372012-12-27 12:37:57 +01009076 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01009077 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009078 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01009079 cur_arg = 1;
9080
9081 while(*args[cur_arg]) {
9082 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009083 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01009084 cur_arg+=2;
9085 continue;
9086 } else
9087 break;
9088 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009089 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009090 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009091 cur_arg = 1;
9092
9093 if (!*args[cur_arg] ||
9094 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9095 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
9096 file, linenum, args[0]);
9097 goto out_err;
9098 }
9099 rule->arg.nice = atoi(args[cur_arg]);
9100 if (rule->arg.nice < -1024)
9101 rule->arg.nice = -1024;
9102 else if (rule->arg.nice > 1024)
9103 rule->arg.nice = 1024;
9104 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009105 } else if (!strcmp(args[0], "set-tos")) {
9106#ifdef IP_TOS
9107 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009108 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009109 cur_arg = 1;
9110
9111 if (!*args[cur_arg] ||
9112 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9113 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9114 file, linenum, args[0]);
9115 goto out_err;
9116 }
9117
9118 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9119 if (err && *err != '\0') {
9120 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9121 file, linenum, err, args[0]);
9122 goto out_err;
9123 }
9124 cur_arg++;
9125#else
9126 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9127 goto out_err;
9128#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009129 } else if (!strcmp(args[0], "set-mark")) {
9130#ifdef SO_MARK
9131 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009132 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009133 cur_arg = 1;
9134
9135 if (!*args[cur_arg] ||
9136 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9137 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9138 file, linenum, args[0]);
9139 goto out_err;
9140 }
9141
9142 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9143 if (err && *err != '\0') {
9144 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9145 file, linenum, err, args[0]);
9146 goto out_err;
9147 }
9148 cur_arg++;
9149 global.last_checks |= LSTCHK_NETADM;
9150#else
9151 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9152 goto out_err;
9153#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009154 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009155 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009156 cur_arg = 1;
9157
9158 if (!*args[cur_arg] ||
9159 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9160 bad_log_level:
9161 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9162 file, linenum, args[0]);
9163 goto out_err;
9164 }
9165 if (strcmp(args[cur_arg], "silent") == 0)
9166 rule->arg.loglevel = -1;
9167 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9168 goto bad_log_level;
9169 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009170 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009171 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009172 cur_arg = 1;
9173
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009174 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9175 (*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 +01009176 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9177 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009178 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009179 }
9180
9181 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9182 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9183 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009184
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009185 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009186 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009187 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9188 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009189 free(proxy->conf.lfs_file);
9190 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9191 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009192 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009193 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009194 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009195 cur_arg = 1;
9196
9197 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009198 (*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 -06009199 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9200 file, linenum, args[0]);
9201 goto out_err;
9202 }
9203
9204 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9205 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9206 LIST_INIT(&rule->arg.hdr_add.fmt);
9207
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009208 error = NULL;
9209 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9210 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9211 args[cur_arg + 1], error);
9212 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009213 goto out_err;
9214 }
9215
9216 proxy->conf.args.ctx = ARGC_HRQ;
9217 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9218 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9219 file, linenum);
9220
9221 free(proxy->conf.lfs_file);
9222 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9223 proxy->conf.lfs_line = proxy->conf.args.line;
9224 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009225 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009226 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009227 cur_arg = 1;
9228
9229 if (!*args[cur_arg] ||
9230 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9231 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9232 file, linenum, args[0]);
9233 goto out_err;
9234 }
9235
9236 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9237 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9238
9239 proxy->conf.args.ctx = ARGC_HRQ;
9240 free(proxy->conf.lfs_file);
9241 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9242 proxy->conf.lfs_line = proxy->conf.args.line;
9243 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009244 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9245 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009246 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009247 struct sample_expr *expr;
9248 unsigned int where;
9249 char *err = NULL;
9250
9251 cur_arg = 1;
9252 proxy->conf.args.ctx = ARGC_TRK;
9253
9254 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9255 if (!expr) {
9256 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9257 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9258 free(err);
9259 goto out_err;
9260 }
9261
9262 where = 0;
9263 if (proxy->cap & PR_CAP_FE)
9264 where |= SMP_VAL_FE_HRQ_HDR;
9265 if (proxy->cap & PR_CAP_BE)
9266 where |= SMP_VAL_BE_HRQ_HDR;
9267
9268 if (!(expr->fetch->val & where)) {
9269 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9270 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9271 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9272 args[cur_arg-1], sample_src_names(expr->fetch->use));
9273 free(expr);
9274 goto out_err;
9275 }
9276
9277 if (strcmp(args[cur_arg], "table") == 0) {
9278 cur_arg++;
9279 if (!args[cur_arg]) {
9280 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9281 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9282 free(expr);
9283 goto out_err;
9284 }
9285 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009286 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02009287 cur_arg++;
9288 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009289 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009290 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009291 } else if (strcmp(args[0], "redirect") == 0) {
9292 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009293 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009294
Willy Tarreaube4653b2015-05-28 15:26:58 +02009295 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009296 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9297 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9298 goto out_err;
9299 }
9300
9301 /* this redirect rule might already contain a parsed condition which
9302 * we'll pass to the http-request rule.
9303 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009304 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009305 rule->arg.redir = redir;
9306 rule->cond = redir->cond;
9307 redir->cond = NULL;
9308 cur_arg = 2;
9309 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009310 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9311 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009312 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009313 /*
9314 * '+ 8' for 'add-acl('
9315 * '- 9' for 'add-acl(' + trailing ')'
9316 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009317 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009318
9319 cur_arg = 1;
9320
9321 if (!*args[cur_arg] ||
9322 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9323 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9324 file, linenum, args[0]);
9325 goto out_err;
9326 }
9327
9328 LIST_INIT(&rule->arg.map.key);
9329 proxy->conf.args.ctx = ARGC_HRQ;
9330 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9331 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9332 file, linenum);
9333 free(proxy->conf.lfs_file);
9334 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9335 proxy->conf.lfs_line = proxy->conf.args.line;
9336 cur_arg += 1;
9337 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9338 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009339 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009340 /*
9341 * '+ 8' for 'del-acl('
9342 * '- 9' for 'del-acl(' + trailing ')'
9343 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009344 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009345
9346 cur_arg = 1;
9347
9348 if (!*args[cur_arg] ||
9349 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9350 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9351 file, linenum, args[0]);
9352 goto out_err;
9353 }
9354
9355 LIST_INIT(&rule->arg.map.key);
9356 proxy->conf.args.ctx = ARGC_HRQ;
9357 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9358 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9359 file, linenum);
9360 free(proxy->conf.lfs_file);
9361 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9362 proxy->conf.lfs_line = proxy->conf.args.line;
9363 cur_arg += 1;
9364 } else if (strncmp(args[0], "del-map", 7) == 0) {
9365 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009366 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009367 /*
9368 * '+ 8' for 'del-map('
9369 * '- 9' for 'del-map(' + trailing ')'
9370 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009371 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009372
9373 cur_arg = 1;
9374
9375 if (!*args[cur_arg] ||
9376 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9377 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9378 file, linenum, args[0]);
9379 goto out_err;
9380 }
9381
9382 LIST_INIT(&rule->arg.map.key);
9383 proxy->conf.args.ctx = ARGC_HRQ;
9384 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9385 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9386 file, linenum);
9387 free(proxy->conf.lfs_file);
9388 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9389 proxy->conf.lfs_line = proxy->conf.args.line;
9390 cur_arg += 1;
9391 } else if (strncmp(args[0], "set-map", 7) == 0) {
9392 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009393 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009394 /*
9395 * '+ 8' for 'set-map('
9396 * '- 9' for 'set-map(' + trailing ')'
9397 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009398 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009399
9400 cur_arg = 1;
9401
9402 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9403 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9404 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9405 file, linenum, args[0]);
9406 goto out_err;
9407 }
9408
9409 LIST_INIT(&rule->arg.map.key);
9410 LIST_INIT(&rule->arg.map.value);
9411 proxy->conf.args.ctx = ARGC_HRQ;
9412
9413 /* key pattern */
9414 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9415 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9416 file, linenum);
9417
9418 /* value pattern */
9419 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9420 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9421 file, linenum);
9422 free(proxy->conf.lfs_file);
9423 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9424 proxy->conf.lfs_line = proxy->conf.args.line;
9425
9426 cur_arg += 2;
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02009427 } else if (strncmp(args[0], "set-src", 7) == 0) {
9428 struct sample_expr *expr;
9429 unsigned int where;
9430 char *err = NULL;
9431
9432 cur_arg = 1;
9433 proxy->conf.args.ctx = ARGC_HRQ;
9434
9435 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9436 if (!expr) {
9437 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9438 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9439 free(err);
9440 goto out_err;
9441 }
9442
9443 where = 0;
9444 if (proxy->cap & PR_CAP_FE)
9445 where |= SMP_VAL_FE_HRQ_HDR;
9446 if (proxy->cap & PR_CAP_BE)
9447 where |= SMP_VAL_BE_HRQ_HDR;
9448
9449 if (!(expr->fetch->val & where)) {
9450 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9451 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9452 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9453 args[cur_arg-1], sample_src_names(expr->fetch->use));
9454 free(expr);
9455 goto out_err;
9456 }
9457
Thierry FOURNIERa002dc92015-07-31 08:50:51 +02009458 rule->arg.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009459 rule->action = ACT_HTTP_REQ_SET_SRC;
William Lallemand73025dd2014-04-24 14:38:37 +02009460 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9461 char *errmsg = NULL;
9462 cur_arg = 1;
9463 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009464 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009465 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009466 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009467 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9468 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9469 free(errmsg);
9470 goto out_err;
9471 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009472 } else {
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02009473 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 +01009474 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009475 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009476 }
9477
9478 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9479 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009480 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009481
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009482 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9483 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9484 file, linenum, args[0], errmsg);
9485 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009486 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009487 }
9488 rule->cond = cond;
9489 }
9490 else if (*args[cur_arg]) {
9491 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9492 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9493 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009494 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009495 }
9496
9497 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009498 out_err:
9499 free(rule);
9500 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009501}
9502
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009503/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009504struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009505{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009506 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009507 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009508 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009509 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009510
9511 rule = calloc(1, sizeof(*rule));
9512 if (!rule) {
9513 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9514 goto out_err;
9515 }
9516
9517 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009518 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009519 cur_arg = 1;
9520 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009521 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009522 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009523 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009524 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +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 value).\n",
9530 file, linenum, args[0]);
9531 goto out_err;
9532 }
9533 rule->arg.nice = atoi(args[cur_arg]);
9534 if (rule->arg.nice < -1024)
9535 rule->arg.nice = -1024;
9536 else if (rule->arg.nice > 1024)
9537 rule->arg.nice = 1024;
9538 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009539 } else if (!strcmp(args[0], "set-tos")) {
9540#ifdef IP_TOS
9541 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009542 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009543 cur_arg = 1;
9544
9545 if (!*args[cur_arg] ||
9546 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9547 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9548 file, linenum, args[0]);
9549 goto out_err;
9550 }
9551
9552 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9553 if (err && *err != '\0') {
9554 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9555 file, linenum, err, args[0]);
9556 goto out_err;
9557 }
9558 cur_arg++;
9559#else
9560 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9561 goto out_err;
9562#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009563 } else if (!strcmp(args[0], "set-mark")) {
9564#ifdef SO_MARK
9565 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009566 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009567 cur_arg = 1;
9568
9569 if (!*args[cur_arg] ||
9570 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9571 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9572 file, linenum, args[0]);
9573 goto out_err;
9574 }
9575
9576 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9577 if (err && *err != '\0') {
9578 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9579 file, linenum, err, args[0]);
9580 goto out_err;
9581 }
9582 cur_arg++;
9583 global.last_checks |= LSTCHK_NETADM;
9584#else
9585 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9586 goto out_err;
9587#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009588 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009589 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009590 cur_arg = 1;
9591
9592 if (!*args[cur_arg] ||
9593 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9594 bad_log_level:
9595 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9596 file, linenum, args[0]);
9597 goto out_err;
9598 }
9599 if (strcmp(args[cur_arg], "silent") == 0)
9600 rule->arg.loglevel = -1;
9601 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9602 goto bad_log_level;
9603 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009604 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009605 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009606 cur_arg = 1;
9607
9608 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9609 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9610 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9611 file, linenum, args[0]);
9612 goto out_err;
9613 }
9614
9615 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9616 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9617 LIST_INIT(&rule->arg.hdr_add.fmt);
9618
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009619 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009620 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009621 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9622 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009623 free(proxy->conf.lfs_file);
9624 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9625 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009626 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009627 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009628 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009629 cur_arg = 1;
9630
9631 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009632 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9633 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009634 file, linenum, args[0]);
9635 goto out_err;
9636 }
9637
9638 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9639 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9640 LIST_INIT(&rule->arg.hdr_add.fmt);
9641
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009642 error = NULL;
9643 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9644 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9645 args[cur_arg + 1], error);
9646 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009647 goto out_err;
9648 }
9649
9650 proxy->conf.args.ctx = ARGC_HRQ;
9651 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9652 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9653 file, linenum);
9654
9655 free(proxy->conf.lfs_file);
9656 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9657 proxy->conf.lfs_line = proxy->conf.args.line;
9658 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009659 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009660 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009661 cur_arg = 1;
9662
9663 if (!*args[cur_arg] ||
9664 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9665 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9666 file, linenum, args[0]);
9667 goto out_err;
9668 }
9669
9670 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9671 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9672
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009673 proxy->conf.args.ctx = ARGC_HRS;
9674 free(proxy->conf.lfs_file);
9675 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9676 proxy->conf.lfs_line = proxy->conf.args.line;
9677 cur_arg += 1;
9678 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9679 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009680 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009681 /*
9682 * '+ 8' for 'add-acl('
9683 * '- 9' for 'add-acl(' + trailing ')'
9684 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009685 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009686
9687 cur_arg = 1;
9688
9689 if (!*args[cur_arg] ||
9690 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9691 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9692 file, linenum, args[0]);
9693 goto out_err;
9694 }
9695
9696 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009697 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009698 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9699 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9700 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009701 free(proxy->conf.lfs_file);
9702 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9703 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009704
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009705 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009706 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9707 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009708 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009709 /*
9710 * '+ 8' for 'del-acl('
9711 * '- 9' for 'del-acl(' + trailing ')'
9712 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009713 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009714
9715 cur_arg = 1;
9716
9717 if (!*args[cur_arg] ||
9718 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9719 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9720 file, linenum, args[0]);
9721 goto out_err;
9722 }
9723
9724 LIST_INIT(&rule->arg.map.key);
9725 proxy->conf.args.ctx = ARGC_HRS;
9726 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9727 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9728 file, linenum);
9729 free(proxy->conf.lfs_file);
9730 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9731 proxy->conf.lfs_line = proxy->conf.args.line;
9732 cur_arg += 1;
9733 } else if (strncmp(args[0], "del-map", 7) == 0) {
9734 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009735 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009736 /*
9737 * '+ 8' for 'del-map('
9738 * '- 9' for 'del-map(' + trailing ')'
9739 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009740 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009741
9742 cur_arg = 1;
9743
9744 if (!*args[cur_arg] ||
9745 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9746 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9747 file, linenum, args[0]);
9748 goto out_err;
9749 }
9750
9751 LIST_INIT(&rule->arg.map.key);
9752 proxy->conf.args.ctx = ARGC_HRS;
9753 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9754 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9755 file, linenum);
9756 free(proxy->conf.lfs_file);
9757 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9758 proxy->conf.lfs_line = proxy->conf.args.line;
9759 cur_arg += 1;
9760 } else if (strncmp(args[0], "set-map", 7) == 0) {
9761 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009762 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009763 /*
9764 * '+ 8' for 'set-map('
9765 * '- 9' for 'set-map(' + trailing ')'
9766 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009767 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009768
9769 cur_arg = 1;
9770
9771 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9772 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9773 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9774 file, linenum, args[0]);
9775 goto out_err;
9776 }
9777
9778 LIST_INIT(&rule->arg.map.key);
9779 LIST_INIT(&rule->arg.map.value);
9780
9781 proxy->conf.args.ctx = ARGC_HRS;
9782
9783 /* key pattern */
9784 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9785 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9786 file, linenum);
9787
9788 /* value pattern */
9789 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9790 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9791 file, linenum);
9792
9793 free(proxy->conf.lfs_file);
9794 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9795 proxy->conf.lfs_line = proxy->conf.args.line;
9796
9797 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009798 } else if (strcmp(args[0], "redirect") == 0) {
9799 struct redirect_rule *redir;
9800 char *errmsg = NULL;
9801
9802 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
9803 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9804 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9805 goto out_err;
9806 }
9807
9808 /* this redirect rule might already contain a parsed condition which
9809 * we'll pass to the http-request rule.
9810 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009811 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009812 rule->arg.redir = redir;
9813 rule->cond = redir->cond;
9814 redir->cond = NULL;
9815 cur_arg = 2;
9816 return rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009817 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9818 char *errmsg = NULL;
9819 cur_arg = 1;
9820 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009821 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009822 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009823 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009824 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9825 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9826 free(errmsg);
9827 goto out_err;
9828 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009829 } else {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02009830 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 +02009831 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9832 goto out_err;
9833 }
9834
9835 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9836 struct acl_cond *cond;
9837 char *errmsg = NULL;
9838
9839 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9840 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9841 file, linenum, args[0], errmsg);
9842 free(errmsg);
9843 goto out_err;
9844 }
9845 rule->cond = cond;
9846 }
9847 else if (*args[cur_arg]) {
9848 Alert("parsing [%s:%d]: 'http-response %s' expects"
9849 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9850 file, linenum, args[0], args[cur_arg]);
9851 goto out_err;
9852 }
9853
9854 return rule;
9855 out_err:
9856 free(rule);
9857 return NULL;
9858}
9859
Willy Tarreau4baae242012-12-27 12:00:31 +01009860/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009861 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009862 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9863 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009864 */
9865struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009866 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009867{
9868 struct redirect_rule *rule;
9869 int cur_arg;
9870 int type = REDIRECT_TYPE_NONE;
9871 int code = 302;
9872 const char *destination = NULL;
9873 const char *cookie = NULL;
9874 int cookie_set = 0;
9875 unsigned int flags = REDIRECT_FLAG_NONE;
9876 struct acl_cond *cond = NULL;
9877
9878 cur_arg = 0;
9879 while (*(args[cur_arg])) {
9880 if (strcmp(args[cur_arg], "location") == 0) {
9881 if (!*args[cur_arg + 1])
9882 goto missing_arg;
9883
9884 type = REDIRECT_TYPE_LOCATION;
9885 cur_arg++;
9886 destination = args[cur_arg];
9887 }
9888 else if (strcmp(args[cur_arg], "prefix") == 0) {
9889 if (!*args[cur_arg + 1])
9890 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009891 type = REDIRECT_TYPE_PREFIX;
9892 cur_arg++;
9893 destination = args[cur_arg];
9894 }
9895 else if (strcmp(args[cur_arg], "scheme") == 0) {
9896 if (!*args[cur_arg + 1])
9897 goto missing_arg;
9898
9899 type = REDIRECT_TYPE_SCHEME;
9900 cur_arg++;
9901 destination = args[cur_arg];
9902 }
9903 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9904 if (!*args[cur_arg + 1])
9905 goto missing_arg;
9906
9907 cur_arg++;
9908 cookie = args[cur_arg];
9909 cookie_set = 1;
9910 }
9911 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9912 if (!*args[cur_arg + 1])
9913 goto missing_arg;
9914
9915 cur_arg++;
9916 cookie = args[cur_arg];
9917 cookie_set = 0;
9918 }
9919 else if (strcmp(args[cur_arg], "code") == 0) {
9920 if (!*args[cur_arg + 1])
9921 goto missing_arg;
9922
9923 cur_arg++;
9924 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009925 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009926 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009927 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009928 args[cur_arg - 1], args[cur_arg]);
9929 return NULL;
9930 }
9931 }
9932 else if (!strcmp(args[cur_arg],"drop-query")) {
9933 flags |= REDIRECT_FLAG_DROP_QS;
9934 }
9935 else if (!strcmp(args[cur_arg],"append-slash")) {
9936 flags |= REDIRECT_FLAG_APPEND_SLASH;
9937 }
9938 else if (strcmp(args[cur_arg], "if") == 0 ||
9939 strcmp(args[cur_arg], "unless") == 0) {
9940 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9941 if (!cond) {
9942 memprintf(errmsg, "error in condition: %s", *errmsg);
9943 return NULL;
9944 }
9945 break;
9946 }
9947 else {
9948 memprintf(errmsg,
9949 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9950 args[cur_arg]);
9951 return NULL;
9952 }
9953 cur_arg++;
9954 }
9955
9956 if (type == REDIRECT_TYPE_NONE) {
9957 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9958 return NULL;
9959 }
9960
Willy Tarreaube4653b2015-05-28 15:26:58 +02009961 if (dir && type != REDIRECT_TYPE_LOCATION) {
9962 memprintf(errmsg, "response only supports redirect type 'location'");
9963 return NULL;
9964 }
9965
Willy Tarreau4baae242012-12-27 12:00:31 +01009966 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9967 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009968 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009969
9970 if (!use_fmt) {
9971 /* old-style static redirect rule */
9972 rule->rdr_str = strdup(destination);
9973 rule->rdr_len = strlen(destination);
9974 }
9975 else {
9976 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009977
9978 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9979 * if prefix == "/", we don't want to add anything, otherwise it
9980 * makes it hard for the user to configure a self-redirection.
9981 */
Godbachd9722032014-12-18 15:44:58 +08009982 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009983 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009984 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009985 dir ? (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRS_HDR : SMP_VAL_BE_HRS_HDR
9986 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009987 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009988 free(curproxy->conf.lfs_file);
9989 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9990 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009991 }
9992 }
9993
Willy Tarreau4baae242012-12-27 12:00:31 +01009994 if (cookie) {
9995 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9996 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9997 */
9998 rule->cookie_len = strlen(cookie);
9999 if (cookie_set) {
10000 rule->cookie_str = malloc(rule->cookie_len + 10);
10001 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10002 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
10003 rule->cookie_len += 9;
10004 } else {
10005 rule->cookie_str = malloc(rule->cookie_len + 21);
10006 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10007 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
10008 rule->cookie_len += 20;
10009 }
10010 }
10011 rule->type = type;
10012 rule->code = code;
10013 rule->flags = flags;
10014 LIST_INIT(&rule->list);
10015 return rule;
10016
10017 missing_arg:
10018 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
10019 return NULL;
10020}
10021
Willy Tarreau8797c062007-05-07 00:55:35 +020010022/************************************************************************/
10023/* The code below is dedicated to ACL parsing and matching */
10024/************************************************************************/
10025
10026
Willy Tarreau14174bc2012-04-16 14:34:04 +020010027/* This function ensures that the prerequisites for an L7 fetch are ready,
10028 * which means that a request or response is ready. If some data is missing,
10029 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +020010030 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
10031 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010032 *
10033 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +020010034 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
10035 * decide whether or not an HTTP message is present ;
10036 * 0 if the requested data cannot be fetched or if it is certain that
10037 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010038 * 1 if an HTTP message is ready
10039 */
James Rosewell91a41cb2015-09-18 17:11:16 +010010040int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010041 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +020010042{
Willy Tarreau192252e2015-04-04 01:47:55 +020010043 struct http_txn *txn;
10044 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010045
Willy Tarreaueee5b512015-04-03 23:46:31 +020010046 /* Note: this function may only be used from places where
10047 * http_init_txn() has already been done, and implies that <s>,
10048 * <txn>, and <hdr_idx.v> are properly set. An extra check protects
10049 * against an eventual mistake in the fetch capability matrix.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010050 */
10051
Willy Tarreau192252e2015-04-04 01:47:55 +020010052 if (!s)
10053 return 0;
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +020010054 if (!s->txn) {
10055 if (unlikely(!http_alloc_txn(s)))
10056 return 0; /* not enough memory */
10057 http_init_txn(s);
10058 }
Willy Tarreau192252e2015-04-04 01:47:55 +020010059 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +020010060 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010061
10062 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010063 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010064
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010065 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010066 /* If the buffer does not leave enough free space at the end,
10067 * we must first realign it.
10068 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010069 if (s->req.buf->p > s->req.buf->data &&
10070 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
10071 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +010010072
Willy Tarreau14174bc2012-04-16 14:34:04 +020010073 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +020010074 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +020010075 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010076
10077 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010078 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +020010079 http_msg_analyzer(msg, &txn->hdr_idx);
10080
10081 /* Still no valid request ? */
10082 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +020010083 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010084 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +020010085 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010086 }
10087 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +020010088 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010089 return 0;
10090 }
10091
10092 /* OK we just got a valid HTTP request. We have some minor
10093 * preparation to perform so that further checks can rely
10094 * on HTTP tests.
10095 */
Willy Tarreauaae75e32013-03-29 12:31:49 +010010096
10097 /* If the request was parsed but was too large, we must absolutely
10098 * return an error so that it is not processed. At the moment this
10099 * cannot happen, but if the parsers are to change in the future,
10100 * we want this check to be maintained.
10101 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010102 if (unlikely(s->req.buf->i + s->req.buf->p >
10103 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010104 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010105 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010106 return 1;
10107 }
10108
Willy Tarreau9b28e032012-10-12 23:49:43 +020010109 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +020010110 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +020010111 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010112
Willy Tarreau506d0502013-07-06 13:29:24 +020010113 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
10114 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010115 }
10116
Willy Tarreau506d0502013-07-06 13:29:24 +020010117 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +020010118 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +020010119 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010120
10121 /* otherwise everything's ready for the request */
10122 }
Willy Tarreau24e32d82012-04-23 23:55:44 +020010123 else {
10124 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +020010125 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
10126 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010127 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +020010128 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010129 }
10130
10131 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010132 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010133 return 1;
10134}
Willy Tarreau8797c062007-05-07 00:55:35 +020010135
Willy Tarreau8797c062007-05-07 00:55:35 +020010136/* 1. Check on METHOD
10137 * We use the pre-parsed method if it is known, and store its number as an
10138 * integer. If it is unknown, we use the pointer and the length.
10139 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010140static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +020010141{
10142 int len, meth;
10143
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010010144 len = strlen(text);
10145 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +020010146
10147 pattern->val.i = meth;
10148 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +020010149 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +020010150 pattern->len = len;
10151 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010152 else {
10153 pattern->ptr.str = NULL;
10154 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010155 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010156 return 1;
10157}
10158
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010159/* This function fetches the method of current HTTP request and stores
10160 * it in the global pattern struct as a chunk. There are two possibilities :
10161 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10162 * in <len> and <ptr> is NULL ;
10163 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10164 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010165 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010166 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010167static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010168smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010169{
10170 int meth;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010171 struct http_txn *txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010172
Willy Tarreau24e32d82012-04-23 23:55:44 +020010173 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010174
Willy Tarreau8797c062007-05-07 00:55:35 +020010175 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010176 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010177 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010178 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010179 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10180 /* ensure the indexes are not affected */
10181 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010182 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010183 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
10184 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010185 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010186 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010187 return 1;
10188}
10189
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010190/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010191static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010192{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010193 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010194 struct pattern_list *lst;
10195 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010196
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010197 list_for_each_entry(lst, &expr->patterns, list) {
10198 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010199
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010200 /* well-known method */
10201 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010202 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010203 return pattern;
10204 else
10205 continue;
10206 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010207
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010208 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010209 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010210 continue;
10211
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010212 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010213 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
10214 (!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 +010010215 return pattern;
10216 }
10217 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010218}
10219
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010220static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010221smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010222{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010223 struct http_txn *txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010224 char *ptr;
10225 int len;
10226
Willy Tarreauc0239e02012-04-16 14:42:55 +020010227 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010228
Willy Tarreau8797c062007-05-07 00:55:35 +020010229 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010230 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010231
10232 while ((len-- > 0) && (*ptr++ != '/'));
10233 if (len <= 0)
10234 return 0;
10235
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010236 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010237 smp->data.u.str.str = ptr;
10238 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010239
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010240 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010241 return 1;
10242}
10243
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010244static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010245smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010246{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010247 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010248 char *ptr;
10249 int len;
10250
Willy Tarreauc0239e02012-04-16 14:42:55 +020010251 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010252
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010253 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010254 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10255 return 0;
10256
Willy Tarreau8797c062007-05-07 00:55:35 +020010257 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010258 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010259
10260 while ((len-- > 0) && (*ptr++ != '/'));
10261 if (len <= 0)
10262 return 0;
10263
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010264 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010265 smp->data.u.str.str = ptr;
10266 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010267
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010268 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010269 return 1;
10270}
10271
10272/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010273static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010274smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010275{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010276 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010277 char *ptr;
10278 int len;
10279
Willy Tarreauc0239e02012-04-16 14:42:55 +020010280 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010281
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010282 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010283 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10284 return 0;
10285
Willy Tarreau8797c062007-05-07 00:55:35 +020010286 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010287 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010288
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010289 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010290 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010291 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010292 return 1;
10293}
10294
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010295/* returns the longest available part of the body. This requires that the body
10296 * has been waited for using http-buffer-request.
10297 */
10298static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010299smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010300{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010301 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010302 struct http_msg *msg;
10303 unsigned long len;
10304 unsigned long block1;
10305 char *body;
10306 struct chunk *temp;
10307
10308 CHECK_HTTP_MESSAGE_FIRST();
10309
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010310 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010311 msg = &txn->req;
10312 else
10313 msg = &txn->rsp;
10314
10315 len = http_body_bytes(msg);
10316 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
10317
10318 block1 = len;
10319 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
10320 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
10321
10322 if (block1 == len) {
10323 /* buffer is not wrapped (or empty) */
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 = body;
10326 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010327 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
10328 }
10329 else {
10330 /* buffer is wrapped, we need to defragment it */
10331 temp = get_trash_chunk();
10332 memcpy(temp->str, body, block1);
10333 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010334 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010335 smp->data.u.str.str = temp->str;
10336 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010337 smp->flags = SMP_F_VOL_TEST;
10338 }
10339 return 1;
10340}
10341
10342
10343/* returns the available length of the body. This requires that the body
10344 * has been waited for using http-buffer-request.
10345 */
10346static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010347smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010348{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010349 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010350 struct http_msg *msg;
10351
10352 CHECK_HTTP_MESSAGE_FIRST();
10353
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010354 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010355 msg = &txn->req;
10356 else
10357 msg = &txn->rsp;
10358
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010359 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010360 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010361
10362 smp->flags = SMP_F_VOL_TEST;
10363 return 1;
10364}
10365
10366
10367/* returns the advertised length of the body, or the advertised size of the
10368 * chunks available in the buffer. This requires that the body has been waited
10369 * for using http-buffer-request.
10370 */
10371static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010372smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010373{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010374 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010375 struct http_msg *msg;
10376
10377 CHECK_HTTP_MESSAGE_FIRST();
10378
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010379 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010380 msg = &txn->req;
10381 else
10382 msg = &txn->rsp;
10383
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010384 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010385 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010386
10387 smp->flags = SMP_F_VOL_TEST;
10388 return 1;
10389}
10390
10391
Willy Tarreau8797c062007-05-07 00:55:35 +020010392/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010393static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010394smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010395{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010396 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010397
Willy Tarreauc0239e02012-04-16 14:42:55 +020010398 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010399
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010400 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010401 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010402 smp->data.u.str.len = txn->req.sl.rq.u_l;
10403 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010404 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010405 return 1;
10406}
10407
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010408static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010409smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010410{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010411 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010412 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010413
Willy Tarreauc0239e02012-04-16 14:42:55 +020010414 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010415
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010416 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010417 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 +020010418 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010419 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010420
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010421 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010422 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010423 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010424 return 1;
10425}
10426
10427static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010428smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010429{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010430 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010431 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010432
Willy Tarreauc0239e02012-04-16 14:42:55 +020010433 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010434
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010435 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010436 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 +020010437 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10438 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010439
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010440 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010441 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010442 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010443 return 1;
10444}
10445
Willy Tarreau185b5c42012-04-26 15:11:51 +020010446/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10447 * Accepts an optional argument of type string containing the header field name,
10448 * and an optional argument of type signed or unsigned integer to request an
10449 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010450 * headers are considered from the first one. It does not stop on commas and
10451 * returns full lines instead (useful for User-Agent or Date for example).
10452 */
10453static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010454smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010455{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010456 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010457 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010458 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010459 int occ = 0;
10460 const char *name_str = NULL;
10461 int name_len = 0;
10462
10463 if (!ctx) {
10464 /* first call */
10465 ctx = &static_hdr_ctx;
10466 ctx->idx = 0;
10467 smp->ctx.a[0] = ctx;
10468 }
10469
10470 if (args) {
10471 if (args[0].type != ARGT_STR)
10472 return 0;
10473 name_str = args[0].data.str.str;
10474 name_len = args[0].data.str.len;
10475
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010476 if (args[1].type == ARGT_SINT)
10477 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010478 }
10479
10480 CHECK_HTTP_MESSAGE_FIRST();
10481
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010482 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010483 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 +020010484
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010485 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10486 /* search for header from the beginning */
10487 ctx->idx = 0;
10488
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010489 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010490 /* no explicit occurrence and single fetch => last header by default */
10491 occ = -1;
10492
10493 if (!occ)
10494 /* prepare to report multiple occurrences for ACL fetches */
10495 smp->flags |= SMP_F_NOT_LAST;
10496
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010497 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010498 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010499 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 +020010500 return 1;
10501
10502 smp->flags &= ~SMP_F_NOT_LAST;
10503 return 0;
10504}
10505
10506/* 6. Check on HTTP header count. The number of occurrences is returned.
10507 * Accepts exactly 1 argument of type string. It does not stop on commas and
10508 * returns full lines instead (useful for User-Agent or Date for example).
10509 */
10510static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010511smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010512{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010513 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010514 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010515 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010516 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010517 const char *name = NULL;
10518 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010519
Willy Tarreau601a4d12015-04-01 19:16:09 +020010520 if (args && args->type == ARGT_STR) {
10521 name = args->data.str.str;
10522 len = args->data.str.len;
10523 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010524
10525 CHECK_HTTP_MESSAGE_FIRST();
10526
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010527 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010528 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 +020010529
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010530 ctx.idx = 0;
10531 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010532 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010533 cnt++;
10534
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010535 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010536 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010537 smp->flags = SMP_F_VOL_HDR;
10538 return 1;
10539}
10540
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010541static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010542smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010543{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010544 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010545 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010546 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010547 struct chunk *temp;
10548 char del = ',';
10549
10550 if (args && args->type == ARGT_STR)
10551 del = *args[0].data.str.str;
10552
10553 CHECK_HTTP_MESSAGE_FIRST();
10554
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010555 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010556 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 +020010557
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010558 temp = get_trash_chunk();
10559
10560 ctx.idx = 0;
10561 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10562 if (temp->len)
10563 temp->str[temp->len++] = del;
10564 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10565 temp->len += ctx.del;
10566 }
10567
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010568 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010569 smp->data.u.str.str = temp->str;
10570 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010571 smp->flags = SMP_F_VOL_HDR;
10572 return 1;
10573}
10574
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010575/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10576 * Accepts an optional argument of type string containing the header field name,
10577 * and an optional argument of type signed or unsigned integer to request an
10578 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010579 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010580 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010581static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010582smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010583{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010584 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010585 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010586 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010587 int occ = 0;
10588 const char *name_str = NULL;
10589 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010590
Willy Tarreaua890d072013-04-02 12:01:06 +020010591 if (!ctx) {
10592 /* first call */
10593 ctx = &static_hdr_ctx;
10594 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010595 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010596 }
10597
Willy Tarreau185b5c42012-04-26 15:11:51 +020010598 if (args) {
10599 if (args[0].type != ARGT_STR)
10600 return 0;
10601 name_str = args[0].data.str.str;
10602 name_len = args[0].data.str.len;
10603
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010604 if (args[1].type == ARGT_SINT)
10605 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010606 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010607
Willy Tarreaue333ec92012-04-16 16:26:40 +020010608 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010609
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010610 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010611 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 +020010612
Willy Tarreau185b5c42012-04-26 15:11:51 +020010613 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010614 /* search for header from the beginning */
10615 ctx->idx = 0;
10616
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010617 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010618 /* no explicit occurrence and single fetch => last header by default */
10619 occ = -1;
10620
10621 if (!occ)
10622 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010623 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010624
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010625 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010626 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010627 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 +020010628 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010629
Willy Tarreau37406352012-04-23 16:16:37 +020010630 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010631 return 0;
10632}
10633
Willy Tarreauc11416f2007-06-17 16:58:38 +020010634/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010635 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010636 */
10637static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010638smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010639{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010640 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010641 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010642 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010643 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010644 const char *name = NULL;
10645 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010646
Willy Tarreau601a4d12015-04-01 19:16:09 +020010647 if (args && args->type == ARGT_STR) {
10648 name = args->data.str.str;
10649 len = args->data.str.len;
10650 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010651
Willy Tarreaue333ec92012-04-16 16:26:40 +020010652 CHECK_HTTP_MESSAGE_FIRST();
10653
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010654 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010655 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 +020010656
Willy Tarreau33a7e692007-06-10 19:45:56 +020010657 ctx.idx = 0;
10658 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010659 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010660 cnt++;
10661
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010662 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010663 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010664 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010665 return 1;
10666}
10667
Willy Tarreau185b5c42012-04-26 15:11:51 +020010668/* Fetch an HTTP header's integer value. The integer value is returned. It
10669 * takes a mandatory argument of type string and an optional one of type int
10670 * to designate a specific occurrence. It returns an unsigned integer, which
10671 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010672 */
10673static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010674smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010675{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010676 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010677
Willy Tarreauf853c462012-04-23 18:53:56 +020010678 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010679 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010680 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010681 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010682
Willy Tarreaud53e2422012-04-16 17:21:11 +020010683 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010684}
10685
Cyril Bonté69fa9922012-10-25 00:01:06 +020010686/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10687 * and an optional one of type int to designate a specific occurrence.
10688 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010689 */
10690static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010691smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010692{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010693 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010694
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010695 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010696 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010697 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010698 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010699 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010700 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010701 if (smp->data.u.str.len < temp->size - 1) {
10702 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10703 temp->str[smp->data.u.str.len] = '\0';
10704 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010705 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010706 break;
10707 }
10708 }
10709 }
10710
Willy Tarreaud53e2422012-04-16 17:21:11 +020010711 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010712 if (!(smp->flags & SMP_F_NOT_LAST))
10713 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010714 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010715 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010716}
10717
Willy Tarreau737b0c12007-06-10 21:28:46 +020010718/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10719 * the first '/' after the possible hostname, and ends before the possible '?'.
10720 */
10721static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010722smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010723{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010724 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010725 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010726
Willy Tarreauc0239e02012-04-16 14:42:55 +020010727 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010728
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010729 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010730 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010731 ptr = http_get_path(txn);
10732 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010733 return 0;
10734
10735 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010736 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010737 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010738
10739 while (ptr < end && *ptr != '?')
10740 ptr++;
10741
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010742 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010743 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010744 return 1;
10745}
10746
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010747/* This produces a concatenation of the first occurrence of the Host header
10748 * followed by the path component if it begins with a slash ('/'). This means
10749 * that '*' will not be added, resulting in exactly the first Host entry.
10750 * If no Host header is found, then the path is returned as-is. The returned
10751 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010752 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010753 */
10754static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010755smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010756{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010757 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010758 char *ptr, *end, *beg;
10759 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010760 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010761
10762 CHECK_HTTP_MESSAGE_FIRST();
10763
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010764 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010765 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010766 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010767 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010768
10769 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010770 temp = get_trash_chunk();
10771 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010772 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010773 smp->data.u.str.str = temp->str;
10774 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010775
10776 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010777 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010778 beg = http_get_path(txn);
10779 if (!beg)
10780 beg = end;
10781
10782 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10783
10784 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010785 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10786 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010787 }
10788
10789 smp->flags = SMP_F_VOL_1ST;
10790 return 1;
10791}
10792
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010793/* This produces a 32-bit hash of the concatenation of the first occurrence of
10794 * the Host header followed by the path component if it begins with a slash ('/').
10795 * This means that '*' will not be added, resulting in exactly the first Host
10796 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010797 * is hashed using the path hash followed by a full avalanche hash and provides a
10798 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010799 * high-traffic sites without having to store whole paths.
10800 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010801int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010802smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010803{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010804 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010805 struct hdr_ctx ctx;
10806 unsigned int hash = 0;
10807 char *ptr, *beg, *end;
10808 int len;
10809
10810 CHECK_HTTP_MESSAGE_FIRST();
10811
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010812 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010813 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010814 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010815 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10816 ptr = ctx.line + ctx.val;
10817 len = ctx.vlen;
10818 while (len--)
10819 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10820 }
10821
10822 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010823 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010824 beg = http_get_path(txn);
10825 if (!beg)
10826 beg = end;
10827
10828 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10829
10830 if (beg < ptr && *beg == '/') {
10831 while (beg < ptr)
10832 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10833 }
10834 hash = full_hash(hash);
10835
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010836 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010837 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010838 smp->flags = SMP_F_VOL_1ST;
10839 return 1;
10840}
10841
Willy Tarreau4a550602012-12-09 14:53:32 +010010842/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010843 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10844 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10845 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010846 * that in environments where IPv6 is insignificant, truncating the output to
10847 * 8 bytes would still work.
10848 */
10849static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010850smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010851{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010852 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010853 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010854
10855 if (!cli_conn)
10856 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010857
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010858 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010859 return 0;
10860
Willy Tarreau47ca5452012-12-23 20:22:19 +010010861 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010862 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010863 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010864
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010865 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010866 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010867 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010868 temp->len += 4;
10869 break;
10870 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010871 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010872 temp->len += 16;
10873 break;
10874 default:
10875 return 0;
10876 }
10877
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010878 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010879 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010880 return 1;
10881}
10882
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010883/* Extracts the query string, which comes after the question mark '?'. If no
10884 * question mark is found, nothing is returned. Otherwise it returns a sample
10885 * of type string carrying the whole query string.
10886 */
10887static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010888smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010889{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010890 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010891 char *ptr, *end;
10892
10893 CHECK_HTTP_MESSAGE_FIRST();
10894
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010895 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010896 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10897 end = ptr + txn->req.sl.rq.u_l;
10898
10899 /* look up the '?' */
10900 do {
10901 if (ptr == end)
10902 return 0;
10903 } while (*ptr++ != '?');
10904
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010905 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010906 smp->data.u.str.str = ptr;
10907 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010908 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10909 return 1;
10910}
10911
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010912static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010913smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010914{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010915 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10916 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10917 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010918
Willy Tarreau24e32d82012-04-23 23:55:44 +020010919 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010920
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010921 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010922 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010923 return 1;
10924}
10925
Willy Tarreau7f18e522010-10-22 20:04:13 +020010926/* return a valid test if the current request is the first one on the connection */
10927static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010928smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010929{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010930 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010931 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010932 return 1;
10933}
10934
Willy Tarreau34db1082012-04-19 17:16:54 +020010935/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010936static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010937smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010938{
10939
Willy Tarreau24e32d82012-04-23 23:55:44 +020010940 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010941 return 0;
10942
Willy Tarreauc0239e02012-04-16 14:42:55 +020010943 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010944
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010945 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010946 return 0;
10947
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010948 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010949 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010950 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010951 return 1;
10952}
Willy Tarreau8797c062007-05-07 00:55:35 +020010953
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010954/* Accepts exactly 1 argument of type userlist */
10955static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010956smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010957{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010958 if (!args || args->type != ARGT_USR)
10959 return 0;
10960
10961 CHECK_HTTP_MESSAGE_FIRST();
10962
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010963 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010964 return 0;
10965
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010966 /* if the user does not belong to the userlist or has a wrong password,
10967 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010968 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010969 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010970 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10971 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010972 return 0;
10973
10974 /* pat_match_auth() will need the user list */
10975 smp->ctx.a[0] = args->data.usr;
10976
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010977 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010978 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010979 smp->data.u.str.str = smp->strm->txn->auth.user;
10980 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010981
10982 return 1;
10983}
10984
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010985/* Try to find the next occurrence of a cookie name in a cookie header value.
10986 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10987 * the cookie value is returned into *value and *value_l, and the function
10988 * returns a pointer to the next pointer to search from if the value was found.
10989 * Otherwise if the cookie was not found, NULL is returned and neither value
10990 * nor value_l are touched. The input <hdr> string should first point to the
10991 * header's value, and the <hdr_end> pointer must point to the first character
10992 * not part of the value. <list> must be non-zero if value may represent a list
10993 * of values (cookie headers). This makes it faster to abort parsing when no
10994 * list is expected.
10995 */
David Carlier4686f792015-09-25 14:10:50 +010010996char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010997extract_cookie_value(char *hdr, const char *hdr_end,
10998 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010999 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011000{
11001 char *equal, *att_end, *att_beg, *val_beg, *val_end;
11002 char *next;
11003
11004 /* we search at least a cookie name followed by an equal, and more
11005 * generally something like this :
11006 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
11007 */
11008 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
11009 /* Iterate through all cookies on this line */
11010
11011 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
11012 att_beg++;
11013
11014 /* find att_end : this is the first character after the last non
11015 * space before the equal. It may be equal to hdr_end.
11016 */
11017 equal = att_end = att_beg;
11018
11019 while (equal < hdr_end) {
11020 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
11021 break;
11022 if (http_is_spht[(unsigned char)*equal++])
11023 continue;
11024 att_end = equal;
11025 }
11026
11027 /* here, <equal> points to '=', a delimitor or the end. <att_end>
11028 * is between <att_beg> and <equal>, both may be identical.
11029 */
11030
11031 /* look for end of cookie if there is an equal sign */
11032 if (equal < hdr_end && *equal == '=') {
11033 /* look for the beginning of the value */
11034 val_beg = equal + 1;
11035 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
11036 val_beg++;
11037
11038 /* find the end of the value, respecting quotes */
11039 next = find_cookie_value_end(val_beg, hdr_end);
11040
11041 /* make val_end point to the first white space or delimitor after the value */
11042 val_end = next;
11043 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
11044 val_end--;
11045 } else {
11046 val_beg = val_end = next = equal;
11047 }
11048
11049 /* We have nothing to do with attributes beginning with '$'. However,
11050 * they will automatically be removed if a header before them is removed,
11051 * since they're supposed to be linked together.
11052 */
11053 if (*att_beg == '$')
11054 continue;
11055
11056 /* Ignore cookies with no equal sign */
11057 if (equal == next)
11058 continue;
11059
11060 /* Now we have the cookie name between att_beg and att_end, and
11061 * its value between val_beg and val_end.
11062 */
11063
11064 if (att_end - att_beg == cookie_name_l &&
11065 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
11066 /* let's return this value and indicate where to go on from */
11067 *value = val_beg;
11068 *value_l = val_end - val_beg;
11069 return next + 1;
11070 }
11071
11072 /* Set-Cookie headers only have the name in the first attr=value part */
11073 if (!list)
11074 break;
11075 }
11076
11077 return NULL;
11078}
11079
William Lallemanda43ba4e2014-01-28 18:14:25 +010011080/* Fetch a captured HTTP request header. The index is the position of
11081 * the "capture" option in the configuration file
11082 */
11083static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011084smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011085{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011086 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011087 int idx;
11088
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011089 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011090 return 0;
11091
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011092 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011093
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011094 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 +010011095 return 0;
11096
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011097 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011098 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011099 smp->data.u.str.str = smp->strm->req_cap[idx];
11100 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011101
11102 return 1;
11103}
11104
11105/* Fetch a captured HTTP response header. The index is the position of
11106 * the "capture" option in the configuration file
11107 */
11108static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011109smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011110{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011111 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011112 int idx;
11113
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011114 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011115 return 0;
11116
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011117 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011118
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011119 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 +010011120 return 0;
11121
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011122 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011123 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011124 smp->data.u.str.str = smp->strm->res_cap[idx];
11125 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011126
11127 return 1;
11128}
11129
William Lallemand65ad6e12014-01-31 15:08:02 +010011130/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
11131static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011132smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011133{
11134 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011135 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010011136 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011137
Willy Tarreau15e91e12015-04-04 00:52:09 +020011138 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011139 return 0;
11140
William Lallemand96a77852014-02-05 00:30:02 +010011141 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011142
William Lallemand96a77852014-02-05 00:30:02 +010011143 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11144 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011145
William Lallemand96a77852014-02-05 00:30:02 +010011146 temp = get_trash_chunk();
11147 temp->str = txn->uri;
11148 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011149 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011150 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011151 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011152
11153 return 1;
11154
11155}
11156
11157/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
11158static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011159smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011160{
11161 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011162 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010011163 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011164
Willy Tarreau15e91e12015-04-04 00:52:09 +020011165 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011166 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010011167
William Lallemand65ad6e12014-01-31 15:08:02 +010011168 ptr = txn->uri;
11169
11170 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11171 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010011172
William Lallemand65ad6e12014-01-31 15:08:02 +010011173 if (!*ptr)
11174 return 0;
11175
11176 ptr++; /* skip the space */
11177
11178 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010011179 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010011180 if (!ptr)
11181 return 0;
11182 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
11183 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011184
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011185 smp->data.u.str = *temp;
11186 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011187 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011188 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011189
11190 return 1;
11191}
11192
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011193/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
11194 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11195 */
11196static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011197smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011198{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011199 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011200
Willy Tarreau15e91e12015-04-04 00:52:09 +020011201 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011202 return 0;
11203
11204 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011205 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011206 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011207 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011208
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011209 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011210 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011211 smp->flags = SMP_F_CONST;
11212 return 1;
11213
11214}
11215
11216/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
11217 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11218 */
11219static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011220smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011221{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011222 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011223
Willy Tarreau15e91e12015-04-04 00:52:09 +020011224 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011225 return 0;
11226
11227 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011228 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011229 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011230 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011231
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011232 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011233 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011234 smp->flags = SMP_F_CONST;
11235 return 1;
11236
11237}
11238
William Lallemand65ad6e12014-01-31 15:08:02 +010011239
Willy Tarreaue333ec92012-04-16 16:26:40 +020011240/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011241 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011242 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011243 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011244 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011245 * Accepts exactly 1 argument of type string. If the input options indicate
11246 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011247 * The returned sample is of type CSTR. Can be used to parse cookies in other
11248 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011249 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011250int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011251{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011252 struct http_txn *txn;
11253 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011254 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011255 const struct http_msg *msg;
11256 const char *hdr_name;
11257 int hdr_name_len;
11258 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011259 int occ = 0;
11260 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011261
Willy Tarreau24e32d82012-04-23 23:55:44 +020011262 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011263 return 0;
11264
Willy Tarreaua890d072013-04-02 12:01:06 +020011265 if (!ctx) {
11266 /* first call */
11267 ctx = &static_hdr_ctx;
11268 ctx->idx = 0;
11269 smp->ctx.a[2] = ctx;
11270 }
11271
Willy Tarreaue333ec92012-04-16 16:26:40 +020011272 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011273
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011274 txn = smp->strm->txn;
11275 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011276
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011277 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011278 msg = &txn->req;
11279 hdr_name = "Cookie";
11280 hdr_name_len = 6;
11281 } else {
11282 msg = &txn->rsp;
11283 hdr_name = "Set-Cookie";
11284 hdr_name_len = 10;
11285 }
11286
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011287 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020011288 /* no explicit occurrence and single fetch => last cookie by default */
11289 occ = -1;
11290
11291 /* OK so basically here, either we want only one value and it's the
11292 * last one, or we want to iterate over all of them and we fetch the
11293 * next one.
11294 */
11295
Willy Tarreau9b28e032012-10-12 23:49:43 +020011296 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011297 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011298 /* search for the header from the beginning, we must first initialize
11299 * the search parameters.
11300 */
Willy Tarreau37406352012-04-23 16:16:37 +020011301 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011302 ctx->idx = 0;
11303 }
11304
Willy Tarreau28376d62012-04-26 21:26:10 +020011305 smp->flags |= SMP_F_VOL_HDR;
11306
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011307 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011308 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11309 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011310 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11311 goto out;
11312
Willy Tarreau24e32d82012-04-23 23:55:44 +020011313 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011314 continue;
11315
Willy Tarreau37406352012-04-23 16:16:37 +020011316 smp->ctx.a[0] = ctx->line + ctx->val;
11317 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011318 }
11319
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011320 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011321 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011322 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011323 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011324 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011325 &smp->data.u.str.str,
11326 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011327 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011328 found = 1;
11329 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011330 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020011331 smp->flags |= SMP_F_NOT_LAST;
11332 return 1;
11333 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011334 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011335 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011336 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011337 /* all cookie headers and values were scanned. If we're looking for the
11338 * last occurrence, we may return it now.
11339 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011340 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011341 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011342 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011343}
11344
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011345/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011346 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011347 * multiple cookies may be parsed on the same line. The returned sample is of
11348 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011349 */
11350static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011351smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011352{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011353 struct http_txn *txn;
11354 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011355 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011356 const struct http_msg *msg;
11357 const char *hdr_name;
11358 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011359 int cnt;
11360 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011361 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011362
Willy Tarreau24e32d82012-04-23 23:55:44 +020011363 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011364 return 0;
11365
Willy Tarreaue333ec92012-04-16 16:26:40 +020011366 CHECK_HTTP_MESSAGE_FIRST();
11367
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011368 txn = smp->strm->txn;
11369 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011370
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011371 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011372 msg = &txn->req;
11373 hdr_name = "Cookie";
11374 hdr_name_len = 6;
11375 } else {
11376 msg = &txn->rsp;
11377 hdr_name = "Set-Cookie";
11378 hdr_name_len = 10;
11379 }
11380
Willy Tarreau9b28e032012-10-12 23:49:43 +020011381 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011382 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011383 ctx.idx = 0;
11384 cnt = 0;
11385
11386 while (1) {
11387 /* Note: val_beg == NULL every time we need to fetch a new header */
11388 if (!val_beg) {
11389 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11390 break;
11391
Willy Tarreau24e32d82012-04-23 23:55:44 +020011392 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011393 continue;
11394
11395 val_beg = ctx.line + ctx.val;
11396 val_end = val_beg + ctx.vlen;
11397 }
11398
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011399 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011400 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011401 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011402 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011403 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011404 &smp->data.u.str.str,
11405 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011406 cnt++;
11407 }
11408 }
11409
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011410 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011411 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011412 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011413 return 1;
11414}
11415
Willy Tarreau51539362012-05-08 12:46:28 +020011416/* Fetch an cookie's integer value. The integer value is returned. It
11417 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11418 */
11419static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011420smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011421{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011422 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011423
11424 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011425 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011426 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020011427 }
11428
11429 return ret;
11430}
11431
Willy Tarreau8797c062007-05-07 00:55:35 +020011432/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011433/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011434/************************************************************************/
11435
David Cournapeau16023ee2010-12-23 20:55:41 +090011436/*
11437 * Given a path string and its length, find the position of beginning of the
11438 * query string. Returns NULL if no query string is found in the path.
11439 *
11440 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11441 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011442 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011443 */
bedis4c75cca2012-10-05 08:38:24 +020011444static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011445{
11446 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011447
bedis4c75cca2012-10-05 08:38:24 +020011448 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011449 return p ? p + 1 : NULL;
11450}
11451
bedis4c75cca2012-10-05 08:38:24 +020011452static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011453{
bedis4c75cca2012-10-05 08:38:24 +020011454 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011455}
11456
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011457/* after increasing a pointer value, it can exceed the first buffer
11458 * size. This function transform the value of <ptr> according with
11459 * the expected position. <chunks> is an array of the one or two
11460 * avalaible chunks. The first value is the start of the first chunk,
11461 * the second value if the end+1 of the first chunks. The third value
11462 * is NULL or the start of the second chunk and the fourth value is
11463 * the end+1 of the second chunk. The function returns 1 if does a
11464 * wrap, else returns 0.
11465 */
11466static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11467{
11468 if (*ptr < chunks[1])
11469 return 0;
11470 if (!chunks[2])
11471 return 0;
11472 *ptr = chunks[2] + ( *ptr - chunks[1] );
11473 return 1;
11474}
11475
David Cournapeau16023ee2010-12-23 20:55:41 +090011476/*
11477 * Given a url parameter, find the starting position of the first occurence,
11478 * or NULL if the parameter is not found.
11479 *
11480 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11481 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011482 *
11483 * Warning:this function returns a pointer that can be point to the first chunk
11484 * or the second chunk. The caller must be check the position before using the
11485 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011486 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011487static const char *
11488find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011489 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011490 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011491{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011492 const char *pos, *last, *equal;
11493 const char **bufs = chunks;
11494 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011495
David Cournapeau16023ee2010-12-23 20:55:41 +090011496
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011497 pos = bufs[0];
11498 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011499 while (pos <= last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011500 /* Check the equal. */
11501 equal = pos + url_param_name_l;
11502 if (fix_pointer_if_wrap(chunks, &equal)) {
11503 if (equal >= chunks[3])
11504 return NULL;
11505 } else {
11506 if (equal >= chunks[1])
11507 return NULL;
11508 }
11509 if (*equal == '=') {
11510 if (pos + url_param_name_l > last) {
11511 /* process wrap case, we detect a wrap. In this case, the
11512 * comparison is performed in two parts.
11513 */
11514
11515 /* This is the end, we dont have any other chunk. */
11516 if (bufs != chunks || !bufs[2])
11517 return NULL;
11518
11519 /* Compute the length of each part of the comparison. */
11520 l1 = last - pos;
11521 l2 = url_param_name_l - l1;
11522
11523 /* The second buffer is too short to contain the compared string. */
11524 if (bufs[2] + l2 > bufs[3])
11525 return NULL;
11526
11527 if (memcmp(pos, url_param_name, l1) == 0 &&
11528 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11529 return pos;
11530
11531 /* Perform wrapping and jump the string who fail the comparison. */
11532 bufs += 2;
11533 pos = bufs[0] + l2;
11534 last = bufs[1];
11535
11536 } else {
11537 /* process a simple comparison. */
11538 if (memcmp(pos, url_param_name, url_param_name_l) == 0) {
11539 return pos; }
11540 pos += url_param_name_l + 1;
11541 if (fix_pointer_if_wrap(chunks, &pos))
11542 last = bufs[2];
11543 }
11544 }
11545
11546 while (1) {
11547 /* Look for the next delimiter. */
11548 while (pos <= last && !is_param_delimiter(*pos, delim))
11549 pos++;
11550 if (pos < last)
11551 break;
11552 /* process buffer wrapping. */
11553 if (bufs != chunks || !bufs[2])
11554 return NULL;
11555 bufs += 2;
11556 pos = bufs[0];
11557 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011558 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011559 pos++;
11560 }
11561 return NULL;
11562}
11563
11564/*
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011565 * Given a url parameter name and a query string, returns its value and size
11566 * into *value and *value_l respectively, and returns non-zero. An empty
11567 * url_param_name matches the first available parameter. If the parameter is
11568 * not found, zero is returned and value/value_l are not touched.
David Cournapeau16023ee2010-12-23 20:55:41 +090011569 */
11570static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011571find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011572 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011573 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011574{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011575 const char *arg_start, *qs_end;
11576 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011577
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011578 arg_start = chunks[0];
11579 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011580 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011581 /* Looks for an argument name. */
11582 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011583 url_param_name, url_param_name_l,
11584 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011585 /* Check for wrapping. */
11586 if (arg_start > qs_end)
11587 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011588 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011589 if (!arg_start)
11590 return 0;
11591
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011592 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011593 while (1) {
11594 /* looks for the first argument. */
11595 value_start = memchr(arg_start, '=', qs_end - arg_start);
11596 if (!value_start) {
11597
11598 /* Check for wrapping. */
11599 if (arg_start >= chunks[0] &&
11600 arg_start <= chunks[1] &&
11601 chunks[2]) {
11602 arg_start = chunks[2];
11603 qs_end = chunks[3];
11604 continue;
11605 }
11606 return 0;
11607 }
11608 break;
11609 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011610 value_start++;
11611 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011612 else {
11613 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011614 value_start = arg_start + url_param_name_l + 1;
11615
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011616 /* Check for pointer wrapping. */
11617 if (fix_pointer_if_wrap(chunks, &value_start)) {
11618 /* Update the end pointer. */
11619 qs_end = chunks[3];
11620
11621 /* Check for overflow. */
11622 if (value_start > qs_end)
11623 return 0;
11624 }
11625 }
11626
David Cournapeau16023ee2010-12-23 20:55:41 +090011627 value_end = value_start;
11628
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011629 while (1) {
11630 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11631 value_end++;
11632 if (value_end < qs_end)
11633 break;
11634 /* process buffer wrapping. */
11635 if (value_end >= chunks[0] &&
11636 value_end <= chunks[1] &&
11637 chunks[2]) {
11638 value_end = chunks[2];
11639 qs_end = chunks[3];
11640 continue;
11641 }
11642 break;
11643 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011644
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011645 *vstart = value_start;
11646 *vend = value_end;
Willy Tarreau00134332011-01-04 14:57:34 +010011647 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090011648}
11649
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011650/* This scans a URL-encoded query string. It takes an optionally wrapping
11651 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11652 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11653 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011654 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011655static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011656smp_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 +090011657{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011658 const char *vstart, *vend;
11659 struct chunk *temp;
11660 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011661
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011662 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011663 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011664 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011665 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011666 return 0;
11667
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011668 /* Create sample. If the value is contiguous, return the pointer as CONST,
11669 * if the value is wrapped, copy-it in a buffer.
11670 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011671 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011672 if (chunks[2] &&
11673 vstart >= chunks[0] && vstart <= chunks[1] &&
11674 vend >= chunks[2] && vend <= chunks[3]) {
11675 /* Wrapped case. */
11676 temp = get_trash_chunk();
11677 memcpy(temp->str, vstart, chunks[1] - vstart);
11678 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011679 smp->data.u.str.str = temp->str;
11680 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011681 } else {
11682 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011683 smp->data.u.str.str = (char *)vstart;
11684 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011685 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11686 }
11687
11688 /* Update context, check wrapping. */
11689 chunks[0] = vend;
11690 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11691 chunks[1] = chunks[3];
11692 chunks[2] = NULL;
11693 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011694
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011695 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011696 smp->flags |= SMP_F_NOT_LAST;
11697
David Cournapeau16023ee2010-12-23 20:55:41 +090011698 return 1;
11699}
11700
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011701/* This function iterates over each parameter of the query string. It uses
11702 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011703 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11704 * An optional parameter name is passed in args[0], otherwise any parameter is
11705 * considered. It supports an optional delimiter argument for the beginning of
11706 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011707 */
11708static int
11709smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11710{
11711 struct http_msg *msg;
11712 char delim = '?';
11713 const char *name;
11714 int name_len;
11715
Dragan Dosen26f77e52015-05-25 10:02:11 +020011716 if (!args ||
11717 (args[0].type && args[0].type != ARGT_STR) ||
11718 (args[1].type && args[1].type != ARGT_STR))
11719 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011720
Dragan Dosen26f77e52015-05-25 10:02:11 +020011721 name = "";
11722 name_len = 0;
11723 if (args->type == ARGT_STR) {
11724 name = args->data.str.str;
11725 name_len = args->data.str.len;
11726 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011727
Dragan Dosen26f77e52015-05-25 10:02:11 +020011728 if (args[1].type)
11729 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011730
Dragan Dosen26f77e52015-05-25 10:02:11 +020011731 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011732 CHECK_HTTP_MESSAGE_FIRST();
11733
11734 msg = &smp->strm->txn->req;
11735
11736 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11737 msg->sl.rq.u_l, delim);
11738 if (!smp->ctx.a[0])
11739 return 0;
11740
11741 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011742
11743 /* Assume that the context is filled with NULL pointer
11744 * before the first call.
11745 * smp->ctx.a[2] = NULL;
11746 * smp->ctx.a[3] = NULL;
11747 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011748 }
11749
11750 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11751}
11752
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011753/* This function iterates over each parameter of the body. This requires
11754 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011755 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11756 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11757 * optional second part if the body wraps at the end of the buffer. An optional
11758 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011759 */
11760static int
11761smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11762{
11763 struct http_txn *txn = smp->strm->txn;
11764 struct http_msg *msg;
11765 unsigned long len;
11766 unsigned long block1;
11767 char *body;
11768 const char *name;
11769 int name_len;
11770
11771 if (!args || (args[0].type && args[0].type != ARGT_STR))
11772 return 0;
11773
11774 name = "";
11775 name_len = 0;
11776 if (args[0].type == ARGT_STR) {
11777 name = args[0].data.str.str;
11778 name_len = args[0].data.str.len;
11779 }
11780
11781 if (!smp->ctx.a[0]) { // first call, find the query string
11782 CHECK_HTTP_MESSAGE_FIRST();
11783
11784 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
11785 msg = &txn->req;
11786 else
11787 msg = &txn->rsp;
11788
11789 len = http_body_bytes(msg);
11790 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11791
11792 block1 = len;
11793 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11794 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11795
11796 if (block1 == len) {
11797 /* buffer is not wrapped (or empty) */
11798 smp->ctx.a[0] = body;
11799 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011800
11801 /* Assume that the context is filled with NULL pointer
11802 * before the first call.
11803 * smp->ctx.a[2] = NULL;
11804 * smp->ctx.a[3] = NULL;
11805 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011806 }
11807 else {
11808 /* buffer is wrapped, we need to defragment it */
11809 smp->ctx.a[0] = body;
11810 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011811 smp->ctx.a[2] = msg->chn->buf->data;
11812 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011813 }
11814 }
11815 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11816}
11817
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011818/* Return the signed integer value for the specified url parameter (see url_param
11819 * above).
11820 */
11821static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011822smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011823{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011824 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011825
11826 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011827 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011828 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011829 }
11830
11831 return ret;
11832}
11833
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011834/* This produces a 32-bit hash of the concatenation of the first occurrence of
11835 * the Host header followed by the path component if it begins with a slash ('/').
11836 * This means that '*' will not be added, resulting in exactly the first Host
11837 * entry. If no Host header is found, then the path is used. The resulting value
11838 * is hashed using the url hash followed by a full avalanche hash and provides a
11839 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11840 * high-traffic sites without having to store whole paths.
11841 * this differs from the base32 functions in that it includes the url parameters
11842 * as well as the path
11843 */
11844static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011845smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011846{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011847 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011848 struct hdr_ctx ctx;
11849 unsigned int hash = 0;
11850 char *ptr, *beg, *end;
11851 int len;
11852
11853 CHECK_HTTP_MESSAGE_FIRST();
11854
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011855 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011856 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011857 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011858 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11859 ptr = ctx.line + ctx.val;
11860 len = ctx.vlen;
11861 while (len--)
11862 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11863 }
11864
11865 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011866 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 +000011867 beg = http_get_path(txn);
11868 if (!beg)
11869 beg = end;
11870
11871 for (ptr = beg; ptr < end ; ptr++);
11872
11873 if (beg < ptr && *beg == '/') {
11874 while (beg < ptr)
11875 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11876 }
11877 hash = full_hash(hash);
11878
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011879 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011880 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011881 smp->flags = SMP_F_VOL_1ST;
11882 return 1;
11883}
11884
11885/* This concatenates the source address with the 32-bit hash of the Host and
11886 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11887 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11888 * on the source address length. The URL hash is stored before the address so
11889 * that in environments where IPv6 is insignificant, truncating the output to
11890 * 8 bytes would still work.
11891 */
11892static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011893smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011894{
11895 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011896 struct connection *cli_conn = objt_conn(smp->sess->origin);
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011897 unsigned int hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011898
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011899 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011900 return 0;
11901
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011902 /* The returned hash is a 32 bytes integer. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011903 hash = smp->data.u.sint;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011904
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011905 temp = get_trash_chunk();
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011906 memcpy(temp->str + temp->len, &hash, sizeof(hash));
11907 temp->len += sizeof(hash);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011908
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011909 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011910 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011911 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011912 temp->len += 4;
11913 break;
11914 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011915 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011916 temp->len += 16;
11917 break;
11918 default:
11919 return 0;
11920 }
11921
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011922 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011923 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011924 return 1;
11925}
11926
Willy Tarreau185b5c42012-04-26 15:11:51 +020011927/* This function is used to validate the arguments passed to any "hdr" fetch
11928 * keyword. These keywords support an optional positive or negative occurrence
11929 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11930 * is assumed that the types are already the correct ones. Returns 0 on error,
11931 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11932 * error message in case of error, that the caller is responsible for freeing.
11933 * The initial location must either be freeable or NULL.
11934 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011935int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011936{
11937 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011938 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011939 return 0;
11940 }
11941 return 1;
11942}
11943
Willy Tarreau276fae92013-07-25 14:36:01 +020011944/* takes an UINT value on input supposed to represent the time since EPOCH,
11945 * adds an optional offset found in args[0] and emits a string representing
11946 * the date in RFC-1123/5322 format.
11947 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011948static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011949{
11950 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
11951 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11952 struct chunk *temp;
11953 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011954 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011955 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020011956
11957 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011958 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020011959 curr_date += args[0].data.sint;
11960
11961 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020011962 if (!tm)
11963 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011964
11965 temp = get_trash_chunk();
11966 temp->len = snprintf(temp->str, temp->size - temp->len,
11967 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11968 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11969 tm->tm_hour, tm->tm_min, tm->tm_sec);
11970
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011971 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011972 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020011973 return 1;
11974}
11975
Thierry FOURNIERad903512014-04-11 17:51:01 +020011976/* Match language range with language tag. RFC2616 14.4:
11977 *
11978 * A language-range matches a language-tag if it exactly equals
11979 * the tag, or if it exactly equals a prefix of the tag such
11980 * that the first tag character following the prefix is "-".
11981 *
11982 * Return 1 if the strings match, else return 0.
11983 */
11984static inline int language_range_match(const char *range, int range_len,
11985 const char *tag, int tag_len)
11986{
11987 const char *end = range + range_len;
11988 const char *tend = tag + tag_len;
11989 while (range < end) {
11990 if (*range == '-' && tag == tend)
11991 return 1;
11992 if (*range != *tag || tag == tend)
11993 return 0;
11994 range++;
11995 tag++;
11996 }
11997 /* Return true only if the last char of the tag is matched. */
11998 return tag == tend;
11999}
12000
12001/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012002static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020012003{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012004 const char *al = smp->data.u.str.str;
12005 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012006 const char *token;
12007 int toklen;
12008 int qvalue;
12009 const char *str;
12010 const char *w;
12011 int best_q = 0;
12012
12013 /* Set the constant to the sample, because the output of the
12014 * function will be peek in the constant configuration string.
12015 */
12016 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012017 smp->data.u.str.size = 0;
12018 smp->data.u.str.str = "";
12019 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012020
12021 /* Parse the accept language */
12022 while (1) {
12023
12024 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012025 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012026 al++;
12027 if (al >= end)
12028 break;
12029
12030 /* Start of the fisrt word. */
12031 token = al;
12032
12033 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012034 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012035 al++;
12036 if (al == token)
12037 goto expect_comma;
12038
12039 /* Length of the token. */
12040 toklen = al - token;
12041 qvalue = 1000;
12042
12043 /* Check if the token exists in the list. If the token not exists,
12044 * jump to the next token.
12045 */
12046 str = args[0].data.str.str;
12047 w = str;
12048 while (1) {
12049 if (*str == ';' || *str == '\0') {
12050 if (language_range_match(token, toklen, w, str-w))
12051 goto look_for_q;
12052 if (*str == '\0')
12053 goto expect_comma;
12054 w = str + 1;
12055 }
12056 str++;
12057 }
12058 goto expect_comma;
12059
12060look_for_q:
12061
12062 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012063 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012064 al++;
12065 if (al >= end)
12066 goto process_value;
12067
12068 /* If ',' is found, process the result */
12069 if (*al == ',')
12070 goto process_value;
12071
12072 /* If the character is different from ';', look
12073 * for the end of the header part in best effort.
12074 */
12075 if (*al != ';')
12076 goto expect_comma;
12077
12078 /* Assumes that the char is ';', now expect "q=". */
12079 al++;
12080
12081 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012082 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012083 al++;
12084 if (al >= end)
12085 goto process_value;
12086
12087 /* Expect 'q'. If no 'q', continue in best effort */
12088 if (*al != 'q')
12089 goto process_value;
12090 al++;
12091
12092 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012093 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012094 al++;
12095 if (al >= end)
12096 goto process_value;
12097
12098 /* Expect '='. If no '=', continue in best effort */
12099 if (*al != '=')
12100 goto process_value;
12101 al++;
12102
12103 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012104 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012105 al++;
12106 if (al >= end)
12107 goto process_value;
12108
12109 /* Parse the q value. */
12110 qvalue = parse_qvalue(al, &al);
12111
12112process_value:
12113
12114 /* If the new q value is the best q value, then store the associated
12115 * language in the response. If qvalue is the biggest value (1000),
12116 * break the process.
12117 */
12118 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012119 smp->data.u.str.str = (char *)w;
12120 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012121 if (qvalue >= 1000)
12122 break;
12123 best_q = qvalue;
12124 }
12125
12126expect_comma:
12127
12128 /* Expect comma or end. If the end is detected, quit the loop. */
12129 while (al < end && *al != ',')
12130 al++;
12131 if (al >= end)
12132 break;
12133
12134 /* Comma is found, jump it and restart the analyzer. */
12135 al++;
12136 }
12137
12138 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012139 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
12140 smp->data.u.str.str = args[1].data.str.str;
12141 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012142 }
12143
12144 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012145 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012146}
12147
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012148/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012149static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012150{
12151 /* If the constant flag is set or if not size is avalaible at
12152 * the end of the buffer, copy the string in other buffer
12153 * before decoding.
12154 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012155 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012156 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012157 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
12158 smp->data.u.str.str = str->str;
12159 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012160 smp->flags &= ~SMP_F_CONST;
12161 }
12162
12163 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012164 smp->data.u.str.str[smp->data.u.str.len] = '\0';
12165 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012166 return 1;
12167}
12168
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012169static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
12170{
12171 struct proxy *fe = strm_fe(smp->strm);
12172 int idx, i;
12173 struct cap_hdr *hdr;
12174 int len;
12175
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012176 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012177 return 0;
12178
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012179 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012180
12181 /* Check the availibity of the capture id. */
12182 if (idx > fe->nb_req_cap - 1)
12183 return 0;
12184
12185 /* Look for the original configuration. */
12186 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
12187 hdr != NULL && i != idx ;
12188 i--, hdr = hdr->next);
12189 if (!hdr)
12190 return 0;
12191
12192 /* check for the memory allocation */
12193 if (smp->strm->req_cap[hdr->index] == NULL)
12194 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
12195 if (smp->strm->req_cap[hdr->index] == NULL)
12196 return 0;
12197
12198 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012199 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012200 if (len > hdr->len)
12201 len = hdr->len;
12202
12203 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012204 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012205 smp->strm->req_cap[idx][len] = '\0';
12206
12207 return 1;
12208}
12209
12210static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
12211{
12212 struct proxy *fe = strm_fe(smp->strm);
12213 int idx, i;
12214 struct cap_hdr *hdr;
12215 int len;
12216
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012217 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012218 return 0;
12219
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012220 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012221
12222 /* Check the availibity of the capture id. */
12223 if (idx > fe->nb_rsp_cap - 1)
12224 return 0;
12225
12226 /* Look for the original configuration. */
12227 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
12228 hdr != NULL && i != idx ;
12229 i--, hdr = hdr->next);
12230 if (!hdr)
12231 return 0;
12232
12233 /* check for the memory allocation */
12234 if (smp->strm->res_cap[hdr->index] == NULL)
12235 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
12236 if (smp->strm->res_cap[hdr->index] == NULL)
12237 return 0;
12238
12239 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012240 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012241 if (len > hdr->len)
12242 len = hdr->len;
12243
12244 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012245 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012246 smp->strm->res_cap[idx][len] = '\0';
12247
12248 return 1;
12249}
12250
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012251/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012252 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012253 * the relevant part of the request line accordingly. Then it updates various
12254 * pointers to the next elements which were moved, and the total buffer length.
12255 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012256 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
12257 * error, though this can be revisited when this code is finally exploited.
12258 *
12259 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
12260 * query string and 3 to replace uri.
12261 *
12262 * In query string case, the mark question '?' must be set at the start of the
12263 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012264 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012265int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012266 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012267{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012268 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012269 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012270 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012271 int delta;
12272
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012273 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012274 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012275 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012276 cur_end = cur_ptr + txn->req.sl.rq.m_l;
12277
12278 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012279 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012280 txn->req.sl.rq.m_l += delta;
12281 txn->req.sl.rq.u += delta;
12282 txn->req.sl.rq.v += delta;
12283 break;
12284
12285 case 1: // path
12286 cur_ptr = http_get_path(txn);
12287 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012288 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012289
12290 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012291 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 +010012292 cur_end++;
12293
12294 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012295 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012296 txn->req.sl.rq.u_l += delta;
12297 txn->req.sl.rq.v += delta;
12298 break;
12299
12300 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012301 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012302 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012303 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12304 while (cur_ptr < cur_end && *cur_ptr != '?')
12305 cur_ptr++;
12306
12307 /* skip the question mark or indicate that we must insert it
12308 * (but only if the format string is not empty then).
12309 */
12310 if (cur_ptr < cur_end)
12311 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012312 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012313 offset = 0;
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 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012322 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012323 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12324
12325 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012326 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012327 txn->req.sl.rq.u_l += delta;
12328 txn->req.sl.rq.v += delta;
12329 break;
12330
12331 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012332 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012333 }
12334
12335 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012336 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020012337 txn->req.sl.rq.l += delta;
12338 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012339 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012340 return 0;
12341}
12342
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012343/* This function replace the HTTP status code and the associated message. The
12344 * variable <status> contains the new status code. This function never fails.
12345 */
12346void http_set_status(unsigned int status, struct stream *s)
12347{
12348 struct http_txn *txn = s->txn;
12349 char *cur_ptr, *cur_end;
12350 int delta;
12351 char *res;
12352 int c_l;
12353 const char *msg;
12354 int msg_len;
12355
12356 chunk_reset(&trash);
12357
12358 res = ultoa_o(status, trash.str, trash.size);
12359 c_l = res - trash.str;
12360
12361 trash.str[c_l] = ' ';
12362 trash.len = c_l + 1;
12363
12364 msg = get_reason(status);
12365 msg_len = strlen(msg);
12366
12367 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
12368 trash.len += msg_len;
12369
12370 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
12371 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
12372
12373 /* commit changes and adjust message */
12374 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
12375
12376 /* adjust res line offsets and lengths */
12377 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
12378 txn->rsp.sl.st.c_l = c_l;
12379 txn->rsp.sl.st.r_l = msg_len;
12380
12381 delta = trash.len - (cur_end - cur_ptr);
12382 txn->rsp.sl.st.l += delta;
12383 txn->hdr_idx.v[0].len += delta;
12384 http_msg_move_end(&txn->rsp, delta);
12385}
12386
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012387/* This function executes one of the set-{method,path,query,uri} actions. It
12388 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012389 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012390 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012391 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
12392 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012393 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012394enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012395 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012396{
12397 chunk_reset(&trash);
12398
12399 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012400 if (rule->arg.http.action == 2)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012401 trash.str[trash.len++] = '?';
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012402 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012403
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012404 http_replace_req_line(rule->arg.http.action, trash.str, trash.len, px, s);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012405 return ACT_RET_CONT;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012406}
12407
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012408/* This function is just a compliant action wrapper for "set-status". */
12409enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012410 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012411{
12412 http_set_status(rule->arg.status.code, s);
12413 return ACT_RET_CONT;
12414}
12415
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012416/* parse an http-request action among :
12417 * set-method
12418 * set-path
12419 * set-query
12420 * set-uri
12421 *
12422 * All of them accept a single argument of type string representing a log-format.
12423 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
12424 * 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 +020012425 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012426 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012427enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
12428 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012429{
12430 int cur_arg = *orig_arg;
12431
Thierry FOURNIER42148732015-09-02 17:17:33 +020012432 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012433
12434 switch (args[0][4]) {
12435 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012436 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012437 rule->action_ptr = http_action_set_req_line;
12438 break;
12439 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012440 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012441 rule->action_ptr = http_action_set_req_line;
12442 break;
12443 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012444 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012445 rule->action_ptr = http_action_set_req_line;
12446 break;
12447 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012448 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012449 rule->action_ptr = http_action_set_req_line;
12450 break;
12451 default:
12452 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012453 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012454 }
12455
12456 if (!*args[cur_arg] ||
12457 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12458 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012459 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012460 }
12461
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012462 LIST_INIT(&rule->arg.http.logfmt);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012463 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012464 parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP,
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012465 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
12466 proxy->conf.args.file, proxy->conf.args.line);
12467
12468 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012469 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012470}
12471
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012472/* parse set-status action:
12473 * This action accepts a single argument of type int representing
12474 * an http status code. It returns ACT_RET_PRS_OK on success,
12475 * ACT_RET_PRS_ERR on error.
12476 */
12477enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12478 struct act_rule *rule, char **err)
12479{
12480 char *error;
12481
Thierry FOURNIER42148732015-09-02 17:17:33 +020012482 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012483 rule->action_ptr = action_http_set_status;
12484
12485 /* Check if an argument is available */
12486 if (!*args[*orig_arg]) {
12487 memprintf(err, "expects exactly 1 argument <status>");
12488 return ACT_RET_PRS_ERR;
12489 }
12490
12491 /* convert status code as integer */
12492 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12493 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12494 memprintf(err, "expects an integer status code between 100 and 999");
12495 return ACT_RET_PRS_ERR;
12496 }
12497
12498 (*orig_arg)++;
12499 return ACT_RET_PRS_OK;
12500}
12501
Willy Tarreaua9083d02015-05-08 15:27:59 +020012502/* This function executes the "capture" action. It executes a fetch expression,
12503 * turns the result into a string and puts it in a capture slot. It always
12504 * returns 1. If an error occurs the action is cancelled, but the rule
12505 * processing continues.
12506 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012507enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012508 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012509{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012510 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012511 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012512 char **cap = s->req_cap;
12513 int len;
12514
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012515 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 +020012516 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012517 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012518
12519 if (cap[h->index] == NULL)
12520 cap[h->index] = pool_alloc2(h->pool);
12521
12522 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012523 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012524
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012525 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012526 if (len > h->len)
12527 len = h->len;
12528
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012529 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012530 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012531 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012532}
12533
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012534/* This function executes the "capture" action and store the result in a
12535 * capture slot if exists. It executes a fetch expression, turns the result
12536 * into a string and puts it in a capture slot. It always returns 1. If an
12537 * error occurs the action is cancelled, but the rule processing continues.
12538 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012539enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012540 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012541{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012542 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012543 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012544 char **cap = s->req_cap;
12545 struct proxy *fe = strm_fe(s);
12546 int len;
12547 int i;
12548
12549 /* Look for the original configuration. */
12550 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012551 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012552 i--, h = h->next);
12553 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012554 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012555
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012556 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 +020012557 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012558 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012559
12560 if (cap[h->index] == NULL)
12561 cap[h->index] = pool_alloc2(h->pool);
12562
12563 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012564 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012565
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012566 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012567 if (len > h->len)
12568 len = h->len;
12569
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012570 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012571 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012572 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012573}
12574
Willy Tarreaua9083d02015-05-08 15:27:59 +020012575/* parse an "http-request capture" action. It takes a single argument which is
12576 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012577 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012578 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012579 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012580enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12581 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012582{
12583 struct sample_expr *expr;
12584 struct cap_hdr *hdr;
12585 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012586 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012587
12588 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12589 if (strcmp(args[cur_arg], "if") == 0 ||
12590 strcmp(args[cur_arg], "unless") == 0)
12591 break;
12592
12593 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012594 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012595 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012596 }
12597
Willy Tarreaua9083d02015-05-08 15:27:59 +020012598 cur_arg = *orig_arg;
12599 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12600 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012601 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012602
12603 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12604 memprintf(err,
12605 "fetch method '%s' extracts information from '%s', none of which is available here",
12606 args[cur_arg-1], sample_src_names(expr->fetch->use));
12607 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012608 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012609 }
12610
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012611 if (!args[cur_arg] || !*args[cur_arg]) {
12612 memprintf(err, "expects 'len or 'id'");
12613 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012614 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012615 }
12616
Willy Tarreaua9083d02015-05-08 15:27:59 +020012617 if (strcmp(args[cur_arg], "len") == 0) {
12618 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012619
12620 if (!(px->cap & PR_CAP_FE)) {
12621 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012622 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012623 }
12624
12625 proxy->conf.args.ctx = ARGC_CAP;
12626
Willy Tarreaua9083d02015-05-08 15:27:59 +020012627 if (!args[cur_arg]) {
12628 memprintf(err, "missing length value");
12629 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012630 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012631 }
12632 /* we copy the table name for now, it will be resolved later */
12633 len = atoi(args[cur_arg]);
12634 if (len <= 0) {
12635 memprintf(err, "length must be > 0");
12636 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012637 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012638 }
12639 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012640
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012641 if (!len) {
12642 memprintf(err, "a positive 'len' argument is mandatory");
12643 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012644 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012645 }
12646
12647 hdr = calloc(sizeof(struct cap_hdr), 1);
12648 hdr->next = px->req_cap;
12649 hdr->name = NULL; /* not a header capture */
12650 hdr->namelen = 0;
12651 hdr->len = len;
12652 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12653 hdr->index = px->nb_req_cap++;
12654
12655 px->req_cap = hdr;
12656 px->to_log |= LW_REQHDR;
12657
Thierry FOURNIER42148732015-09-02 17:17:33 +020012658 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012659 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012660 rule->arg.cap.expr = expr;
12661 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012662 }
12663
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012664 else if (strcmp(args[cur_arg], "id") == 0) {
12665 int id;
12666 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012667
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012668 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012669
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012670 if (!args[cur_arg]) {
12671 memprintf(err, "missing id value");
12672 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012673 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012674 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012675
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012676 id = strtol(args[cur_arg], &error, 10);
12677 if (*error != '\0') {
12678 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12679 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012680 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012681 }
12682 cur_arg++;
12683
12684 proxy->conf.args.ctx = ARGC_CAP;
12685
Thierry FOURNIER42148732015-09-02 17:17:33 +020012686 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012687 rule->action_ptr = http_action_req_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012688 rule->arg.capid.expr = expr;
12689 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012690 }
12691
12692 else {
12693 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12694 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012695 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012696 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012697
12698 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012699 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012700}
12701
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012702/* This function executes the "capture" action and store the result in a
12703 * capture slot if exists. It executes a fetch expression, turns the result
12704 * into a string and puts it in a capture slot. It always returns 1. If an
12705 * error occurs the action is cancelled, but the rule processing continues.
12706 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012707enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012708 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012709{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012710 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012711 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012712 char **cap = s->res_cap;
12713 struct proxy *fe = strm_fe(s);
12714 int len;
12715 int i;
12716
12717 /* Look for the original configuration. */
12718 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012719 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012720 i--, h = h->next);
12721 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012722 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012723
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012724 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 +020012725 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012726 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012727
12728 if (cap[h->index] == NULL)
12729 cap[h->index] = pool_alloc2(h->pool);
12730
12731 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012732 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012733
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012734 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012735 if (len > h->len)
12736 len = h->len;
12737
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012738 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012739 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012740 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012741}
12742
12743/* parse an "http-response capture" action. It takes a single argument which is
12744 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12745 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012746 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012747 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012748enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12749 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012750{
12751 struct sample_expr *expr;
12752 int cur_arg;
12753 int id;
12754 char *error;
12755
12756 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12757 if (strcmp(args[cur_arg], "if") == 0 ||
12758 strcmp(args[cur_arg], "unless") == 0)
12759 break;
12760
12761 if (cur_arg < *orig_arg + 3) {
12762 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012763 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012764 }
12765
12766 cur_arg = *orig_arg;
12767 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12768 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012769 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012770
12771 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12772 memprintf(err,
12773 "fetch method '%s' extracts information from '%s', none of which is available here",
12774 args[cur_arg-1], sample_src_names(expr->fetch->use));
12775 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012776 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012777 }
12778
12779 if (!args[cur_arg] || !*args[cur_arg]) {
12780 memprintf(err, "expects 'len or 'id'");
12781 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012782 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012783 }
12784
12785 if (strcmp(args[cur_arg], "id") != 0) {
12786 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12787 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012788 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012789 }
12790
12791 cur_arg++;
12792
12793 if (!args[cur_arg]) {
12794 memprintf(err, "missing id value");
12795 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012796 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012797 }
12798
12799 id = strtol(args[cur_arg], &error, 10);
12800 if (*error != '\0') {
12801 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12802 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012803 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012804 }
12805 cur_arg++;
12806
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012807 proxy->conf.args.ctx = ARGC_CAP;
12808
Thierry FOURNIER42148732015-09-02 17:17:33 +020012809 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012810 rule->action_ptr = http_action_res_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012811 rule->arg.capid.expr = expr;
12812 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012813
12814 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012815 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012816}
12817
William Lallemand73025dd2014-04-24 14:38:37 +020012818/*
12819 * Return the struct http_req_action_kw associated to a keyword.
12820 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012821struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012822{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012823 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012824}
12825
12826/*
12827 * Return the struct http_res_action_kw associated to a keyword.
12828 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012829struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012830{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012831 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012832}
12833
Willy Tarreau4a568972010-05-12 08:08:50 +020012834/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012835/* All supported ACL keywords must be declared here. */
12836/************************************************************************/
12837
12838/* Note: must not be declared <const> as its list will be overwritten.
12839 * Please take care of keeping this list alphabetically sorted.
12840 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012841static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012842 { "base", "base", PAT_MATCH_STR },
12843 { "base_beg", "base", PAT_MATCH_BEG },
12844 { "base_dir", "base", PAT_MATCH_DIR },
12845 { "base_dom", "base", PAT_MATCH_DOM },
12846 { "base_end", "base", PAT_MATCH_END },
12847 { "base_len", "base", PAT_MATCH_LEN },
12848 { "base_reg", "base", PAT_MATCH_REG },
12849 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012850
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012851 { "cook", "req.cook", PAT_MATCH_STR },
12852 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12853 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12854 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12855 { "cook_end", "req.cook", PAT_MATCH_END },
12856 { "cook_len", "req.cook", PAT_MATCH_LEN },
12857 { "cook_reg", "req.cook", PAT_MATCH_REG },
12858 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012859
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012860 { "hdr", "req.hdr", PAT_MATCH_STR },
12861 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12862 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12863 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12864 { "hdr_end", "req.hdr", PAT_MATCH_END },
12865 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12866 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12867 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012868
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012869 /* these two declarations uses strings with list storage (in place
12870 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12871 * and delete functions are relative to the list management. The parse
12872 * and match method are related to the corresponding fetch methods. This
12873 * is very particular ACL declaration mode.
12874 */
12875 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12876 { "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 +020012877
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012878 { "path", "path", PAT_MATCH_STR },
12879 { "path_beg", "path", PAT_MATCH_BEG },
12880 { "path_dir", "path", PAT_MATCH_DIR },
12881 { "path_dom", "path", PAT_MATCH_DOM },
12882 { "path_end", "path", PAT_MATCH_END },
12883 { "path_len", "path", PAT_MATCH_LEN },
12884 { "path_reg", "path", PAT_MATCH_REG },
12885 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012886
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012887 { "req_ver", "req.ver", PAT_MATCH_STR },
12888 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012889
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012890 { "scook", "res.cook", PAT_MATCH_STR },
12891 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12892 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12893 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12894 { "scook_end", "res.cook", PAT_MATCH_END },
12895 { "scook_len", "res.cook", PAT_MATCH_LEN },
12896 { "scook_reg", "res.cook", PAT_MATCH_REG },
12897 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012898
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012899 { "shdr", "res.hdr", PAT_MATCH_STR },
12900 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12901 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12902 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12903 { "shdr_end", "res.hdr", PAT_MATCH_END },
12904 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12905 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12906 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012907
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012908 { "url", "url", PAT_MATCH_STR },
12909 { "url_beg", "url", PAT_MATCH_BEG },
12910 { "url_dir", "url", PAT_MATCH_DIR },
12911 { "url_dom", "url", PAT_MATCH_DOM },
12912 { "url_end", "url", PAT_MATCH_END },
12913 { "url_len", "url", PAT_MATCH_LEN },
12914 { "url_reg", "url", PAT_MATCH_REG },
12915 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012916
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012917 { "urlp", "urlp", PAT_MATCH_STR },
12918 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12919 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12920 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12921 { "urlp_end", "urlp", PAT_MATCH_END },
12922 { "urlp_len", "urlp", PAT_MATCH_LEN },
12923 { "urlp_reg", "urlp", PAT_MATCH_REG },
12924 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012925
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012926 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012927}};
12928
12929/************************************************************************/
12930/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012931/************************************************************************/
12932/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012933static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012934 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012935 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012936 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12937
Willy Tarreau87b09662015-04-03 00:22:06 +020012938 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012939 { "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 +020012940
12941 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012942 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12943 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12944 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012945
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012946 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
12947 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012948
Willy Tarreau409bcde2013-01-08 00:31:00 +010012949 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12950 * are only here to match the ACL's name, are request-only and are used
12951 * for ACL compatibility only.
12952 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012953 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12954 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012955 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12956 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012957
12958 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12959 * only here to match the ACL's name, are request-only and are used for
12960 * ACL compatibility only.
12961 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012962 { "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 +020012963 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012964 { "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 +020012965 { "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 +010012966
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012967 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012968 { "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 +010012969 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012970 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012971 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012972 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012973
12974 /* HTTP protocol on the request path */
12975 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012976 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012977
12978 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012979 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12980 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012981
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012982 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012983 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
12984 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012985 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012986
Willy Tarreau18ed2562013-01-14 15:56:36 +010012987 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012988 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12989 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012990
Willy Tarreau18ed2562013-01-14 15:56:36 +010012991 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012992 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012993 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12994 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012995
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012996 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012997 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012998 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012999 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013000 { "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 +010013001 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013002 { "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 +010013003
13004 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013005 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013006 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13007 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013008
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013009 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013010 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013011 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013012 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013013 { "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 +010013014 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013015 { "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 +010013016
Willy Tarreau409bcde2013-01-08 00:31:00 +010013017 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013018 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013019 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13020 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013021 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010013022
13023 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013024 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013025 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013026 { "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 +020013027 { "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 +010013028
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013029 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013030 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013031 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000013032 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013033 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013034 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020013035 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13036 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013037 { "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 +010013038 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020013039}};
13040
Willy Tarreau8797c062007-05-07 00:55:35 +020013041
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013042/************************************************************************/
13043/* All supported converter keywords must be declared here. */
13044/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020013045/* Note: must not be declared <const> as its list will be overwritten */
13046static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013047 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020013048 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013049 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
13050 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020013051 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020013052 { NULL, NULL, 0, 0, 0 },
13053}};
13054
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020013055
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013056/************************************************************************/
13057/* All supported http-request action keywords must be declared here. */
13058/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013059struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013060 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020013061 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013062 { "set-method", parse_set_req_line },
13063 { "set-path", parse_set_req_line },
13064 { "set-query", parse_set_req_line },
13065 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020013066 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013067 }
13068};
13069
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013070struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013071 .kw = {
13072 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020013073 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013074 { NULL, NULL }
13075 }
13076};
13077
Willy Tarreau8797c062007-05-07 00:55:35 +020013078__attribute__((constructor))
13079static void __http_protocol_init(void)
13080{
13081 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020013082 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020013083 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013084 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013085 http_res_keywords_register(&http_res_actions);
Willy Tarreau8797c062007-05-07 00:55:35 +020013086}
13087
13088
Willy Tarreau58f10d72006-12-04 02:26:12 +010013089/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020013090 * Local variables:
13091 * c-indent-level: 8
13092 * c-basic-offset: 8
13093 * End:
13094 */