blob: 104df2020d7a60d951f39ee3ac3e7992c68b3f32 [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 Tarreauf68a15a2011-01-06 16:53:21 +01003500
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003501 /* If "the current_rule_list" match the executed rule list, we are in
3502 * resume condition. If a resume is needed it is always in the action
3503 * and never in the ACL or converters. In this case, we initialise the
3504 * current rule, and go to the action execution point.
3505 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003506 if (s->current_rule) {
3507 rule = s->current_rule;
3508 s->current_rule = NULL;
3509 if (s->current_rule_list == rules)
3510 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003511 }
3512 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003513
Willy Tarreauff011f22011-01-06 17:51:27 +01003514 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003515
Willy Tarreau96257ec2012-12-27 10:46:37 +01003516 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003517 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003518 int ret;
3519
Willy Tarreau192252e2015-04-04 01:47:55 +02003520 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003521 ret = acl_pass(ret);
3522
Willy Tarreauff011f22011-01-06 17:51:27 +01003523 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003524 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003525
3526 if (!ret) /* condition not matched */
3527 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003528 }
3529
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003530resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003531 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003532 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003533 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003534
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003535 case ACT_ACTION_DENY:
CJ Ess108b1dd2015-04-07 12:03:37 -04003536 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003537 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003538
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003539 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01003540 txn->flags |= TX_CLTARPIT;
CJ Ess108b1dd2015-04-07 12:03:37 -04003541 txn->rule_deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003542 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003543
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003544 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003545 /* Auth might be performed on regular http-req rules as well as on stats */
3546 auth_realm = rule->arg.auth.realm;
3547 if (!auth_realm) {
3548 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3549 auth_realm = STATS_DEFAULT_REALM;
3550 else
3551 auth_realm = px->id;
3552 }
3553 /* send 401/407 depending on whether we use a proxy or not. We still
3554 * count one error, because normal browsing won't significantly
3555 * increase the counter but brute force attempts will.
3556 */
3557 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3558 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
3559 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02003560 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003561 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003562
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003563 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003564 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3565 return HTTP_RULE_RES_BADREQ;
3566 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003567
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003568 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003569 s->task->nice = rule->arg.nice;
3570 break;
3571
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003572 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003573 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003574 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003575 break;
3576
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003577 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003578#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003579 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003580 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003581#endif
3582 break;
3583
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003584 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003585 s->logs.level = rule->arg.loglevel;
3586 break;
3587
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003588 case ACT_HTTP_REPLACE_HDR:
3589 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003590 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3591 rule->arg.hdr_add.name_len,
3592 &rule->arg.hdr_add.fmt,
3593 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003594 return HTTP_RULE_RES_BADREQ;
3595 break;
3596
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003597 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003598 ctx.idx = 0;
3599 /* remove all occurrences of the header */
3600 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3601 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3602 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003603 }
Willy Tarreau85603282015-01-21 20:39:27 +01003604 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003605
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003606 case ACT_HTTP_SET_HDR:
3607 case ACT_HTTP_ADD_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003608 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3609 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3610 trash.len = rule->arg.hdr_add.name_len;
3611 trash.str[trash.len++] = ':';
3612 trash.str[trash.len++] = ' ';
3613 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 +01003614
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003615 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003616 /* remove all occurrences of the header */
3617 ctx.idx = 0;
3618 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3619 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3620 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3621 }
3622 }
3623
Willy Tarreau96257ec2012-12-27 10:46:37 +01003624 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3625 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003626
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003627 case ACT_HTTP_DEL_ACL:
3628 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003629 struct pat_ref *ref;
3630 char *key;
3631 int len;
3632
3633 /* collect reference */
3634 ref = pat_ref_lookup(rule->arg.map.ref);
3635 if (!ref)
3636 continue;
3637
3638 /* collect key */
3639 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3640 key = trash.str;
3641 key[len] = '\0';
3642
3643 /* perform update */
3644 /* returned code: 1=ok, 0=ko */
3645 pat_ref_delete(ref, key);
3646
3647 break;
3648 }
3649
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003650 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003651 struct pat_ref *ref;
3652 char *key;
3653 struct chunk *trash_key;
3654 int len;
3655
3656 trash_key = get_trash_chunk();
3657
3658 /* collect reference */
3659 ref = pat_ref_lookup(rule->arg.map.ref);
3660 if (!ref)
3661 continue;
3662
3663 /* collect key */
3664 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3665 key = trash_key->str;
3666 key[len] = '\0';
3667
3668 /* perform update */
3669 /* add entry only if it does not already exist */
3670 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003671 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003672
3673 break;
3674 }
3675
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003676 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003677 struct pat_ref *ref;
3678 char *key, *value;
3679 struct chunk *trash_key, *trash_value;
3680 int len;
3681
3682 trash_key = get_trash_chunk();
3683 trash_value = get_trash_chunk();
3684
3685 /* collect reference */
3686 ref = pat_ref_lookup(rule->arg.map.ref);
3687 if (!ref)
3688 continue;
3689
3690 /* collect key */
3691 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3692 key = trash_key->str;
3693 key[len] = '\0';
3694
3695 /* collect value */
3696 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3697 value = trash_value->str;
3698 value[len] = '\0';
3699
3700 /* perform update */
3701 if (pat_ref_find_elt(ref, key) != NULL)
3702 /* update entry if it exists */
3703 pat_ref_set(ref, key, value, NULL);
3704 else
3705 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003706 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003707
3708 break;
3709 }
William Lallemand73025dd2014-04-24 14:38:37 +02003710
Thierry FOURNIER42148732015-09-02 17:17:33 +02003711 case ACT_CUSTOM:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003712 switch (rule->action_ptr(rule, px, s->sess, s)) {
3713 case ACT_RET_ERR:
3714 case ACT_RET_CONT:
3715 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003716 case ACT_RET_STOP:
3717 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003718 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003719 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003720 return HTTP_RULE_RES_YIELD;
3721 }
William Lallemand73025dd2014-04-24 14:38:37 +02003722 break;
3723
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003724 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02003725 /* Note: only the first valid tracking parameter of each
3726 * applies.
3727 */
3728
3729 if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) {
3730 struct stktable *t;
3731 struct stksess *ts;
3732 struct stktable_key *key;
3733 void *ptr;
3734
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02003735 t = rule->arg.trk_ctr.table.t;
3736 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 +02003737
3738 if (key && (ts = stktable_get_entry(t, key))) {
Willy Tarreau87b09662015-04-03 00:22:06 +02003739 stream_track_stkctr(&s->stkctr[http_req_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02003740
3741 /* let's count a new HTTP request as it's the first time we do it */
3742 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3743 if (ptr)
3744 stktable_data_cast(ptr, http_req_cnt)++;
3745
3746 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3747 if (ptr)
3748 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3749 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3750
3751 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003752 if (sess->fe != s->be)
Willy Tarreau09448f72014-06-25 18:12:15 +02003753 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3754 }
3755 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003756 break;
3757
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003758 case ACT_HTTP_REQ_SET_SRC:
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003759 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) {
3760 struct sample *smp;
3761
Thierry FOURNIERa002dc92015-07-31 08:50:51 +02003762 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 +02003763
3764 if (smp) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003765 if (smp->data.type == SMP_T_IPV4) {
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003766 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_family = AF_INET;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003767 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr = smp->data.u.ipv4.s_addr;
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003768 ((struct sockaddr_in *)&cli_conn->addr.from)->sin_port = 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003769 } else if (smp->data.type == SMP_T_IPV6) {
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003770 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_family = AF_INET6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003771 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 +02003772 ((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_port = 0;
3773 }
3774 }
3775 }
3776 break;
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003777
3778 /* other flags exists, but normaly, they never be matched. */
3779 default:
3780 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003781 }
3782 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003783
3784 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003785 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003786}
3787
Willy Tarreau71241ab2012-12-27 11:30:54 +01003788
Willy Tarreau51d861a2015-05-22 17:30:48 +02003789/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3790 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3791 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3792 * is returned, the process can continue the evaluation of next rule list. If
3793 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3794 * is returned, it means the operation could not be processed and a server error
3795 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3796 * deny rule. If *YIELD is returned, the caller must call again the function
3797 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003798 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003799static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003800http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003801{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003802 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003803 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003804 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003805 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003806 struct hdr_ctx ctx;
3807
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003808 /* If "the current_rule_list" match the executed rule list, we are in
3809 * resume condition. If a resume is needed it is always in the action
3810 * and never in the ACL or converters. In this case, we initialise the
3811 * current rule, and go to the action execution point.
3812 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003813 if (s->current_rule) {
3814 rule = s->current_rule;
3815 s->current_rule = NULL;
3816 if (s->current_rule_list == rules)
3817 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003818 }
3819 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003820
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003821 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003822
3823 /* check optional condition */
3824 if (rule->cond) {
3825 int ret;
3826
Willy Tarreau192252e2015-04-04 01:47:55 +02003827 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003828 ret = acl_pass(ret);
3829
3830 if (rule->cond->pol == ACL_COND_UNLESS)
3831 ret = !ret;
3832
3833 if (!ret) /* condition not matched */
3834 continue;
3835 }
3836
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003837resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003838 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003839 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003840 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003841
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003842 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003843 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003844 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003845
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003846 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003847 s->task->nice = rule->arg.nice;
3848 break;
3849
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003850 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003851 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003852 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003853 break;
3854
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003855 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003856#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003857 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003858 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003859#endif
3860 break;
3861
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003862 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003863 s->logs.level = rule->arg.loglevel;
3864 break;
3865
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003866 case ACT_HTTP_REPLACE_HDR:
3867 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003868 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3869 rule->arg.hdr_add.name_len,
3870 &rule->arg.hdr_add.fmt,
3871 &rule->arg.hdr_add.re, rule->action))
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003872 return HTTP_RULE_RES_STOP; /* note: we should report an error here */
Sasha Pachev218f0642014-06-16 12:05:59 -06003873 break;
3874
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003875 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003876 ctx.idx = 0;
3877 /* remove all occurrences of the header */
3878 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3879 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3880 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3881 }
Willy Tarreau85603282015-01-21 20:39:27 +01003882 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003883
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003884 case ACT_HTTP_SET_HDR:
3885 case ACT_HTTP_ADD_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003886 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3887 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3888 trash.len = rule->arg.hdr_add.name_len;
3889 trash.str[trash.len++] = ':';
3890 trash.str[trash.len++] = ' ';
3891 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 +01003892
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003893 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003894 /* remove all occurrences of the header */
3895 ctx.idx = 0;
3896 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3897 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3898 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3899 }
3900 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003901 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3902 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003903
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003904 case ACT_HTTP_DEL_ACL:
3905 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003906 struct pat_ref *ref;
3907 char *key;
3908 int len;
3909
3910 /* collect reference */
3911 ref = pat_ref_lookup(rule->arg.map.ref);
3912 if (!ref)
3913 continue;
3914
3915 /* collect key */
3916 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3917 key = trash.str;
3918 key[len] = '\0';
3919
3920 /* perform update */
3921 /* returned code: 1=ok, 0=ko */
3922 pat_ref_delete(ref, key);
3923
3924 break;
3925 }
3926
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003927 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003928 struct pat_ref *ref;
3929 char *key;
3930 struct chunk *trash_key;
3931 int len;
3932
3933 trash_key = get_trash_chunk();
3934
3935 /* collect reference */
3936 ref = pat_ref_lookup(rule->arg.map.ref);
3937 if (!ref)
3938 continue;
3939
3940 /* collect key */
3941 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3942 key = trash_key->str;
3943 key[len] = '\0';
3944
3945 /* perform update */
3946 /* check if the entry already exists */
3947 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003948 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003949
3950 break;
3951 }
3952
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003953 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003954 struct pat_ref *ref;
3955 char *key, *value;
3956 struct chunk *trash_key, *trash_value;
3957 int len;
3958
3959 trash_key = get_trash_chunk();
3960 trash_value = get_trash_chunk();
3961
3962 /* collect reference */
3963 ref = pat_ref_lookup(rule->arg.map.ref);
3964 if (!ref)
3965 continue;
3966
3967 /* collect key */
3968 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3969 key = trash_key->str;
3970 key[len] = '\0';
3971
3972 /* collect value */
3973 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3974 value = trash_value->str;
3975 value[len] = '\0';
3976
3977 /* perform update */
3978 if (pat_ref_find_elt(ref, key) != NULL)
3979 /* update entry if it exists */
3980 pat_ref_set(ref, key, value, NULL);
3981 else
3982 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003983 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003984
3985 break;
3986 }
William Lallemand73025dd2014-04-24 14:38:37 +02003987
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003988 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02003989 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3990 return HTTP_RULE_RES_BADREQ;
3991 return HTTP_RULE_RES_DONE;
3992
Thierry FOURNIER42148732015-09-02 17:17:33 +02003993 case ACT_CUSTOM:
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003994 switch (rule->action_ptr(rule, px, s->sess, s)) {
3995 case ACT_RET_ERR:
3996 case ACT_RET_CONT:
3997 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003998 case ACT_RET_STOP:
3999 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004000 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02004001 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004002 return HTTP_RULE_RES_YIELD;
4003 }
William Lallemand73025dd2014-04-24 14:38:37 +02004004 break;
4005
Thierry FOURNIER22e49012015-08-05 19:13:48 +02004006 /* other flags exists, but normaly, they never be matched. */
4007 default:
4008 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004009 }
4010 }
4011
4012 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01004013 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004014}
4015
4016
Willy Tarreau71241ab2012-12-27 11:30:54 +01004017/* Perform an HTTP redirect based on the information in <rule>. The function
4018 * returns non-zero on success, or zero in case of a, irrecoverable error such
4019 * as too large a request to build a valid response.
4020 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004021static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01004022{
Willy Tarreaub329a312015-05-22 16:27:37 +02004023 struct http_msg *req = &txn->req;
4024 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004025 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004026 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004027
4028 /* build redirect message */
4029 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04004030 case 308:
4031 msg_fmt = HTTP_308;
4032 break;
4033 case 307:
4034 msg_fmt = HTTP_307;
4035 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004036 case 303:
4037 msg_fmt = HTTP_303;
4038 break;
4039 case 301:
4040 msg_fmt = HTTP_301;
4041 break;
4042 case 302:
4043 default:
4044 msg_fmt = HTTP_302;
4045 break;
4046 }
4047
4048 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
4049 return 0;
4050
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004051 location = trash.str + trash.len;
4052
Willy Tarreau71241ab2012-12-27 11:30:54 +01004053 switch(rule->type) {
4054 case REDIRECT_TYPE_SCHEME: {
4055 const char *path;
4056 const char *host;
4057 struct hdr_ctx ctx;
4058 int pathlen;
4059 int hostlen;
4060
4061 host = "";
4062 hostlen = 0;
4063 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02004064 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004065 host = ctx.line + ctx.val;
4066 hostlen = ctx.vlen;
4067 }
4068
4069 path = http_get_path(txn);
4070 /* build message using path */
4071 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004072 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004073 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4074 int qs = 0;
4075 while (qs < pathlen) {
4076 if (path[qs] == '?') {
4077 pathlen = qs;
4078 break;
4079 }
4080 qs++;
4081 }
4082 }
4083 } else {
4084 path = "/";
4085 pathlen = 1;
4086 }
4087
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004088 if (rule->rdr_str) { /* this is an old "redirect" rule */
4089 /* check if we can add scheme + "://" + host + path */
4090 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
4091 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004092
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004093 /* add scheme */
4094 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4095 trash.len += rule->rdr_len;
4096 }
4097 else {
4098 /* add scheme with executing log format */
4099 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004100
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004101 /* check if we can add scheme + "://" + host + path */
4102 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
4103 return 0;
4104 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004105 /* add "://" */
4106 memcpy(trash.str + trash.len, "://", 3);
4107 trash.len += 3;
4108
4109 /* add host */
4110 memcpy(trash.str + trash.len, host, hostlen);
4111 trash.len += hostlen;
4112
4113 /* add path */
4114 memcpy(trash.str + trash.len, path, pathlen);
4115 trash.len += pathlen;
4116
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004117 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004118 if (trash.len && trash.str[trash.len - 1] != '/' &&
4119 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4120 if (trash.len > trash.size - 5)
4121 return 0;
4122 trash.str[trash.len] = '/';
4123 trash.len++;
4124 }
4125
4126 break;
4127 }
4128 case REDIRECT_TYPE_PREFIX: {
4129 const char *path;
4130 int pathlen;
4131
4132 path = http_get_path(txn);
4133 /* build message using path */
4134 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004135 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004136 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4137 int qs = 0;
4138 while (qs < pathlen) {
4139 if (path[qs] == '?') {
4140 pathlen = qs;
4141 break;
4142 }
4143 qs++;
4144 }
4145 }
4146 } else {
4147 path = "/";
4148 pathlen = 1;
4149 }
4150
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004151 if (rule->rdr_str) { /* this is an old "redirect" rule */
4152 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
4153 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004154
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004155 /* add prefix. Note that if prefix == "/", we don't want to
4156 * add anything, otherwise it makes it hard for the user to
4157 * configure a self-redirection.
4158 */
4159 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
4160 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4161 trash.len += rule->rdr_len;
4162 }
4163 }
4164 else {
4165 /* add prefix with executing log format */
4166 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
4167
4168 /* Check length */
4169 if (trash.len + pathlen > trash.size - 4)
4170 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004171 }
4172
4173 /* add path */
4174 memcpy(trash.str + trash.len, path, pathlen);
4175 trash.len += pathlen;
4176
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004177 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004178 if (trash.len && trash.str[trash.len - 1] != '/' &&
4179 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4180 if (trash.len > trash.size - 5)
4181 return 0;
4182 trash.str[trash.len] = '/';
4183 trash.len++;
4184 }
4185
4186 break;
4187 }
4188 case REDIRECT_TYPE_LOCATION:
4189 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004190 if (rule->rdr_str) { /* this is an old "redirect" rule */
4191 if (trash.len + rule->rdr_len > trash.size - 4)
4192 return 0;
4193
4194 /* add location */
4195 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4196 trash.len += rule->rdr_len;
4197 }
4198 else {
4199 /* add location with executing log format */
4200 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004201
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004202 /* Check left length */
4203 if (trash.len > trash.size - 4)
4204 return 0;
4205 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004206 break;
4207 }
4208
4209 if (rule->cookie_len) {
4210 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
4211 trash.len += 14;
4212 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
4213 trash.len += rule->cookie_len;
4214 memcpy(trash.str + trash.len, "\r\n", 2);
4215 trash.len += 2;
4216 }
4217
4218 /* add end of headers and the keep-alive/close status.
4219 * We may choose to set keep-alive if the Location begins
4220 * with a slash, because the client will come back to the
4221 * same server.
4222 */
4223 txn->status = rule->code;
4224 /* let's log the request time */
4225 s->logs.tv_request = now;
4226
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004227 if (*location == '/' &&
Willy Tarreaub329a312015-05-22 16:27:37 +02004228 (req->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau2de8a502015-05-28 17:23:54 +02004229 ((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004230 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4231 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4232 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02004233 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004234 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4235 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
4236 trash.len += 30;
4237 } else {
4238 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
4239 trash.len += 24;
4240 }
4241 }
4242 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
4243 trash.len += 4;
Willy Tarreaub329a312015-05-22 16:27:37 +02004244 bo_inject(res->chn, trash.str, trash.len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004245 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02004246 bi_fast_delete(req->chn->buf, req->sov);
4247 req->next -= req->sov;
4248 req->sov = 0;
4249 s->req.analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004250 s->res.analysers = AN_RES_HTTP_XFER_BODY;
Willy Tarreaub329a312015-05-22 16:27:37 +02004251 req->msg_state = HTTP_MSG_CLOSED;
4252 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02004253 /* Trim any possible response */
4254 res->chn->buf->i = 0;
4255 res->next = res->sov = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004256 } else {
4257 /* keep-alive not possible */
4258 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4259 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4260 trash.len += 29;
4261 } else {
4262 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
4263 trash.len += 23;
4264 }
Willy Tarreau350f4872014-11-28 14:42:25 +01004265 stream_int_retnclose(&s->si[0], &trash);
Willy Tarreaub329a312015-05-22 16:27:37 +02004266 req->chn->analysers = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004267 }
4268
Willy Tarreaue7dff022015-04-03 01:14:29 +02004269 if (!(s->flags & SF_ERR_MASK))
4270 s->flags |= SF_ERR_LOCAL;
4271 if (!(s->flags & SF_FINST_MASK))
4272 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004273
4274 return 1;
4275}
4276
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004277/* This stream analyser runs all HTTP request processing which is common to
4278 * frontends and backends, which means blocking ACLs, filters, connection-close,
4279 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004280 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004281 * either needs more data or wants to immediately abort the request (eg: deny,
4282 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004283 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004284int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004285{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004286 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004287 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02004288 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004289 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004290 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004291 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02004292
Willy Tarreau655dce92009-11-08 13:10:58 +01004293 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004294 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004295 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004296 }
4297
Willy Tarreau87b09662015-04-03 00:22:06 +02004298 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 +02004299 now_ms, __FUNCTION__,
4300 s,
4301 req,
4302 req->rex, req->wex,
4303 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004304 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004305 req->analysers);
4306
Willy Tarreau65410832014-04-28 21:25:43 +02004307 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02004308 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02004309
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004310 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004311 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004312 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Willy Tarreau51425942010-02-01 10:40:19 +01004313
Willy Tarreau0b748332014-04-29 00:13:29 +02004314 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004315 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4316 goto return_prx_yield;
4317
Willy Tarreau0b748332014-04-29 00:13:29 +02004318 case HTTP_RULE_RES_CONT:
4319 case HTTP_RULE_RES_STOP: /* nothing to do */
4320 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004321
Willy Tarreau0b748332014-04-29 00:13:29 +02004322 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4323 if (txn->flags & TX_CLTARPIT)
4324 goto tarpit;
4325 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004326
Willy Tarreau0b748332014-04-29 00:13:29 +02004327 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4328 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004329
Willy Tarreau0b748332014-04-29 00:13:29 +02004330 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004331 goto done;
4332
Willy Tarreau0b748332014-04-29 00:13:29 +02004333 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4334 goto return_bad_req;
4335 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004336 }
4337
Willy Tarreau52542592014-04-28 18:33:26 +02004338 /* OK at this stage, we know that the request was accepted according to
4339 * the http-request rules, we can check for the stats. Note that the
4340 * URI is detected *before* the req* rules in order not to be affected
4341 * by a possible reqrep, while they are processed *after* so that a
4342 * reqdeny can still block them. This clearly needs to change in 1.6!
4343 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004344 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004345 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004346 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004347 txn->status = 500;
4348 s->logs.tv_request = now;
Willy Tarreau350f4872014-11-28 14:42:25 +01004349 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004350
Willy Tarreaue7dff022015-04-03 01:14:29 +02004351 if (!(s->flags & SF_ERR_MASK))
4352 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02004353 goto return_prx_cond;
4354 }
4355
4356 /* parse the whole stats request and extract the relevant information */
4357 http_handle_stats(s, req);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02004358 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Willy Tarreau0b748332014-04-29 00:13:29 +02004359 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004360
Willy Tarreau0b748332014-04-29 00:13:29 +02004361 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4362 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004363
Willy Tarreau0b748332014-04-29 00:13:29 +02004364 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4365 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004366 }
4367
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004368 /* evaluate the req* rules except reqadd */
4369 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004370 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004371 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004372
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004373 if (txn->flags & TX_CLDENY)
4374 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004375
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004376 if (txn->flags & TX_CLTARPIT)
4377 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004378 }
Willy Tarreau06619262006-12-17 08:37:22 +01004379
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004380 /* add request headers from the rule sets in the same order */
4381 list_for_each_entry(wl, &px->req_add, list) {
4382 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004383 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004384 ret = acl_pass(ret);
4385 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4386 ret = !ret;
4387 if (!ret)
4388 continue;
4389 }
4390
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004391 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004392 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004393 }
4394
Willy Tarreau52542592014-04-28 18:33:26 +02004395
4396 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004397 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004398 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004399 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
4400 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004401
Willy Tarreaue7dff022015-04-03 01:14:29 +02004402 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
4403 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
4404 if (!(s->flags & SF_FINST_MASK))
4405 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004406
Willy Tarreau70730dd2014-04-24 18:06:27 +02004407 /* we may want to compress the stats page */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004408 if (sess->fe->comp || s->be->comp)
Willy Tarreau70730dd2014-04-24 18:06:27 +02004409 select_compression_request_header(s, req->buf);
4410
4411 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreau5506e3f2014-11-20 22:23:10 +01004412 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004413 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004414 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004415
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004416 /* check whether we have some ACLs set to redirect this request */
4417 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004418 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004419 int ret;
4420
Willy Tarreau192252e2015-04-04 01:47:55 +02004421 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004422 ret = acl_pass(ret);
4423 if (rule->cond->pol == ACL_COND_UNLESS)
4424 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004425 if (!ret)
4426 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004427 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004428 if (!http_apply_redirect_rule(rule, s, txn))
4429 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004430 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004431 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004432
Willy Tarreau2be39392010-01-03 17:24:51 +01004433 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4434 * If this happens, then the data will not come immediately, so we must
4435 * send all what we have without waiting. Note that due to the small gain
4436 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004437 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004438 * itself once used.
4439 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004440 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004441
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004442 done: /* done with this analyser, continue with next ones that the calling
4443 * points will have set, if any.
4444 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004445 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004446 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4447 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004448 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004449
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004450 tarpit:
4451 /* When a connection is tarpitted, we use the tarpit timeout,
4452 * which may be the same as the connect timeout if unspecified.
4453 * If unset, then set it to zero because we really want it to
4454 * eventually expire. We build the tarpit as an analyser.
4455 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004456 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004457
4458 /* wipe the request out so that we can drop the connection early
4459 * if the client closes first.
4460 */
4461 channel_dont_connect(req);
4462 req->analysers = 0; /* remove switching rules etc... */
4463 req->analysers |= AN_REQ_HTTP_TARPIT;
4464 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4465 if (!req->analyse_exp)
4466 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02004467 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004468 sess->fe->fe_counters.denied_req++;
4469 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004470 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004471 if (sess->listener->counters)
4472 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004473 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004474
4475 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004476 txn->flags |= TX_CLDENY;
CJ Ess108b1dd2015-04-07 12:03:37 -04004477 txn->status = http_err_codes[txn->rule_deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004478 s->logs.tv_request = now;
CJ Ess108b1dd2015-04-07 12:03:37 -04004479 stream_int_retnclose(&s->si[0], http_error_message(s, txn->rule_deny_status));
Willy Tarreau87b09662015-04-03 00:22:06 +02004480 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004481 sess->fe->fe_counters.denied_req++;
4482 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004483 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004484 if (sess->listener->counters)
4485 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004486 goto return_prx_cond;
4487
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004488 return_bad_req:
4489 /* We centralize bad requests processing here */
4490 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4491 /* we detected a parsing error. We want to archive this request
4492 * in the dedicated proxy area for later troubleshooting.
4493 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004494 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004495 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004496
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004497 txn->req.msg_state = HTTP_MSG_ERROR;
4498 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01004499 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004500
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004501 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004502 if (sess->listener->counters)
4503 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004504
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004505 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02004506 if (!(s->flags & SF_ERR_MASK))
4507 s->flags |= SF_ERR_PRXCOND;
4508 if (!(s->flags & SF_FINST_MASK))
4509 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004510
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004511 req->analysers = 0;
4512 req->analyse_exp = TICK_ETERNITY;
4513 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004514
4515 return_prx_yield:
4516 channel_dont_connect(req);
4517 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004518}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004519
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004520/* This function performs all the processing enabled for the current request.
4521 * It returns 1 if the processing can continue on next analysers, or zero if it
4522 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004523 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004524 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004525int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004526{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004527 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004528 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004529 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02004530 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004531
Willy Tarreau655dce92009-11-08 13:10:58 +01004532 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004533 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004534 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004535 return 0;
4536 }
4537
Willy Tarreau87b09662015-04-03 00:22:06 +02004538 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 +02004539 now_ms, __FUNCTION__,
4540 s,
4541 req,
4542 req->rex, req->wex,
4543 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004544 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004545 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004546
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004547 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02004548 select_compression_request_header(s, req->buf);
4549
Willy Tarreau59234e92008-11-30 23:51:27 +01004550 /*
4551 * Right now, we know that we have processed the entire headers
4552 * and that unwanted requests have been filtered out. We can do
4553 * whatever we want with the remaining request. Also, now we
4554 * may have separate values for ->fe, ->be.
4555 */
Willy Tarreau06619262006-12-17 08:37:22 +01004556
Willy Tarreau59234e92008-11-30 23:51:27 +01004557 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004558 * If HTTP PROXY is set we simply get remote server address parsing
4559 * incoming request. Note that this requires that a connection is
4560 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004561 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004562 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004563 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004564 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004565
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004566 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau973a5422015-08-05 21:47:23 +02004567 if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004568 txn->req.msg_state = HTTP_MSG_ERROR;
4569 txn->status = 500;
4570 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004571 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004572
Willy Tarreaue7dff022015-04-03 01:14:29 +02004573 if (!(s->flags & SF_ERR_MASK))
4574 s->flags |= SF_ERR_RESOURCE;
4575 if (!(s->flags & SF_FINST_MASK))
4576 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004577
4578 return 0;
4579 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004580
4581 path = http_get_path(txn);
4582 url2sa(req->buf->p + msg->sl.rq.u,
4583 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004584 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004585 /* if the path was found, we have to remove everything between
4586 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4587 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4588 * u_l characters by a single "/".
4589 */
4590 if (path) {
4591 char *cur_ptr = req->buf->p;
4592 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4593 int delta;
4594
4595 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4596 http_msg_move_end(&txn->req, delta);
4597 cur_end += delta;
4598 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4599 goto return_bad_req;
4600 }
4601 else {
4602 char *cur_ptr = req->buf->p;
4603 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4604 int delta;
4605
4606 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4607 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4608 http_msg_move_end(&txn->req, delta);
4609 cur_end += delta;
4610 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4611 goto return_bad_req;
4612 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004613 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004614
Willy Tarreau59234e92008-11-30 23:51:27 +01004615 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004616 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004617 * Note that doing so might move headers in the request, but
4618 * the fields will stay coherent and the URI will not move.
4619 * This should only be performed in the backend.
4620 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02004621 if ((s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004622 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4623 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004624
William Lallemanda73203e2012-03-12 12:48:57 +01004625 /* add unique-id if "header-unique-id" is specified */
4626
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004627 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004628 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4629 goto return_bad_req;
4630 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004631 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004632 }
William Lallemanda73203e2012-03-12 12:48:57 +01004633
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004634 if (sess->fe->header_unique_id && s->unique_id) {
4635 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004636 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004637 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004638 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004639 goto return_bad_req;
4640 }
4641
Cyril Bontéb21570a2009-11-29 20:04:48 +01004642 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004643 * 9: add X-Forwarded-For if either the frontend or the backend
4644 * asks for it.
4645 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004646 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004647 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004648 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
4649 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
4650 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004651 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004652 /* The header is set to be added only if none is present
4653 * and we found it, so don't do anything.
4654 */
4655 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004656 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004657 /* Add an X-Forwarded-For header unless the source IP is
4658 * in the 'except' network range.
4659 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004660 if ((!sess->fe->except_mask.s_addr ||
4661 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
4662 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01004663 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004664 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004665 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004666 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004667 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004668 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004669
4670 /* Note: we rely on the backend to get the header name to be used for
4671 * x-forwarded-for, because the header is really meant for the backends.
4672 * However, if the backend did not specify any option, we have to rely
4673 * on the frontend's header name.
4674 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004675 if (s->be->fwdfor_hdr_len) {
4676 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004677 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004678 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004679 len = sess->fe->fwdfor_hdr_len;
4680 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004681 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004682 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 +01004683
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004684 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004685 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004686 }
4687 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004688 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004689 /* FIXME: for the sake of completeness, we should also support
4690 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004691 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004692 int len;
4693 char pn[INET6_ADDRSTRLEN];
4694 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004695 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004696 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004697
Willy Tarreau59234e92008-11-30 23:51:27 +01004698 /* Note: we rely on the backend to get the header name to be used for
4699 * x-forwarded-for, because the header is really meant for the backends.
4700 * However, if the backend did not specify any option, we have to rely
4701 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004702 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004703 if (s->be->fwdfor_hdr_len) {
4704 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004705 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004706 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004707 len = sess->fe->fwdfor_hdr_len;
4708 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004709 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004710 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004711
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004712 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004713 goto return_bad_req;
4714 }
4715 }
4716
4717 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004718 * 10: add X-Original-To if either the frontend or the backend
4719 * asks for it.
4720 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004721 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004722
4723 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004724 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004725 /* Add an X-Original-To header unless the destination IP is
4726 * in the 'except' network range.
4727 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004728 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004729
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004730 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004731 ((!sess->fe->except_mask_to.s_addr ||
4732 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
4733 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004734 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004735 (((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 +02004736 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004737 int len;
4738 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004739 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004740
4741 /* Note: we rely on the backend to get the header name to be used for
4742 * x-original-to, because the header is really meant for the backends.
4743 * However, if the backend did not specify any option, we have to rely
4744 * on the frontend's header name.
4745 */
4746 if (s->be->orgto_hdr_len) {
4747 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004748 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004749 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004750 len = sess->fe->orgto_hdr_len;
4751 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004752 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004753 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 +02004754
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004755 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004756 goto return_bad_req;
4757 }
4758 }
4759 }
4760
Willy Tarreau50fc7772012-11-11 22:19:57 +01004761 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4762 * If an "Upgrade" token is found, the header is left untouched in order not to have
4763 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4764 * "Upgrade" is present in the Connection header.
4765 */
4766 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4767 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004768 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004769 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004770 unsigned int want_flags = 0;
4771
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004772 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004773 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004774 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004775 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004776 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004777 want_flags |= TX_CON_CLO_SET;
4778 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004779 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004780 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004781 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004782 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004783 want_flags |= TX_CON_KAL_SET;
4784 }
4785
4786 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004787 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004788 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004789
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004790
Willy Tarreau522d6c02009-12-06 18:49:18 +01004791 /* If we have no server assigned yet and we're balancing on url_param
4792 * with a POST request, we may be interested in checking the body for
4793 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004794 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004795 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02004796 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004797 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004798 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004799 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004800 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004801
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004802 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004803 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004804#ifdef TCP_QUICKACK
4805 /* We expect some data from the client. Unless we know for sure
4806 * we already have a full request, we have to re-enable quick-ack
4807 * in case we previously disabled it, otherwise we might cause
4808 * the client to delay further data.
4809 */
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004810 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004811 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004812 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004813 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004814 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004815#endif
4816 }
Willy Tarreau03945942009-12-22 16:50:27 +01004817
Willy Tarreau59234e92008-11-30 23:51:27 +01004818 /*************************************************************
4819 * OK, that's finished for the headers. We have done what we *
4820 * could. Let's switch to the DATA state. *
4821 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004822 req->analyse_exp = TICK_ETERNITY;
4823 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004824
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004825 /* if the server closes the connection, we want to immediately react
4826 * and close the socket to save packets and syscalls.
4827 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004828 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
Willy Tarreau350f4872014-11-28 14:42:25 +01004829 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004830
Willy Tarreau59234e92008-11-30 23:51:27 +01004831 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004832 /* OK let's go on with the BODY now */
4833 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004834
Willy Tarreau59234e92008-11-30 23:51:27 +01004835 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004836 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004837 /* we detected a parsing error. We want to archive this request
4838 * in the dedicated proxy area for later troubleshooting.
4839 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004840 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004841 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004842
Willy Tarreau59234e92008-11-30 23:51:27 +01004843 txn->req.msg_state = HTTP_MSG_ERROR;
4844 txn->status = 400;
4845 req->analysers = 0;
Willy Tarreau350f4872014-11-28 14:42:25 +01004846 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004847
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004848 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004849 if (sess->listener->counters)
4850 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004851
Willy Tarreaue7dff022015-04-03 01:14:29 +02004852 if (!(s->flags & SF_ERR_MASK))
4853 s->flags |= SF_ERR_PRXCOND;
4854 if (!(s->flags & SF_FINST_MASK))
4855 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004856 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004857}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004858
Willy Tarreau60b85b02008-11-30 23:28:40 +01004859/* This function is an analyser which processes the HTTP tarpit. It always
4860 * returns zero, at the beginning because it prevents any other processing
4861 * from occurring, and at the end because it terminates the request.
4862 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004863int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004864{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004865 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004866
4867 /* This connection is being tarpitted. The CLIENT side has
4868 * already set the connect expiration date to the right
4869 * timeout. We just have to check that the client is still
4870 * there and that the timeout has not expired.
4871 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004872 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004873 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004874 !tick_is_expired(req->analyse_exp, now_ms))
4875 return 0;
4876
4877 /* We will set the queue timer to the time spent, just for
4878 * logging purposes. We fake a 500 server error, so that the
4879 * attacker will not suspect his connection has been tarpitted.
4880 * It will not cause trouble to the logs because we can exclude
4881 * the tarpitted connections by filtering on the 'PT' status flags.
4882 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004883 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4884
4885 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004886 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau350f4872014-11-28 14:42:25 +01004887 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004888
4889 req->analysers = 0;
4890 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004891
Willy Tarreaue7dff022015-04-03 01:14:29 +02004892 if (!(s->flags & SF_ERR_MASK))
4893 s->flags |= SF_ERR_PRXCOND;
4894 if (!(s->flags & SF_FINST_MASK))
4895 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004896 return 0;
4897}
4898
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004899/* This function is an analyser which waits for the HTTP request body. It waits
4900 * for either the buffer to be full, or the full advertised contents to have
4901 * reached the buffer. It must only be called after the standard HTTP request
4902 * processing has occurred, because it expects the request to be parsed and will
4903 * look for the Expect header. It may send a 100-Continue interim response. It
4904 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4905 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4906 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004907 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004908int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004909{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004910 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004911 struct http_txn *txn = s->txn;
4912 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004913
4914 /* We have to parse the HTTP request body to find any required data.
4915 * "balance url_param check_post" should have been the only way to get
4916 * into this. We were brought here after HTTP header analysis, so all
4917 * related structures are ready.
4918 */
4919
Willy Tarreau890988f2014-04-10 11:59:33 +02004920 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4921 /* This is the first call */
4922 if (msg->msg_state < HTTP_MSG_BODY)
4923 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004924
Willy Tarreau890988f2014-04-10 11:59:33 +02004925 if (msg->msg_state < HTTP_MSG_100_SENT) {
4926 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4927 * send an HTTP/1.1 100 Continue intermediate response.
4928 */
4929 if (msg->flags & HTTP_MSGF_VER_11) {
4930 struct hdr_ctx ctx;
4931 ctx.idx = 0;
4932 /* Expect is allowed in 1.1, look for it */
4933 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4934 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004935 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau890988f2014-04-10 11:59:33 +02004936 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004937 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004938 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004939 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004940
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004941 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004942 * req->buf->p still points to the beginning of the message. We
4943 * must save the body in msg->next because it survives buffer
4944 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004945 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004946 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004947
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004948 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004949 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4950 else
4951 msg->msg_state = HTTP_MSG_DATA;
4952 }
4953
Willy Tarreau890988f2014-04-10 11:59:33 +02004954 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4955 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004956 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004957 goto missing_data;
4958
4959 /* OK we have everything we need now */
4960 goto http_end;
4961 }
4962
4963 /* OK here we're parsing a chunked-encoded message */
4964
Willy Tarreau522d6c02009-12-06 18:49:18 +01004965 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004966 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004967 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004968 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004969 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004970 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004971
Willy Tarreau115acb92009-12-26 13:56:06 +01004972 if (!ret)
4973 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004974 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004975 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004976 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004977 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004978 }
4979
Willy Tarreaud98cf932009-12-27 22:54:55 +01004980 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02004981 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4982 * for at least a whole chunk or the whole content length bytes after
4983 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004984 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004985 if (msg->msg_state == HTTP_MSG_TRAILERS)
4986 goto http_end;
4987
Willy Tarreaue115b492015-05-01 23:05:14 +02004988 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004989 goto http_end;
4990
4991 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004992 /* we get here if we need to wait for more data. If the buffer is full,
4993 * we have the maximum we can expect.
4994 */
4995 if (buffer_full(req->buf, global.tune.maxrewrite))
4996 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004997
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004998 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004999 txn->status = 408;
Willy Tarreau350f4872014-11-28 14:42:25 +01005000 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02005001
Willy Tarreaue7dff022015-04-03 01:14:29 +02005002 if (!(s->flags & SF_ERR_MASK))
5003 s->flags |= SF_ERR_CLITO;
5004 if (!(s->flags & SF_FINST_MASK))
5005 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005006 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01005007 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01005008
5009 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02005010 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01005011 /* Not enough data. We'll re-use the http-request
5012 * timeout here. Ideally, we should set the timeout
5013 * relative to the accept() date. We just set the
5014 * request timeout once at the beginning of the
5015 * request.
5016 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005017 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01005018 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02005019 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01005020 return 0;
5021 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01005022
5023 http_end:
5024 /* The situation will not evolve, so let's give up on the analysis. */
5025 s->logs.tv_request = now; /* update the request timer to reflect full request */
5026 req->analysers &= ~an_bit;
5027 req->analyse_exp = TICK_ETERNITY;
5028 return 1;
5029
5030 return_bad_req: /* let's centralize all bad requests */
5031 txn->req.msg_state = HTTP_MSG_ERROR;
5032 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01005033 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01005034
Willy Tarreaue7dff022015-04-03 01:14:29 +02005035 if (!(s->flags & SF_ERR_MASK))
5036 s->flags |= SF_ERR_PRXCOND;
5037 if (!(s->flags & SF_FINST_MASK))
5038 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005039
Willy Tarreau522d6c02009-12-06 18:49:18 +01005040 return_err_msg:
5041 req->analysers = 0;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005042 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005043 if (sess->listener->counters)
5044 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005045 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01005046}
5047
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005048/* send a server's name with an outgoing request over an established connection.
5049 * Note: this function is designed to be called once the request has been scheduled
5050 * for being forwarded. This is the reason why it rewinds the buffer before
5051 * proceeding.
5052 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01005053int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05005054
5055 struct hdr_ctx ctx;
5056
Mark Lamourinec2247f02012-01-04 13:02:01 -05005057 char *hdr_name = be->server_id_hdr_name;
5058 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02005059 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005060 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005061 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005062
William Lallemandd9e90662012-01-30 17:27:17 +01005063 ctx.idx = 0;
5064
Willy Tarreau211cdec2014-04-17 20:18:08 +02005065 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005066 if (old_o) {
5067 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005068 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02005069 txn->req.next += old_o;
5070 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005071 }
5072
Willy Tarreau9b28e032012-10-12 23:49:43 +02005073 old_i = chn->buf->i;
5074 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 -05005075 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005076 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005077 }
5078
5079 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005080 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005081 memcpy(hdr_val, hdr_name, hdr_name_len);
5082 hdr_val += hdr_name_len;
5083 *hdr_val++ = ':';
5084 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005085 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
5086 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005087
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005088 if (old_o) {
5089 /* If this was a forwarded request, we must readjust the amount of
5090 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02005091 * variations. Note that the current state is >= HTTP_MSG_BODY,
5092 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005093 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02005094 old_o += chn->buf->i - old_i;
5095 b_adv(chn->buf, old_o);
5096 txn->req.next -= old_o;
5097 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005098 }
5099
Mark Lamourinec2247f02012-01-04 13:02:01 -05005100 return 0;
5101}
5102
Willy Tarreau610ecce2010-01-04 21:15:02 +01005103/* Terminate current transaction and prepare a new one. This is very tricky
5104 * right now but it works.
5105 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005106void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005107{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005108 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005109 struct proxy *fe = strm_fe(s);
Willy Tarreau323a2d92015-08-04 19:00:17 +02005110 struct connection *srv_conn;
5111 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02005112 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01005113
Willy Tarreau610ecce2010-01-04 21:15:02 +01005114 /* FIXME: We need a more portable way of releasing a backend's and a
5115 * server's connections. We need a safer way to reinitialize buffer
5116 * flags. We also need a more accurate method for computing per-request
5117 * data.
5118 */
Willy Tarreau323a2d92015-08-04 19:00:17 +02005119 srv_conn = objt_conn(s->si[1].end);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005120
Willy Tarreau4213a112013-12-15 10:25:42 +01005121 /* unless we're doing keep-alive, we want to quickly close the connection
5122 * to the server.
5123 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005124 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005125 !si_conn_ready(&s->si[1])) {
5126 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
5127 si_shutr(&s->si[1]);
5128 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01005129 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005130
Willy Tarreaue7dff022015-04-03 01:14:29 +02005131 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005132 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01005133 if (unlikely(s->srv_conn))
5134 sess_change_server(s, NULL);
5135 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005136
5137 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02005138 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005139
Willy Tarreaueee5b512015-04-03 23:46:31 +02005140 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005141 int n;
5142
Willy Tarreaueee5b512015-04-03 23:46:31 +02005143 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005144 if (n < 1 || n > 5)
5145 n = 0;
5146
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005147 if (fe->mode == PR_MODE_HTTP) {
5148 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005149 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005150 fe->fe_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005151 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02005152 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005153 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005154 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005155 s->be->be_counters.p.http.cum_req++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005156 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005157 s->be->be_counters.p.http.comp_rsp++;
5158 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005159 }
5160
5161 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005162 s->logs.bytes_in -= s->req.buf->i;
5163 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005164
5165 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005166 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005167 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005168 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005169 s->do_log(s);
5170 }
5171
Willy Tarreaud713bcc2014-06-25 15:36:04 +02005172 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02005173 stream_stop_content_counters(s);
5174 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02005175
Willy Tarreau610ecce2010-01-04 21:15:02 +01005176 s->logs.accept_date = date; /* user-visible date for logging */
5177 s->logs.tv_accept = now; /* corrected date for internal use */
5178 tv_zero(&s->logs.tv_request);
5179 s->logs.t_queue = -1;
5180 s->logs.t_connect = -1;
5181 s->logs.t_data = -1;
5182 s->logs.t_close = 0;
5183 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
5184 s->logs.srv_queue_size = 0; /* we will get this number soon */
5185
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005186 s->logs.bytes_in = s->req.total = s->req.buf->i;
5187 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005188
5189 if (s->pend_pos)
5190 pendconn_free(s->pend_pos);
5191
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005192 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005193 if (s->flags & SF_CURR_SESS) {
5194 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005195 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005196 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005197 if (may_dequeue_tasks(objt_server(s->target), s->be))
5198 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01005199 }
5200
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005201 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005202
Willy Tarreau4213a112013-12-15 10:25:42 +01005203 /* only release our endpoint if we don't intend to reuse the
5204 * connection.
5205 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005206 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005207 !si_conn_ready(&s->si[1])) {
5208 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02005209 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005210 }
5211
Willy Tarreau350f4872014-11-28 14:42:25 +01005212 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
5213 s->si[1].err_type = SI_ET_NONE;
5214 s->si[1].conn_retries = 0; /* used for logging too */
5215 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02005216 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 +01005217 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);
5218 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 +02005219 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
5220 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
5221 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005222
Willy Tarreaueee5b512015-04-03 23:46:31 +02005223 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005224 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02005225 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005226
5227 if (prev_status == 401 || prev_status == 407) {
5228 /* In HTTP keep-alive mode, if we receive a 401, we still have
5229 * a chance of being able to send the visitor again to the same
5230 * server over the same connection. This is required by some
5231 * broken protocols such as NTLM, and anyway whenever there is
5232 * an opportunity for sending the challenge to the proper place,
5233 * it's better to do it (at least it helps with debugging).
5234 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005235 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02005236 if (srv_conn)
5237 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01005238 }
5239
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005240 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005241 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005242
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005243 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005244 s->req.flags |= CF_NEVER_WAIT;
5245 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005246 }
5247
Willy Tarreau610ecce2010-01-04 21:15:02 +01005248 /* if the request buffer is not empty, it means we're
5249 * about to process another request, so send pending
5250 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005251 * Just don't do this if the buffer is close to be full,
5252 * because the request will wait for it to flush a little
5253 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005254 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005255 if (s->req.buf->i) {
5256 if (s->res.buf->o &&
5257 !buffer_full(s->res.buf, global.tune.maxrewrite) &&
5258 bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)
5259 s->res.flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005260 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005261
5262 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005263 channel_auto_read(&s->req);
5264 channel_auto_close(&s->req);
5265 channel_auto_read(&s->res);
5266 channel_auto_close(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005267
Willy Tarreau27375622013-12-17 00:00:28 +01005268 /* we're in keep-alive with an idle connection, monitor it */
Willy Tarreau4320eaa2015-08-05 11:08:30 +02005269 if (srv_conn) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02005270 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005271 if (!srv)
5272 si_idle_conn(&s->si[1], NULL);
5273 else if ((srv_conn->flags & CO_FL_PRIVATE) ||
5274 ((s->be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR))
5275 si_idle_conn(&s->si[1], &srv->priv_conns);
Willy Tarreau449d74a2015-08-05 17:16:33 +02005276 else if (prev_flags & TX_NOT_FIRST)
5277 /* note: we check the request, not the connection, but
5278 * this is valid for strategies SAFE and AGGR, and in
5279 * case of ALWS, we don't care anyway.
5280 */
5281 si_idle_conn(&s->si[1], &srv->safe_conns);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005282 else
5283 si_idle_conn(&s->si[1], &srv->idle_conns);
Willy Tarreau4320eaa2015-08-05 11:08:30 +02005284 }
Willy Tarreau27375622013-12-17 00:00:28 +01005285
Willy Tarreauc29d0cd2015-09-23 12:18:14 +02005286 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005287 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005288}
5289
5290
5291/* This function updates the request state machine according to the response
5292 * state machine and buffer flags. It returns 1 if it changes anything (flag
5293 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5294 * it is only used to find when a request/response couple is complete. Both
5295 * this function and its equivalent should loop until both return zero. It
5296 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5297 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005298int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005299{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005300 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005301 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005302 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005303 unsigned int old_state = txn->req.msg_state;
5304
Willy Tarreau610ecce2010-01-04 21:15:02 +01005305 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5306 return 0;
5307
5308 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005309 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005310 * We can shut the read side unless we want to abort_on_close,
5311 * or we have a POST request. The issue with POST requests is
5312 * that some browsers still send a CRLF after the request, and
5313 * this CRLF must be read so that it does not remain in the kernel
5314 * buffers, otherwise a close could cause an RST on some systems
5315 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005316 * Note that if we're using keep-alive on the client side, we'd
5317 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005318 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005319 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005320 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005321 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5322 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5323 !(s->be->options & PR_O_ABRT_CLOSE) &&
5324 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005325 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005326
Willy Tarreau40f151a2012-12-20 12:10:09 +01005327 /* if the server closes the connection, we want to immediately react
5328 * and close the socket to save packets and syscalls.
5329 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005330 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005331
Willy Tarreau610ecce2010-01-04 21:15:02 +01005332 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5333 goto wait_other_side;
5334
5335 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5336 /* The server has not finished to respond, so we
5337 * don't want to move in order not to upset it.
5338 */
5339 goto wait_other_side;
5340 }
5341
5342 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5343 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005344 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005345 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005346 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005347 goto wait_other_side;
5348 }
5349
5350 /* When we get here, it means that both the request and the
5351 * response have finished receiving. Depending on the connection
5352 * mode, we'll have to wait for the last bytes to leave in either
5353 * direction, and sometimes for a close to be effective.
5354 */
5355
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005356 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5357 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005358 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5359 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005360 }
5361 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5362 /* Option forceclose is set, or either side wants to close,
5363 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005364 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005365 * once both states are CLOSED.
5366 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005367 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5368 channel_shutr_now(chn);
5369 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005370 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005371 }
5372 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005373 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5374 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005375 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005376 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5377 channel_auto_read(chn);
5378 txn->req.msg_state = HTTP_MSG_TUNNEL;
5379 chn->flags |= CF_NEVER_WAIT;
5380 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005381 }
5382
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005383 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005384 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005385 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005386
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005387 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005388 txn->req.msg_state = HTTP_MSG_CLOSING;
5389 goto http_msg_closing;
5390 }
5391 else {
5392 txn->req.msg_state = HTTP_MSG_CLOSED;
5393 goto http_msg_closed;
5394 }
5395 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005396 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005397 }
5398
5399 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5400 http_msg_closing:
5401 /* nothing else to forward, just waiting for the output buffer
5402 * to be empty and for the shutw_now to take effect.
5403 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005404 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005405 txn->req.msg_state = HTTP_MSG_CLOSED;
5406 goto http_msg_closed;
5407 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005408 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005409 txn->req.msg_state = HTTP_MSG_ERROR;
5410 goto wait_other_side;
5411 }
5412 }
5413
5414 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5415 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005416 /* see above in MSG_DONE why we only do this in these states */
5417 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5418 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5419 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005420 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005421 goto wait_other_side;
5422 }
5423
5424 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005425 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005426}
5427
5428
5429/* This function updates the response state machine according to the request
5430 * state machine and buffer flags. It returns 1 if it changes anything (flag
5431 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5432 * it is only used to find when a request/response couple is complete. Both
5433 * this function and its equivalent should loop until both return zero. It
5434 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5435 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005436int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005437{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005438 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005439 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005440 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005441 unsigned int old_state = txn->rsp.msg_state;
5442
Willy Tarreau610ecce2010-01-04 21:15:02 +01005443 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5444 return 0;
5445
5446 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5447 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005448 * still monitor the server connection for a possible close
5449 * while the request is being uploaded, so we don't disable
5450 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005451 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005452 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005453
5454 if (txn->req.msg_state == HTTP_MSG_ERROR)
5455 goto wait_other_side;
5456
5457 if (txn->req.msg_state < HTTP_MSG_DONE) {
5458 /* The client seems to still be sending data, probably
5459 * because we got an error response during an upload.
5460 * We have the choice of either breaking the connection
5461 * or letting it pass through. Let's do the later.
5462 */
5463 goto wait_other_side;
5464 }
5465
5466 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5467 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005468 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005469 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005470 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005471 goto wait_other_side;
5472 }
5473
5474 /* When we get here, it means that both the request and the
5475 * response have finished receiving. Depending on the connection
5476 * mode, we'll have to wait for the last bytes to leave in either
5477 * direction, and sometimes for a close to be effective.
5478 */
5479
5480 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5481 /* Server-close mode : shut read and wait for the request
5482 * side to close its output buffer. The caller will detect
5483 * when we're in DONE and the other is in CLOSED and will
5484 * catch that for the final cleanup.
5485 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005486 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5487 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005488 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005489 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5490 /* Option forceclose is set, or either side wants to close,
5491 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005492 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005493 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005494 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005495 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5496 channel_shutr_now(chn);
5497 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005498 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005499 }
5500 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005501 /* The last possible modes are keep-alive and tunnel. Tunnel will
5502 * need to forward remaining data. Keep-alive will need to monitor
5503 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005504 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005505 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005506 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005507 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5508 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005509 }
5510
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005511 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005512 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005513 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005514 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5515 goto http_msg_closing;
5516 }
5517 else {
5518 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5519 goto http_msg_closed;
5520 }
5521 }
5522 goto wait_other_side;
5523 }
5524
5525 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5526 http_msg_closing:
5527 /* nothing else to forward, just waiting for the output buffer
5528 * to be empty and for the shutw_now to take effect.
5529 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005530 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005531 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5532 goto http_msg_closed;
5533 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005534 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005535 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005536 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005537 if (objt_server(s->target))
5538 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005539 goto wait_other_side;
5540 }
5541 }
5542
5543 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5544 http_msg_closed:
5545 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005546 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005547 channel_auto_close(chn);
5548 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005549 goto wait_other_side;
5550 }
5551
5552 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005553 /* We force the response to leave immediately if we're waiting for the
5554 * other side, since there is no pending shutdown to push it out.
5555 */
5556 if (!channel_is_empty(chn))
5557 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005558 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005559}
5560
5561
5562/* Resync the request and response state machines. Return 1 if either state
5563 * changes.
5564 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005565int http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005566{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005567 struct http_txn *txn = s->txn;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005568 int old_req_state = txn->req.msg_state;
5569 int old_res_state = txn->rsp.msg_state;
5570
Willy Tarreau610ecce2010-01-04 21:15:02 +01005571 http_sync_req_state(s);
5572 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005573 if (!http_sync_res_state(s))
5574 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005575 if (!http_sync_req_state(s))
5576 break;
5577 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005578
Willy Tarreau610ecce2010-01-04 21:15:02 +01005579 /* OK, both state machines agree on a compatible state.
5580 * There are a few cases we're interested in :
5581 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5582 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5583 * directions, so let's simply disable both analysers.
5584 * - HTTP_MSG_CLOSED on the response only means we must abort the
5585 * request.
5586 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5587 * with server-close mode means we've completed one request and we
5588 * must re-initialize the server connection.
5589 */
5590
5591 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5592 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5593 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5594 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005595 s->req.analysers = 0;
5596 channel_auto_close(&s->req);
5597 channel_auto_read(&s->req);
5598 s->res.analysers = 0;
5599 channel_auto_close(&s->res);
5600 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005601 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005602 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005603 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005604 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005605 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005606 s->res.analysers = 0;
5607 channel_auto_close(&s->res);
5608 channel_auto_read(&s->res);
5609 s->req.analysers = 0;
5610 channel_abort(&s->req);
5611 channel_auto_close(&s->req);
5612 channel_auto_read(&s->req);
5613 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005614 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005615 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5616 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005617 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005618 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5619 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5620 /* server-close/keep-alive: terminate this transaction,
5621 * possibly killing the server connection and reinitialize
5622 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005623 */
5624 http_end_txn_clean_session(s);
5625 }
5626
Willy Tarreau610ecce2010-01-04 21:15:02 +01005627 return txn->req.msg_state != old_req_state ||
5628 txn->rsp.msg_state != old_res_state;
5629}
5630
Willy Tarreaud98cf932009-12-27 22:54:55 +01005631/* This function is an analyser which forwards request body (including chunk
5632 * sizes if any). It is called as soon as we must forward, even if we forward
5633 * zero byte. The only situation where it must not be called is when we're in
5634 * tunnel mode and we want to forward till the close. It's used both to forward
5635 * remaining data and to resync after end of body. It expects the msg_state to
5636 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005637 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005638 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005639 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005640 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005641int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005642{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005643 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005644 struct http_txn *txn = s->txn;
5645 struct http_msg *msg = &s->txn->req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005646
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005647 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5648 return 0;
5649
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005650 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005651 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005652 /* Output closed while we were sending data. We must abort and
5653 * wake the other side up.
5654 */
5655 msg->msg_state = HTTP_MSG_ERROR;
5656 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005657 return 1;
5658 }
5659
Willy Tarreaud98cf932009-12-27 22:54:55 +01005660 /* Note that we don't have to send 100-continue back because we don't
5661 * need the data to complete our job, and it's up to the server to
5662 * decide whether to return 100, 417 or anything else in return of
5663 * an "Expect: 100-continue" header.
5664 */
5665
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005666 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005667 /* we have msg->sov which points to the first byte of message
5668 * body, and req->buf.p still points to the beginning of the
5669 * message. We forward the headers now, as we don't need them
5670 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005671 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005672 b_adv(req->buf, msg->sov);
5673 msg->next -= msg->sov;
5674 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005675
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005676 /* The previous analysers guarantee that the state is somewhere
5677 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5678 * msg->next are always correct.
5679 */
5680 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5681 if (msg->flags & HTTP_MSGF_TE_CHNK)
5682 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5683 else
5684 msg->msg_state = HTTP_MSG_DATA;
5685 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005686 }
5687
Willy Tarreau7ba23542014-04-17 21:50:00 +02005688 /* Some post-connect processing might want us to refrain from starting to
5689 * forward data. Currently, the only reason for this is "balance url_param"
5690 * whichs need to parse/process the request after we've enabled forwarding.
5691 */
5692 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005693 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005694 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005695 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005696 goto missing_data;
5697 }
5698 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5699 }
5700
Willy Tarreau80a92c02014-03-12 10:41:13 +01005701 /* in most states, we should abort in case of early close */
5702 channel_auto_close(req);
5703
Willy Tarreauefdf0942014-04-24 20:08:57 +02005704 if (req->to_forward) {
5705 /* We can't process the buffer's contents yet */
5706 req->flags |= CF_WAKE_WRITE;
5707 goto missing_data;
5708 }
5709
Willy Tarreaud98cf932009-12-27 22:54:55 +01005710 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005711 if (msg->msg_state == HTTP_MSG_DATA) {
5712 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005713 /* we may have some pending data starting at req->buf->p */
5714 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005715 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005716 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005717 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005718 msg->next += msg->chunk_len;
5719 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005720
5721 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005722 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005723 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005724 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005725 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005726 }
5727 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005728 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005729 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005730 * TRAILERS state.
5731 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005732 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005733
Willy Tarreau54d23df2012-10-25 19:04:45 +02005734 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005735 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005736 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005737 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005738 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005739 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005740 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005741 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005742 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005743 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005744 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005745 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005746 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005747
5748 if (ret == 0)
5749 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005750 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005751 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005752 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005753 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005754 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005755 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005756 /* we're in MSG_CHUNK_SIZE now */
5757 }
5758 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005759 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005760
5761 if (ret == 0)
5762 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005763 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005764 stream_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005765 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005766 http_capture_bad_message(&sess->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005767 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005768 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005769 /* we're in HTTP_MSG_DONE now */
5770 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005771 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005772 int old_state = msg->msg_state;
5773
Willy Tarreau610ecce2010-01-04 21:15:02 +01005774 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005775
5776 /* we may have some pending data starting at req->buf->p
5777 * such as last chunk of data or trailers.
5778 */
5779 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005780 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005781 msg->sov -= msg->next;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005782 msg->next = 0;
5783
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005784 /* we don't want to forward closes on DONE except in
5785 * tunnel mode.
5786 */
5787 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005788 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005789 if (http_resync_states(s)) {
5790 /* some state changes occurred, maybe the analyser
5791 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005792 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005793 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005794 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005795 /* request errors are most likely due to
5796 * the server aborting the transfer.
5797 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005798 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005799 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005800 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005801 http_capture_bad_message(&sess->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005802 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005803 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005804 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005805 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005806
5807 /* If "option abortonclose" is set on the backend, we
5808 * want to monitor the client's connection and forward
5809 * any shutdown notification to the server, which will
5810 * decide whether to close or to go on processing the
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005811 * request. We only do that in tunnel mode, and not in
5812 * other modes since it can be abused to exhaust source
5813 * ports.
Willy Tarreau5c54c712010-07-17 08:02:58 +02005814 */
5815 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005816 channel_auto_read(req);
Willy Tarreaubbfb6c42015-05-11 18:30:33 +02005817 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
5818 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
5819 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005820 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005821 }
Willy Tarreaueee5b512015-04-03 23:46:31 +02005822 else if (s->txn->meth == HTTP_METH_POST) {
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005823 /* POST requests may require to read extra CRLF
5824 * sent by broken browsers and which could cause
5825 * an RST to be sent upon close on some systems
5826 * (eg: Linux).
5827 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005828 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005829 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005830
Willy Tarreau610ecce2010-01-04 21:15:02 +01005831 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005832 }
5833 }
5834
Willy Tarreaud98cf932009-12-27 22:54:55 +01005835 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005836 /* we may have some pending data starting at req->buf->p */
5837 b_adv(req->buf, msg->next);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005838 if (unlikely(!(s->req.flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005839 msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i);
5840
Willy Tarreaubed410e2014-04-22 08:19:34 +02005841 msg->next = 0;
5842 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5843
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005844 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005845 if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005846 if (!(s->flags & SF_ERR_MASK))
5847 s->flags |= SF_ERR_CLICL;
5848 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005849 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005850 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005851 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005852 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005853 }
5854
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005855 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005856 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005857 if (objt_server(s->target))
5858 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005859
5860 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005861 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005862
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005863 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005864 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005865 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005866
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005867 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005868 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005869 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005870 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005871 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005872
Willy Tarreau5c620922011-05-11 19:56:11 +02005873 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005874 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005875 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005876 * modes are already handled by the stream sock layer. We must not do
5877 * this in content-length mode because it could present the MSG_MORE
5878 * flag with the last block of forwarded data, which would cause an
5879 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005880 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005881 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005882 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005883
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005884 return 0;
5885
Willy Tarreaud98cf932009-12-27 22:54:55 +01005886 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005887 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005888 if (sess->listener->counters)
5889 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005890
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005891 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005892 /* we may have some pending data starting at req->buf->p */
5893 b_adv(req->buf, msg->next);
5894 msg->next = 0;
5895
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005896 txn->req.msg_state = HTTP_MSG_ERROR;
5897 if (txn->status) {
5898 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005899 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005900 } else {
5901 txn->status = 400;
Willy Tarreau350f4872014-11-28 14:42:25 +01005902 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005903 }
5904 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005905 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005906
Willy Tarreaue7dff022015-04-03 01:14:29 +02005907 if (!(s->flags & SF_ERR_MASK))
5908 s->flags |= SF_ERR_PRXCOND;
5909 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005910 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005911 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005912 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005913 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005914 }
5915 return 0;
5916
5917 aborted_xfer:
5918 txn->req.msg_state = HTTP_MSG_ERROR;
5919 if (txn->status) {
5920 /* Note: we don't send any error if some data were already sent */
Willy Tarreau350f4872014-11-28 14:42:25 +01005921 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005922 } else {
5923 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005924 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005925 }
5926 req->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005927 s->res.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005928
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005929 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005930 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005931 if (objt_server(s->target))
5932 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005933
Willy Tarreaue7dff022015-04-03 01:14:29 +02005934 if (!(s->flags & SF_ERR_MASK))
5935 s->flags |= SF_ERR_SRVCL;
5936 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005937 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005938 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005939 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005940 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005941 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005942 return 0;
5943}
5944
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005945/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5946 * processing can continue on next analysers, or zero if it either needs more
5947 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005948 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005949 * when it has nothing left to do, and may remove any analyser when it wants to
5950 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005951 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005952int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005953{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005954 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005955 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005956 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005957 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005958 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005959 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005960 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005961
Willy Tarreau87b09662015-04-03 00:22:06 +02005962 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 +02005963 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005964 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005965 rep,
5966 rep->rex, rep->wex,
5967 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005968 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005969 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005970
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005971 /*
5972 * Now parse the partial (or complete) lines.
5973 * We will check the response syntax, and also join multi-line
5974 * headers. An index of all the lines will be elaborated while
5975 * parsing.
5976 *
5977 * For the parsing, we use a 28 states FSM.
5978 *
5979 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005980 * rep->buf->p = beginning of response
5981 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5982 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005983 * msg->eol = end of current header or line (LF or CRLF)
5984 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005985 */
5986
Willy Tarreau628c40c2014-04-24 19:11:26 +02005987 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005988 /* There's a protected area at the end of the buffer for rewriting
5989 * purposes. We don't want to start to parse the request if the
5990 * protected area is affected, because we may have to move processed
5991 * data later, which is much more complicated.
5992 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005993 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005994 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005995 /* some data has still not left the buffer, wake us once that's done */
5996 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5997 goto abort_response;
5998 channel_dont_close(rep);
5999 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01006000 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02006001 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01006002 }
6003
Willy Tarreau379357a2013-06-08 12:55:46 +02006004 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
6005 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
6006 buffer_slow_realign(rep->buf);
6007
Willy Tarreau9b28e032012-10-12 23:49:43 +02006008 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01006009 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01006010 }
6011
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006012 /* 1: we might have to print this header in debug mode */
6013 if (unlikely((global.mode & MODE_DEBUG) &&
6014 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02006015 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006016 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006017
Willy Tarreau9b28e032012-10-12 23:49:43 +02006018 sol = rep->buf->p;
6019 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006020 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006021
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006022 sol += hdr_idx_first_pos(&txn->hdr_idx);
6023 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006024
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006025 while (cur_idx) {
6026 eol = sol + txn->hdr_idx.v[cur_idx].len;
6027 debug_hdr("srvhdr", s, sol, eol);
6028 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
6029 cur_idx = txn->hdr_idx.v[cur_idx].next;
6030 }
6031 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006032
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006033 /*
6034 * Now we quickly check if we have found a full valid response.
6035 * If not so, we check the FD and buffer states before leaving.
6036 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01006037 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006038 * responses are checked first.
6039 *
6040 * Depending on whether the client is still there or not, we
6041 * may send an error response back or not. Note that normally
6042 * we should only check for HTTP status there, and check I/O
6043 * errors somewhere else.
6044 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006045
Willy Tarreau655dce92009-11-08 13:10:58 +01006046 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006047 /* Invalid response */
6048 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6049 /* we detected a parsing error. We want to archive this response
6050 * in the dedicated proxy area for later troubleshooting.
6051 */
6052 hdr_response_bad:
6053 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006054 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006055
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006056 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006057 if (objt_server(s->target)) {
6058 objt_server(s->target)->counters.failed_resp++;
6059 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006060 }
Willy Tarreau64648412010-03-05 10:41:54 +01006061 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006062 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006063 rep->analysers = 0;
6064 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006065 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006066 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006067 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006068
Willy Tarreaue7dff022015-04-03 01:14:29 +02006069 if (!(s->flags & SF_ERR_MASK))
6070 s->flags |= SF_ERR_PRXCOND;
6071 if (!(s->flags & SF_FINST_MASK))
6072 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006073
6074 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006075 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006076
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006077 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006078 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02006079 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02006080 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006081 goto hdr_response_bad;
6082 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006083
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006084 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006085 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006086 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006087 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006088 else if (txn->flags & TX_NOT_FIRST)
6089 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02006090
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006091 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006092 if (objt_server(s->target)) {
6093 objt_server(s->target)->counters.failed_resp++;
6094 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006095 }
Willy Tarreau461f6622008-08-15 23:43:19 +02006096
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006097 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006098 rep->analysers = 0;
6099 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006100 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006101 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006102 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02006103
Willy Tarreaue7dff022015-04-03 01:14:29 +02006104 if (!(s->flags & SF_ERR_MASK))
6105 s->flags |= SF_ERR_SRVCL;
6106 if (!(s->flags & SF_FINST_MASK))
6107 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02006108 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006109 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006110
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02006111 /* read timeout : return a 504 to the client. */
6112 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006113 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006114 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006115 else if (txn->flags & TX_NOT_FIRST)
6116 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006117
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006118 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006119 if (objt_server(s->target)) {
6120 objt_server(s->target)->counters.failed_resp++;
6121 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006122 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006123
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006124 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006125 rep->analysers = 0;
6126 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01006127 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006128 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006129 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02006130
Willy Tarreaue7dff022015-04-03 01:14:29 +02006131 if (!(s->flags & SF_ERR_MASK))
6132 s->flags |= SF_ERR_SRVTO;
6133 if (!(s->flags & SF_FINST_MASK))
6134 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006135 return 0;
6136 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02006137
Willy Tarreauf003d372012-11-26 13:35:37 +01006138 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006139 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006140 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01006141 s->be->be_counters.cli_aborts++;
6142 if (objt_server(s->target))
6143 objt_server(s->target)->counters.cli_aborts++;
6144
6145 rep->analysers = 0;
6146 channel_auto_close(rep);
6147
6148 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01006149 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006150 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_400));
Willy Tarreauf003d372012-11-26 13:35:37 +01006151
Willy Tarreaue7dff022015-04-03 01:14:29 +02006152 if (!(s->flags & SF_ERR_MASK))
6153 s->flags |= SF_ERR_CLICL;
6154 if (!(s->flags & SF_FINST_MASK))
6155 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01006156
Willy Tarreau87b09662015-04-03 00:22:06 +02006157 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01006158 return 0;
6159 }
6160
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006161 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006162 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006163 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006164 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006165 else if (txn->flags & TX_NOT_FIRST)
6166 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006167
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006168 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006169 if (objt_server(s->target)) {
6170 objt_server(s->target)->counters.failed_resp++;
6171 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006172 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006173
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006174 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006175 rep->analysers = 0;
6176 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006177 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006178 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006179 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01006180
Willy Tarreaue7dff022015-04-03 01:14:29 +02006181 if (!(s->flags & SF_ERR_MASK))
6182 s->flags |= SF_ERR_SRVCL;
6183 if (!(s->flags & SF_FINST_MASK))
6184 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006185 return 0;
6186 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006187
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006188 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006189 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006190 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006191 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006192 else if (txn->flags & TX_NOT_FIRST)
6193 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006194
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006195 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006196 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006197 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006198
Willy Tarreaue7dff022015-04-03 01:14:29 +02006199 if (!(s->flags & SF_ERR_MASK))
6200 s->flags |= SF_ERR_CLICL;
6201 if (!(s->flags & SF_FINST_MASK))
6202 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006203
Willy Tarreau87b09662015-04-03 00:22:06 +02006204 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006205 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006206 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006207
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006208 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01006209 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006210 return 0;
6211 }
6212
6213 /* More interesting part now : we know that we have a complete
6214 * response which at least looks like HTTP. We have an indicator
6215 * of each header's length, so we can parse them quickly.
6216 */
6217
6218 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006219 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006220
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006221 /*
6222 * 1: get the status code
6223 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006224 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006225 if (n < 1 || n > 5)
6226 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02006227 /* when the client triggers a 4xx from the server, it's most often due
6228 * to a missing object or permission. These events should be tracked
6229 * because if they happen often, it may indicate a brute force or a
6230 * vulnerability scan.
6231 */
6232 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02006233 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02006234
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006235 if (objt_server(s->target))
6236 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006237
Willy Tarreau91852eb2015-05-01 13:26:00 +02006238 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
6239 * exactly one digit "." one digit. This check may be disabled using
6240 * option accept-invalid-http-response.
6241 */
6242 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
6243 if (msg->sl.st.v_l != 8) {
6244 msg->err_pos = 0;
6245 goto hdr_response_bad;
6246 }
6247
6248 if (rep->buf->p[4] != '/' ||
6249 !isdigit((unsigned char)rep->buf->p[5]) ||
6250 rep->buf->p[6] != '.' ||
6251 !isdigit((unsigned char)rep->buf->p[7])) {
6252 msg->err_pos = 4;
6253 goto hdr_response_bad;
6254 }
6255 }
6256
Willy Tarreau5b154472009-12-21 20:11:07 +01006257 /* check if the response is HTTP/1.1 or above */
6258 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006259 ((rep->buf->p[5] > '1') ||
6260 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006261 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006262
6263 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006264 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 +01006265
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006266 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006267 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006268
Willy Tarreau9b28e032012-10-12 23:49:43 +02006269 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006270
Willy Tarreau39650402010-03-15 19:44:39 +01006271 /* Adjust server's health based on status code. Note: status codes 501
6272 * and 505 are triggered on demand by client request, so we must not
6273 * count them as server failures.
6274 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006275 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006276 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006277 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006278 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006279 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006280 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006281
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006282 /*
6283 * 2: check for cacheability.
6284 */
6285
6286 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006287 case 100:
6288 /*
6289 * We may be facing a 100-continue response, in which case this
6290 * is not the right response, and we're waiting for the next one.
6291 * Let's allow this response to go to the client and wait for the
6292 * next one.
6293 */
6294 hdr_idx_init(&txn->hdr_idx);
6295 msg->next -= channel_forward(rep, msg->next);
6296 msg->msg_state = HTTP_MSG_RPBEFORE;
6297 txn->status = 0;
6298 s->logs.t_data = -1; /* was not a response yet */
6299 goto next_one;
6300
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006301 case 200:
6302 case 203:
6303 case 206:
6304 case 300:
6305 case 301:
6306 case 410:
6307 /* RFC2616 @13.4:
6308 * "A response received with a status code of
6309 * 200, 203, 206, 300, 301 or 410 MAY be stored
6310 * by a cache (...) unless a cache-control
6311 * directive prohibits caching."
6312 *
6313 * RFC2616 @9.5: POST method :
6314 * "Responses to this method are not cacheable,
6315 * unless the response includes appropriate
6316 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006317 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006318 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006319 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006320 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6321 break;
6322 default:
6323 break;
6324 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006325
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006326 /*
6327 * 3: we may need to capture headers
6328 */
6329 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006330 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006331 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006332 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006333
Willy Tarreau557f1992015-05-01 10:05:17 +02006334 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6335 * by RFC7230#3.3.3 :
6336 *
6337 * The length of a message body is determined by one of the following
6338 * (in order of precedence):
6339 *
6340 * 1. Any response to a HEAD request and any response with a 1xx
6341 * (Informational), 204 (No Content), or 304 (Not Modified) status
6342 * code is always terminated by the first empty line after the
6343 * header fields, regardless of the header fields present in the
6344 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006345 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006346 * 2. Any 2xx (Successful) response to a CONNECT request implies that
6347 * the connection will become a tunnel immediately after the empty
6348 * line that concludes the header fields. A client MUST ignore any
6349 * Content-Length or Transfer-Encoding header fields received in
6350 * such a message.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006351 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006352 * 3. If a Transfer-Encoding header field is present and the chunked
6353 * transfer coding (Section 4.1) is the final encoding, the message
6354 * body length is determined by reading and decoding the chunked
6355 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006356 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006357 * If a Transfer-Encoding header field is present in a response and
6358 * the chunked transfer coding is not the final encoding, the
6359 * message body length is determined by reading the connection until
6360 * it is closed by the server. If a Transfer-Encoding header field
6361 * is present in a request and the chunked transfer coding is not
6362 * the final encoding, the message body length cannot be determined
6363 * reliably; the server MUST respond with the 400 (Bad Request)
6364 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006365 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006366 * If a message is received with both a Transfer-Encoding and a
6367 * Content-Length header field, the Transfer-Encoding overrides the
6368 * Content-Length. Such a message might indicate an attempt to
6369 * perform request smuggling (Section 9.5) or response splitting
6370 * (Section 9.4) and ought to be handled as an error. A sender MUST
6371 * remove the received Content-Length field prior to forwarding such
6372 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006373 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006374 * 4. If a message is received without Transfer-Encoding and with
6375 * either multiple Content-Length header fields having differing
6376 * field-values or a single Content-Length header field having an
6377 * invalid value, then the message framing is invalid and the
6378 * recipient MUST treat it as an unrecoverable error. If this is a
6379 * request message, the server MUST respond with a 400 (Bad Request)
6380 * status code and then close the connection. If this is a response
6381 * message received by a proxy, the proxy MUST close the connection
6382 * to the server, discard the received response, and send a 502 (Bad
6383 * Gateway) response to the client. If this is a response message
6384 * received by a user agent, the user agent MUST close the
6385 * connection to the server and discard the received response.
6386 *
6387 * 5. If a valid Content-Length header field is present without
6388 * Transfer-Encoding, its decimal value defines the expected message
6389 * body length in octets. If the sender closes the connection or
6390 * the recipient times out before the indicated number of octets are
6391 * received, the recipient MUST consider the message to be
6392 * incomplete and close the connection.
6393 *
6394 * 6. If this is a request message and none of the above are true, then
6395 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006396 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006397 * 7. Otherwise, this is a response message without a declared message
6398 * body length, so the message body length is determined by the
6399 * number of octets received prior to the server closing the
6400 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006401 */
6402
6403 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006404 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006405 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006406 * FIXME: should we parse anyway and return an error on chunked encoding ?
6407 */
6408 if (txn->meth == HTTP_METH_HEAD ||
6409 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006410 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006411 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01006412 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006413 goto skip_content_length;
6414 }
6415
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006416 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006417 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02006418 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006419 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006420 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6421 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006422 /* bad transfer-encoding (chunked followed by something else) */
6423 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006424 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006425 break;
6426 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006427 }
6428
Willy Tarreau1c913912015-04-30 10:57:51 +02006429 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006430 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006431 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02006432 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6433 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6434 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006435 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006436 signed long long cl;
6437
Willy Tarreauad14f752011-09-02 20:33:27 +02006438 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006439 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006440 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006441 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006442
Willy Tarreauad14f752011-09-02 20:33:27 +02006443 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006444 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006445 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006446 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006447
Willy Tarreauad14f752011-09-02 20:33:27 +02006448 if (cl < 0) {
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 Tarreaua36fc4d2012-02-17 17:39:37 +01006453 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != 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; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006456 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006457
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006458 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006459 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006460 }
6461
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006462 if (sess->fe->comp || s->be->comp)
William Lallemand82fe75c2012-10-23 10:25:10 +02006463 select_compression_response_header(s, rep->buf);
6464
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006465skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006466 /* Now we have to check if we need to modify the Connection header.
6467 * This is more difficult on the response than it is on the request,
6468 * because we can have two different HTTP versions and we don't know
6469 * how the client will interprete a response. For instance, let's say
6470 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6471 * HTTP/1.1 response without any header. Maybe it will bound itself to
6472 * HTTP/1.0 because it only knows about it, and will consider the lack
6473 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6474 * the lack of header as a keep-alive. Thus we will use two flags
6475 * indicating how a request MAY be understood by the client. In case
6476 * of multiple possibilities, we'll fix the header to be explicit. If
6477 * ambiguous cases such as both close and keepalive are seen, then we
6478 * will fall back to explicit close. Note that we won't take risks with
6479 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006480 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006481 */
6482
Willy Tarreaudc008c52010-02-01 16:20:08 +01006483 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6484 txn->status == 101)) {
6485 /* Either we've established an explicit tunnel, or we're
6486 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006487 * to understand the next protocols. We have to switch to tunnel
6488 * mode, so that we transfer the request and responses then let
6489 * this protocol pass unmodified. When we later implement specific
6490 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006491 * header which contains information about that protocol for
6492 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006493 */
6494 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6495 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006496 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6497 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006498 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006499 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006500 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006501
Willy Tarreau70dffda2014-01-30 03:07:23 +01006502 /* this situation happens when combining pretend-keepalive with httpclose. */
6503 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006504 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006505 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006506 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6507
Willy Tarreau60466522010-01-18 19:08:45 +01006508 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006509 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006510 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6511 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006512
Willy Tarreau60466522010-01-18 19:08:45 +01006513 /* now adjust header transformations depending on current state */
6514 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6515 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6516 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006517 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006518 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006519 }
Willy Tarreau60466522010-01-18 19:08:45 +01006520 else { /* SCL / KAL */
6521 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006522 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006523 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006524 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006525
Willy Tarreau60466522010-01-18 19:08:45 +01006526 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006527 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006528
Willy Tarreau60466522010-01-18 19:08:45 +01006529 /* Some keep-alive responses are converted to Server-close if
6530 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006531 */
Willy Tarreau60466522010-01-18 19:08:45 +01006532 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6533 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006534 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006535 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006536 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006537 }
6538
Willy Tarreau7959a552013-09-23 16:44:27 +02006539 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006540 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006541
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006542 /* end of job, return OK */
6543 rep->analysers &= ~an_bit;
6544 rep->analyse_exp = TICK_ETERNITY;
6545 channel_auto_close(rep);
6546 return 1;
6547
6548 abort_keep_alive:
6549 /* A keep-alive request to the server failed on a network error.
6550 * The client is required to retry. We need to close without returning
6551 * any other information so that the client retries.
6552 */
6553 txn->status = 0;
6554 rep->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006555 s->req.analysers = 0;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006556 channel_auto_close(rep);
6557 s->logs.logwait = 0;
6558 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006559 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006560 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006561 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006562 return 0;
6563}
6564
6565/* This function performs all the processing enabled for the current response.
6566 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006567 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006568 * other functions. It works like process_request (see indications above).
6569 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006570int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006571{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006572 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006573 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006574 struct http_msg *msg = &txn->rsp;
6575 struct proxy *cur_proxy;
6576 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006577 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006578
Willy Tarreau87b09662015-04-03 00:22:06 +02006579 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 +02006580 now_ms, __FUNCTION__,
6581 s,
6582 rep,
6583 rep->rex, rep->wex,
6584 rep->flags,
6585 rep->buf->i,
6586 rep->analysers);
6587
6588 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6589 return 0;
6590
Willy Tarreau70730dd2014-04-24 18:06:27 +02006591 /* The stats applet needs to adjust the Connection header but we don't
6592 * apply any filter there.
6593 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006594 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6595 rep->analysers &= ~an_bit;
6596 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006597 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006598 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006599
Willy Tarreau58975672014-04-24 21:13:57 +02006600 /*
6601 * We will have to evaluate the filters.
6602 * As opposed to version 1.2, now they will be evaluated in the
6603 * filters order and not in the header order. This means that
6604 * each filter has to be validated among all headers.
6605 *
6606 * Filters are tried with ->be first, then with ->fe if it is
6607 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006608 *
6609 * Maybe we are in resume condiion. In this case I choose the
6610 * "struct proxy" which contains the rule list matching the resume
6611 * pointer. If none of theses "struct proxy" match, I initialise
6612 * the process with the first one.
6613 *
6614 * In fact, I check only correspondance betwwen the current list
6615 * pointer and the ->fe rule list. If it doesn't match, I initialize
6616 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006617 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006618 if (s->current_rule_list == &sess->fe->http_res_rules)
6619 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006620 else
6621 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006622 while (1) {
6623 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006624
Willy Tarreau58975672014-04-24 21:13:57 +02006625 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02006626 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02006627 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006628
Willy Tarreau51d861a2015-05-22 17:30:48 +02006629 if (ret == HTTP_RULE_RES_BADREQ)
6630 goto return_srv_prx_502;
6631
6632 if (ret == HTTP_RULE_RES_DONE) {
6633 rep->analysers &= ~an_bit;
6634 rep->analyse_exp = TICK_ETERNITY;
6635 return 1;
6636 }
6637 }
6638
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006639 /* we need to be called again. */
6640 if (ret == HTTP_RULE_RES_YIELD) {
6641 channel_dont_close(rep);
6642 return 0;
6643 }
6644
Willy Tarreau58975672014-04-24 21:13:57 +02006645 /* try headers filters */
6646 if (rule_set->rsp_exp != NULL) {
6647 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6648 return_bad_resp:
6649 if (objt_server(s->target)) {
6650 objt_server(s->target)->counters.failed_resp++;
6651 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006652 }
Willy Tarreau58975672014-04-24 21:13:57 +02006653 s->be->be_counters.failed_resp++;
6654 return_srv_prx_502:
6655 rep->analysers = 0;
6656 txn->status = 502;
6657 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006658 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006659 channel_truncate(rep);
Willy Tarreau350f4872014-11-28 14:42:25 +01006660 stream_int_retnclose(&s->si[0], http_error_message(s, HTTP_ERR_502));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006661 if (!(s->flags & SF_ERR_MASK))
6662 s->flags |= SF_ERR_PRXCOND;
6663 if (!(s->flags & SF_FINST_MASK))
6664 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006665 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006666 }
Willy Tarreau58975672014-04-24 21:13:57 +02006667 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006668
Willy Tarreau58975672014-04-24 21:13:57 +02006669 /* has the response been denied ? */
6670 if (txn->flags & TX_SVDENY) {
6671 if (objt_server(s->target))
6672 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006673
Willy Tarreau58975672014-04-24 21:13:57 +02006674 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006675 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006676 if (sess->listener->counters)
6677 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006678
Willy Tarreau58975672014-04-24 21:13:57 +02006679 goto return_srv_prx_502;
6680 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006681
Willy Tarreau58975672014-04-24 21:13:57 +02006682 /* add response headers from the rule sets in the same order */
6683 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006684 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006685 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006686 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006687 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02006688 ret = acl_pass(ret);
6689 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6690 ret = !ret;
6691 if (!ret)
6692 continue;
6693 }
6694 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6695 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006696 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006697
Willy Tarreau58975672014-04-24 21:13:57 +02006698 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006699 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006700 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006701 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006702 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006703
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006704 /* After this point, this anayzer can't return yield, so we can
6705 * remove the bit corresponding to this analyzer from the list.
6706 *
6707 * Note that the intermediate returns and goto found previously
6708 * reset the analyzers.
6709 */
6710 rep->analysers &= ~an_bit;
6711 rep->analyse_exp = TICK_ETERNITY;
6712
Willy Tarreau58975672014-04-24 21:13:57 +02006713 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006714 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006715 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006716
Willy Tarreau58975672014-04-24 21:13:57 +02006717 /*
6718 * Now check for a server cookie.
6719 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006720 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02006721 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006722
Willy Tarreau58975672014-04-24 21:13:57 +02006723 /*
6724 * Check for cache-control or pragma headers if required.
6725 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006726 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 +02006727 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006728
Willy Tarreau58975672014-04-24 21:13:57 +02006729 /*
6730 * Add server cookie in the response if needed
6731 */
6732 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6733 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006734 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006735 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6736 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6737 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6738 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6739 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006740 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006741 /* the server is known, it's not the one the client requested, or the
6742 * cookie's last seen date needs to be refreshed. We have to
6743 * insert a set-cookie here, except if we want to insert only on POST
6744 * requests and this one isn't. Note that servers which don't have cookies
6745 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006746 */
Willy Tarreau58975672014-04-24 21:13:57 +02006747 if (!objt_server(s->target)->cookie) {
6748 chunk_printf(&trash,
6749 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6750 s->be->cookie_name);
6751 }
6752 else {
6753 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006754
Willy Tarreau58975672014-04-24 21:13:57 +02006755 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6756 /* emit last_date, which is mandatory */
6757 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6758 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6759 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006760
Willy Tarreau58975672014-04-24 21:13:57 +02006761 if (s->be->cookie_maxlife) {
6762 /* emit first_date, which is either the original one or
6763 * the current date.
6764 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006765 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006766 s30tob64(txn->cookie_first_date ?
6767 txn->cookie_first_date >> 2 :
6768 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006769 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006770 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006771 }
Willy Tarreau58975672014-04-24 21:13:57 +02006772 chunk_appendf(&trash, "; path=/");
6773 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006774
Willy Tarreau58975672014-04-24 21:13:57 +02006775 if (s->be->cookie_domain)
6776 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006777
Willy Tarreau58975672014-04-24 21:13:57 +02006778 if (s->be->ck_opts & PR_CK_HTTPONLY)
6779 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006780
Willy Tarreau58975672014-04-24 21:13:57 +02006781 if (s->be->ck_opts & PR_CK_SECURE)
6782 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006783
Willy Tarreau58975672014-04-24 21:13:57 +02006784 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6785 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006786
Willy Tarreau58975672014-04-24 21:13:57 +02006787 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006788 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006789 /* the server did not change, only the date was updated */
6790 txn->flags |= TX_SCK_UPDATED;
6791 else
6792 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006793
Willy Tarreau58975672014-04-24 21:13:57 +02006794 /* Here, we will tell an eventual cache on the client side that we don't
6795 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6796 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6797 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006798 */
Willy Tarreau58975672014-04-24 21:13:57 +02006799 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006800
Willy Tarreau58975672014-04-24 21:13:57 +02006801 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006802
Willy Tarreau58975672014-04-24 21:13:57 +02006803 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6804 "Cache-control: private", 22) < 0))
6805 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006806 }
Willy Tarreau58975672014-04-24 21:13:57 +02006807 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006808
Willy Tarreau58975672014-04-24 21:13:57 +02006809 /*
6810 * Check if result will be cacheable with a cookie.
6811 * We'll block the response if security checks have caught
6812 * nasty things such as a cacheable cookie.
6813 */
6814 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6815 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6816 (s->be->options & PR_O_CHK_CACHE)) {
6817 /* we're in presence of a cacheable response containing
6818 * a set-cookie header. We'll block it as requested by
6819 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006820 */
Willy Tarreau58975672014-04-24 21:13:57 +02006821 if (objt_server(s->target))
6822 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006823
Willy Tarreau58975672014-04-24 21:13:57 +02006824 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006825 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006826 if (sess->listener->counters)
6827 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006828
Willy Tarreau58975672014-04-24 21:13:57 +02006829 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6830 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6831 send_log(s->be, LOG_ALERT,
6832 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6833 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6834 goto return_srv_prx_502;
6835 }
Willy Tarreau03945942009-12-22 16:50:27 +01006836
Willy Tarreau70730dd2014-04-24 18:06:27 +02006837 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006838 /*
6839 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6840 * If an "Upgrade" token is found, the header is left untouched in order
6841 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006842 * if anything but "Upgrade" is present in the Connection header. We don't
6843 * want to touch any 101 response either since it's switching to another
6844 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006845 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006846 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006847 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006848 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006849 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6850 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006851
Willy Tarreau58975672014-04-24 21:13:57 +02006852 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6853 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6854 /* we want a keep-alive response here. Keep-alive header
6855 * required if either side is not 1.1.
6856 */
6857 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6858 want_flags |= TX_CON_KAL_SET;
6859 }
6860 else {
6861 /* we want a close response here. Close header required if
6862 * the server is 1.1, regardless of the client.
6863 */
6864 if (msg->flags & HTTP_MSGF_VER_11)
6865 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006866 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006867
Willy Tarreau58975672014-04-24 21:13:57 +02006868 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6869 http_change_connection_header(txn, msg, want_flags);
6870 }
6871
6872 skip_header_mangling:
6873 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6874 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6875 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006876
Willy Tarreau58975672014-04-24 21:13:57 +02006877 /* if the user wants to log as soon as possible, without counting
6878 * bytes from the server, then this is the right moment. We have
6879 * to temporarily assign bytes_out to log what we currently have.
6880 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006881 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006882 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6883 s->logs.bytes_out = txn->rsp.eoh;
6884 s->do_log(s);
6885 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006886 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006887 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006888}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006889
Willy Tarreaud98cf932009-12-27 22:54:55 +01006890/* This function is an analyser which forwards response body (including chunk
6891 * sizes if any). It is called as soon as we must forward, even if we forward
6892 * zero byte. The only situation where it must not be called is when we're in
6893 * tunnel mode and we want to forward till the close. It's used both to forward
6894 * remaining data and to resync after end of body. It expects the msg_state to
6895 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006896 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006897 *
6898 * It is capable of compressing response data both in content-length mode and
6899 * in chunked mode. The state machines follows different flows depending on
6900 * whether content-length and chunked modes are used, since there are no
6901 * trailers in content-length :
6902 *
6903 * chk-mode cl-mode
6904 * ,----- BODY -----.
6905 * / \
6906 * V size > 0 V chk-mode
6907 * .--> SIZE -------------> DATA -------------> CRLF
6908 * | | size == 0 | last byte |
6909 * | v final crlf v inspected |
6910 * | TRAILERS -----------> DONE |
6911 * | |
6912 * `----------------------------------------------'
6913 *
6914 * Compression only happens in the DATA state, and must be flushed in final
6915 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6916 * is performed at once on final states for all bytes parsed, or when leaving
6917 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006918 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006919int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006920{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006921 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006922 struct http_txn *txn = s->txn;
6923 struct http_msg *msg = &s->txn->rsp;
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006924 static struct buffer *tmpbuf = &buf_empty;
William Lallemand82fe75c2012-10-23 10:25:10 +02006925 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006926 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006927
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006928 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6929 return 0;
6930
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006931 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006932 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006933 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006934 /* Output closed while we were sending data. We must abort and
6935 * wake the other side up.
6936 */
6937 msg->msg_state = HTTP_MSG_ERROR;
6938 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006939 return 1;
6940 }
6941
Willy Tarreau4fe41902010-06-07 22:27:41 +02006942 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006943 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006944
Willy Tarreaubb2e6692014-07-10 19:06:10 +02006945 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006946 /* we have msg->sov which points to the first byte of message
6947 * body, and res->buf.p still points to the beginning of the
6948 * message. We forward the headers now, as we don't need them
6949 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006950 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006951 b_adv(res->buf, msg->sov);
6952 msg->next -= msg->sov;
6953 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006954
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006955 /* The previous analysers guarantee that the state is somewhere
6956 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6957 * msg->next are always correct.
6958 */
6959 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6960 if (msg->flags & HTTP_MSGF_TE_CHNK)
6961 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6962 else
6963 msg->msg_state = HTTP_MSG_DATA;
6964 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006965 }
6966
Willy Tarreauefdf0942014-04-24 20:08:57 +02006967 if (res->to_forward) {
6968 /* We can't process the buffer's contents yet */
6969 res->flags |= CF_WAKE_WRITE;
6970 goto missing_data;
6971 }
6972
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006973 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6974 /* We need a compression buffer in the DATA state to put the
6975 * output of compressed data, and in CRLF state to let the
6976 * TRAILERS state finish the job of removing the trailing CRLF.
6977 */
Willy Tarreauf2f7d6b2014-11-24 11:55:08 +01006978 if (unlikely(!tmpbuf->size)) {
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006979 /* this is the first time we need the compression buffer */
Willy Tarreaue583ea52014-11-24 11:30:16 +01006980 if (b_alloc(&tmpbuf) == NULL)
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006981 goto aborted_xfer; /* no memory */
6982 }
6983
6984 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006985 if (ret < 0) {
6986 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006987 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006988 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006989 compressing = 1;
6990 }
6991
Willy Tarreaud98cf932009-12-27 22:54:55 +01006992 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006993 switch (msg->msg_state - HTTP_MSG_DATA) {
6994 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006995 /* we may have some pending data starting at res->buf->p */
6996 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02006997 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01006998 if (ret < 0)
6999 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02007000
Willy Tarreaud5a67832014-04-21 10:54:27 +02007001 if (msg->chunk_len) {
7002 /* input empty or output full */
7003 if (res->buf->i > msg->next)
7004 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02007005 goto missing_data;
7006 }
William Lallemandbf3ae612012-11-19 12:35:37 +01007007 }
Willy Tarreauc623c172014-04-18 09:53:50 +02007008 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02007009 if (msg->chunk_len > res->buf->i - msg->next) {
7010 /* output full */
7011 res->flags |= CF_WAKE_WRITE;
7012 goto missing_data;
7013 }
Willy Tarreauc623c172014-04-18 09:53:50 +02007014 msg->next += msg->chunk_len;
7015 msg->chunk_len = 0;
7016 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01007017
7018 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01007019 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02007020 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01007021 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01007022 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007023 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01007024 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007025 /* fall through for HTTP_MSG_CHUNK_CRLF */
7026
7027 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
7028 /* we want the CRLF after the data */
7029
7030 ret = http_skip_chunk_crlf(msg);
7031 if (ret == 0)
7032 goto missing_data;
7033 else if (ret < 0) {
7034 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007035 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, sess->fe);
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007036 goto return_bad_res;
7037 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007038 /* we're in MSG_CHUNK_SIZE now, fall through */
7039
7040 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01007041 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02007042 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01007043 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01007044 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01007045
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007046 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02007047 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01007048 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007049 else if (ret < 0) {
7050 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007051 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007052 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007053 }
Willy Tarreau0161d622013-04-02 01:26:55 +02007054 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007055 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01007056
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007057 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02007058 if (unlikely(compressing)) {
7059 /* we need to flush output contents before syncing FSMs */
7060 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
7061 compressing = 0;
7062 }
7063
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007064 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007065 if (ret == 0)
7066 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007067 else if (ret < 0) {
7068 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007069 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, sess->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007070 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007071 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02007072 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007073
7074 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01007075 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02007076 if (unlikely(compressing)) {
7077 /* we need to flush output contents before syncing FSMs */
7078 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
7079 compressing = 0;
7080 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007081
Willy Tarreauc623c172014-04-18 09:53:50 +02007082 /* we may have some pending data starting at res->buf->p
7083 * such as a last chunk of data or trailers.
7084 */
7085 b_adv(res->buf, msg->next);
7086 msg->next = 0;
7087
Willy Tarreaud655ffe2013-04-02 01:48:58 +02007088 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02007089 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007090 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
7091 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02007092 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02007093
Willy Tarreau610ecce2010-01-04 21:15:02 +01007094 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01007095 /* some state changes occurred, maybe the analyser
7096 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01007097 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007098 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007099 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007100 /* response errors are most likely due to
7101 * the client aborting the transfer.
7102 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007103 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007104 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01007105 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007106 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, sess->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01007107 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01007108 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01007109 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01007110 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01007111 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007112 }
7113 }
7114
Willy Tarreaud98cf932009-12-27 22:54:55 +01007115 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02007116 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02007117 if (unlikely(compressing)) {
7118 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02007119 compressing = 0;
7120 }
Willy Tarreauf003d372012-11-26 13:35:37 +01007121
Willy Tarreauc623c172014-04-18 09:53:50 +02007122 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
7123 b_adv(res->buf, msg->next);
7124 msg->next = 0;
7125 msg->chunk_len -= channel_forward(res, msg->chunk_len);
7126 }
7127
Willy Tarreauf003d372012-11-26 13:35:37 +01007128 if (res->flags & CF_SHUTW)
7129 goto aborted_xfer;
7130
7131 /* stop waiting for data if the input is closed before the end. If the
7132 * client side was already closed, it means that the client has aborted,
7133 * so we don't want to count this as a server abort. Otherwise it's a
7134 * server abort.
7135 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007136 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007137 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01007138 goto aborted_xfer;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007139 if (!(s->flags & SF_ERR_MASK))
7140 s->flags |= SF_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007141 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007142 if (objt_server(s->target))
7143 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007144 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01007145 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01007146
Willy Tarreau40dba092010-03-04 18:14:51 +01007147 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007148 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01007149 goto return_bad_res;
7150
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007151 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007152 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007153 * Similarly, with keep-alive on the client side, we don't want to forward a
7154 * close.
7155 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02007156 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007157 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
7158 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02007159 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02007160
Willy Tarreau5c620922011-05-11 19:56:11 +02007161 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007162 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02007163 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01007164 * modes are already handled by the stream sock layer. We must not do
7165 * this in content-length mode because it could present the MSG_MORE
7166 * flag with the last block of forwarded data, which would cause an
7167 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02007168 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02007169 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007170 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02007171
Willy Tarreau87b09662015-04-03 00:22:06 +02007172 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01007173 return 0;
7174
Willy Tarreau40dba092010-03-04 18:14:51 +01007175 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007176 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007177 if (objt_server(s->target))
7178 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007179
7180 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02007181 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02007182 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02007183 compressing = 0;
7184 }
7185
Willy Tarreauc623c172014-04-18 09:53:50 +02007186 /* we may have some pending data starting at res->buf->p */
7187 if (s->comp_algo == NULL) {
7188 b_adv(res->buf, msg->next);
7189 msg->next = 0;
7190 }
7191
Willy Tarreaud98cf932009-12-27 22:54:55 +01007192 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01007193 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01007194 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007195 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007196 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007197 if (objt_server(s->target))
7198 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007199
Willy Tarreaue7dff022015-04-03 01:14:29 +02007200 if (!(s->flags & SF_ERR_MASK))
7201 s->flags |= SF_ERR_PRXCOND;
7202 if (!(s->flags & SF_FINST_MASK))
7203 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007204 return 0;
7205
7206 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02007207 if (unlikely(compressing)) {
7208 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
7209 compressing = 0;
7210 }
7211
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007212 txn->rsp.msg_state = HTTP_MSG_ERROR;
7213 /* don't send any error message as we're in the body */
Willy Tarreau350f4872014-11-28 14:42:25 +01007214 stream_int_retnclose(&s->si[0], NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007215 res->analysers = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007216 s->req.analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007217
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007218 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007219 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007220 if (objt_server(s->target))
7221 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007222
Willy Tarreaue7dff022015-04-03 01:14:29 +02007223 if (!(s->flags & SF_ERR_MASK))
7224 s->flags |= SF_ERR_CLICL;
7225 if (!(s->flags & SF_FINST_MASK))
7226 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007227 return 0;
7228}
7229
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007230/* Iterate the same filter through all request headers.
7231 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007232 * Since it can manage the switch to another backend, it updates the per-proxy
7233 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007234 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007235int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007236{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007237 char *cur_ptr, *cur_end, *cur_next;
7238 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007239 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007240 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007241 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01007242
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007243 last_hdr = 0;
7244
Willy Tarreau9b28e032012-10-12 23:49:43 +02007245 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007246 old_idx = 0;
7247
7248 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007249 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007250 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007251 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007252 (exp->action == ACT_ALLOW ||
7253 exp->action == ACT_DENY ||
7254 exp->action == ACT_TARPIT))
7255 return 0;
7256
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007257 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007258 if (!cur_idx)
7259 break;
7260
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007261 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007262 cur_ptr = cur_next;
7263 cur_end = cur_ptr + cur_hdr->len;
7264 cur_next = cur_end + cur_hdr->cr + 1;
7265
7266 /* Now we have one header between cur_ptr and cur_end,
7267 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007268 */
7269
Willy Tarreau15a53a42015-01-21 13:39:42 +01007270 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007271 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007272 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007273 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007274 last_hdr = 1;
7275 break;
7276
7277 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007278 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007279 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007280 break;
7281
7282 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007283 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007284 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007285 break;
7286
7287 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007288 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7289 if (trash.len < 0)
7290 return -1;
7291
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007292 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007293 /* FIXME: if the user adds a newline in the replacement, the
7294 * index will not be recalculated for now, and the new line
7295 * will not be counted as a new header.
7296 */
7297
7298 cur_end += delta;
7299 cur_next += delta;
7300 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007301 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007302 break;
7303
7304 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007305 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007306 cur_next += delta;
7307
Willy Tarreaufa355d42009-11-29 18:12:29 +01007308 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007309 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7310 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007311 cur_hdr->len = 0;
7312 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007313 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007314 break;
7315
7316 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007317 }
7318
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007319 /* keep the link from this header to next one in case of later
7320 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007321 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007322 old_idx = cur_idx;
7323 }
7324 return 0;
7325}
7326
7327
7328/* Apply the filter to the request line.
7329 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7330 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007331 * Since it can manage the switch to another backend, it updates the per-proxy
7332 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007333 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007334int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007335{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007336 char *cur_ptr, *cur_end;
7337 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007338 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007339 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007340
Willy Tarreau3d300592007-03-18 18:34:41 +01007341 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007342 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007343 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007344 (exp->action == ACT_ALLOW ||
7345 exp->action == ACT_DENY ||
7346 exp->action == ACT_TARPIT))
7347 return 0;
7348 else if (exp->action == ACT_REMOVE)
7349 return 0;
7350
7351 done = 0;
7352
Willy Tarreau9b28e032012-10-12 23:49:43 +02007353 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007354 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007355
7356 /* Now we have the request line between cur_ptr and cur_end */
7357
Willy Tarreau15a53a42015-01-21 13:39:42 +01007358 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007359 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007360 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007361 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007362 done = 1;
7363 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007364
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007365 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007366 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007367 done = 1;
7368 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007369
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007370 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007371 txn->flags |= TX_CLTARPIT;
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_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007376 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7377 if (trash.len < 0)
7378 return -1;
7379
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007380 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007381 /* FIXME: if the user adds a newline in the replacement, the
7382 * index will not be recalculated for now, and the new line
7383 * will not be counted as a new header.
7384 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007385
Willy Tarreaufa355d42009-11-29 18:12:29 +01007386 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007387 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007388 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007389 HTTP_MSG_RQMETH,
7390 cur_ptr, cur_end + 1,
7391 NULL, NULL);
7392 if (unlikely(!cur_end))
7393 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007394
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007395 /* we have a full request and we know that we have either a CR
7396 * or an LF at <ptr>.
7397 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007398 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7399 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007400 /* there is no point trying this regex on headers */
7401 return 1;
7402 }
7403 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007404 return done;
7405}
Willy Tarreau97de6242006-12-27 17:18:38 +01007406
Willy Tarreau58f10d72006-12-04 02:26:12 +01007407
Willy Tarreau58f10d72006-12-04 02:26:12 +01007408
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007409/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007410 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007411 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007412 * unparsable request. Since it can manage the switch to another backend, it
7413 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007414 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007415int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007416{
Willy Tarreau192252e2015-04-04 01:47:55 +02007417 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007418 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007419 struct hdr_exp *exp;
7420
7421 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007422 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007423
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007424 /*
7425 * The interleaving of transformations and verdicts
7426 * makes it difficult to decide to continue or stop
7427 * the evaluation.
7428 */
7429
Willy Tarreau6c123b12010-01-28 20:22:06 +01007430 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7431 break;
7432
Willy Tarreau3d300592007-03-18 18:34:41 +01007433 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007434 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007435 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007436 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007437
7438 /* if this filter had a condition, evaluate it now and skip to
7439 * next filter if the condition does not match.
7440 */
7441 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007442 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007443 ret = acl_pass(ret);
7444 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7445 ret = !ret;
7446
7447 if (!ret)
7448 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007449 }
7450
7451 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007452 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007453 if (unlikely(ret < 0))
7454 return -1;
7455
7456 if (likely(ret == 0)) {
7457 /* The filter did not match the request, it can be
7458 * iterated through all headers.
7459 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007460 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7461 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007462 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007463 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007464 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007465}
7466
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007467
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007468/* Find the end of a cookie value contained between <s> and <e>. It works the
7469 * same way as with headers above except that the semi-colon also ends a token.
7470 * See RFC2965 for more information. Note that it requires a valid header to
7471 * return a valid result.
7472 */
7473char *find_cookie_value_end(char *s, const char *e)
7474{
7475 int quoted, qdpair;
7476
7477 quoted = qdpair = 0;
7478 for (; s < e; s++) {
7479 if (qdpair) qdpair = 0;
7480 else if (quoted) {
7481 if (*s == '\\') qdpair = 1;
7482 else if (*s == '"') quoted = 0;
7483 }
7484 else if (*s == '"') quoted = 1;
7485 else if (*s == ',' || *s == ';') return s;
7486 }
7487 return s;
7488}
7489
7490/* Delete a value in a header between delimiters <from> and <next> in buffer
7491 * <buf>. The number of characters displaced is returned, and the pointer to
7492 * the first delimiter is updated if required. The function tries as much as
7493 * possible to respect the following principles :
7494 * - replace <from> delimiter by the <next> one unless <from> points to a
7495 * colon, in which case <next> is simply removed
7496 * - set exactly one space character after the new first delimiter, unless
7497 * there are not enough characters in the block being moved to do so.
7498 * - remove unneeded spaces before the previous delimiter and after the new
7499 * one.
7500 *
7501 * It is the caller's responsibility to ensure that :
7502 * - <from> points to a valid delimiter or the colon ;
7503 * - <next> points to a valid delimiter or the final CR/LF ;
7504 * - there are non-space chars before <from> ;
7505 * - there is a CR/LF at or after <next>.
7506 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007507int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007508{
7509 char *prev = *from;
7510
7511 if (*prev == ':') {
7512 /* We're removing the first value, preserve the colon and add a
7513 * space if possible.
7514 */
7515 if (!http_is_crlf[(unsigned char)*next])
7516 next++;
7517 prev++;
7518 if (prev < next)
7519 *prev++ = ' ';
7520
7521 while (http_is_spht[(unsigned char)*next])
7522 next++;
7523 } else {
7524 /* Remove useless spaces before the old delimiter. */
7525 while (http_is_spht[(unsigned char)*(prev-1)])
7526 prev--;
7527 *from = prev;
7528
7529 /* copy the delimiter and if possible a space if we're
7530 * not at the end of the line.
7531 */
7532 if (!http_is_crlf[(unsigned char)*next]) {
7533 *prev++ = *next++;
7534 if (prev + 1 < next)
7535 *prev++ = ' ';
7536 while (http_is_spht[(unsigned char)*next])
7537 next++;
7538 }
7539 }
7540 return buffer_replace2(buf, prev, next, NULL, 0);
7541}
7542
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007543/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007544 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007545 * desirable to call it only when needed. This code is quite complex because
7546 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7547 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007548 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007549void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007550{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007551 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007552 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007553 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007554 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007555 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7556 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007557
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007558 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007559 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007560 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007561
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007562 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007563 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007564 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007565
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007566 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007567 hdr_beg = hdr_next;
7568 hdr_end = hdr_beg + cur_hdr->len;
7569 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007570
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007571 /* We have one full header between hdr_beg and hdr_end, and the
7572 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007573 * "Cookie:" headers.
7574 */
7575
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007576 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007577 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007578 old_idx = cur_idx;
7579 continue;
7580 }
7581
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007582 del_from = NULL; /* nothing to be deleted */
7583 preserve_hdr = 0; /* assume we may kill the whole header */
7584
Willy Tarreau58f10d72006-12-04 02:26:12 +01007585 /* Now look for cookies. Conforming to RFC2109, we have to support
7586 * attributes whose name begin with a '$', and associate them with
7587 * the right cookie, if we want to delete this cookie.
7588 * So there are 3 cases for each cookie read :
7589 * 1) it's a special attribute, beginning with a '$' : ignore it.
7590 * 2) it's a server id cookie that we *MAY* want to delete : save
7591 * some pointers on it (last semi-colon, beginning of cookie...)
7592 * 3) it's an application cookie : we *MAY* have to delete a previous
7593 * "special" cookie.
7594 * At the end of loop, if a "special" cookie remains, we may have to
7595 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007596 * *MUST* delete it.
7597 *
7598 * Note: RFC2965 is unclear about the processing of spaces around
7599 * the equal sign in the ATTR=VALUE form. A careful inspection of
7600 * the RFC explicitly allows spaces before it, and not within the
7601 * tokens (attrs or values). An inspection of RFC2109 allows that
7602 * too but section 10.1.3 lets one think that spaces may be allowed
7603 * after the equal sign too, resulting in some (rare) buggy
7604 * implementations trying to do that. So let's do what servers do.
7605 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7606 * allowed quoted strings in values, with any possible character
7607 * after a backslash, including control chars and delimitors, which
7608 * causes parsing to become ambiguous. Browsers also allow spaces
7609 * within values even without quotes.
7610 *
7611 * We have to keep multiple pointers in order to support cookie
7612 * removal at the beginning, middle or end of header without
7613 * corrupting the header. All of these headers are valid :
7614 *
7615 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7616 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7617 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7618 * | | | | | | | | |
7619 * | | | | | | | | hdr_end <--+
7620 * | | | | | | | +--> next
7621 * | | | | | | +----> val_end
7622 * | | | | | +-----------> val_beg
7623 * | | | | +--------------> equal
7624 * | | | +----------------> att_end
7625 * | | +---------------------> att_beg
7626 * | +--------------------------> prev
7627 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007628 */
7629
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007630 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7631 /* Iterate through all cookies on this line */
7632
7633 /* find att_beg */
7634 att_beg = prev + 1;
7635 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7636 att_beg++;
7637
7638 /* find att_end : this is the first character after the last non
7639 * space before the equal. It may be equal to hdr_end.
7640 */
7641 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007642
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007643 while (equal < hdr_end) {
7644 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007645 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007646 if (http_is_spht[(unsigned char)*equal++])
7647 continue;
7648 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007649 }
7650
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007651 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7652 * is between <att_beg> and <equal>, both may be identical.
7653 */
7654
7655 /* look for end of cookie if there is an equal sign */
7656 if (equal < hdr_end && *equal == '=') {
7657 /* look for the beginning of the value */
7658 val_beg = equal + 1;
7659 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7660 val_beg++;
7661
7662 /* find the end of the value, respecting quotes */
7663 next = find_cookie_value_end(val_beg, hdr_end);
7664
7665 /* make val_end point to the first white space or delimitor after the value */
7666 val_end = next;
7667 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7668 val_end--;
7669 } else {
7670 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007671 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007672
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007673 /* We have nothing to do with attributes beginning with '$'. However,
7674 * they will automatically be removed if a header before them is removed,
7675 * since they're supposed to be linked together.
7676 */
7677 if (*att_beg == '$')
7678 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007679
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007680 /* Ignore cookies with no equal sign */
7681 if (equal == next) {
7682 /* This is not our cookie, so we must preserve it. But if we already
7683 * scheduled another cookie for removal, we cannot remove the
7684 * complete header, but we can remove the previous block itself.
7685 */
7686 preserve_hdr = 1;
7687 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007688 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007689 val_end += delta;
7690 next += delta;
7691 hdr_end += delta;
7692 hdr_next += delta;
7693 cur_hdr->len += delta;
7694 http_msg_move_end(&txn->req, delta);
7695 prev = del_from;
7696 del_from = NULL;
7697 }
7698 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007699 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007700
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007701 /* if there are spaces around the equal sign, we need to
7702 * strip them otherwise we'll get trouble for cookie captures,
7703 * or even for rewrites. Since this happens extremely rarely,
7704 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007705 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007706 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7707 int stripped_before = 0;
7708 int stripped_after = 0;
7709
7710 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007711 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007712 equal += stripped_before;
7713 val_beg += stripped_before;
7714 }
7715
7716 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007717 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007718 val_beg += stripped_after;
7719 stripped_before += stripped_after;
7720 }
7721
7722 val_end += stripped_before;
7723 next += stripped_before;
7724 hdr_end += stripped_before;
7725 hdr_next += stripped_before;
7726 cur_hdr->len += stripped_before;
7727 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007728 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007729 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007730
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007731 /* First, let's see if we want to capture this cookie. We check
7732 * that we don't already have a client side cookie, because we
7733 * can only capture one. Also as an optimisation, we ignore
7734 * cookies shorter than the declared name.
7735 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007736 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7737 (val_end - att_beg >= sess->fe->capture_namelen) &&
7738 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007739 int log_len = val_end - att_beg;
7740
7741 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7742 Alert("HTTP logging : out of memory.\n");
7743 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007744 if (log_len > sess->fe->capture_len)
7745 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007746 memcpy(txn->cli_cookie, att_beg, log_len);
7747 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007748 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007749 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007750
Willy Tarreaubca99692010-10-06 19:25:55 +02007751 /* Persistence cookies in passive, rewrite or insert mode have the
7752 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007753 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007754 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007755 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007756 * For cookies in prefix mode, the form is :
7757 *
7758 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007759 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007760 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7761 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7762 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007763 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007764
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007765 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7766 * have the server ID between val_beg and delim, and the original cookie between
7767 * delim+1 and val_end. Otherwise, delim==val_end :
7768 *
7769 * Cookie: NAME=SRV; # in all but prefix modes
7770 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7771 * | || || | |+-> next
7772 * | || || | +--> val_end
7773 * | || || +---------> delim
7774 * | || |+------------> val_beg
7775 * | || +-------------> att_end = equal
7776 * | |+-----------------> att_beg
7777 * | +------------------> prev
7778 * +-------------------------> hdr_beg
7779 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007780
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007781 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007782 for (delim = val_beg; delim < val_end; delim++)
7783 if (*delim == COOKIE_DELIM)
7784 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007785 } else {
7786 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007787 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007788 /* Now check if the cookie contains a date field, which would
7789 * appear after a vertical bar ('|') just after the server name
7790 * and before the delimiter.
7791 */
7792 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7793 if (vbar1) {
7794 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007795 * right is the last seen date. It is a base64 encoded
7796 * 30-bit value representing the UNIX date since the
7797 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007798 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007799 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007800 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007801 if (val_end - vbar1 >= 5) {
7802 val = b64tos30(vbar1);
7803 if (val > 0)
7804 txn->cookie_last_date = val << 2;
7805 }
7806 /* look for a second vertical bar */
7807 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7808 if (vbar1 && (val_end - vbar1 > 5)) {
7809 val = b64tos30(vbar1 + 1);
7810 if (val > 0)
7811 txn->cookie_first_date = val << 2;
7812 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007813 }
7814 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007815
Willy Tarreauf64d1412010-10-07 20:06:11 +02007816 /* if the cookie has an expiration date and the proxy wants to check
7817 * it, then we do that now. We first check if the cookie is too old,
7818 * then only if it has expired. We detect strict overflow because the
7819 * time resolution here is not great (4 seconds). Cookies with dates
7820 * in the future are ignored if their offset is beyond one day. This
7821 * allows an admin to fix timezone issues without expiring everyone
7822 * and at the same time avoids keeping unwanted side effects for too
7823 * long.
7824 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007825 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7826 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007827 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007828 txn->flags &= ~TX_CK_MASK;
7829 txn->flags |= TX_CK_OLD;
7830 delim = val_beg; // let's pretend we have not found the cookie
7831 txn->cookie_first_date = 0;
7832 txn->cookie_last_date = 0;
7833 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007834 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7835 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007836 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007837 txn->flags &= ~TX_CK_MASK;
7838 txn->flags |= TX_CK_EXPIRED;
7839 delim = val_beg; // let's pretend we have not found the cookie
7840 txn->cookie_first_date = 0;
7841 txn->cookie_last_date = 0;
7842 }
7843
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007844 /* Here, we'll look for the first running server which supports the cookie.
7845 * This allows to share a same cookie between several servers, for example
7846 * to dedicate backup servers to specific servers only.
7847 * However, to prevent clients from sticking to cookie-less backup server
7848 * when they have incidentely learned an empty cookie, we simply ignore
7849 * empty cookies and mark them as invalid.
7850 * The same behaviour is applied when persistence must be ignored.
7851 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007852 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007853 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007854
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007855 while (srv) {
7856 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7857 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007858 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007859 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007860 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007861 /* we found the server and we can use it */
7862 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007863 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007864 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007865 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007866 break;
7867 } else {
7868 /* we found a server, but it's down,
7869 * mark it as such and go on in case
7870 * another one is available.
7871 */
7872 txn->flags &= ~TX_CK_MASK;
7873 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007874 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007875 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007876 srv = srv->next;
7877 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007878
Willy Tarreauf64d1412010-10-07 20:06:11 +02007879 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007880 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007881 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007882 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007883 txn->flags |= TX_CK_UNUSED;
7884 else
7885 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007886 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007887
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007888 /* depending on the cookie mode, we may have to either :
7889 * - delete the complete cookie if we're in insert+indirect mode, so that
7890 * the server never sees it ;
7891 * - remove the server id from the cookie value, and tag the cookie as an
7892 * application cookie so that it does not get accidentely removed later,
7893 * if we're in cookie prefix mode
7894 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007895 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007896 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007897
Willy Tarreau9b28e032012-10-12 23:49:43 +02007898 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007899 val_end += delta;
7900 next += delta;
7901 hdr_end += delta;
7902 hdr_next += delta;
7903 cur_hdr->len += delta;
7904 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007905
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007906 del_from = NULL;
7907 preserve_hdr = 1; /* we want to keep this cookie */
7908 }
7909 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007910 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007911 del_from = prev;
7912 }
7913 } else {
7914 /* This is not our cookie, so we must preserve it. But if we already
7915 * scheduled another cookie for removal, we cannot remove the
7916 * complete header, but we can remove the previous block itself.
7917 */
7918 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007919
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007920 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007921 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007922 if (att_beg >= del_from)
7923 att_beg += delta;
7924 if (att_end >= del_from)
7925 att_end += delta;
7926 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007927 val_end += delta;
7928 next += delta;
7929 hdr_end += delta;
7930 hdr_next += delta;
7931 cur_hdr->len += delta;
7932 http_msg_move_end(&txn->req, delta);
7933 prev = del_from;
7934 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007935 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007936 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007937
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007938 /* continue with next cookie on this header line */
7939 att_beg = next;
7940 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007941
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007942 /* There are no more cookies on this line.
7943 * We may still have one (or several) marked for deletion at the
7944 * end of the line. We must do this now in two ways :
7945 * - if some cookies must be preserved, we only delete from the
7946 * mark to the end of line ;
7947 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007948 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007949 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007950 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007951 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007952 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007953 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007954 cur_hdr->len += delta;
7955 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007956 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007957
7958 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007959 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7960 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007961 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007962 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007963 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007964 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007965 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007966 }
7967
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007968 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007969 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007970 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007971}
7972
7973
Willy Tarreaua15645d2007-03-18 16:22:39 +01007974/* Iterate the same filter through all response headers contained in <rtr>.
7975 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7976 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007977int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007978{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007979 char *cur_ptr, *cur_end, *cur_next;
7980 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007981 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007982 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007983 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007984
7985 last_hdr = 0;
7986
Willy Tarreau9b28e032012-10-12 23:49:43 +02007987 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007988 old_idx = 0;
7989
7990 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007991 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007992 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007993 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007994 (exp->action == ACT_ALLOW ||
7995 exp->action == ACT_DENY))
7996 return 0;
7997
7998 cur_idx = txn->hdr_idx.v[old_idx].next;
7999 if (!cur_idx)
8000 break;
8001
8002 cur_hdr = &txn->hdr_idx.v[cur_idx];
8003 cur_ptr = cur_next;
8004 cur_end = cur_ptr + cur_hdr->len;
8005 cur_next = cur_end + cur_hdr->cr + 1;
8006
8007 /* Now we have one header between cur_ptr and cur_end,
8008 * and the next header starts at cur_next.
8009 */
8010
Willy Tarreau15a53a42015-01-21 13:39:42 +01008011 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008012 switch (exp->action) {
8013 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008014 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008015 last_hdr = 1;
8016 break;
8017
8018 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008019 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008020 last_hdr = 1;
8021 break;
8022
8023 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008024 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8025 if (trash.len < 0)
8026 return -1;
8027
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008028 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008029 /* FIXME: if the user adds a newline in the replacement, the
8030 * index will not be recalculated for now, and the new line
8031 * will not be counted as a new header.
8032 */
8033
8034 cur_end += delta;
8035 cur_next += delta;
8036 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008037 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008038 break;
8039
8040 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02008041 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008042 cur_next += delta;
8043
Willy Tarreaufa355d42009-11-29 18:12:29 +01008044 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008045 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8046 txn->hdr_idx.used--;
8047 cur_hdr->len = 0;
8048 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01008049 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008050 break;
8051
8052 }
8053 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008054
8055 /* keep the link from this header to next one in case of later
8056 * removal of next header.
8057 */
8058 old_idx = cur_idx;
8059 }
8060 return 0;
8061}
8062
8063
8064/* Apply the filter to the status line in the response buffer <rtr>.
8065 * Returns 0 if nothing has been done, 1 if the filter has been applied,
8066 * or -1 if a replacement resulted in an invalid status line.
8067 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008068int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008069{
Willy Tarreaua15645d2007-03-18 16:22:39 +01008070 char *cur_ptr, *cur_end;
8071 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008072 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008073 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008074
8075
Willy Tarreau3d300592007-03-18 18:34:41 +01008076 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008077 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008078 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008079 (exp->action == ACT_ALLOW ||
8080 exp->action == ACT_DENY))
8081 return 0;
8082 else if (exp->action == ACT_REMOVE)
8083 return 0;
8084
8085 done = 0;
8086
Willy Tarreau9b28e032012-10-12 23:49:43 +02008087 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008088 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008089
8090 /* Now we have the status line between cur_ptr and cur_end */
8091
Willy Tarreau15a53a42015-01-21 13:39:42 +01008092 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008093 switch (exp->action) {
8094 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008095 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008096 done = 1;
8097 break;
8098
8099 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008100 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008101 done = 1;
8102 break;
8103
8104 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008105 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8106 if (trash.len < 0)
8107 return -1;
8108
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008109 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008110 /* FIXME: if the user adds a newline in the replacement, the
8111 * index will not be recalculated for now, and the new line
8112 * will not be counted as a new header.
8113 */
8114
Willy Tarreaufa355d42009-11-29 18:12:29 +01008115 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008116 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008117 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02008118 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01008119 cur_ptr, cur_end + 1,
8120 NULL, NULL);
8121 if (unlikely(!cur_end))
8122 return -1;
8123
8124 /* we have a full respnse and we know that we have either a CR
8125 * or an LF at <ptr>.
8126 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008127 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008128 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01008129 /* there is no point trying this regex on headers */
8130 return 1;
8131 }
8132 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008133 return done;
8134}
8135
8136
8137
8138/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008139 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008140 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
8141 * unparsable response.
8142 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008143int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008144{
Willy Tarreau192252e2015-04-04 01:47:55 +02008145 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008146 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008147 struct hdr_exp *exp;
8148
8149 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008150 int ret;
8151
8152 /*
8153 * The interleaving of transformations and verdicts
8154 * makes it difficult to decide to continue or stop
8155 * the evaluation.
8156 */
8157
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008158 if (txn->flags & TX_SVDENY)
8159 break;
8160
Willy Tarreau3d300592007-03-18 18:34:41 +01008161 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008162 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
8163 exp->action == ACT_PASS)) {
8164 exp = exp->next;
8165 continue;
8166 }
8167
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008168 /* if this filter had a condition, evaluate it now and skip to
8169 * next filter if the condition does not match.
8170 */
8171 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02008172 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008173 ret = acl_pass(ret);
8174 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8175 ret = !ret;
8176 if (!ret)
8177 continue;
8178 }
8179
Willy Tarreaua15645d2007-03-18 16:22:39 +01008180 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008181 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008182 if (unlikely(ret < 0))
8183 return -1;
8184
8185 if (likely(ret == 0)) {
8186 /* The filter did not match the response, it can be
8187 * iterated through all headers.
8188 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008189 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8190 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008191 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008192 }
8193 return 0;
8194}
8195
8196
Willy Tarreaua15645d2007-03-18 16:22:39 +01008197/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008198 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008199 * desirable to call it only when needed. This function is also used when we
8200 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008201 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008202void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008203{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008204 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008205 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01008206 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008207 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008208 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008209 char *hdr_beg, *hdr_end, *hdr_next;
8210 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008211
Willy Tarreaua15645d2007-03-18 16:22:39 +01008212 /* Iterate through the headers.
8213 * we start with the start line.
8214 */
8215 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008216 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008217
8218 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8219 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008220 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008221
8222 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008223 hdr_beg = hdr_next;
8224 hdr_end = hdr_beg + cur_hdr->len;
8225 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008226
Willy Tarreau24581ba2010-08-31 22:39:35 +02008227 /* We have one full header between hdr_beg and hdr_end, and the
8228 * next header starts at hdr_next. We're only interested in
8229 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008230 */
8231
Willy Tarreau24581ba2010-08-31 22:39:35 +02008232 is_cookie2 = 0;
8233 prev = hdr_beg + 10;
8234 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008235 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008236 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8237 if (!val) {
8238 old_idx = cur_idx;
8239 continue;
8240 }
8241 is_cookie2 = 1;
8242 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008243 }
8244
Willy Tarreau24581ba2010-08-31 22:39:35 +02008245 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8246 * <prev> points to the colon.
8247 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008248 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008249
Willy Tarreau24581ba2010-08-31 22:39:35 +02008250 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8251 * check-cache is enabled) and we are not interested in checking
8252 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008253 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02008254 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008255 return;
8256
Willy Tarreau24581ba2010-08-31 22:39:35 +02008257 /* OK so now we know we have to process this response cookie.
8258 * The format of the Set-Cookie header is slightly different
8259 * from the format of the Cookie header in that it does not
8260 * support the comma as a cookie delimiter (thus the header
8261 * cannot be folded) because the Expires attribute described in
8262 * the original Netscape's spec may contain an unquoted date
8263 * with a comma inside. We have to live with this because
8264 * many browsers don't support Max-Age and some browsers don't
8265 * support quoted strings. However the Set-Cookie2 header is
8266 * clean.
8267 *
8268 * We have to keep multiple pointers in order to support cookie
8269 * removal at the beginning, middle or end of header without
8270 * corrupting the header (in case of set-cookie2). A special
8271 * pointer, <scav> points to the beginning of the set-cookie-av
8272 * fields after the first semi-colon. The <next> pointer points
8273 * either to the end of line (set-cookie) or next unquoted comma
8274 * (set-cookie2). All of these headers are valid :
8275 *
8276 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8277 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8278 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8279 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8280 * | | | | | | | | | |
8281 * | | | | | | | | +-> next hdr_end <--+
8282 * | | | | | | | +------------> scav
8283 * | | | | | | +--------------> val_end
8284 * | | | | | +--------------------> val_beg
8285 * | | | | +----------------------> equal
8286 * | | | +------------------------> att_end
8287 * | | +----------------------------> att_beg
8288 * | +------------------------------> prev
8289 * +-----------------------------------------> hdr_beg
8290 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008291
Willy Tarreau24581ba2010-08-31 22:39:35 +02008292 for (; prev < hdr_end; prev = next) {
8293 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008294
Willy Tarreau24581ba2010-08-31 22:39:35 +02008295 /* find att_beg */
8296 att_beg = prev + 1;
8297 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8298 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008299
Willy Tarreau24581ba2010-08-31 22:39:35 +02008300 /* find att_end : this is the first character after the last non
8301 * space before the equal. It may be equal to hdr_end.
8302 */
8303 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008304
Willy Tarreau24581ba2010-08-31 22:39:35 +02008305 while (equal < hdr_end) {
8306 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8307 break;
8308 if (http_is_spht[(unsigned char)*equal++])
8309 continue;
8310 att_end = equal;
8311 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008312
Willy Tarreau24581ba2010-08-31 22:39:35 +02008313 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8314 * is between <att_beg> and <equal>, both may be identical.
8315 */
8316
8317 /* look for end of cookie if there is an equal sign */
8318 if (equal < hdr_end && *equal == '=') {
8319 /* look for the beginning of the value */
8320 val_beg = equal + 1;
8321 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8322 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008323
Willy Tarreau24581ba2010-08-31 22:39:35 +02008324 /* find the end of the value, respecting quotes */
8325 next = find_cookie_value_end(val_beg, hdr_end);
8326
8327 /* make val_end point to the first white space or delimitor after the value */
8328 val_end = next;
8329 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8330 val_end--;
8331 } else {
8332 /* <equal> points to next comma, semi-colon or EOL */
8333 val_beg = val_end = next = equal;
8334 }
8335
8336 if (next < hdr_end) {
8337 /* Set-Cookie2 supports multiple cookies, and <next> points to
8338 * a colon or semi-colon before the end. So skip all attr-value
8339 * pairs and look for the next comma. For Set-Cookie, since
8340 * commas are permitted in values, skip to the end.
8341 */
8342 if (is_cookie2)
8343 next = find_hdr_value_end(next, hdr_end);
8344 else
8345 next = hdr_end;
8346 }
8347
8348 /* Now everything is as on the diagram above */
8349
8350 /* Ignore cookies with no equal sign */
8351 if (equal == val_end)
8352 continue;
8353
8354 /* If there are spaces around the equal sign, we need to
8355 * strip them otherwise we'll get trouble for cookie captures,
8356 * or even for rewrites. Since this happens extremely rarely,
8357 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008358 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008359 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8360 int stripped_before = 0;
8361 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008362
Willy Tarreau24581ba2010-08-31 22:39:35 +02008363 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008364 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008365 equal += stripped_before;
8366 val_beg += stripped_before;
8367 }
8368
8369 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008370 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008371 val_beg += stripped_after;
8372 stripped_before += stripped_after;
8373 }
8374
8375 val_end += stripped_before;
8376 next += stripped_before;
8377 hdr_end += stripped_before;
8378 hdr_next += stripped_before;
8379 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008380 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008381 }
8382
8383 /* First, let's see if we want to capture this cookie. We check
8384 * that we don't already have a server side cookie, because we
8385 * can only capture one. Also as an optimisation, we ignore
8386 * cookies shorter than the declared name.
8387 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008388 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008389 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008390 (val_end - att_beg >= sess->fe->capture_namelen) &&
8391 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008392 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008393 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008394 Alert("HTTP logging : out of memory.\n");
8395 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008396 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008397 if (log_len > sess->fe->capture_len)
8398 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008399 memcpy(txn->srv_cookie, att_beg, log_len);
8400 txn->srv_cookie[log_len] = 0;
8401 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008402 }
8403
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008404 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008405 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008406 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008407 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8408 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008409 /* assume passive cookie by default */
8410 txn->flags &= ~TX_SCK_MASK;
8411 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008412
8413 /* If the cookie is in insert mode on a known server, we'll delete
8414 * this occurrence because we'll insert another one later.
8415 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008416 * a direct access.
8417 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008418 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008419 /* The "preserve" flag was set, we don't want to touch the
8420 * server's cookie.
8421 */
8422 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008423 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008424 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008425 /* this cookie must be deleted */
8426 if (*prev == ':' && next == hdr_end) {
8427 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008428 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008429 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8430 txn->hdr_idx.used--;
8431 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008432 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008433 hdr_next += delta;
8434 http_msg_move_end(&txn->rsp, delta);
8435 /* note: while both invalid now, <next> and <hdr_end>
8436 * are still equal, so the for() will stop as expected.
8437 */
8438 } else {
8439 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008440 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008441 next = prev;
8442 hdr_end += delta;
8443 hdr_next += delta;
8444 cur_hdr->len += delta;
8445 http_msg_move_end(&txn->rsp, delta);
8446 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008447 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008448 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008449 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008450 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008451 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008452 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008453 * with this server since we know it.
8454 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008455 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008456 next += delta;
8457 hdr_end += delta;
8458 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008459 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008460 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008461
Willy Tarreauf1348312010-10-07 15:54:11 +02008462 txn->flags &= ~TX_SCK_MASK;
8463 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008464 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008465 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008466 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008467 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008468 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008469 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008470 next += delta;
8471 hdr_end += delta;
8472 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008473 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008474 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008475
Willy Tarreau827aee92011-03-10 16:55:02 +01008476 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008477 txn->flags &= ~TX_SCK_MASK;
8478 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008479 }
8480 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008481 /* that's done for this cookie, check the next one on the same
8482 * line when next != hdr_end (only if is_cookie2).
8483 */
8484 }
8485 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008486 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008487 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008488}
8489
8490
Willy Tarreaua15645d2007-03-18 16:22:39 +01008491/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008492 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008493 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008494void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008495{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008496 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008497 char *p1, *p2;
8498
8499 char *cur_ptr, *cur_end, *cur_next;
8500 int cur_idx;
8501
Willy Tarreau5df51872007-11-25 16:20:08 +01008502 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008503 return;
8504
8505 /* Iterate through the headers.
8506 * we start with the start line.
8507 */
8508 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008509 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008510
8511 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8512 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008513 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008514
8515 cur_hdr = &txn->hdr_idx.v[cur_idx];
8516 cur_ptr = cur_next;
8517 cur_end = cur_ptr + cur_hdr->len;
8518 cur_next = cur_end + cur_hdr->cr + 1;
8519
8520 /* We have one full header between cur_ptr and cur_end, and the
8521 * next header starts at cur_next. We're only interested in
8522 * "Cookie:" headers.
8523 */
8524
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008525 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8526 if (val) {
8527 if ((cur_end - (cur_ptr + val) >= 8) &&
8528 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8529 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8530 return;
8531 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008532 }
8533
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008534 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8535 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008536 continue;
8537
8538 /* OK, right now we know we have a cache-control header at cur_ptr */
8539
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008540 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008541
8542 if (p1 >= cur_end) /* no more info */
8543 continue;
8544
8545 /* p1 is at the beginning of the value */
8546 p2 = p1;
8547
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008548 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008549 p2++;
8550
8551 /* we have a complete value between p1 and p2 */
8552 if (p2 < cur_end && *p2 == '=') {
8553 /* we have something of the form no-cache="set-cookie" */
8554 if ((cur_end - p1 >= 21) &&
8555 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8556 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008557 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008558 continue;
8559 }
8560
8561 /* OK, so we know that either p2 points to the end of string or to a comma */
8562 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008563 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008564 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8565 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8566 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008567 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008568 return;
8569 }
8570
8571 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008572 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008573 continue;
8574 }
8575 }
8576}
8577
Willy Tarreau58f10d72006-12-04 02:26:12 +01008578
Willy Tarreaub2513902006-12-17 14:52:38 +01008579/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008580 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008581 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008582 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008583 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008584 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008585 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008586 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008587 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008588int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008589{
8590 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008591 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008592 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008593
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008594 if (!uri_auth)
8595 return 0;
8596
Cyril Bonté70be45d2010-10-12 00:14:35 +02008597 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008598 return 0;
8599
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008600 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008601 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008602 return 0;
8603
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008604 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008605 return 0;
8606
Willy Tarreaub2513902006-12-17 14:52:38 +01008607 return 1;
8608}
8609
Willy Tarreau4076a152009-04-02 15:18:36 +02008610/*
8611 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008612 * By default it tries to report the error position as msg->err_pos. However if
8613 * this one is not set, it will then report msg->next, which is the last known
8614 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008615 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008616 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008617void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008618 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008619 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008620{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008621 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008622 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008623 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008624
Willy Tarreau9b28e032012-10-12 23:49:43 +02008625 es->len = MIN(chn->buf->i, sizeof(es->buf));
8626 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008627 len1 = MIN(len1, es->len);
8628 len2 = es->len - len1; /* remaining data if buffer wraps */
8629
Willy Tarreau9b28e032012-10-12 23:49:43 +02008630 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008631 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008632 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008633
Willy Tarreau4076a152009-04-02 15:18:36 +02008634 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008635 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008636 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008637 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008638
Willy Tarreau4076a152009-04-02 15:18:36 +02008639 es->when = date; // user-visible date
8640 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008641 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008642 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008643 if (objt_conn(sess->origin))
8644 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008645 else
8646 memset(&es->src, 0, sizeof(es->src));
8647
Willy Tarreau078272e2010-12-12 12:46:33 +01008648 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008649 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008650 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008651 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008652 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008653 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008654 es->b_out = chn->buf->o;
8655 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008656 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008657 es->m_clen = msg->chunk_len;
8658 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008659}
Willy Tarreaub2513902006-12-17 14:52:38 +01008660
Willy Tarreau294c4732011-12-16 21:35:50 +01008661/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8662 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8663 * performed over the whole headers. Otherwise it must contain a valid header
8664 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8665 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8666 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8667 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008668 * -1. The value fetch stops at commas, so this function is suited for use with
8669 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008670 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008671 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008672unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008673 struct hdr_idx *idx, int occ,
8674 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008675{
Willy Tarreau294c4732011-12-16 21:35:50 +01008676 struct hdr_ctx local_ctx;
8677 char *ptr_hist[MAX_HDR_HISTORY];
8678 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008679 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008680 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008681
Willy Tarreau294c4732011-12-16 21:35:50 +01008682 if (!ctx) {
8683 local_ctx.idx = 0;
8684 ctx = &local_ctx;
8685 }
8686
Willy Tarreaubce70882009-09-07 11:51:47 +02008687 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008688 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008689 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008690 occ--;
8691 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008692 *vptr = ctx->line + ctx->val;
8693 *vlen = ctx->vlen;
8694 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008695 }
8696 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008697 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008698 }
8699
8700 /* negative occurrence, we scan all the list then walk back */
8701 if (-occ > MAX_HDR_HISTORY)
8702 return 0;
8703
Willy Tarreau294c4732011-12-16 21:35:50 +01008704 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008705 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008706 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8707 len_hist[hist_ptr] = ctx->vlen;
8708 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008709 hist_ptr = 0;
8710 found++;
8711 }
8712 if (-occ > found)
8713 return 0;
8714 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008715 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8716 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8717 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008718 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008719 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008720 if (hist_ptr >= MAX_HDR_HISTORY)
8721 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008722 *vptr = ptr_hist[hist_ptr];
8723 *vlen = len_hist[hist_ptr];
8724 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008725}
8726
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008727/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8728 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8729 * performed over the whole headers. Otherwise it must contain a valid header
8730 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8731 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8732 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8733 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8734 * -1. This function differs from http_get_hdr() in that it only returns full
8735 * line header values and does not stop at commas.
8736 * The return value is 0 if nothing was found, or non-zero otherwise.
8737 */
8738unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8739 struct hdr_idx *idx, int occ,
8740 struct hdr_ctx *ctx, char **vptr, int *vlen)
8741{
8742 struct hdr_ctx local_ctx;
8743 char *ptr_hist[MAX_HDR_HISTORY];
8744 int len_hist[MAX_HDR_HISTORY];
8745 unsigned int hist_ptr;
8746 int found;
8747
8748 if (!ctx) {
8749 local_ctx.idx = 0;
8750 ctx = &local_ctx;
8751 }
8752
8753 if (occ >= 0) {
8754 /* search from the beginning */
8755 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8756 occ--;
8757 if (occ <= 0) {
8758 *vptr = ctx->line + ctx->val;
8759 *vlen = ctx->vlen;
8760 return 1;
8761 }
8762 }
8763 return 0;
8764 }
8765
8766 /* negative occurrence, we scan all the list then walk back */
8767 if (-occ > MAX_HDR_HISTORY)
8768 return 0;
8769
8770 found = hist_ptr = 0;
8771 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8772 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8773 len_hist[hist_ptr] = ctx->vlen;
8774 if (++hist_ptr >= MAX_HDR_HISTORY)
8775 hist_ptr = 0;
8776 found++;
8777 }
8778 if (-occ > found)
8779 return 0;
8780 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8781 * find occurrence -occ, so we have to check [hist_ptr+occ].
8782 */
8783 hist_ptr += occ;
8784 if (hist_ptr >= MAX_HDR_HISTORY)
8785 hist_ptr -= MAX_HDR_HISTORY;
8786 *vptr = ptr_hist[hist_ptr];
8787 *vlen = len_hist[hist_ptr];
8788 return 1;
8789}
8790
Willy Tarreaubaaee002006-06-26 02:48:02 +02008791/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008792 * Print a debug line with a header. Always stop at the first CR or LF char,
8793 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8794 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008795 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008796void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008797{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008798 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008799 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008800
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008801 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008802 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008803 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008804 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 +02008805
8806 for (max = 0; start + max < end; max++)
8807 if (start[max] == '\r' || start[max] == '\n')
8808 break;
8809
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008810 UBOUND(max, trash.size - trash.len - 3);
8811 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8812 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008813 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008814}
8815
Willy Tarreaueee5b512015-04-03 23:46:31 +02008816
8817/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8818 * The hdr_idx is allocated as well. In case of allocation failure, everything
8819 * allocated is freed and NULL is returned. Otherwise the new transaction is
8820 * assigned to the stream and returned.
8821 */
8822struct http_txn *http_alloc_txn(struct stream *s)
8823{
8824 struct http_txn *txn = s->txn;
8825
8826 if (txn)
8827 return txn;
8828
8829 txn = pool_alloc2(pool2_http_txn);
8830 if (!txn)
8831 return txn;
8832
8833 txn->hdr_idx.size = global.tune.max_http_hdr;
8834 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8835 if (!txn->hdr_idx.v) {
8836 pool_free2(pool2_http_txn, txn);
8837 return NULL;
8838 }
8839
8840 s->txn = txn;
8841 return txn;
8842}
8843
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008844void http_txn_reset_req(struct http_txn *txn)
8845{
8846 txn->req.flags = 0;
8847 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8848 txn->req.next = 0;
8849 txn->req.chunk_len = 0LL;
8850 txn->req.body_len = 0LL;
8851 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8852}
8853
8854void http_txn_reset_res(struct http_txn *txn)
8855{
8856 txn->rsp.flags = 0;
8857 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8858 txn->rsp.next = 0;
8859 txn->rsp.chunk_len = 0LL;
8860 txn->rsp.body_len = 0LL;
8861 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8862}
8863
Willy Tarreau0937bc42009-12-22 15:03:09 +01008864/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008865 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008866 * the required fields are properly allocated and that we only need to (re)init
8867 * them. This should be used before processing any new request.
8868 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008869void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008870{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008871 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008872 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008873
8874 txn->flags = 0;
8875 txn->status = -1;
8876
Willy Tarreauf64d1412010-10-07 20:06:11 +02008877 txn->cookie_first_date = 0;
8878 txn->cookie_last_date = 0;
8879
Willy Tarreaueee5b512015-04-03 23:46:31 +02008880 txn->srv_cookie = NULL;
8881 txn->cli_cookie = NULL;
8882 txn->uri = NULL;
8883
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008884 http_txn_reset_req(txn);
8885 http_txn_reset_res(txn);
8886
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008887 txn->req.chn = &s->req;
8888 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008889
8890 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008891
8892 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8893 if (fe->options2 & PR_O2_REQBUG_OK)
8894 txn->req.err_pos = -1; /* let buggy requests pass */
8895
Willy Tarreau0937bc42009-12-22 15:03:09 +01008896 if (txn->hdr_idx.v)
8897 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008898
8899 vars_init(&s->vars_txn, SCOPE_TXN);
8900 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008901}
8902
8903/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008904void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008905{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008906 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008907 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008908
Willy Tarreau75195602014-03-11 15:48:55 +01008909 /* release any possible compression context */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008910 if (s->flags & SF_COMP_READY)
Willy Tarreau75195602014-03-11 15:48:55 +01008911 s->comp_algo->end(&s->comp_ctx);
8912 s->comp_algo = NULL;
Willy Tarreaue7dff022015-04-03 01:14:29 +02008913 s->flags &= ~SF_COMP_READY;
Willy Tarreau75195602014-03-11 15:48:55 +01008914
Willy Tarreau0937bc42009-12-22 15:03:09 +01008915 /* these ones will have been dynamically allocated */
8916 pool_free2(pool2_requri, txn->uri);
8917 pool_free2(pool2_capture, txn->cli_cookie);
8918 pool_free2(pool2_capture, txn->srv_cookie);
William Lallemanda73203e2012-03-12 12:48:57 +01008919 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008920
William Lallemanda73203e2012-03-12 12:48:57 +01008921 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008922 txn->uri = NULL;
8923 txn->srv_cookie = NULL;
8924 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008925
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008926 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008927 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008928 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008929 pool_free2(h->pool, s->req_cap[h->index]);
8930 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008931 }
8932
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008933 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008934 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008935 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008936 pool_free2(h->pool, s->res_cap[h->index]);
8937 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008938 }
8939
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008940 vars_prune(&s->vars_txn, s);
8941 vars_prune(&s->vars_reqres, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008942}
8943
8944/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008945void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008946{
8947 http_end_txn(s);
8948 http_init_txn(s);
8949
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008950 /* reinitialise the current rule list pointer to NULL. We are sure that
8951 * any rulelist match the NULL pointer.
8952 */
8953 s->current_rule_list = NULL;
8954
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008955 s->be = strm_fe(s);
8956 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008957 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008958 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008959 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008960 /* re-init store persistence */
8961 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008962 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008963
Willy Tarreau0937bc42009-12-22 15:03:09 +01008964 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008965
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008966 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008967
Willy Tarreau739cfba2010-01-25 23:11:14 +01008968 /* We must trim any excess data from the response buffer, because we
8969 * may have blocked an invalid response from a server that we don't
8970 * want to accidentely forward once we disable the analysers, nor do
8971 * we want those data to come along with next response. A typical
8972 * example of such data would be from a buggy server responding to
8973 * a HEAD with some data, or sending more than the advertised
8974 * content-length.
8975 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008976 if (unlikely(s->res.buf->i))
8977 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008978
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008979 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008980 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008981
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008982 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008983 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008984
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008985 s->req.rex = TICK_ETERNITY;
8986 s->req.wex = TICK_ETERNITY;
8987 s->req.analyse_exp = TICK_ETERNITY;
8988 s->res.rex = TICK_ETERNITY;
8989 s->res.wex = TICK_ETERNITY;
8990 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008991}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008992
Sasha Pachev218f0642014-06-16 12:05:59 -06008993void free_http_res_rules(struct list *r)
8994{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008995 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06008996
8997 list_for_each_entry_safe(pr, tr, r, list) {
8998 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008999 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06009000 free(pr);
9001 }
9002}
9003
9004void free_http_req_rules(struct list *r)
9005{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009006 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01009007
9008 list_for_each_entry_safe(pr, tr, r, list) {
9009 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009010 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009011 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01009012
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009013 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01009014 free(pr);
9015 }
9016}
9017
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009018/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009019struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01009020{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009021 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009022 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009023 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009024 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01009025
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009026 rule = (struct act_rule*)calloc(1, sizeof(struct act_rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01009027 if (!rule) {
9028 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009029 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009030 }
9031
CJ Ess108b1dd2015-04-07 12:03:37 -04009032 rule->deny_status = HTTP_ERR_403;
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009033 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009034 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01009035 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02009036 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04009037 int code;
9038 int hc;
9039
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009040 rule->action = ACT_ACTION_DENY;
Willy Tarreauff011f22011-01-06 17:51:27 +01009041 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04009042 if (strcmp(args[cur_arg], "deny_status") == 0) {
9043 cur_arg++;
9044 if (!args[cur_arg]) {
9045 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
9046 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9047 goto out_err;
9048 }
9049
9050 code = atol(args[cur_arg]);
9051 cur_arg++;
9052 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
9053 if (http_err_codes[hc] == code) {
9054 rule->deny_status = hc;
9055 break;
9056 }
9057 }
9058
9059 if (hc >= HTTP_ERR_SIZE) {
9060 Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n",
9061 file, linenum, code);
9062 }
9063 }
Willy Tarreauccbcc372012-12-27 12:37:57 +01009064 } else if (!strcmp(args[0], "tarpit")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009065 rule->action = ACT_HTTP_REQ_TARPIT;
Willy Tarreauccbcc372012-12-27 12:37:57 +01009066 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01009067 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009068 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01009069 cur_arg = 1;
9070
9071 while(*args[cur_arg]) {
9072 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009073 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01009074 cur_arg+=2;
9075 continue;
9076 } else
9077 break;
9078 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009079 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009080 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009081 cur_arg = 1;
9082
9083 if (!*args[cur_arg] ||
9084 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9085 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
9086 file, linenum, args[0]);
9087 goto out_err;
9088 }
9089 rule->arg.nice = atoi(args[cur_arg]);
9090 if (rule->arg.nice < -1024)
9091 rule->arg.nice = -1024;
9092 else if (rule->arg.nice > 1024)
9093 rule->arg.nice = 1024;
9094 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009095 } else if (!strcmp(args[0], "set-tos")) {
9096#ifdef IP_TOS
9097 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009098 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009099 cur_arg = 1;
9100
9101 if (!*args[cur_arg] ||
9102 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9103 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9104 file, linenum, args[0]);
9105 goto out_err;
9106 }
9107
9108 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9109 if (err && *err != '\0') {
9110 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9111 file, linenum, err, args[0]);
9112 goto out_err;
9113 }
9114 cur_arg++;
9115#else
9116 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9117 goto out_err;
9118#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009119 } else if (!strcmp(args[0], "set-mark")) {
9120#ifdef SO_MARK
9121 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009122 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009123 cur_arg = 1;
9124
9125 if (!*args[cur_arg] ||
9126 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9127 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9128 file, linenum, args[0]);
9129 goto out_err;
9130 }
9131
9132 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9133 if (err && *err != '\0') {
9134 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9135 file, linenum, err, args[0]);
9136 goto out_err;
9137 }
9138 cur_arg++;
9139 global.last_checks |= LSTCHK_NETADM;
9140#else
9141 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9142 goto out_err;
9143#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009144 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009145 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009146 cur_arg = 1;
9147
9148 if (!*args[cur_arg] ||
9149 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9150 bad_log_level:
9151 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9152 file, linenum, args[0]);
9153 goto out_err;
9154 }
9155 if (strcmp(args[cur_arg], "silent") == 0)
9156 rule->arg.loglevel = -1;
9157 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9158 goto bad_log_level;
9159 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009160 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009161 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009162 cur_arg = 1;
9163
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009164 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9165 (*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 +01009166 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9167 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009168 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009169 }
9170
9171 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9172 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9173 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009174
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009175 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009176 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009177 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9178 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009179 free(proxy->conf.lfs_file);
9180 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9181 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009182 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009183 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009184 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009185 cur_arg = 1;
9186
9187 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009188 (*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 -06009189 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9190 file, linenum, args[0]);
9191 goto out_err;
9192 }
9193
9194 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9195 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9196 LIST_INIT(&rule->arg.hdr_add.fmt);
9197
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009198 error = NULL;
9199 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9200 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9201 args[cur_arg + 1], error);
9202 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009203 goto out_err;
9204 }
9205
9206 proxy->conf.args.ctx = ARGC_HRQ;
9207 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9208 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9209 file, linenum);
9210
9211 free(proxy->conf.lfs_file);
9212 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9213 proxy->conf.lfs_line = proxy->conf.args.line;
9214 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009215 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009216 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009217 cur_arg = 1;
9218
9219 if (!*args[cur_arg] ||
9220 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9221 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9222 file, linenum, args[0]);
9223 goto out_err;
9224 }
9225
9226 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9227 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9228
9229 proxy->conf.args.ctx = ARGC_HRQ;
9230 free(proxy->conf.lfs_file);
9231 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9232 proxy->conf.lfs_line = proxy->conf.args.line;
9233 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009234 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9235 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009236 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009237 struct sample_expr *expr;
9238 unsigned int where;
9239 char *err = NULL;
9240
9241 cur_arg = 1;
9242 proxy->conf.args.ctx = ARGC_TRK;
9243
9244 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9245 if (!expr) {
9246 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9247 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9248 free(err);
9249 goto out_err;
9250 }
9251
9252 where = 0;
9253 if (proxy->cap & PR_CAP_FE)
9254 where |= SMP_VAL_FE_HRQ_HDR;
9255 if (proxy->cap & PR_CAP_BE)
9256 where |= SMP_VAL_BE_HRQ_HDR;
9257
9258 if (!(expr->fetch->val & where)) {
9259 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9260 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9261 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9262 args[cur_arg-1], sample_src_names(expr->fetch->use));
9263 free(expr);
9264 goto out_err;
9265 }
9266
9267 if (strcmp(args[cur_arg], "table") == 0) {
9268 cur_arg++;
9269 if (!args[cur_arg]) {
9270 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9271 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9272 free(expr);
9273 goto out_err;
9274 }
9275 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009276 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02009277 cur_arg++;
9278 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009279 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009280 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009281 } else if (strcmp(args[0], "redirect") == 0) {
9282 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009283 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009284
Willy Tarreaube4653b2015-05-28 15:26:58 +02009285 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009286 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9287 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9288 goto out_err;
9289 }
9290
9291 /* this redirect rule might already contain a parsed condition which
9292 * we'll pass to the http-request rule.
9293 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009294 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009295 rule->arg.redir = redir;
9296 rule->cond = redir->cond;
9297 redir->cond = NULL;
9298 cur_arg = 2;
9299 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009300 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9301 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009302 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009303 /*
9304 * '+ 8' for 'add-acl('
9305 * '- 9' for 'add-acl(' + trailing ')'
9306 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009307 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009308
9309 cur_arg = 1;
9310
9311 if (!*args[cur_arg] ||
9312 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9313 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9314 file, linenum, args[0]);
9315 goto out_err;
9316 }
9317
9318 LIST_INIT(&rule->arg.map.key);
9319 proxy->conf.args.ctx = ARGC_HRQ;
9320 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9321 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9322 file, linenum);
9323 free(proxy->conf.lfs_file);
9324 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9325 proxy->conf.lfs_line = proxy->conf.args.line;
9326 cur_arg += 1;
9327 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9328 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009329 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009330 /*
9331 * '+ 8' for 'del-acl('
9332 * '- 9' for 'del-acl(' + trailing ')'
9333 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009334 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009335
9336 cur_arg = 1;
9337
9338 if (!*args[cur_arg] ||
9339 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9340 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9341 file, linenum, args[0]);
9342 goto out_err;
9343 }
9344
9345 LIST_INIT(&rule->arg.map.key);
9346 proxy->conf.args.ctx = ARGC_HRQ;
9347 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9348 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9349 file, linenum);
9350 free(proxy->conf.lfs_file);
9351 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9352 proxy->conf.lfs_line = proxy->conf.args.line;
9353 cur_arg += 1;
9354 } else if (strncmp(args[0], "del-map", 7) == 0) {
9355 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009356 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009357 /*
9358 * '+ 8' for 'del-map('
9359 * '- 9' for 'del-map(' + trailing ')'
9360 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009361 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009362
9363 cur_arg = 1;
9364
9365 if (!*args[cur_arg] ||
9366 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9367 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9368 file, linenum, args[0]);
9369 goto out_err;
9370 }
9371
9372 LIST_INIT(&rule->arg.map.key);
9373 proxy->conf.args.ctx = ARGC_HRQ;
9374 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9375 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9376 file, linenum);
9377 free(proxy->conf.lfs_file);
9378 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9379 proxy->conf.lfs_line = proxy->conf.args.line;
9380 cur_arg += 1;
9381 } else if (strncmp(args[0], "set-map", 7) == 0) {
9382 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009383 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009384 /*
9385 * '+ 8' for 'set-map('
9386 * '- 9' for 'set-map(' + trailing ')'
9387 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009388 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009389
9390 cur_arg = 1;
9391
9392 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9393 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9394 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9395 file, linenum, args[0]);
9396 goto out_err;
9397 }
9398
9399 LIST_INIT(&rule->arg.map.key);
9400 LIST_INIT(&rule->arg.map.value);
9401 proxy->conf.args.ctx = ARGC_HRQ;
9402
9403 /* key pattern */
9404 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9405 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9406 file, linenum);
9407
9408 /* value pattern */
9409 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9410 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9411 file, linenum);
9412 free(proxy->conf.lfs_file);
9413 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9414 proxy->conf.lfs_line = proxy->conf.args.line;
9415
9416 cur_arg += 2;
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02009417 } else if (strncmp(args[0], "set-src", 7) == 0) {
9418 struct sample_expr *expr;
9419 unsigned int where;
9420 char *err = NULL;
9421
9422 cur_arg = 1;
9423 proxy->conf.args.ctx = ARGC_HRQ;
9424
9425 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9426 if (!expr) {
9427 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9428 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9429 free(err);
9430 goto out_err;
9431 }
9432
9433 where = 0;
9434 if (proxy->cap & PR_CAP_FE)
9435 where |= SMP_VAL_FE_HRQ_HDR;
9436 if (proxy->cap & PR_CAP_BE)
9437 where |= SMP_VAL_BE_HRQ_HDR;
9438
9439 if (!(expr->fetch->val & where)) {
9440 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9441 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9442 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9443 args[cur_arg-1], sample_src_names(expr->fetch->use));
9444 free(expr);
9445 goto out_err;
9446 }
9447
Thierry FOURNIERa002dc92015-07-31 08:50:51 +02009448 rule->arg.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009449 rule->action = ACT_HTTP_REQ_SET_SRC;
William Lallemand73025dd2014-04-24 14:38:37 +02009450 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9451 char *errmsg = NULL;
9452 cur_arg = 1;
9453 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009454 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009455 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009456 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009457 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9458 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9459 free(errmsg);
9460 goto out_err;
9461 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009462 } else {
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02009463 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 +01009464 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009465 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009466 }
9467
9468 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9469 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009470 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009471
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009472 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9473 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9474 file, linenum, args[0], errmsg);
9475 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009476 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009477 }
9478 rule->cond = cond;
9479 }
9480 else if (*args[cur_arg]) {
9481 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9482 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9483 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009484 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009485 }
9486
9487 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009488 out_err:
9489 free(rule);
9490 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009491}
9492
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009493/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009494struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009495{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009496 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009497 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009498 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009499 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009500
9501 rule = calloc(1, sizeof(*rule));
9502 if (!rule) {
9503 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9504 goto out_err;
9505 }
9506
9507 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009508 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009509 cur_arg = 1;
9510 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009511 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009512 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009513 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009514 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009515 cur_arg = 1;
9516
9517 if (!*args[cur_arg] ||
9518 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9519 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9520 file, linenum, args[0]);
9521 goto out_err;
9522 }
9523 rule->arg.nice = atoi(args[cur_arg]);
9524 if (rule->arg.nice < -1024)
9525 rule->arg.nice = -1024;
9526 else if (rule->arg.nice > 1024)
9527 rule->arg.nice = 1024;
9528 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009529 } else if (!strcmp(args[0], "set-tos")) {
9530#ifdef IP_TOS
9531 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009532 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009533 cur_arg = 1;
9534
9535 if (!*args[cur_arg] ||
9536 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9537 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9538 file, linenum, args[0]);
9539 goto out_err;
9540 }
9541
9542 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9543 if (err && *err != '\0') {
9544 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9545 file, linenum, err, args[0]);
9546 goto out_err;
9547 }
9548 cur_arg++;
9549#else
9550 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9551 goto out_err;
9552#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009553 } else if (!strcmp(args[0], "set-mark")) {
9554#ifdef SO_MARK
9555 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009556 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009557 cur_arg = 1;
9558
9559 if (!*args[cur_arg] ||
9560 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9561 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9562 file, linenum, args[0]);
9563 goto out_err;
9564 }
9565
9566 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9567 if (err && *err != '\0') {
9568 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9569 file, linenum, err, args[0]);
9570 goto out_err;
9571 }
9572 cur_arg++;
9573 global.last_checks |= LSTCHK_NETADM;
9574#else
9575 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9576 goto out_err;
9577#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009578 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009579 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009580 cur_arg = 1;
9581
9582 if (!*args[cur_arg] ||
9583 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9584 bad_log_level:
9585 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9586 file, linenum, args[0]);
9587 goto out_err;
9588 }
9589 if (strcmp(args[cur_arg], "silent") == 0)
9590 rule->arg.loglevel = -1;
9591 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9592 goto bad_log_level;
9593 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009594 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009595 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009596 cur_arg = 1;
9597
9598 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9599 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9600 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9601 file, linenum, args[0]);
9602 goto out_err;
9603 }
9604
9605 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9606 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9607 LIST_INIT(&rule->arg.hdr_add.fmt);
9608
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009609 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009610 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009611 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9612 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009613 free(proxy->conf.lfs_file);
9614 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9615 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009616 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009617 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009618 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009619 cur_arg = 1;
9620
9621 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009622 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9623 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009624 file, linenum, args[0]);
9625 goto out_err;
9626 }
9627
9628 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9629 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9630 LIST_INIT(&rule->arg.hdr_add.fmt);
9631
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009632 error = NULL;
9633 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9634 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9635 args[cur_arg + 1], error);
9636 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009637 goto out_err;
9638 }
9639
9640 proxy->conf.args.ctx = ARGC_HRQ;
9641 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9642 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9643 file, linenum);
9644
9645 free(proxy->conf.lfs_file);
9646 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9647 proxy->conf.lfs_line = proxy->conf.args.line;
9648 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009649 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009650 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009651 cur_arg = 1;
9652
9653 if (!*args[cur_arg] ||
9654 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9655 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9656 file, linenum, args[0]);
9657 goto out_err;
9658 }
9659
9660 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9661 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9662
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009663 proxy->conf.args.ctx = ARGC_HRS;
9664 free(proxy->conf.lfs_file);
9665 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9666 proxy->conf.lfs_line = proxy->conf.args.line;
9667 cur_arg += 1;
9668 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9669 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009670 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009671 /*
9672 * '+ 8' for 'add-acl('
9673 * '- 9' for 'add-acl(' + trailing ')'
9674 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009675 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009676
9677 cur_arg = 1;
9678
9679 if (!*args[cur_arg] ||
9680 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9681 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9682 file, linenum, args[0]);
9683 goto out_err;
9684 }
9685
9686 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009687 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009688 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9689 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9690 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009691 free(proxy->conf.lfs_file);
9692 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9693 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009694
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009695 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009696 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9697 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009698 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009699 /*
9700 * '+ 8' for 'del-acl('
9701 * '- 9' for 'del-acl(' + trailing ')'
9702 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009703 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009704
9705 cur_arg = 1;
9706
9707 if (!*args[cur_arg] ||
9708 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9709 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9710 file, linenum, args[0]);
9711 goto out_err;
9712 }
9713
9714 LIST_INIT(&rule->arg.map.key);
9715 proxy->conf.args.ctx = ARGC_HRS;
9716 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9717 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9718 file, linenum);
9719 free(proxy->conf.lfs_file);
9720 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9721 proxy->conf.lfs_line = proxy->conf.args.line;
9722 cur_arg += 1;
9723 } else if (strncmp(args[0], "del-map", 7) == 0) {
9724 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009725 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009726 /*
9727 * '+ 8' for 'del-map('
9728 * '- 9' for 'del-map(' + trailing ')'
9729 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009730 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009731
9732 cur_arg = 1;
9733
9734 if (!*args[cur_arg] ||
9735 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9736 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9737 file, linenum, args[0]);
9738 goto out_err;
9739 }
9740
9741 LIST_INIT(&rule->arg.map.key);
9742 proxy->conf.args.ctx = ARGC_HRS;
9743 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9744 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9745 file, linenum);
9746 free(proxy->conf.lfs_file);
9747 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9748 proxy->conf.lfs_line = proxy->conf.args.line;
9749 cur_arg += 1;
9750 } else if (strncmp(args[0], "set-map", 7) == 0) {
9751 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009752 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009753 /*
9754 * '+ 8' for 'set-map('
9755 * '- 9' for 'set-map(' + trailing ')'
9756 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009757 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009758
9759 cur_arg = 1;
9760
9761 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9762 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9763 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9764 file, linenum, args[0]);
9765 goto out_err;
9766 }
9767
9768 LIST_INIT(&rule->arg.map.key);
9769 LIST_INIT(&rule->arg.map.value);
9770
9771 proxy->conf.args.ctx = ARGC_HRS;
9772
9773 /* key pattern */
9774 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9775 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9776 file, linenum);
9777
9778 /* value pattern */
9779 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9780 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9781 file, linenum);
9782
9783 free(proxy->conf.lfs_file);
9784 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9785 proxy->conf.lfs_line = proxy->conf.args.line;
9786
9787 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009788 } else if (strcmp(args[0], "redirect") == 0) {
9789 struct redirect_rule *redir;
9790 char *errmsg = NULL;
9791
9792 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
9793 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9794 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9795 goto out_err;
9796 }
9797
9798 /* this redirect rule might already contain a parsed condition which
9799 * we'll pass to the http-request rule.
9800 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009801 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009802 rule->arg.redir = redir;
9803 rule->cond = redir->cond;
9804 redir->cond = NULL;
9805 cur_arg = 2;
9806 return rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009807 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9808 char *errmsg = NULL;
9809 cur_arg = 1;
9810 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009811 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009812 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009813 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009814 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9815 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9816 free(errmsg);
9817 goto out_err;
9818 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009819 } else {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02009820 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 +02009821 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9822 goto out_err;
9823 }
9824
9825 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9826 struct acl_cond *cond;
9827 char *errmsg = NULL;
9828
9829 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9830 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9831 file, linenum, args[0], errmsg);
9832 free(errmsg);
9833 goto out_err;
9834 }
9835 rule->cond = cond;
9836 }
9837 else if (*args[cur_arg]) {
9838 Alert("parsing [%s:%d]: 'http-response %s' expects"
9839 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9840 file, linenum, args[0], args[cur_arg]);
9841 goto out_err;
9842 }
9843
9844 return rule;
9845 out_err:
9846 free(rule);
9847 return NULL;
9848}
9849
Willy Tarreau4baae242012-12-27 12:00:31 +01009850/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009851 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009852 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9853 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009854 */
9855struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009856 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009857{
9858 struct redirect_rule *rule;
9859 int cur_arg;
9860 int type = REDIRECT_TYPE_NONE;
9861 int code = 302;
9862 const char *destination = NULL;
9863 const char *cookie = NULL;
9864 int cookie_set = 0;
9865 unsigned int flags = REDIRECT_FLAG_NONE;
9866 struct acl_cond *cond = NULL;
9867
9868 cur_arg = 0;
9869 while (*(args[cur_arg])) {
9870 if (strcmp(args[cur_arg], "location") == 0) {
9871 if (!*args[cur_arg + 1])
9872 goto missing_arg;
9873
9874 type = REDIRECT_TYPE_LOCATION;
9875 cur_arg++;
9876 destination = args[cur_arg];
9877 }
9878 else if (strcmp(args[cur_arg], "prefix") == 0) {
9879 if (!*args[cur_arg + 1])
9880 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009881 type = REDIRECT_TYPE_PREFIX;
9882 cur_arg++;
9883 destination = args[cur_arg];
9884 }
9885 else if (strcmp(args[cur_arg], "scheme") == 0) {
9886 if (!*args[cur_arg + 1])
9887 goto missing_arg;
9888
9889 type = REDIRECT_TYPE_SCHEME;
9890 cur_arg++;
9891 destination = args[cur_arg];
9892 }
9893 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9894 if (!*args[cur_arg + 1])
9895 goto missing_arg;
9896
9897 cur_arg++;
9898 cookie = args[cur_arg];
9899 cookie_set = 1;
9900 }
9901 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9902 if (!*args[cur_arg + 1])
9903 goto missing_arg;
9904
9905 cur_arg++;
9906 cookie = args[cur_arg];
9907 cookie_set = 0;
9908 }
9909 else if (strcmp(args[cur_arg], "code") == 0) {
9910 if (!*args[cur_arg + 1])
9911 goto missing_arg;
9912
9913 cur_arg++;
9914 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009915 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009916 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009917 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009918 args[cur_arg - 1], args[cur_arg]);
9919 return NULL;
9920 }
9921 }
9922 else if (!strcmp(args[cur_arg],"drop-query")) {
9923 flags |= REDIRECT_FLAG_DROP_QS;
9924 }
9925 else if (!strcmp(args[cur_arg],"append-slash")) {
9926 flags |= REDIRECT_FLAG_APPEND_SLASH;
9927 }
9928 else if (strcmp(args[cur_arg], "if") == 0 ||
9929 strcmp(args[cur_arg], "unless") == 0) {
9930 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9931 if (!cond) {
9932 memprintf(errmsg, "error in condition: %s", *errmsg);
9933 return NULL;
9934 }
9935 break;
9936 }
9937 else {
9938 memprintf(errmsg,
9939 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9940 args[cur_arg]);
9941 return NULL;
9942 }
9943 cur_arg++;
9944 }
9945
9946 if (type == REDIRECT_TYPE_NONE) {
9947 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9948 return NULL;
9949 }
9950
Willy Tarreaube4653b2015-05-28 15:26:58 +02009951 if (dir && type != REDIRECT_TYPE_LOCATION) {
9952 memprintf(errmsg, "response only supports redirect type 'location'");
9953 return NULL;
9954 }
9955
Willy Tarreau4baae242012-12-27 12:00:31 +01009956 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9957 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009958 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009959
9960 if (!use_fmt) {
9961 /* old-style static redirect rule */
9962 rule->rdr_str = strdup(destination);
9963 rule->rdr_len = strlen(destination);
9964 }
9965 else {
9966 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009967
9968 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9969 * if prefix == "/", we don't want to add anything, otherwise it
9970 * makes it hard for the user to configure a self-redirection.
9971 */
Godbachd9722032014-12-18 15:44:58 +08009972 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009973 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009974 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009975 dir ? (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRS_HDR : SMP_VAL_BE_HRS_HDR
9976 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009977 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009978 free(curproxy->conf.lfs_file);
9979 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9980 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009981 }
9982 }
9983
Willy Tarreau4baae242012-12-27 12:00:31 +01009984 if (cookie) {
9985 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9986 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9987 */
9988 rule->cookie_len = strlen(cookie);
9989 if (cookie_set) {
9990 rule->cookie_str = malloc(rule->cookie_len + 10);
9991 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9992 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9993 rule->cookie_len += 9;
9994 } else {
9995 rule->cookie_str = malloc(rule->cookie_len + 21);
9996 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9997 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9998 rule->cookie_len += 20;
9999 }
10000 }
10001 rule->type = type;
10002 rule->code = code;
10003 rule->flags = flags;
10004 LIST_INIT(&rule->list);
10005 return rule;
10006
10007 missing_arg:
10008 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
10009 return NULL;
10010}
10011
Willy Tarreau8797c062007-05-07 00:55:35 +020010012/************************************************************************/
10013/* The code below is dedicated to ACL parsing and matching */
10014/************************************************************************/
10015
10016
Willy Tarreau14174bc2012-04-16 14:34:04 +020010017/* This function ensures that the prerequisites for an L7 fetch are ready,
10018 * which means that a request or response is ready. If some data is missing,
10019 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +020010020 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
10021 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010022 *
10023 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +020010024 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
10025 * decide whether or not an HTTP message is present ;
10026 * 0 if the requested data cannot be fetched or if it is certain that
10027 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010028 * 1 if an HTTP message is ready
10029 */
James Rosewell91a41cb2015-09-18 17:11:16 +010010030int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010031 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +020010032{
Willy Tarreau192252e2015-04-04 01:47:55 +020010033 struct http_txn *txn;
10034 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010035
Willy Tarreaueee5b512015-04-03 23:46:31 +020010036 /* Note: this function may only be used from places where
10037 * http_init_txn() has already been done, and implies that <s>,
10038 * <txn>, and <hdr_idx.v> are properly set. An extra check protects
10039 * against an eventual mistake in the fetch capability matrix.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010040 */
10041
Willy Tarreau192252e2015-04-04 01:47:55 +020010042 if (!s)
10043 return 0;
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +020010044 if (!s->txn) {
10045 if (unlikely(!http_alloc_txn(s)))
10046 return 0; /* not enough memory */
10047 http_init_txn(s);
10048 }
Willy Tarreau192252e2015-04-04 01:47:55 +020010049 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +020010050 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010051
10052 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010053 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010054
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010055 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010056 /* If the buffer does not leave enough free space at the end,
10057 * we must first realign it.
10058 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010059 if (s->req.buf->p > s->req.buf->data &&
10060 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
10061 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +010010062
Willy Tarreau14174bc2012-04-16 14:34:04 +020010063 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +020010064 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +020010065 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010066
10067 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010068 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +020010069 http_msg_analyzer(msg, &txn->hdr_idx);
10070
10071 /* Still no valid request ? */
10072 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +020010073 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010074 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +020010075 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010076 }
10077 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +020010078 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010079 return 0;
10080 }
10081
10082 /* OK we just got a valid HTTP request. We have some minor
10083 * preparation to perform so that further checks can rely
10084 * on HTTP tests.
10085 */
Willy Tarreauaae75e32013-03-29 12:31:49 +010010086
10087 /* If the request was parsed but was too large, we must absolutely
10088 * return an error so that it is not processed. At the moment this
10089 * cannot happen, but if the parsers are to change in the future,
10090 * we want this check to be maintained.
10091 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010092 if (unlikely(s->req.buf->i + s->req.buf->p >
10093 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010094 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010095 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010096 return 1;
10097 }
10098
Willy Tarreau9b28e032012-10-12 23:49:43 +020010099 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +020010100 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +020010101 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010102
Willy Tarreau506d0502013-07-06 13:29:24 +020010103 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
10104 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010105 }
10106
Willy Tarreau506d0502013-07-06 13:29:24 +020010107 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +020010108 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +020010109 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010110
10111 /* otherwise everything's ready for the request */
10112 }
Willy Tarreau24e32d82012-04-23 23:55:44 +020010113 else {
10114 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +020010115 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
10116 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010117 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +020010118 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010119 }
10120
10121 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010122 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010123 return 1;
10124}
Willy Tarreau8797c062007-05-07 00:55:35 +020010125
Willy Tarreau8797c062007-05-07 00:55:35 +020010126/* 1. Check on METHOD
10127 * We use the pre-parsed method if it is known, and store its number as an
10128 * integer. If it is unknown, we use the pointer and the length.
10129 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010130static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +020010131{
10132 int len, meth;
10133
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010010134 len = strlen(text);
10135 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +020010136
10137 pattern->val.i = meth;
10138 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +020010139 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +020010140 pattern->len = len;
10141 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010142 else {
10143 pattern->ptr.str = NULL;
10144 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010145 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010146 return 1;
10147}
10148
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010149/* This function fetches the method of current HTTP request and stores
10150 * it in the global pattern struct as a chunk. There are two possibilities :
10151 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10152 * in <len> and <ptr> is NULL ;
10153 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10154 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010155 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010156 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010157static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010158smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010159{
10160 int meth;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010161 struct http_txn *txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010162
Willy Tarreau24e32d82012-04-23 23:55:44 +020010163 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010164
Willy Tarreau8797c062007-05-07 00:55:35 +020010165 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010166 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010167 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010168 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010169 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10170 /* ensure the indexes are not affected */
10171 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010172 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010173 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
10174 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010175 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010176 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010177 return 1;
10178}
10179
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010180/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010181static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010182{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010183 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010184 struct pattern_list *lst;
10185 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010186
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010187 list_for_each_entry(lst, &expr->patterns, list) {
10188 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010189
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010190 /* well-known method */
10191 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010192 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010193 return pattern;
10194 else
10195 continue;
10196 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010197
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010198 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010199 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010200 continue;
10201
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010202 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010203 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
10204 (!icase && strncmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010205 return pattern;
10206 }
10207 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010208}
10209
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010210static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010211smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010212{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010213 struct http_txn *txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010214 char *ptr;
10215 int len;
10216
Willy Tarreauc0239e02012-04-16 14:42:55 +020010217 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010218
Willy Tarreau8797c062007-05-07 00:55:35 +020010219 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010220 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010221
10222 while ((len-- > 0) && (*ptr++ != '/'));
10223 if (len <= 0)
10224 return 0;
10225
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010226 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010227 smp->data.u.str.str = ptr;
10228 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010229
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010230 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010231 return 1;
10232}
10233
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010234static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010235smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010236{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010237 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010238 char *ptr;
10239 int len;
10240
Willy Tarreauc0239e02012-04-16 14:42:55 +020010241 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010242
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010243 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010244 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10245 return 0;
10246
Willy Tarreau8797c062007-05-07 00:55:35 +020010247 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010248 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010249
10250 while ((len-- > 0) && (*ptr++ != '/'));
10251 if (len <= 0)
10252 return 0;
10253
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010254 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010255 smp->data.u.str.str = ptr;
10256 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010257
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010258 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010259 return 1;
10260}
10261
10262/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010263static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010264smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010265{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010266 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010267 char *ptr;
10268 int len;
10269
Willy Tarreauc0239e02012-04-16 14:42:55 +020010270 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010271
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010272 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010273 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10274 return 0;
10275
Willy Tarreau8797c062007-05-07 00:55:35 +020010276 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010277 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010278
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010279 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010280 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010281 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010282 return 1;
10283}
10284
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010285/* returns the longest available part of the body. This requires that the body
10286 * has been waited for using http-buffer-request.
10287 */
10288static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010289smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010290{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010291 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010292 struct http_msg *msg;
10293 unsigned long len;
10294 unsigned long block1;
10295 char *body;
10296 struct chunk *temp;
10297
10298 CHECK_HTTP_MESSAGE_FIRST();
10299
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010300 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010301 msg = &txn->req;
10302 else
10303 msg = &txn->rsp;
10304
10305 len = http_body_bytes(msg);
10306 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
10307
10308 block1 = len;
10309 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
10310 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
10311
10312 if (block1 == len) {
10313 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010314 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010315 smp->data.u.str.str = body;
10316 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010317 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
10318 }
10319 else {
10320 /* buffer is wrapped, we need to defragment it */
10321 temp = get_trash_chunk();
10322 memcpy(temp->str, body, block1);
10323 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010324 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010325 smp->data.u.str.str = temp->str;
10326 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010327 smp->flags = SMP_F_VOL_TEST;
10328 }
10329 return 1;
10330}
10331
10332
10333/* returns the available length of the body. This requires that the body
10334 * has been waited for using http-buffer-request.
10335 */
10336static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010337smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010338{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010339 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010340 struct http_msg *msg;
10341
10342 CHECK_HTTP_MESSAGE_FIRST();
10343
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010344 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010345 msg = &txn->req;
10346 else
10347 msg = &txn->rsp;
10348
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010349 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010350 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010351
10352 smp->flags = SMP_F_VOL_TEST;
10353 return 1;
10354}
10355
10356
10357/* returns the advertised length of the body, or the advertised size of the
10358 * chunks available in the buffer. This requires that the body has been waited
10359 * for using http-buffer-request.
10360 */
10361static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010362smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010363{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010364 struct http_txn *txn = smp->strm->txn;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010365 struct http_msg *msg;
10366
10367 CHECK_HTTP_MESSAGE_FIRST();
10368
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010369 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010370 msg = &txn->req;
10371 else
10372 msg = &txn->rsp;
10373
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010374 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010375 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010376
10377 smp->flags = SMP_F_VOL_TEST;
10378 return 1;
10379}
10380
10381
Willy Tarreau8797c062007-05-07 00:55:35 +020010382/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010383static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010384smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010385{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010386 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010387
Willy Tarreauc0239e02012-04-16 14:42:55 +020010388 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010389
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010390 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010391 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010392 smp->data.u.str.len = txn->req.sl.rq.u_l;
10393 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010394 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010395 return 1;
10396}
10397
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010398static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010399smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010400{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010401 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010402 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010403
Willy Tarreauc0239e02012-04-16 14:42:55 +020010404 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010405
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010406 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010407 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010408 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010409 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010410
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010411 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010412 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010413 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010414 return 1;
10415}
10416
10417static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010418smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010419{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010420 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010421 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010422
Willy Tarreauc0239e02012-04-16 14:42:55 +020010423 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010424
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010425 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010426 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010427 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10428 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010429
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010430 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010431 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010432 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010433 return 1;
10434}
10435
Willy Tarreau185b5c42012-04-26 15:11:51 +020010436/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10437 * Accepts an optional argument of type string containing the header field name,
10438 * and an optional argument of type signed or unsigned integer to request an
10439 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010440 * headers are considered from the first one. It does not stop on commas and
10441 * returns full lines instead (useful for User-Agent or Date for example).
10442 */
10443static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010444smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010445{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010446 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010447 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010448 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010449 int occ = 0;
10450 const char *name_str = NULL;
10451 int name_len = 0;
10452
10453 if (!ctx) {
10454 /* first call */
10455 ctx = &static_hdr_ctx;
10456 ctx->idx = 0;
10457 smp->ctx.a[0] = ctx;
10458 }
10459
10460 if (args) {
10461 if (args[0].type != ARGT_STR)
10462 return 0;
10463 name_str = args[0].data.str.str;
10464 name_len = args[0].data.str.len;
10465
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010466 if (args[1].type == ARGT_SINT)
10467 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010468 }
10469
10470 CHECK_HTTP_MESSAGE_FIRST();
10471
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010472 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010473 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010474
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010475 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10476 /* search for header from the beginning */
10477 ctx->idx = 0;
10478
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010479 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010480 /* no explicit occurrence and single fetch => last header by default */
10481 occ = -1;
10482
10483 if (!occ)
10484 /* prepare to report multiple occurrences for ACL fetches */
10485 smp->flags |= SMP_F_NOT_LAST;
10486
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010487 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010488 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010489 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.str, &smp->data.u.str.len))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010490 return 1;
10491
10492 smp->flags &= ~SMP_F_NOT_LAST;
10493 return 0;
10494}
10495
10496/* 6. Check on HTTP header count. The number of occurrences is returned.
10497 * Accepts exactly 1 argument of type string. It does not stop on commas and
10498 * returns full lines instead (useful for User-Agent or Date for example).
10499 */
10500static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010501smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010502{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010503 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010504 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010505 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010506 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010507 const char *name = NULL;
10508 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010509
Willy Tarreau601a4d12015-04-01 19:16:09 +020010510 if (args && args->type == ARGT_STR) {
10511 name = args->data.str.str;
10512 len = args->data.str.len;
10513 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010514
10515 CHECK_HTTP_MESSAGE_FIRST();
10516
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010517 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010518 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010519
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010520 ctx.idx = 0;
10521 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010522 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010523 cnt++;
10524
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010525 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010526 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010527 smp->flags = SMP_F_VOL_HDR;
10528 return 1;
10529}
10530
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010531static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010532smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010533{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010534 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010535 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010536 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010537 struct chunk *temp;
10538 char del = ',';
10539
10540 if (args && args->type == ARGT_STR)
10541 del = *args[0].data.str.str;
10542
10543 CHECK_HTTP_MESSAGE_FIRST();
10544
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010545 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010546 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010547
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010548 temp = get_trash_chunk();
10549
10550 ctx.idx = 0;
10551 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10552 if (temp->len)
10553 temp->str[temp->len++] = del;
10554 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10555 temp->len += ctx.del;
10556 }
10557
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010558 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010559 smp->data.u.str.str = temp->str;
10560 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010561 smp->flags = SMP_F_VOL_HDR;
10562 return 1;
10563}
10564
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010565/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10566 * Accepts an optional argument of type string containing the header field name,
10567 * and an optional argument of type signed or unsigned integer to request an
10568 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010569 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010570 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010571static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010572smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010573{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010574 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010575 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010576 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010577 int occ = 0;
10578 const char *name_str = NULL;
10579 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010580
Willy Tarreaua890d072013-04-02 12:01:06 +020010581 if (!ctx) {
10582 /* first call */
10583 ctx = &static_hdr_ctx;
10584 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010585 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010586 }
10587
Willy Tarreau185b5c42012-04-26 15:11:51 +020010588 if (args) {
10589 if (args[0].type != ARGT_STR)
10590 return 0;
10591 name_str = args[0].data.str.str;
10592 name_len = args[0].data.str.len;
10593
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010594 if (args[1].type == ARGT_SINT)
10595 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010596 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010597
Willy Tarreaue333ec92012-04-16 16:26:40 +020010598 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010599
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010600 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010601 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010602
Willy Tarreau185b5c42012-04-26 15:11:51 +020010603 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010604 /* search for header from the beginning */
10605 ctx->idx = 0;
10606
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010607 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010608 /* no explicit occurrence and single fetch => last header by default */
10609 occ = -1;
10610
10611 if (!occ)
10612 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010613 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010614
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010615 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010616 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010617 if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.str, &smp->data.u.str.len))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010618 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010619
Willy Tarreau37406352012-04-23 16:16:37 +020010620 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010621 return 0;
10622}
10623
Willy Tarreauc11416f2007-06-17 16:58:38 +020010624/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010625 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010626 */
10627static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010628smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010629{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010630 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010631 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010632 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010633 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010634 const char *name = NULL;
10635 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010636
Willy Tarreau601a4d12015-04-01 19:16:09 +020010637 if (args && args->type == ARGT_STR) {
10638 name = args->data.str.str;
10639 len = args->data.str.len;
10640 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010641
Willy Tarreaue333ec92012-04-16 16:26:40 +020010642 CHECK_HTTP_MESSAGE_FIRST();
10643
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010644 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010645 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010646
Willy Tarreau33a7e692007-06-10 19:45:56 +020010647 ctx.idx = 0;
10648 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010649 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010650 cnt++;
10651
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010652 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010653 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010654 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010655 return 1;
10656}
10657
Willy Tarreau185b5c42012-04-26 15:11:51 +020010658/* Fetch an HTTP header's integer value. The integer value is returned. It
10659 * takes a mandatory argument of type string and an optional one of type int
10660 * to designate a specific occurrence. It returns an unsigned integer, which
10661 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010662 */
10663static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010664smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010665{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010666 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010667
Willy Tarreauf853c462012-04-23 18:53:56 +020010668 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010669 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010670 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010671 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010672
Willy Tarreaud53e2422012-04-16 17:21:11 +020010673 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010674}
10675
Cyril Bonté69fa9922012-10-25 00:01:06 +020010676/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10677 * and an optional one of type int to designate a specific occurrence.
10678 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010679 */
10680static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010681smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010682{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010683 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010684
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010685 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010686 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010687 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010688 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010689 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010690 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010691 if (smp->data.u.str.len < temp->size - 1) {
10692 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10693 temp->str[smp->data.u.str.len] = '\0';
10694 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010695 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010696 break;
10697 }
10698 }
10699 }
10700
Willy Tarreaud53e2422012-04-16 17:21:11 +020010701 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010702 if (!(smp->flags & SMP_F_NOT_LAST))
10703 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010704 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010705 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010706}
10707
Willy Tarreau737b0c12007-06-10 21:28:46 +020010708/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10709 * the first '/' after the possible hostname, and ends before the possible '?'.
10710 */
10711static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010712smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010713{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010714 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010715 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010716
Willy Tarreauc0239e02012-04-16 14:42:55 +020010717 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010718
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010719 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010720 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010721 ptr = http_get_path(txn);
10722 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010723 return 0;
10724
10725 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010726 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010727 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010728
10729 while (ptr < end && *ptr != '?')
10730 ptr++;
10731
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010732 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010733 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010734 return 1;
10735}
10736
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010737/* This produces a concatenation of the first occurrence of the Host header
10738 * followed by the path component if it begins with a slash ('/'). This means
10739 * that '*' will not be added, resulting in exactly the first Host entry.
10740 * If no Host header is found, then the path is returned as-is. The returned
10741 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010742 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010743 */
10744static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010745smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010746{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010747 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010748 char *ptr, *end, *beg;
10749 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010750 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010751
10752 CHECK_HTTP_MESSAGE_FIRST();
10753
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010754 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010755 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010756 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010757 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010758
10759 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010760 temp = get_trash_chunk();
10761 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010762 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010763 smp->data.u.str.str = temp->str;
10764 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010765
10766 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010767 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010768 beg = http_get_path(txn);
10769 if (!beg)
10770 beg = end;
10771
10772 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10773
10774 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010775 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10776 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010777 }
10778
10779 smp->flags = SMP_F_VOL_1ST;
10780 return 1;
10781}
10782
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010783/* This produces a 32-bit hash of the concatenation of the first occurrence of
10784 * the Host header followed by the path component if it begins with a slash ('/').
10785 * This means that '*' will not be added, resulting in exactly the first Host
10786 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010787 * is hashed using the path hash followed by a full avalanche hash and provides a
10788 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010789 * high-traffic sites without having to store whole paths.
10790 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010791int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010792smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010793{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010794 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010795 struct hdr_ctx ctx;
10796 unsigned int hash = 0;
10797 char *ptr, *beg, *end;
10798 int len;
10799
10800 CHECK_HTTP_MESSAGE_FIRST();
10801
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010802 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010803 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010804 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010805 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10806 ptr = ctx.line + ctx.val;
10807 len = ctx.vlen;
10808 while (len--)
10809 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10810 }
10811
10812 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010813 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010814 beg = http_get_path(txn);
10815 if (!beg)
10816 beg = end;
10817
10818 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10819
10820 if (beg < ptr && *beg == '/') {
10821 while (beg < ptr)
10822 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10823 }
10824 hash = full_hash(hash);
10825
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010826 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010827 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010828 smp->flags = SMP_F_VOL_1ST;
10829 return 1;
10830}
10831
Willy Tarreau4a550602012-12-09 14:53:32 +010010832/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010833 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10834 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10835 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010836 * that in environments where IPv6 is insignificant, truncating the output to
10837 * 8 bytes would still work.
10838 */
10839static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010840smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010841{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010842 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010843 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010844
10845 if (!cli_conn)
10846 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010847
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010848 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010849 return 0;
10850
Willy Tarreau47ca5452012-12-23 20:22:19 +010010851 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010852 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010853 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010854
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010855 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010856 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010857 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010858 temp->len += 4;
10859 break;
10860 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010861 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010862 temp->len += 16;
10863 break;
10864 default:
10865 return 0;
10866 }
10867
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010868 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010869 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010870 return 1;
10871}
10872
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010873/* Extracts the query string, which comes after the question mark '?'. If no
10874 * question mark is found, nothing is returned. Otherwise it returns a sample
10875 * of type string carrying the whole query string.
10876 */
10877static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010878smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010879{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010880 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010881 char *ptr, *end;
10882
10883 CHECK_HTTP_MESSAGE_FIRST();
10884
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010885 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010886 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10887 end = ptr + txn->req.sl.rq.u_l;
10888
10889 /* look up the '?' */
10890 do {
10891 if (ptr == end)
10892 return 0;
10893 } while (*ptr++ != '?');
10894
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010895 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010896 smp->data.u.str.str = ptr;
10897 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010898 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10899 return 1;
10900}
10901
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010902static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010903smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010904{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010905 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10906 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10907 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010908
Willy Tarreau24e32d82012-04-23 23:55:44 +020010909 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010910
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010911 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010912 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010913 return 1;
10914}
10915
Willy Tarreau7f18e522010-10-22 20:04:13 +020010916/* return a valid test if the current request is the first one on the connection */
10917static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010918smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010919{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010920 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010921 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010922 return 1;
10923}
10924
Willy Tarreau34db1082012-04-19 17:16:54 +020010925/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010926static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010927smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010928{
10929
Willy Tarreau24e32d82012-04-23 23:55:44 +020010930 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010931 return 0;
10932
Willy Tarreauc0239e02012-04-16 14:42:55 +020010933 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010934
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010935 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010936 return 0;
10937
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010938 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010939 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010940 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010941 return 1;
10942}
Willy Tarreau8797c062007-05-07 00:55:35 +020010943
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010944/* Accepts exactly 1 argument of type userlist */
10945static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010946smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010947{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010948 if (!args || args->type != ARGT_USR)
10949 return 0;
10950
10951 CHECK_HTTP_MESSAGE_FIRST();
10952
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010953 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010954 return 0;
10955
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010956 /* if the user does not belong to the userlist or has a wrong password,
10957 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010958 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010959 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010960 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10961 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010962 return 0;
10963
10964 /* pat_match_auth() will need the user list */
10965 smp->ctx.a[0] = args->data.usr;
10966
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010967 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010968 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010969 smp->data.u.str.str = smp->strm->txn->auth.user;
10970 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010971
10972 return 1;
10973}
10974
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010975/* Try to find the next occurrence of a cookie name in a cookie header value.
10976 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10977 * the cookie value is returned into *value and *value_l, and the function
10978 * returns a pointer to the next pointer to search from if the value was found.
10979 * Otherwise if the cookie was not found, NULL is returned and neither value
10980 * nor value_l are touched. The input <hdr> string should first point to the
10981 * header's value, and the <hdr_end> pointer must point to the first character
10982 * not part of the value. <list> must be non-zero if value may represent a list
10983 * of values (cookie headers). This makes it faster to abort parsing when no
10984 * list is expected.
10985 */
10986static char *
10987extract_cookie_value(char *hdr, const char *hdr_end,
10988 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010989 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010990{
10991 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10992 char *next;
10993
10994 /* we search at least a cookie name followed by an equal, and more
10995 * generally something like this :
10996 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10997 */
10998 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10999 /* Iterate through all cookies on this line */
11000
11001 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
11002 att_beg++;
11003
11004 /* find att_end : this is the first character after the last non
11005 * space before the equal. It may be equal to hdr_end.
11006 */
11007 equal = att_end = att_beg;
11008
11009 while (equal < hdr_end) {
11010 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
11011 break;
11012 if (http_is_spht[(unsigned char)*equal++])
11013 continue;
11014 att_end = equal;
11015 }
11016
11017 /* here, <equal> points to '=', a delimitor or the end. <att_end>
11018 * is between <att_beg> and <equal>, both may be identical.
11019 */
11020
11021 /* look for end of cookie if there is an equal sign */
11022 if (equal < hdr_end && *equal == '=') {
11023 /* look for the beginning of the value */
11024 val_beg = equal + 1;
11025 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
11026 val_beg++;
11027
11028 /* find the end of the value, respecting quotes */
11029 next = find_cookie_value_end(val_beg, hdr_end);
11030
11031 /* make val_end point to the first white space or delimitor after the value */
11032 val_end = next;
11033 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
11034 val_end--;
11035 } else {
11036 val_beg = val_end = next = equal;
11037 }
11038
11039 /* We have nothing to do with attributes beginning with '$'. However,
11040 * they will automatically be removed if a header before them is removed,
11041 * since they're supposed to be linked together.
11042 */
11043 if (*att_beg == '$')
11044 continue;
11045
11046 /* Ignore cookies with no equal sign */
11047 if (equal == next)
11048 continue;
11049
11050 /* Now we have the cookie name between att_beg and att_end, and
11051 * its value between val_beg and val_end.
11052 */
11053
11054 if (att_end - att_beg == cookie_name_l &&
11055 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
11056 /* let's return this value and indicate where to go on from */
11057 *value = val_beg;
11058 *value_l = val_end - val_beg;
11059 return next + 1;
11060 }
11061
11062 /* Set-Cookie headers only have the name in the first attr=value part */
11063 if (!list)
11064 break;
11065 }
11066
11067 return NULL;
11068}
11069
William Lallemanda43ba4e2014-01-28 18:14:25 +010011070/* Fetch a captured HTTP request header. The index is the position of
11071 * the "capture" option in the configuration file
11072 */
11073static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011074smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011075{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011076 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011077 int idx;
11078
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011079 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011080 return 0;
11081
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011082 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011083
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011084 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011085 return 0;
11086
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011087 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011088 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011089 smp->data.u.str.str = smp->strm->req_cap[idx];
11090 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011091
11092 return 1;
11093}
11094
11095/* Fetch a captured HTTP response header. The index is the position of
11096 * the "capture" option in the configuration file
11097 */
11098static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011099smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011100{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011101 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011102 int idx;
11103
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011104 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011105 return 0;
11106
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011107 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011108
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011109 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011110 return 0;
11111
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011112 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011113 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011114 smp->data.u.str.str = smp->strm->res_cap[idx];
11115 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011116
11117 return 1;
11118}
11119
William Lallemand65ad6e12014-01-31 15:08:02 +010011120/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
11121static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011122smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011123{
11124 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011125 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010011126 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011127
Willy Tarreau15e91e12015-04-04 00:52:09 +020011128 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011129 return 0;
11130
William Lallemand96a77852014-02-05 00:30:02 +010011131 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011132
William Lallemand96a77852014-02-05 00:30:02 +010011133 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11134 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011135
William Lallemand96a77852014-02-05 00:30:02 +010011136 temp = get_trash_chunk();
11137 temp->str = txn->uri;
11138 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011139 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011140 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011141 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011142
11143 return 1;
11144
11145}
11146
11147/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
11148static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011149smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011150{
11151 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011152 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010011153 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011154
Willy Tarreau15e91e12015-04-04 00:52:09 +020011155 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011156 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010011157
William Lallemand65ad6e12014-01-31 15:08:02 +010011158 ptr = txn->uri;
11159
11160 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11161 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010011162
William Lallemand65ad6e12014-01-31 15:08:02 +010011163 if (!*ptr)
11164 return 0;
11165
11166 ptr++; /* skip the space */
11167
11168 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010011169 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010011170 if (!ptr)
11171 return 0;
11172 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
11173 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011174
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011175 smp->data.u.str = *temp;
11176 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011177 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011178 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011179
11180 return 1;
11181}
11182
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011183/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
11184 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11185 */
11186static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011187smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011188{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011189 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011190
Willy Tarreau15e91e12015-04-04 00:52:09 +020011191 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011192 return 0;
11193
11194 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011195 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011196 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011197 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011198
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011199 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011200 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011201 smp->flags = SMP_F_CONST;
11202 return 1;
11203
11204}
11205
11206/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
11207 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11208 */
11209static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011210smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011211{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011212 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011213
Willy Tarreau15e91e12015-04-04 00:52:09 +020011214 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011215 return 0;
11216
11217 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011218 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011219 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011220 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011221
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011222 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011223 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011224 smp->flags = SMP_F_CONST;
11225 return 1;
11226
11227}
11228
William Lallemand65ad6e12014-01-31 15:08:02 +010011229
Willy Tarreaue333ec92012-04-16 16:26:40 +020011230/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011231 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011232 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011233 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011234 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011235 * Accepts exactly 1 argument of type string. If the input options indicate
11236 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011237 * The returned sample is of type CSTR. Can be used to parse cookies in other
11238 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011239 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011240int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011241{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011242 struct http_txn *txn;
11243 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011244 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011245 const struct http_msg *msg;
11246 const char *hdr_name;
11247 int hdr_name_len;
11248 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011249 int occ = 0;
11250 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011251
Willy Tarreau24e32d82012-04-23 23:55:44 +020011252 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011253 return 0;
11254
Willy Tarreaua890d072013-04-02 12:01:06 +020011255 if (!ctx) {
11256 /* first call */
11257 ctx = &static_hdr_ctx;
11258 ctx->idx = 0;
11259 smp->ctx.a[2] = ctx;
11260 }
11261
Willy Tarreaue333ec92012-04-16 16:26:40 +020011262 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011263
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011264 txn = smp->strm->txn;
11265 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011266
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011267 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011268 msg = &txn->req;
11269 hdr_name = "Cookie";
11270 hdr_name_len = 6;
11271 } else {
11272 msg = &txn->rsp;
11273 hdr_name = "Set-Cookie";
11274 hdr_name_len = 10;
11275 }
11276
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011277 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020011278 /* no explicit occurrence and single fetch => last cookie by default */
11279 occ = -1;
11280
11281 /* OK so basically here, either we want only one value and it's the
11282 * last one, or we want to iterate over all of them and we fetch the
11283 * next one.
11284 */
11285
Willy Tarreau9b28e032012-10-12 23:49:43 +020011286 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011287 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011288 /* search for the header from the beginning, we must first initialize
11289 * the search parameters.
11290 */
Willy Tarreau37406352012-04-23 16:16:37 +020011291 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011292 ctx->idx = 0;
11293 }
11294
Willy Tarreau28376d62012-04-26 21:26:10 +020011295 smp->flags |= SMP_F_VOL_HDR;
11296
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011297 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011298 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11299 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011300 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11301 goto out;
11302
Willy Tarreau24e32d82012-04-23 23:55:44 +020011303 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011304 continue;
11305
Willy Tarreau37406352012-04-23 16:16:37 +020011306 smp->ctx.a[0] = ctx->line + ctx->val;
11307 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011308 }
11309
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011310 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011311 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011312 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011313 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011314 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011315 &smp->data.u.str.str,
11316 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011317 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011318 found = 1;
11319 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011320 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020011321 smp->flags |= SMP_F_NOT_LAST;
11322 return 1;
11323 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011324 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011325 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011326 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011327 /* all cookie headers and values were scanned. If we're looking for the
11328 * last occurrence, we may return it now.
11329 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011330 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011331 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011332 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011333}
11334
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011335/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011336 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011337 * multiple cookies may be parsed on the same line. The returned sample is of
11338 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011339 */
11340static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011341smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011342{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011343 struct http_txn *txn;
11344 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011345 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011346 const struct http_msg *msg;
11347 const char *hdr_name;
11348 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011349 int cnt;
11350 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011351 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011352
Willy Tarreau24e32d82012-04-23 23:55:44 +020011353 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011354 return 0;
11355
Willy Tarreaue333ec92012-04-16 16:26:40 +020011356 CHECK_HTTP_MESSAGE_FIRST();
11357
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011358 txn = smp->strm->txn;
11359 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011360
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011361 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011362 msg = &txn->req;
11363 hdr_name = "Cookie";
11364 hdr_name_len = 6;
11365 } else {
11366 msg = &txn->rsp;
11367 hdr_name = "Set-Cookie";
11368 hdr_name_len = 10;
11369 }
11370
Willy Tarreau9b28e032012-10-12 23:49:43 +020011371 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011372 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011373 ctx.idx = 0;
11374 cnt = 0;
11375
11376 while (1) {
11377 /* Note: val_beg == NULL every time we need to fetch a new header */
11378 if (!val_beg) {
11379 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11380 break;
11381
Willy Tarreau24e32d82012-04-23 23:55:44 +020011382 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011383 continue;
11384
11385 val_beg = ctx.line + ctx.val;
11386 val_end = val_beg + ctx.vlen;
11387 }
11388
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011389 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011390 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011391 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011392 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011393 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011394 &smp->data.u.str.str,
11395 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011396 cnt++;
11397 }
11398 }
11399
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011400 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011401 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011402 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011403 return 1;
11404}
11405
Willy Tarreau51539362012-05-08 12:46:28 +020011406/* Fetch an cookie's integer value. The integer value is returned. It
11407 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11408 */
11409static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011410smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011411{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011412 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011413
11414 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011415 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011416 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020011417 }
11418
11419 return ret;
11420}
11421
Willy Tarreau8797c062007-05-07 00:55:35 +020011422/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011423/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011424/************************************************************************/
11425
David Cournapeau16023ee2010-12-23 20:55:41 +090011426/*
11427 * Given a path string and its length, find the position of beginning of the
11428 * query string. Returns NULL if no query string is found in the path.
11429 *
11430 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11431 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011432 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011433 */
bedis4c75cca2012-10-05 08:38:24 +020011434static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011435{
11436 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011437
bedis4c75cca2012-10-05 08:38:24 +020011438 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011439 return p ? p + 1 : NULL;
11440}
11441
bedis4c75cca2012-10-05 08:38:24 +020011442static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011443{
bedis4c75cca2012-10-05 08:38:24 +020011444 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011445}
11446
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011447/* after increasing a pointer value, it can exceed the first buffer
11448 * size. This function transform the value of <ptr> according with
11449 * the expected position. <chunks> is an array of the one or two
11450 * avalaible chunks. The first value is the start of the first chunk,
11451 * the second value if the end+1 of the first chunks. The third value
11452 * is NULL or the start of the second chunk and the fourth value is
11453 * the end+1 of the second chunk. The function returns 1 if does a
11454 * wrap, else returns 0.
11455 */
11456static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11457{
11458 if (*ptr < chunks[1])
11459 return 0;
11460 if (!chunks[2])
11461 return 0;
11462 *ptr = chunks[2] + ( *ptr - chunks[1] );
11463 return 1;
11464}
11465
David Cournapeau16023ee2010-12-23 20:55:41 +090011466/*
11467 * Given a url parameter, find the starting position of the first occurence,
11468 * or NULL if the parameter is not found.
11469 *
11470 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11471 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011472 *
11473 * Warning:this function returns a pointer that can be point to the first chunk
11474 * or the second chunk. The caller must be check the position before using the
11475 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011476 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011477static const char *
11478find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011479 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011480 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011481{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011482 const char *pos, *last, *equal;
11483 const char **bufs = chunks;
11484 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011485
David Cournapeau16023ee2010-12-23 20:55:41 +090011486
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011487 pos = bufs[0];
11488 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011489 while (pos <= last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011490 /* Check the equal. */
11491 equal = pos + url_param_name_l;
11492 if (fix_pointer_if_wrap(chunks, &equal)) {
11493 if (equal >= chunks[3])
11494 return NULL;
11495 } else {
11496 if (equal >= chunks[1])
11497 return NULL;
11498 }
11499 if (*equal == '=') {
11500 if (pos + url_param_name_l > last) {
11501 /* process wrap case, we detect a wrap. In this case, the
11502 * comparison is performed in two parts.
11503 */
11504
11505 /* This is the end, we dont have any other chunk. */
11506 if (bufs != chunks || !bufs[2])
11507 return NULL;
11508
11509 /* Compute the length of each part of the comparison. */
11510 l1 = last - pos;
11511 l2 = url_param_name_l - l1;
11512
11513 /* The second buffer is too short to contain the compared string. */
11514 if (bufs[2] + l2 > bufs[3])
11515 return NULL;
11516
11517 if (memcmp(pos, url_param_name, l1) == 0 &&
11518 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11519 return pos;
11520
11521 /* Perform wrapping and jump the string who fail the comparison. */
11522 bufs += 2;
11523 pos = bufs[0] + l2;
11524 last = bufs[1];
11525
11526 } else {
11527 /* process a simple comparison. */
11528 if (memcmp(pos, url_param_name, url_param_name_l) == 0) {
11529 return pos; }
11530 pos += url_param_name_l + 1;
11531 if (fix_pointer_if_wrap(chunks, &pos))
11532 last = bufs[2];
11533 }
11534 }
11535
11536 while (1) {
11537 /* Look for the next delimiter. */
11538 while (pos <= last && !is_param_delimiter(*pos, delim))
11539 pos++;
11540 if (pos < last)
11541 break;
11542 /* process buffer wrapping. */
11543 if (bufs != chunks || !bufs[2])
11544 return NULL;
11545 bufs += 2;
11546 pos = bufs[0];
11547 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011548 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011549 pos++;
11550 }
11551 return NULL;
11552}
11553
11554/*
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011555 * Given a url parameter name and a query string, returns its value and size
11556 * into *value and *value_l respectively, and returns non-zero. An empty
11557 * url_param_name matches the first available parameter. If the parameter is
11558 * not found, zero is returned and value/value_l are not touched.
David Cournapeau16023ee2010-12-23 20:55:41 +090011559 */
11560static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011561find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011562 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011563 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011564{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011565 const char *arg_start, *qs_end;
11566 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011567
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011568 arg_start = chunks[0];
11569 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011570 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011571 /* Looks for an argument name. */
11572 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011573 url_param_name, url_param_name_l,
11574 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011575 /* Check for wrapping. */
11576 if (arg_start > qs_end)
11577 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011578 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011579 if (!arg_start)
11580 return 0;
11581
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011582 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011583 while (1) {
11584 /* looks for the first argument. */
11585 value_start = memchr(arg_start, '=', qs_end - arg_start);
11586 if (!value_start) {
11587
11588 /* Check for wrapping. */
11589 if (arg_start >= chunks[0] &&
11590 arg_start <= chunks[1] &&
11591 chunks[2]) {
11592 arg_start = chunks[2];
11593 qs_end = chunks[3];
11594 continue;
11595 }
11596 return 0;
11597 }
11598 break;
11599 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011600 value_start++;
11601 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011602 else {
11603 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011604 value_start = arg_start + url_param_name_l + 1;
11605
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011606 /* Check for pointer wrapping. */
11607 if (fix_pointer_if_wrap(chunks, &value_start)) {
11608 /* Update the end pointer. */
11609 qs_end = chunks[3];
11610
11611 /* Check for overflow. */
11612 if (value_start > qs_end)
11613 return 0;
11614 }
11615 }
11616
David Cournapeau16023ee2010-12-23 20:55:41 +090011617 value_end = value_start;
11618
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011619 while (1) {
11620 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11621 value_end++;
11622 if (value_end < qs_end)
11623 break;
11624 /* process buffer wrapping. */
11625 if (value_end >= chunks[0] &&
11626 value_end <= chunks[1] &&
11627 chunks[2]) {
11628 value_end = chunks[2];
11629 qs_end = chunks[3];
11630 continue;
11631 }
11632 break;
11633 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011634
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011635 *vstart = value_start;
11636 *vend = value_end;
Willy Tarreau00134332011-01-04 14:57:34 +010011637 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090011638}
11639
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011640/* This scans a URL-encoded query string. It takes an optionally wrapping
11641 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11642 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11643 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011644 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011645static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011646smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
David Cournapeau16023ee2010-12-23 20:55:41 +090011647{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011648 const char *vstart, *vend;
11649 struct chunk *temp;
11650 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011651
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011652 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011653 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011654 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011655 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011656 return 0;
11657
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011658 /* Create sample. If the value is contiguous, return the pointer as CONST,
11659 * if the value is wrapped, copy-it in a buffer.
11660 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011661 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011662 if (chunks[2] &&
11663 vstart >= chunks[0] && vstart <= chunks[1] &&
11664 vend >= chunks[2] && vend <= chunks[3]) {
11665 /* Wrapped case. */
11666 temp = get_trash_chunk();
11667 memcpy(temp->str, vstart, chunks[1] - vstart);
11668 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011669 smp->data.u.str.str = temp->str;
11670 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011671 } else {
11672 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011673 smp->data.u.str.str = (char *)vstart;
11674 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011675 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11676 }
11677
11678 /* Update context, check wrapping. */
11679 chunks[0] = vend;
11680 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11681 chunks[1] = chunks[3];
11682 chunks[2] = NULL;
11683 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011684
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011685 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011686 smp->flags |= SMP_F_NOT_LAST;
11687
David Cournapeau16023ee2010-12-23 20:55:41 +090011688 return 1;
11689}
11690
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011691/* This function iterates over each parameter of the query string. It uses
11692 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011693 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11694 * An optional parameter name is passed in args[0], otherwise any parameter is
11695 * considered. It supports an optional delimiter argument for the beginning of
11696 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011697 */
11698static int
11699smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11700{
11701 struct http_msg *msg;
11702 char delim = '?';
11703 const char *name;
11704 int name_len;
11705
Dragan Dosen26f77e52015-05-25 10:02:11 +020011706 if (!args ||
11707 (args[0].type && args[0].type != ARGT_STR) ||
11708 (args[1].type && args[1].type != ARGT_STR))
11709 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011710
Dragan Dosen26f77e52015-05-25 10:02:11 +020011711 name = "";
11712 name_len = 0;
11713 if (args->type == ARGT_STR) {
11714 name = args->data.str.str;
11715 name_len = args->data.str.len;
11716 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011717
Dragan Dosen26f77e52015-05-25 10:02:11 +020011718 if (args[1].type)
11719 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011720
Dragan Dosen26f77e52015-05-25 10:02:11 +020011721 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011722 CHECK_HTTP_MESSAGE_FIRST();
11723
11724 msg = &smp->strm->txn->req;
11725
11726 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11727 msg->sl.rq.u_l, delim);
11728 if (!smp->ctx.a[0])
11729 return 0;
11730
11731 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011732
11733 /* Assume that the context is filled with NULL pointer
11734 * before the first call.
11735 * smp->ctx.a[2] = NULL;
11736 * smp->ctx.a[3] = NULL;
11737 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011738 }
11739
11740 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11741}
11742
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011743/* This function iterates over each parameter of the body. This requires
11744 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011745 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11746 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11747 * optional second part if the body wraps at the end of the buffer. An optional
11748 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011749 */
11750static int
11751smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11752{
11753 struct http_txn *txn = smp->strm->txn;
11754 struct http_msg *msg;
11755 unsigned long len;
11756 unsigned long block1;
11757 char *body;
11758 const char *name;
11759 int name_len;
11760
11761 if (!args || (args[0].type && args[0].type != ARGT_STR))
11762 return 0;
11763
11764 name = "";
11765 name_len = 0;
11766 if (args[0].type == ARGT_STR) {
11767 name = args[0].data.str.str;
11768 name_len = args[0].data.str.len;
11769 }
11770
11771 if (!smp->ctx.a[0]) { // first call, find the query string
11772 CHECK_HTTP_MESSAGE_FIRST();
11773
11774 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
11775 msg = &txn->req;
11776 else
11777 msg = &txn->rsp;
11778
11779 len = http_body_bytes(msg);
11780 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11781
11782 block1 = len;
11783 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11784 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11785
11786 if (block1 == len) {
11787 /* buffer is not wrapped (or empty) */
11788 smp->ctx.a[0] = body;
11789 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011790
11791 /* Assume that the context is filled with NULL pointer
11792 * before the first call.
11793 * smp->ctx.a[2] = NULL;
11794 * smp->ctx.a[3] = NULL;
11795 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011796 }
11797 else {
11798 /* buffer is wrapped, we need to defragment it */
11799 smp->ctx.a[0] = body;
11800 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011801 smp->ctx.a[2] = msg->chn->buf->data;
11802 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011803 }
11804 }
11805 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11806}
11807
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011808/* Return the signed integer value for the specified url parameter (see url_param
11809 * above).
11810 */
11811static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011812smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011813{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011814 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011815
11816 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011817 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011818 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011819 }
11820
11821 return ret;
11822}
11823
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011824/* This produces a 32-bit hash of the concatenation of the first occurrence of
11825 * the Host header followed by the path component if it begins with a slash ('/').
11826 * This means that '*' will not be added, resulting in exactly the first Host
11827 * entry. If no Host header is found, then the path is used. The resulting value
11828 * is hashed using the url hash followed by a full avalanche hash and provides a
11829 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11830 * high-traffic sites without having to store whole paths.
11831 * this differs from the base32 functions in that it includes the url parameters
11832 * as well as the path
11833 */
11834static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011835smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011836{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011837 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011838 struct hdr_ctx ctx;
11839 unsigned int hash = 0;
11840 char *ptr, *beg, *end;
11841 int len;
11842
11843 CHECK_HTTP_MESSAGE_FIRST();
11844
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011845 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011846 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011847 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011848 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11849 ptr = ctx.line + ctx.val;
11850 len = ctx.vlen;
11851 while (len--)
11852 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11853 }
11854
11855 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011856 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011857 beg = http_get_path(txn);
11858 if (!beg)
11859 beg = end;
11860
11861 for (ptr = beg; ptr < end ; ptr++);
11862
11863 if (beg < ptr && *beg == '/') {
11864 while (beg < ptr)
11865 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11866 }
11867 hash = full_hash(hash);
11868
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011869 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011870 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011871 smp->flags = SMP_F_VOL_1ST;
11872 return 1;
11873}
11874
11875/* This concatenates the source address with the 32-bit hash of the Host and
11876 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11877 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11878 * on the source address length. The URL hash is stored before the address so
11879 * that in environments where IPv6 is insignificant, truncating the output to
11880 * 8 bytes would still work.
11881 */
11882static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011883smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011884{
11885 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011886 struct connection *cli_conn = objt_conn(smp->sess->origin);
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011887 unsigned int hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011888
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011889 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011890 return 0;
11891
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011892 /* The returned hash is a 32 bytes integer. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011893 hash = smp->data.u.sint;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011894
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011895 temp = get_trash_chunk();
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011896 memcpy(temp->str + temp->len, &hash, sizeof(hash));
11897 temp->len += sizeof(hash);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011898
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011899 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011900 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011901 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011902 temp->len += 4;
11903 break;
11904 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011905 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011906 temp->len += 16;
11907 break;
11908 default:
11909 return 0;
11910 }
11911
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011912 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011913 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011914 return 1;
11915}
11916
Willy Tarreau185b5c42012-04-26 15:11:51 +020011917/* This function is used to validate the arguments passed to any "hdr" fetch
11918 * keyword. These keywords support an optional positive or negative occurrence
11919 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11920 * is assumed that the types are already the correct ones. Returns 0 on error,
11921 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11922 * error message in case of error, that the caller is responsible for freeing.
11923 * The initial location must either be freeable or NULL.
11924 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011925int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011926{
11927 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011928 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011929 return 0;
11930 }
11931 return 1;
11932}
11933
Willy Tarreau276fae92013-07-25 14:36:01 +020011934/* takes an UINT value on input supposed to represent the time since EPOCH,
11935 * adds an optional offset found in args[0] and emits a string representing
11936 * the date in RFC-1123/5322 format.
11937 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011938static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011939{
11940 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
11941 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11942 struct chunk *temp;
11943 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011944 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011945 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020011946
11947 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011948 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020011949 curr_date += args[0].data.sint;
11950
11951 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020011952 if (!tm)
11953 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011954
11955 temp = get_trash_chunk();
11956 temp->len = snprintf(temp->str, temp->size - temp->len,
11957 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11958 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11959 tm->tm_hour, tm->tm_min, tm->tm_sec);
11960
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011961 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011962 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020011963 return 1;
11964}
11965
Thierry FOURNIERad903512014-04-11 17:51:01 +020011966/* Match language range with language tag. RFC2616 14.4:
11967 *
11968 * A language-range matches a language-tag if it exactly equals
11969 * the tag, or if it exactly equals a prefix of the tag such
11970 * that the first tag character following the prefix is "-".
11971 *
11972 * Return 1 if the strings match, else return 0.
11973 */
11974static inline int language_range_match(const char *range, int range_len,
11975 const char *tag, int tag_len)
11976{
11977 const char *end = range + range_len;
11978 const char *tend = tag + tag_len;
11979 while (range < end) {
11980 if (*range == '-' && tag == tend)
11981 return 1;
11982 if (*range != *tag || tag == tend)
11983 return 0;
11984 range++;
11985 tag++;
11986 }
11987 /* Return true only if the last char of the tag is matched. */
11988 return tag == tend;
11989}
11990
11991/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011992static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011993{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011994 const char *al = smp->data.u.str.str;
11995 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011996 const char *token;
11997 int toklen;
11998 int qvalue;
11999 const char *str;
12000 const char *w;
12001 int best_q = 0;
12002
12003 /* Set the constant to the sample, because the output of the
12004 * function will be peek in the constant configuration string.
12005 */
12006 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012007 smp->data.u.str.size = 0;
12008 smp->data.u.str.str = "";
12009 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012010
12011 /* Parse the accept language */
12012 while (1) {
12013
12014 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012015 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012016 al++;
12017 if (al >= end)
12018 break;
12019
12020 /* Start of the fisrt word. */
12021 token = al;
12022
12023 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012024 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012025 al++;
12026 if (al == token)
12027 goto expect_comma;
12028
12029 /* Length of the token. */
12030 toklen = al - token;
12031 qvalue = 1000;
12032
12033 /* Check if the token exists in the list. If the token not exists,
12034 * jump to the next token.
12035 */
12036 str = args[0].data.str.str;
12037 w = str;
12038 while (1) {
12039 if (*str == ';' || *str == '\0') {
12040 if (language_range_match(token, toklen, w, str-w))
12041 goto look_for_q;
12042 if (*str == '\0')
12043 goto expect_comma;
12044 w = str + 1;
12045 }
12046 str++;
12047 }
12048 goto expect_comma;
12049
12050look_for_q:
12051
12052 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012053 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012054 al++;
12055 if (al >= end)
12056 goto process_value;
12057
12058 /* If ',' is found, process the result */
12059 if (*al == ',')
12060 goto process_value;
12061
12062 /* If the character is different from ';', look
12063 * for the end of the header part in best effort.
12064 */
12065 if (*al != ';')
12066 goto expect_comma;
12067
12068 /* Assumes that the char is ';', now expect "q=". */
12069 al++;
12070
12071 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012072 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012073 al++;
12074 if (al >= end)
12075 goto process_value;
12076
12077 /* Expect 'q'. If no 'q', continue in best effort */
12078 if (*al != 'q')
12079 goto process_value;
12080 al++;
12081
12082 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012083 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012084 al++;
12085 if (al >= end)
12086 goto process_value;
12087
12088 /* Expect '='. If no '=', continue in best effort */
12089 if (*al != '=')
12090 goto process_value;
12091 al++;
12092
12093 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012094 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012095 al++;
12096 if (al >= end)
12097 goto process_value;
12098
12099 /* Parse the q value. */
12100 qvalue = parse_qvalue(al, &al);
12101
12102process_value:
12103
12104 /* If the new q value is the best q value, then store the associated
12105 * language in the response. If qvalue is the biggest value (1000),
12106 * break the process.
12107 */
12108 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012109 smp->data.u.str.str = (char *)w;
12110 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012111 if (qvalue >= 1000)
12112 break;
12113 best_q = qvalue;
12114 }
12115
12116expect_comma:
12117
12118 /* Expect comma or end. If the end is detected, quit the loop. */
12119 while (al < end && *al != ',')
12120 al++;
12121 if (al >= end)
12122 break;
12123
12124 /* Comma is found, jump it and restart the analyzer. */
12125 al++;
12126 }
12127
12128 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012129 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
12130 smp->data.u.str.str = args[1].data.str.str;
12131 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012132 }
12133
12134 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012135 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012136}
12137
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012138/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012139static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012140{
12141 /* If the constant flag is set or if not size is avalaible at
12142 * the end of the buffer, copy the string in other buffer
12143 * before decoding.
12144 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012145 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012146 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012147 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
12148 smp->data.u.str.str = str->str;
12149 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012150 smp->flags &= ~SMP_F_CONST;
12151 }
12152
12153 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012154 smp->data.u.str.str[smp->data.u.str.len] = '\0';
12155 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012156 return 1;
12157}
12158
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012159static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
12160{
12161 struct proxy *fe = strm_fe(smp->strm);
12162 int idx, i;
12163 struct cap_hdr *hdr;
12164 int len;
12165
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012166 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012167 return 0;
12168
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012169 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012170
12171 /* Check the availibity of the capture id. */
12172 if (idx > fe->nb_req_cap - 1)
12173 return 0;
12174
12175 /* Look for the original configuration. */
12176 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
12177 hdr != NULL && i != idx ;
12178 i--, hdr = hdr->next);
12179 if (!hdr)
12180 return 0;
12181
12182 /* check for the memory allocation */
12183 if (smp->strm->req_cap[hdr->index] == NULL)
12184 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
12185 if (smp->strm->req_cap[hdr->index] == NULL)
12186 return 0;
12187
12188 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012189 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012190 if (len > hdr->len)
12191 len = hdr->len;
12192
12193 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012194 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012195 smp->strm->req_cap[idx][len] = '\0';
12196
12197 return 1;
12198}
12199
12200static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
12201{
12202 struct proxy *fe = strm_fe(smp->strm);
12203 int idx, i;
12204 struct cap_hdr *hdr;
12205 int len;
12206
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012207 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012208 return 0;
12209
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012210 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012211
12212 /* Check the availibity of the capture id. */
12213 if (idx > fe->nb_rsp_cap - 1)
12214 return 0;
12215
12216 /* Look for the original configuration. */
12217 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
12218 hdr != NULL && i != idx ;
12219 i--, hdr = hdr->next);
12220 if (!hdr)
12221 return 0;
12222
12223 /* check for the memory allocation */
12224 if (smp->strm->res_cap[hdr->index] == NULL)
12225 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
12226 if (smp->strm->res_cap[hdr->index] == NULL)
12227 return 0;
12228
12229 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012230 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012231 if (len > hdr->len)
12232 len = hdr->len;
12233
12234 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012235 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012236 smp->strm->res_cap[idx][len] = '\0';
12237
12238 return 1;
12239}
12240
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012241/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012242 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012243 * the relevant part of the request line accordingly. Then it updates various
12244 * pointers to the next elements which were moved, and the total buffer length.
12245 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012246 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
12247 * error, though this can be revisited when this code is finally exploited.
12248 *
12249 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
12250 * query string and 3 to replace uri.
12251 *
12252 * In query string case, the mark question '?' must be set at the start of the
12253 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012254 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012255int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012256 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012257{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012258 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012259 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012260 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012261 int delta;
12262
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012263 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012264 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012265 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012266 cur_end = cur_ptr + txn->req.sl.rq.m_l;
12267
12268 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012269 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012270 txn->req.sl.rq.m_l += delta;
12271 txn->req.sl.rq.u += delta;
12272 txn->req.sl.rq.v += delta;
12273 break;
12274
12275 case 1: // path
12276 cur_ptr = http_get_path(txn);
12277 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012278 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012279
12280 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012281 while (cur_end < s->req.buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?')
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012282 cur_end++;
12283
12284 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012285 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012286 txn->req.sl.rq.u_l += delta;
12287 txn->req.sl.rq.v += delta;
12288 break;
12289
12290 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012291 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012292 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012293 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12294 while (cur_ptr < cur_end && *cur_ptr != '?')
12295 cur_ptr++;
12296
12297 /* skip the question mark or indicate that we must insert it
12298 * (but only if the format string is not empty then).
12299 */
12300 if (cur_ptr < cur_end)
12301 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012302 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012303 offset = 0;
12304
12305 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012306 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012307 txn->req.sl.rq.u_l += delta;
12308 txn->req.sl.rq.v += delta;
12309 break;
12310
12311 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012312 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012313 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12314
12315 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012316 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012317 txn->req.sl.rq.u_l += delta;
12318 txn->req.sl.rq.v += delta;
12319 break;
12320
12321 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012322 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012323 }
12324
12325 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012326 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020012327 txn->req.sl.rq.l += delta;
12328 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012329 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012330 return 0;
12331}
12332
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012333/* This function replace the HTTP status code and the associated message. The
12334 * variable <status> contains the new status code. This function never fails.
12335 */
12336void http_set_status(unsigned int status, struct stream *s)
12337{
12338 struct http_txn *txn = s->txn;
12339 char *cur_ptr, *cur_end;
12340 int delta;
12341 char *res;
12342 int c_l;
12343 const char *msg;
12344 int msg_len;
12345
12346 chunk_reset(&trash);
12347
12348 res = ultoa_o(status, trash.str, trash.size);
12349 c_l = res - trash.str;
12350
12351 trash.str[c_l] = ' ';
12352 trash.len = c_l + 1;
12353
12354 msg = get_reason(status);
12355 msg_len = strlen(msg);
12356
12357 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
12358 trash.len += msg_len;
12359
12360 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
12361 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
12362
12363 /* commit changes and adjust message */
12364 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
12365
12366 /* adjust res line offsets and lengths */
12367 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
12368 txn->rsp.sl.st.c_l = c_l;
12369 txn->rsp.sl.st.r_l = msg_len;
12370
12371 delta = trash.len - (cur_end - cur_ptr);
12372 txn->rsp.sl.st.l += delta;
12373 txn->hdr_idx.v[0].len += delta;
12374 http_msg_move_end(&txn->rsp, delta);
12375}
12376
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012377/* This function executes one of the set-{method,path,query,uri} actions. It
12378 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012379 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012380 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012381 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
12382 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012383 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012384enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
12385 struct session *sess, struct stream *s)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012386{
12387 chunk_reset(&trash);
12388
12389 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012390 if (rule->arg.http.action == 2)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012391 trash.str[trash.len++] = '?';
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012392 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012393
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012394 http_replace_req_line(rule->arg.http.action, trash.str, trash.len, px, s);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012395 return ACT_RET_CONT;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012396}
12397
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012398/* This function is just a compliant action wrapper for "set-status". */
12399enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
12400 struct session *sess, struct stream *s)
12401{
12402 http_set_status(rule->arg.status.code, s);
12403 return ACT_RET_CONT;
12404}
12405
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012406/* parse an http-request action among :
12407 * set-method
12408 * set-path
12409 * set-query
12410 * set-uri
12411 *
12412 * All of them accept a single argument of type string representing a log-format.
12413 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
12414 * head, and p[2] to store the action as an int (0=method, 1=path, 2=query, 3=uri).
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012415 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012416 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012417enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
12418 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012419{
12420 int cur_arg = *orig_arg;
12421
Thierry FOURNIER42148732015-09-02 17:17:33 +020012422 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012423
12424 switch (args[0][4]) {
12425 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012426 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012427 rule->action_ptr = http_action_set_req_line;
12428 break;
12429 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012430 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012431 rule->action_ptr = http_action_set_req_line;
12432 break;
12433 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012434 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012435 rule->action_ptr = http_action_set_req_line;
12436 break;
12437 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012438 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012439 rule->action_ptr = http_action_set_req_line;
12440 break;
12441 default:
12442 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012443 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012444 }
12445
12446 if (!*args[cur_arg] ||
12447 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12448 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012449 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012450 }
12451
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012452 LIST_INIT(&rule->arg.http.logfmt);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012453 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012454 parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP,
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012455 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
12456 proxy->conf.args.file, proxy->conf.args.line);
12457
12458 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012459 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012460}
12461
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012462/* parse set-status action:
12463 * This action accepts a single argument of type int representing
12464 * an http status code. It returns ACT_RET_PRS_OK on success,
12465 * ACT_RET_PRS_ERR on error.
12466 */
12467enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12468 struct act_rule *rule, char **err)
12469{
12470 char *error;
12471
Thierry FOURNIER42148732015-09-02 17:17:33 +020012472 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012473 rule->action_ptr = action_http_set_status;
12474
12475 /* Check if an argument is available */
12476 if (!*args[*orig_arg]) {
12477 memprintf(err, "expects exactly 1 argument <status>");
12478 return ACT_RET_PRS_ERR;
12479 }
12480
12481 /* convert status code as integer */
12482 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12483 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12484 memprintf(err, "expects an integer status code between 100 and 999");
12485 return ACT_RET_PRS_ERR;
12486 }
12487
12488 (*orig_arg)++;
12489 return ACT_RET_PRS_OK;
12490}
12491
Willy Tarreaua9083d02015-05-08 15:27:59 +020012492/* This function executes the "capture" action. It executes a fetch expression,
12493 * turns the result into a string and puts it in a capture slot. It always
12494 * returns 1. If an error occurs the action is cancelled, but the rule
12495 * processing continues.
12496 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012497enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
12498 struct session *sess, struct stream *s)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012499{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012500 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012501 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012502 char **cap = s->req_cap;
12503 int len;
12504
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012505 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.cap.expr, SMP_T_STR);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012506 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012507 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012508
12509 if (cap[h->index] == NULL)
12510 cap[h->index] = pool_alloc2(h->pool);
12511
12512 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012513 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012514
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012515 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012516 if (len > h->len)
12517 len = h->len;
12518
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012519 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012520 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012521 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012522}
12523
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012524/* This function executes the "capture" action and store the result in a
12525 * capture slot if exists. It executes a fetch expression, turns the result
12526 * into a string and puts it in a capture slot. It always returns 1. If an
12527 * error occurs the action is cancelled, but the rule processing continues.
12528 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012529enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
12530 struct session *sess, struct stream *s)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012531{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012532 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012533 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012534 char **cap = s->req_cap;
12535 struct proxy *fe = strm_fe(s);
12536 int len;
12537 int i;
12538
12539 /* Look for the original configuration. */
12540 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012541 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012542 i--, h = h->next);
12543 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012544 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012545
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012546 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012547 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012548 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012549
12550 if (cap[h->index] == NULL)
12551 cap[h->index] = pool_alloc2(h->pool);
12552
12553 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012554 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012555
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012556 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012557 if (len > h->len)
12558 len = h->len;
12559
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012560 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012561 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012562 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012563}
12564
Willy Tarreaua9083d02015-05-08 15:27:59 +020012565/* parse an "http-request capture" action. It takes a single argument which is
12566 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012567 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012568 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012569 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012570enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12571 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012572{
12573 struct sample_expr *expr;
12574 struct cap_hdr *hdr;
12575 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012576 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012577
12578 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12579 if (strcmp(args[cur_arg], "if") == 0 ||
12580 strcmp(args[cur_arg], "unless") == 0)
12581 break;
12582
12583 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012584 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012585 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012586 }
12587
Willy Tarreaua9083d02015-05-08 15:27:59 +020012588 cur_arg = *orig_arg;
12589 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12590 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012591 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012592
12593 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12594 memprintf(err,
12595 "fetch method '%s' extracts information from '%s', none of which is available here",
12596 args[cur_arg-1], sample_src_names(expr->fetch->use));
12597 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012598 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012599 }
12600
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012601 if (!args[cur_arg] || !*args[cur_arg]) {
12602 memprintf(err, "expects 'len or 'id'");
12603 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012604 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012605 }
12606
Willy Tarreaua9083d02015-05-08 15:27:59 +020012607 if (strcmp(args[cur_arg], "len") == 0) {
12608 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012609
12610 if (!(px->cap & PR_CAP_FE)) {
12611 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012612 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012613 }
12614
12615 proxy->conf.args.ctx = ARGC_CAP;
12616
Willy Tarreaua9083d02015-05-08 15:27:59 +020012617 if (!args[cur_arg]) {
12618 memprintf(err, "missing length value");
12619 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012620 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012621 }
12622 /* we copy the table name for now, it will be resolved later */
12623 len = atoi(args[cur_arg]);
12624 if (len <= 0) {
12625 memprintf(err, "length must be > 0");
12626 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012627 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012628 }
12629 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012630
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012631 if (!len) {
12632 memprintf(err, "a positive 'len' argument is mandatory");
12633 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012634 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012635 }
12636
12637 hdr = calloc(sizeof(struct cap_hdr), 1);
12638 hdr->next = px->req_cap;
12639 hdr->name = NULL; /* not a header capture */
12640 hdr->namelen = 0;
12641 hdr->len = len;
12642 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12643 hdr->index = px->nb_req_cap++;
12644
12645 px->req_cap = hdr;
12646 px->to_log |= LW_REQHDR;
12647
Thierry FOURNIER42148732015-09-02 17:17:33 +020012648 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012649 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012650 rule->arg.cap.expr = expr;
12651 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012652 }
12653
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012654 else if (strcmp(args[cur_arg], "id") == 0) {
12655 int id;
12656 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012657
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012658 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012659
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012660 if (!args[cur_arg]) {
12661 memprintf(err, "missing id value");
12662 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012663 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012664 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012665
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012666 id = strtol(args[cur_arg], &error, 10);
12667 if (*error != '\0') {
12668 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12669 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012670 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012671 }
12672 cur_arg++;
12673
12674 proxy->conf.args.ctx = ARGC_CAP;
12675
Thierry FOURNIER42148732015-09-02 17:17:33 +020012676 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012677 rule->action_ptr = http_action_req_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012678 rule->arg.capid.expr = expr;
12679 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012680 }
12681
12682 else {
12683 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12684 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012685 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012686 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012687
12688 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012689 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012690}
12691
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012692/* This function executes the "capture" action and store the result in a
12693 * capture slot if exists. It executes a fetch expression, turns the result
12694 * into a string and puts it in a capture slot. It always returns 1. If an
12695 * error occurs the action is cancelled, but the rule processing continues.
12696 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012697enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
12698 struct session *sess, struct stream *s)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012699{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012700 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012701 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012702 char **cap = s->res_cap;
12703 struct proxy *fe = strm_fe(s);
12704 int len;
12705 int i;
12706
12707 /* Look for the original configuration. */
12708 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012709 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012710 i--, h = h->next);
12711 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012712 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012713
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012714 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, rule->arg.capid.expr, SMP_T_STR);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012715 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012716 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012717
12718 if (cap[h->index] == NULL)
12719 cap[h->index] = pool_alloc2(h->pool);
12720
12721 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012722 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012723
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012724 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012725 if (len > h->len)
12726 len = h->len;
12727
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012728 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012729 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012730 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012731}
12732
12733/* parse an "http-response capture" action. It takes a single argument which is
12734 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12735 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012736 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012737 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012738enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12739 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012740{
12741 struct sample_expr *expr;
12742 int cur_arg;
12743 int id;
12744 char *error;
12745
12746 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12747 if (strcmp(args[cur_arg], "if") == 0 ||
12748 strcmp(args[cur_arg], "unless") == 0)
12749 break;
12750
12751 if (cur_arg < *orig_arg + 3) {
12752 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012753 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012754 }
12755
12756 cur_arg = *orig_arg;
12757 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12758 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012759 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012760
12761 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12762 memprintf(err,
12763 "fetch method '%s' extracts information from '%s', none of which is available here",
12764 args[cur_arg-1], sample_src_names(expr->fetch->use));
12765 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012766 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012767 }
12768
12769 if (!args[cur_arg] || !*args[cur_arg]) {
12770 memprintf(err, "expects 'len or 'id'");
12771 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012772 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012773 }
12774
12775 if (strcmp(args[cur_arg], "id") != 0) {
12776 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12777 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012778 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012779 }
12780
12781 cur_arg++;
12782
12783 if (!args[cur_arg]) {
12784 memprintf(err, "missing id value");
12785 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012786 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012787 }
12788
12789 id = strtol(args[cur_arg], &error, 10);
12790 if (*error != '\0') {
12791 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12792 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012793 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012794 }
12795 cur_arg++;
12796
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012797 proxy->conf.args.ctx = ARGC_CAP;
12798
Thierry FOURNIER42148732015-09-02 17:17:33 +020012799 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012800 rule->action_ptr = http_action_res_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012801 rule->arg.capid.expr = expr;
12802 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012803
12804 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012805 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012806}
12807
William Lallemand73025dd2014-04-24 14:38:37 +020012808/*
12809 * Return the struct http_req_action_kw associated to a keyword.
12810 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012811struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012812{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012813 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012814}
12815
12816/*
12817 * Return the struct http_res_action_kw associated to a keyword.
12818 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012819struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012820{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012821 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012822}
12823
Willy Tarreau4a568972010-05-12 08:08:50 +020012824/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012825/* All supported ACL keywords must be declared here. */
12826/************************************************************************/
12827
12828/* Note: must not be declared <const> as its list will be overwritten.
12829 * Please take care of keeping this list alphabetically sorted.
12830 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012831static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012832 { "base", "base", PAT_MATCH_STR },
12833 { "base_beg", "base", PAT_MATCH_BEG },
12834 { "base_dir", "base", PAT_MATCH_DIR },
12835 { "base_dom", "base", PAT_MATCH_DOM },
12836 { "base_end", "base", PAT_MATCH_END },
12837 { "base_len", "base", PAT_MATCH_LEN },
12838 { "base_reg", "base", PAT_MATCH_REG },
12839 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012840
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012841 { "cook", "req.cook", PAT_MATCH_STR },
12842 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12843 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12844 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12845 { "cook_end", "req.cook", PAT_MATCH_END },
12846 { "cook_len", "req.cook", PAT_MATCH_LEN },
12847 { "cook_reg", "req.cook", PAT_MATCH_REG },
12848 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012849
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012850 { "hdr", "req.hdr", PAT_MATCH_STR },
12851 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12852 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12853 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12854 { "hdr_end", "req.hdr", PAT_MATCH_END },
12855 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12856 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12857 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012858
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012859 /* these two declarations uses strings with list storage (in place
12860 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12861 * and delete functions are relative to the list management. The parse
12862 * and match method are related to the corresponding fetch methods. This
12863 * is very particular ACL declaration mode.
12864 */
12865 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12866 { "method", NULL, PAT_MATCH_STR, pat_parse_meth, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_meth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012867
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012868 { "path", "path", PAT_MATCH_STR },
12869 { "path_beg", "path", PAT_MATCH_BEG },
12870 { "path_dir", "path", PAT_MATCH_DIR },
12871 { "path_dom", "path", PAT_MATCH_DOM },
12872 { "path_end", "path", PAT_MATCH_END },
12873 { "path_len", "path", PAT_MATCH_LEN },
12874 { "path_reg", "path", PAT_MATCH_REG },
12875 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012876
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012877 { "req_ver", "req.ver", PAT_MATCH_STR },
12878 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012879
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012880 { "scook", "res.cook", PAT_MATCH_STR },
12881 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12882 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12883 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12884 { "scook_end", "res.cook", PAT_MATCH_END },
12885 { "scook_len", "res.cook", PAT_MATCH_LEN },
12886 { "scook_reg", "res.cook", PAT_MATCH_REG },
12887 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012888
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012889 { "shdr", "res.hdr", PAT_MATCH_STR },
12890 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12891 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12892 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12893 { "shdr_end", "res.hdr", PAT_MATCH_END },
12894 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12895 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12896 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012897
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012898 { "url", "url", PAT_MATCH_STR },
12899 { "url_beg", "url", PAT_MATCH_BEG },
12900 { "url_dir", "url", PAT_MATCH_DIR },
12901 { "url_dom", "url", PAT_MATCH_DOM },
12902 { "url_end", "url", PAT_MATCH_END },
12903 { "url_len", "url", PAT_MATCH_LEN },
12904 { "url_reg", "url", PAT_MATCH_REG },
12905 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012906
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012907 { "urlp", "urlp", PAT_MATCH_STR },
12908 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12909 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12910 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12911 { "urlp_end", "urlp", PAT_MATCH_END },
12912 { "urlp_len", "urlp", PAT_MATCH_LEN },
12913 { "urlp_reg", "urlp", PAT_MATCH_REG },
12914 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012915
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012916 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012917}};
12918
12919/************************************************************************/
12920/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012921/************************************************************************/
12922/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012923static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012924 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012925 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012926 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12927
Willy Tarreau87b09662015-04-03 00:22:06 +020012928 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012929 { "capture.req.hdr", smp_fetch_capture_header_req, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012930
12931 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012932 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12933 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12934 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012935
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012936 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
12937 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012938
Willy Tarreau409bcde2013-01-08 00:31:00 +010012939 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12940 * are only here to match the ACL's name, are request-only and are used
12941 * for ACL compatibility only.
12942 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012943 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12944 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012945 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12946 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012947
12948 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12949 * only here to match the ACL's name, are request-only and are used for
12950 * ACL compatibility only.
12951 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012952 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012953 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012954 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012955 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012956
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012957 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012958 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012959 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012960 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012961 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012962 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012963
12964 /* HTTP protocol on the request path */
12965 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012966 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012967
12968 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012969 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12970 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012971
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012972 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012973 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
12974 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012975 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012976
Willy Tarreau18ed2562013-01-14 15:56:36 +010012977 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012978 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12979 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012980
Willy Tarreau18ed2562013-01-14 15:56:36 +010012981 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012982 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012983 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12984 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012985
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012986 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012987 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012988 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012989 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012990 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
Willy Tarreaueb27ec72015-02-20 13:55:29 +010012991 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012992 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012993
12994 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012995 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012996 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12997 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012998
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012999 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013000 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013001 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013002 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013003 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
Willy Tarreaueb27ec72015-02-20 13:55:29 +010013004 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013005 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013006
Willy Tarreau409bcde2013-01-08 00:31:00 +010013007 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013008 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013009 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13010 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013011 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010013012
13013 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013014 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013015 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013016 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013017 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013018
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013019 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013020 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013021 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000013022 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013023 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013024 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020013025 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13026 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013027 { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013028 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020013029}};
13030
Willy Tarreau8797c062007-05-07 00:55:35 +020013031
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013032/************************************************************************/
13033/* All supported converter keywords must be declared here. */
13034/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020013035/* Note: must not be declared <const> as its list will be overwritten */
13036static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013037 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020013038 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013039 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
13040 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020013041 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020013042 { NULL, NULL, 0, 0, 0 },
13043}};
13044
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020013045
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013046/************************************************************************/
13047/* All supported http-request action keywords must be declared here. */
13048/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013049struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013050 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020013051 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013052 { "set-method", parse_set_req_line },
13053 { "set-path", parse_set_req_line },
13054 { "set-query", parse_set_req_line },
13055 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020013056 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013057 }
13058};
13059
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013060struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013061 .kw = {
13062 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020013063 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013064 { NULL, NULL }
13065 }
13066};
13067
Willy Tarreau8797c062007-05-07 00:55:35 +020013068__attribute__((constructor))
13069static void __http_protocol_init(void)
13070{
13071 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020013072 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020013073 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013074 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013075 http_res_keywords_register(&http_res_actions);
Willy Tarreau8797c062007-05-07 00:55:35 +020013076}
13077
13078
Willy Tarreau58f10d72006-12-04 02:26:12 +010013079/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020013080 * Local variables:
13081 * c-indent-level: 8
13082 * c-basic-offset: 8
13083 * End:
13084 */