blob: 724916f9491bdc5f87407c3f93a01244ff315cd7 [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 Tarreau12207b32016-11-22 19:48:51 +010042#include <types/cli.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020043#include <types/filters.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044#include <types/global.h>
William Lallemand9ed62032016-11-21 17:49:11 +010045#include <types/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020046
Willy Tarreau8797c062007-05-07 00:55:35 +020047#include <proto/acl.h>
Thierry FOURNIER322a1242015-08-19 09:07:47 +020048#include <proto/action.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020049#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010050#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020051#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020052#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010053#include <proto/checks.h>
Willy Tarreau12207b32016-11-22 19:48:51 +010054#include <proto/cli.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020055#include <proto/compression.h>
William Lallemand9ed62032016-11-21 17:49:11 +010056#include <proto/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020057#include <proto/fd.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020058#include <proto/filters.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020059#include <proto/frontend.h>
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020060#include <proto/h1.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010062#include <proto/hdr_idx.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010063#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020064#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020065#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010066#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020067#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020068#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010069#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020070#include <proto/stream.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010071#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020072#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020073#include <proto/pattern.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020074#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020075
Willy Tarreau522d6c02009-12-06 18:49:18 +010076const char HTTP_100[] =
77 "HTTP/1.1 100 Continue\r\n\r\n";
78
79const struct chunk http_100_chunk = {
80 .str = (char *)&HTTP_100,
81 .len = sizeof(HTTP_100)-1
82};
83
Willy Tarreaua9679ac2010-01-03 17:32:57 +010084/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020085const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010086 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010087 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020088 "Location: "; /* not terminated since it will be concatenated with the URL */
89
Willy Tarreau0f772532006-12-23 20:51:41 +010090const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010091 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010092 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010093 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010094 "Location: "; /* not terminated since it will be concatenated with the URL */
95
96/* same as 302 except that the browser MUST retry with the GET method */
97const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010098 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010099 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +0100100 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100101 "Location: "; /* not terminated since it will be concatenated with the URL */
102
Yves Lafon3e8d1ae2013-03-11 11:06:05 -0400103
104/* same as 302 except that the browser MUST retry with the same method */
105const char *HTTP_307 =
106 "HTTP/1.1 307 Temporary Redirect\r\n"
107 "Cache-Control: no-cache\r\n"
108 "Content-length: 0\r\n"
109 "Location: "; /* not terminated since it will be concatenated with the URL */
110
111/* same as 301 except that the browser MUST retry with the same method */
112const char *HTTP_308 =
113 "HTTP/1.1 308 Permanent Redirect\r\n"
114 "Content-length: 0\r\n"
115 "Location: "; /* not terminated since it will be concatenated with the URL */
116
Willy Tarreaubaaee002006-06-26 02:48:02 +0200117/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
118const char *HTTP_401_fmt =
119 "HTTP/1.0 401 Unauthorized\r\n"
120 "Cache-Control: no-cache\r\n"
121 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200122 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200123 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
124 "\r\n"
125 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
126
Willy Tarreau844a7e72010-01-31 21:46:18 +0100127const char *HTTP_407_fmt =
128 "HTTP/1.0 407 Unauthorized\r\n"
129 "Cache-Control: no-cache\r\n"
130 "Connection: close\r\n"
131 "Content-Type: text/html\r\n"
132 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
133 "\r\n"
Godbach1f1fae62014-12-17 16:32:05 +0800134 "<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 +0100135
Willy Tarreau0f772532006-12-23 20:51:41 +0100136
137const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200138 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100139 [HTTP_ERR_400] = 400,
140 [HTTP_ERR_403] = 403,
CJ Ess108b1dd2015-04-07 12:03:37 -0400141 [HTTP_ERR_405] = 405,
Willy Tarreau0f772532006-12-23 20:51:41 +0100142 [HTTP_ERR_408] = 408,
Olivier Houchard51a76d82017-10-02 16:12:07 +0200143 [HTTP_ERR_425] = 425,
CJ Ess108b1dd2015-04-07 12:03:37 -0400144 [HTTP_ERR_429] = 429,
Willy Tarreau0f772532006-12-23 20:51:41 +0100145 [HTTP_ERR_500] = 500,
146 [HTTP_ERR_502] = 502,
147 [HTTP_ERR_503] = 503,
148 [HTTP_ERR_504] = 504,
149};
150
Willy Tarreau80587432006-12-24 17:47:20 +0100151static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200152 [HTTP_ERR_200] =
153 "HTTP/1.0 200 OK\r\n"
154 "Cache-Control: no-cache\r\n"
155 "Connection: close\r\n"
156 "Content-Type: text/html\r\n"
157 "\r\n"
158 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
159
Willy Tarreau0f772532006-12-23 20:51:41 +0100160 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100161 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100162 "Cache-Control: no-cache\r\n"
163 "Connection: close\r\n"
164 "Content-Type: text/html\r\n"
165 "\r\n"
166 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
167
168 [HTTP_ERR_403] =
169 "HTTP/1.0 403 Forbidden\r\n"
170 "Cache-Control: no-cache\r\n"
171 "Connection: close\r\n"
172 "Content-Type: text/html\r\n"
173 "\r\n"
174 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
175
CJ Ess108b1dd2015-04-07 12:03:37 -0400176 [HTTP_ERR_405] =
177 "HTTP/1.0 405 Method Not Allowed\r\n"
178 "Cache-Control: no-cache\r\n"
179 "Connection: close\r\n"
180 "Content-Type: text/html\r\n"
181 "\r\n"
182 "<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",
183
Willy Tarreau0f772532006-12-23 20:51:41 +0100184 [HTTP_ERR_408] =
185 "HTTP/1.0 408 Request Time-out\r\n"
186 "Cache-Control: no-cache\r\n"
187 "Connection: close\r\n"
188 "Content-Type: text/html\r\n"
189 "\r\n"
190 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
191
Olivier Houchard51a76d82017-10-02 16:12:07 +0200192 [HTTP_ERR_425] =
193 "HTTP/1.0 425 Too Early\r\n"
194 "Cache-Control: no-cache\r\n"
195 "Connection: close\r\n"
196 "Content-Type: text/html\r\n"
197 "\r\n"
198 "<html><body><h1>425 Too Early</h1>\nYour browser sent early data.\n</body></html>\n",
199
CJ Ess108b1dd2015-04-07 12:03:37 -0400200 [HTTP_ERR_429] =
201 "HTTP/1.0 429 Too Many Requests\r\n"
202 "Cache-Control: no-cache\r\n"
203 "Connection: close\r\n"
204 "Content-Type: text/html\r\n"
205 "\r\n"
206 "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n",
207
Willy Tarreau0f772532006-12-23 20:51:41 +0100208 [HTTP_ERR_500] =
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200209 "HTTP/1.0 500 Internal Server Error\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100210 "Cache-Control: no-cache\r\n"
211 "Connection: close\r\n"
212 "Content-Type: text/html\r\n"
213 "\r\n"
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200214 "<html><body><h1>500 Internal Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
Willy Tarreau0f772532006-12-23 20:51:41 +0100215
216 [HTTP_ERR_502] =
217 "HTTP/1.0 502 Bad Gateway\r\n"
218 "Cache-Control: no-cache\r\n"
219 "Connection: close\r\n"
220 "Content-Type: text/html\r\n"
221 "\r\n"
222 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
223
224 [HTTP_ERR_503] =
225 "HTTP/1.0 503 Service Unavailable\r\n"
226 "Cache-Control: no-cache\r\n"
227 "Connection: close\r\n"
228 "Content-Type: text/html\r\n"
229 "\r\n"
230 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
231
232 [HTTP_ERR_504] =
233 "HTTP/1.0 504 Gateway Time-out\r\n"
234 "Cache-Control: no-cache\r\n"
235 "Connection: close\r\n"
236 "Content-Type: text/html\r\n"
237 "\r\n"
238 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
239
240};
241
Cyril Bonté19979e12012-04-04 12:57:21 +0200242/* status codes available for the stats admin page (strictly 4 chars length) */
243const char *stat_status_codes[STAT_STATUS_SIZE] = {
244 [STAT_STATUS_DENY] = "DENY",
245 [STAT_STATUS_DONE] = "DONE",
246 [STAT_STATUS_ERRP] = "ERRP",
247 [STAT_STATUS_EXCD] = "EXCD",
248 [STAT_STATUS_NONE] = "NONE",
249 [STAT_STATUS_PART] = "PART",
250 [STAT_STATUS_UNKN] = "UNKN",
251};
252
253
William Lallemand73025dd2014-04-24 14:38:37 +0200254/* List head of all known action keywords for "http-request" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200255struct action_kw_list http_req_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200256 .list = LIST_HEAD_INIT(http_req_keywords.list)
257};
258
259/* List head of all known action keywords for "http-response" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200260struct action_kw_list http_res_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200261 .list = LIST_HEAD_INIT(http_res_keywords.list)
262};
263
Willy Tarreau80587432006-12-24 17:47:20 +0100264/* We must put the messages here since GCC cannot initialize consts depending
265 * on strlen().
266 */
267struct chunk http_err_chunks[HTTP_ERR_SIZE];
268
Willy Tarreaua890d072013-04-02 12:01:06 +0200269/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
270static struct hdr_ctx static_hdr_ctx;
271
Willy Tarreau42250582007-04-01 01:30:43 +0200272#define FD_SETS_ARE_BITFIELDS
273#ifdef FD_SETS_ARE_BITFIELDS
274/*
275 * This map is used with all the FD_* macros to check whether a particular bit
276 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
277 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
278 * byte should be encoded. Be careful to always pass bytes from 0 to 255
279 * exclusively to the macros.
280 */
281fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
282fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100283fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200284
285#else
286#error "Check if your OS uses bitfields for fd_sets"
287#endif
288
Willy Tarreau87b09662015-04-03 00:22:06 +0200289static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn);
Willy Tarreau0b748332014-04-29 00:13:29 +0200290
David Carlier7365f7d2016-04-04 11:54:42 +0100291static inline int http_msg_forward_body(struct stream *s, struct http_msg *msg);
292static inline int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +0100293
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200294/* This function returns a reason associated with the HTTP status.
295 * This function never fails, a message is always returned.
296 */
297const char *get_reason(unsigned int status)
298{
299 switch (status) {
300 case 100: return "Continue";
301 case 101: return "Switching Protocols";
302 case 102: return "Processing";
303 case 200: return "OK";
304 case 201: return "Created";
305 case 202: return "Accepted";
306 case 203: return "Non-Authoritative Information";
307 case 204: return "No Content";
308 case 205: return "Reset Content";
309 case 206: return "Partial Content";
310 case 207: return "Multi-Status";
311 case 210: return "Content Different";
312 case 226: return "IM Used";
313 case 300: return "Multiple Choices";
314 case 301: return "Moved Permanently";
315 case 302: return "Moved Temporarily";
316 case 303: return "See Other";
317 case 304: return "Not Modified";
318 case 305: return "Use Proxy";
319 case 307: return "Temporary Redirect";
320 case 308: return "Permanent Redirect";
321 case 310: return "Too many Redirects";
322 case 400: return "Bad Request";
323 case 401: return "Unauthorized";
324 case 402: return "Payment Required";
325 case 403: return "Forbidden";
326 case 404: return "Not Found";
327 case 405: return "Method Not Allowed";
328 case 406: return "Not Acceptable";
329 case 407: return "Proxy Authentication Required";
330 case 408: return "Request Time-out";
331 case 409: return "Conflict";
332 case 410: return "Gone";
333 case 411: return "Length Required";
334 case 412: return "Precondition Failed";
335 case 413: return "Request Entity Too Large";
336 case 414: return "Request-URI Too Long";
337 case 415: return "Unsupported Media Type";
338 case 416: return "Requested range unsatisfiable";
339 case 417: return "Expectation failed";
340 case 418: return "I'm a teapot";
341 case 422: return "Unprocessable entity";
342 case 423: return "Locked";
343 case 424: return "Method failure";
Olivier Houchard51a76d82017-10-02 16:12:07 +0200344 case 425: return "Too Early";
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200345 case 426: return "Upgrade Required";
346 case 428: return "Precondition Required";
347 case 429: return "Too Many Requests";
348 case 431: return "Request Header Fields Too Large";
349 case 449: return "Retry With";
350 case 450: return "Blocked by Windows Parental Controls";
351 case 451: return "Unavailable For Legal Reasons";
352 case 456: return "Unrecoverable Error";
353 case 499: return "client has closed connection";
354 case 500: return "Internal Server Error";
355 case 501: return "Not Implemented";
Jarno Huuskonen59af2df2016-12-28 10:49:01 +0200356 case 502: return "Bad Gateway or Proxy Error";
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200357 case 503: return "Service Unavailable";
358 case 504: return "Gateway Time-out";
359 case 505: return "HTTP Version not supported";
360 case 506: return "Variant also negociate";
361 case 507: return "Insufficient storage";
362 case 508: return "Loop detected";
363 case 509: return "Bandwidth Limit Exceeded";
364 case 510: return "Not extended";
365 case 511: return "Network authentication required";
366 case 520: return "Web server is returning an unknown error";
367 default:
368 switch (status) {
369 case 100 ... 199: return "Informational";
370 case 200 ... 299: return "Success";
371 case 300 ... 399: return "Redirection";
372 case 400 ... 499: return "Client Error";
373 case 500 ... 599: return "Server Error";
374 default: return "Other";
375 }
376 }
377}
378
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200379/* This function returns HTTP_ERR_<num> (enum) matching http status code.
380 * Returned value should match codes from http_err_codes.
381 */
382static const int http_get_status_idx(unsigned int status)
383{
384 switch (status) {
385 case 200: return HTTP_ERR_200;
386 case 400: return HTTP_ERR_400;
387 case 403: return HTTP_ERR_403;
388 case 405: return HTTP_ERR_405;
389 case 408: return HTTP_ERR_408;
Olivier Houchard51a76d82017-10-02 16:12:07 +0200390 case 425: return HTTP_ERR_425;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200391 case 429: return HTTP_ERR_429;
392 case 500: return HTTP_ERR_500;
393 case 502: return HTTP_ERR_502;
394 case 503: return HTTP_ERR_503;
395 case 504: return HTTP_ERR_504;
396 default: return HTTP_ERR_500;
397 }
398}
399
Willy Tarreau80587432006-12-24 17:47:20 +0100400void init_proto_http()
401{
Willy Tarreau42250582007-04-01 01:30:43 +0200402 int i;
403 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100404 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200405
Willy Tarreau80587432006-12-24 17:47:20 +0100406 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
407 if (!http_err_msgs[msg]) {
408 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
409 abort();
410 }
411
412 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
413 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
414 }
Willy Tarreau42250582007-04-01 01:30:43 +0200415
416 /* initialize the log header encoding map : '{|}"#' should be encoded with
417 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
418 * URL encoding only requires '"', '#' to be encoded as well as non-
419 * printable characters above.
420 */
421 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
422 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100423 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200424 for (i = 0; i < 32; i++) {
425 FD_SET(i, hdr_encode_map);
426 FD_SET(i, url_encode_map);
427 }
428 for (i = 127; i < 256; i++) {
429 FD_SET(i, hdr_encode_map);
430 FD_SET(i, url_encode_map);
431 }
432
433 tmp = "\"#{|}";
434 while (*tmp) {
435 FD_SET(*tmp, hdr_encode_map);
436 tmp++;
437 }
438
439 tmp = "\"#";
440 while (*tmp) {
441 FD_SET(*tmp, url_encode_map);
442 tmp++;
443 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200444
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100445 /* initialize the http header encoding map. The draft httpbis define the
446 * header content as:
447 *
448 * HTTP-message = start-line
449 * *( header-field CRLF )
450 * CRLF
451 * [ message-body ]
452 * header-field = field-name ":" OWS field-value OWS
453 * field-value = *( field-content / obs-fold )
454 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
455 * obs-fold = CRLF 1*( SP / HTAB )
456 * field-vchar = VCHAR / obs-text
457 * VCHAR = %x21-7E
458 * obs-text = %x80-FF
459 *
460 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
461 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
462 * "obs-fold" is volontary forgotten because haproxy remove this.
463 */
464 memset(http_encode_map, 0, sizeof(http_encode_map));
465 for (i = 0x00; i <= 0x08; i++)
466 FD_SET(i, http_encode_map);
467 for (i = 0x0a; i <= 0x1f; i++)
468 FD_SET(i, http_encode_map);
469 FD_SET(0x7f, http_encode_map);
470
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200471 /* memory allocations */
Willy Tarreau63986c72015-04-03 22:55:33 +0200472 pool2_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100473 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100474}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200475
Willy Tarreau53b6c742006-12-17 13:37:46 +0100476/*
477 * We have 26 list of methods (1 per first letter), each of which can have
478 * up to 3 entries (2 valid, 1 null).
479 */
480struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100481 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100482 int len;
483 const char text[8];
484};
485
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100486const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100487 ['C' - 'A'] = {
488 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
489 },
490 ['D' - 'A'] = {
491 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
492 },
493 ['G' - 'A'] = {
494 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
495 },
496 ['H' - 'A'] = {
497 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
498 },
Christopher Fauletd57ad642015-07-31 14:26:57 +0200499 ['O' - 'A'] = {
500 [0] = { .meth = HTTP_METH_OPTIONS , .len=7, .text="OPTIONS" },
501 },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100502 ['P' - 'A'] = {
503 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
504 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
505 },
506 ['T' - 'A'] = {
507 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
508 },
509 /* rest is empty like this :
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200510 * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100511 */
512};
513
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100514const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100515 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
516 [HTTP_METH_GET] = { "GET", 3 },
517 [HTTP_METH_HEAD] = { "HEAD", 4 },
518 [HTTP_METH_POST] = { "POST", 4 },
519 [HTTP_METH_PUT] = { "PUT", 3 },
520 [HTTP_METH_DELETE] = { "DELETE", 6 },
521 [HTTP_METH_TRACE] = { "TRACE", 5 },
522 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
523};
524
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100525/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100526 * Adds a header and its CRLF at the tail of the message's buffer, just before
527 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100528 * The header is also automatically added to the index <hdr_idx>, and the end
529 * of headers is automatically adjusted. The number of bytes added is returned
530 * on success, otherwise <0 is returned indicating an error.
531 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100532int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100533{
534 int bytes, len;
535
536 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200537 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100538 if (!bytes)
539 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100540 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100541 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
542}
543
544/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100545 * Adds a header and its CRLF at the tail of the message's buffer, just before
546 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100547 * the buffer is only opened and the space reserved, but nothing is copied.
548 * The header is also automatically added to the index <hdr_idx>, and the end
549 * of headers is automatically adjusted. The number of bytes added is returned
550 * on success, otherwise <0 is returned indicating an error.
551 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100552int http_header_add_tail2(struct http_msg *msg,
553 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100554{
555 int bytes;
556
Willy Tarreau9b28e032012-10-12 23:49:43 +0200557 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100558 if (!bytes)
559 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100560 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100561 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
562}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200563
564/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100565 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
566 * If so, returns the position of the first non-space character relative to
567 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
568 * to return a pointer to the place after the first space. Returns 0 if the
569 * header name does not match. Checks are case-insensitive.
570 */
571int http_header_match2(const char *hdr, const char *end,
572 const char *name, int len)
573{
574 const char *val;
575
576 if (hdr + len >= end)
577 return 0;
578 if (hdr[len] != ':')
579 return 0;
580 if (strncasecmp(hdr, name, len) != 0)
581 return 0;
582 val = hdr + len + 1;
583 while (val < end && HTTP_IS_SPHT(*val))
584 val++;
585 if ((val >= end) && (len + 2 <= end - hdr))
586 return len + 2; /* we may replace starting from second space */
587 return val - hdr;
588}
589
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200590/* Find the first or next occurrence of header <name> in message buffer <sol>
591 * using headers index <idx>, and return it in the <ctx> structure. This
592 * structure holds everything necessary to use the header and find next
593 * occurrence. If its <idx> member is 0, the header is searched from the
594 * beginning. Otherwise, the next occurrence is returned. The function returns
595 * 1 when it finds a value, and 0 when there is no more. It is very similar to
596 * http_find_header2() except that it is designed to work with full-line headers
597 * whose comma is not a delimiter but is part of the syntax. As a special case,
598 * if ctx->val is NULL when searching for a new values of a header, the current
599 * header is rescanned. This allows rescanning after a header deletion.
600 */
601int http_find_full_header2(const char *name, int len,
602 char *sol, struct hdr_idx *idx,
603 struct hdr_ctx *ctx)
604{
605 char *eol, *sov;
606 int cur_idx, old_idx;
607
608 cur_idx = ctx->idx;
609 if (cur_idx) {
610 /* We have previously returned a header, let's search another one */
611 sol = ctx->line;
612 eol = sol + idx->v[cur_idx].len;
613 goto next_hdr;
614 }
615
616 /* first request for this header */
617 sol += hdr_idx_first_pos(idx);
618 old_idx = 0;
619 cur_idx = hdr_idx_first_idx(idx);
620 while (cur_idx) {
621 eol = sol + idx->v[cur_idx].len;
622
623 if (len == 0) {
624 /* No argument was passed, we want any header.
625 * To achieve this, we simply build a fake request. */
626 while (sol + len < eol && sol[len] != ':')
627 len++;
628 name = sol;
629 }
630
631 if ((len < eol - sol) &&
632 (sol[len] == ':') &&
633 (strncasecmp(sol, name, len) == 0)) {
634 ctx->del = len;
635 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100636 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200637 sov++;
638
639 ctx->line = sol;
640 ctx->prev = old_idx;
641 ctx->idx = cur_idx;
642 ctx->val = sov - sol;
643 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100644 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200645 eol--;
646 ctx->tws++;
647 }
648 ctx->vlen = eol - sov;
649 return 1;
650 }
651 next_hdr:
652 sol = eol + idx->v[cur_idx].cr + 1;
653 old_idx = cur_idx;
654 cur_idx = idx->v[cur_idx].next;
655 }
656 return 0;
657}
658
Willy Tarreauc90dc232015-02-20 13:51:36 +0100659/* Find the first or next header field in message buffer <sol> using headers
660 * index <idx>, and return it in the <ctx> structure. This structure holds
661 * everything necessary to use the header and find next occurrence. If its
662 * <idx> member is 0, the first header is retrieved. Otherwise, the next
663 * occurrence is returned. The function returns 1 when it finds a value, and
664 * 0 when there is no more. It is equivalent to http_find_full_header2() with
665 * no header name.
666 */
667int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
668{
669 char *eol, *sov;
670 int cur_idx, old_idx;
671 int len;
672
673 cur_idx = ctx->idx;
674 if (cur_idx) {
675 /* We have previously returned a header, let's search another one */
676 sol = ctx->line;
677 eol = sol + idx->v[cur_idx].len;
678 goto next_hdr;
679 }
680
681 /* first request for this header */
682 sol += hdr_idx_first_pos(idx);
683 old_idx = 0;
684 cur_idx = hdr_idx_first_idx(idx);
685 while (cur_idx) {
686 eol = sol + idx->v[cur_idx].len;
687
688 len = 0;
689 while (1) {
690 if (len >= eol - sol)
691 goto next_hdr;
692 if (sol[len] == ':')
693 break;
694 len++;
695 }
696
697 ctx->del = len;
698 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100699 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreauc90dc232015-02-20 13:51:36 +0100700 sov++;
701
702 ctx->line = sol;
703 ctx->prev = old_idx;
704 ctx->idx = cur_idx;
705 ctx->val = sov - sol;
706 ctx->tws = 0;
707
Willy Tarreau2235b262016-11-05 15:50:20 +0100708 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreauc90dc232015-02-20 13:51:36 +0100709 eol--;
710 ctx->tws++;
711 }
712 ctx->vlen = eol - sov;
713 return 1;
714
715 next_hdr:
716 sol = eol + idx->v[cur_idx].cr + 1;
717 old_idx = cur_idx;
718 cur_idx = idx->v[cur_idx].next;
719 }
720 return 0;
721}
722
Lukas Tribus23953682017-04-28 13:24:30 +0000723/* Find the end of the header value contained between <s> and <e>. See RFC7230,
724 * par 3.2 for more information. Note that it requires a valid header to return
Willy Tarreau68085d82010-01-18 14:54:04 +0100725 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200726 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100727char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200728{
729 int quoted, qdpair;
730
731 quoted = qdpair = 0;
Willy Tarreaue6d9c212016-11-05 18:23:38 +0100732
733#if defined(__x86_64__) || \
734 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
735 defined(__ARM_ARCH_7A__)
736 /* speedup: skip everything not a comma nor a double quote */
737 for (; s <= e - sizeof(int); s += sizeof(int)) {
738 unsigned int c = *(int *)s; // comma
739 unsigned int q = c; // quote
740
741 c ^= 0x2c2c2c2c; // contains one zero on a comma
742 q ^= 0x22222222; // contains one zero on a quote
743
744 c = (c - 0x01010101) & ~c; // contains 0x80 below a comma
745 q = (q - 0x01010101) & ~q; // contains 0x80 below a quote
746
747 if ((c | q) & 0x80808080)
748 break; // found a comma or a quote
749 }
750#endif
Willy Tarreau33a7e692007-06-10 19:45:56 +0200751 for (; s < e; s++) {
752 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200753 else if (quoted) {
754 if (*s == '\\') qdpair = 1;
755 else if (*s == '"') quoted = 0;
756 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200757 else if (*s == '"') quoted = 1;
758 else if (*s == ',') return s;
759 }
760 return s;
761}
762
763/* Find the first or next occurrence of header <name> in message buffer <sol>
764 * using headers index <idx>, and return it in the <ctx> structure. This
765 * structure holds everything necessary to use the header and find next
766 * occurrence. If its <idx> member is 0, the header is searched from the
767 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100768 * 1 when it finds a value, and 0 when there is no more. It is designed to work
769 * with headers defined as comma-separated lists. As a special case, if ctx->val
770 * is NULL when searching for a new values of a header, the current header is
771 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200772 */
773int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100774 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200775 struct hdr_ctx *ctx)
776{
Willy Tarreau68085d82010-01-18 14:54:04 +0100777 char *eol, *sov;
778 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200779
Willy Tarreau68085d82010-01-18 14:54:04 +0100780 cur_idx = ctx->idx;
781 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200782 /* We have previously returned a value, let's search
783 * another one on the same line.
784 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200785 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200786 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100787 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200788 eol = sol + idx->v[cur_idx].len;
789
790 if (sov >= eol)
791 /* no more values in this header */
792 goto next_hdr;
793
Willy Tarreau68085d82010-01-18 14:54:04 +0100794 /* values remaining for this header, skip the comma but save it
795 * for later use (eg: for header deletion).
796 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200797 sov++;
Willy Tarreau2235b262016-11-05 15:50:20 +0100798 while (sov < eol && HTTP_IS_LWS((*sov)))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200799 sov++;
800
801 goto return_hdr;
802 }
803
804 /* first request for this header */
805 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100806 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200807 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200808 while (cur_idx) {
809 eol = sol + idx->v[cur_idx].len;
810
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200811 if (len == 0) {
812 /* No argument was passed, we want any header.
813 * To achieve this, we simply build a fake request. */
814 while (sol + len < eol && sol[len] != ':')
815 len++;
816 name = sol;
817 }
818
Willy Tarreau33a7e692007-06-10 19:45:56 +0200819 if ((len < eol - sol) &&
820 (sol[len] == ':') &&
821 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100822 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200823 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100824 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200825 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100826
Willy Tarreau33a7e692007-06-10 19:45:56 +0200827 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100828 ctx->prev = old_idx;
829 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200830 ctx->idx = cur_idx;
831 ctx->val = sov - sol;
832
833 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200834 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100835 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200836 eol--;
837 ctx->tws++;
838 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200839 ctx->vlen = eol - sov;
840 return 1;
841 }
842 next_hdr:
843 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100844 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200845 cur_idx = idx->v[cur_idx].next;
846 }
847 return 0;
848}
849
850int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100851 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200852 struct hdr_ctx *ctx)
853{
854 return http_find_header2(name, strlen(name), sol, idx, ctx);
855}
856
Willy Tarreau68085d82010-01-18 14:54:04 +0100857/* Remove one value of a header. This only works on a <ctx> returned by one of
858 * the http_find_header functions. The value is removed, as well as surrounding
859 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100860 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100861 * message <msg>. The new index is returned. If it is zero, it means there is
862 * no more header, so any processing may stop. The ctx is always left in a form
863 * that can be handled by http_find_header2() to find next occurrence.
864 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100865int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100866{
867 int cur_idx = ctx->idx;
868 char *sol = ctx->line;
869 struct hdr_idx_elem *hdr;
870 int delta, skip_comma;
871
872 if (!cur_idx)
873 return 0;
874
875 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200876 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100877 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200878 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100879 http_msg_move_end(msg, delta);
880 idx->used--;
881 hdr->len = 0; /* unused entry */
882 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100883 if (idx->tail == ctx->idx)
884 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100885 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100886 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100887 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200888 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100889 return ctx->idx;
890 }
891
892 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200893 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
894 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100895 */
896
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200897 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200898 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200899 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100900 NULL, 0);
901 hdr->len += delta;
902 http_msg_move_end(msg, delta);
903 ctx->val = ctx->del;
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
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100908/* This function handles a server error at the stream interface level. The
909 * stream interface is assumed to be already in a closed state. An optional
Willy Tarreau2019f952017-03-14 11:07:31 +0100910 * message is copied into the input buffer.
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100911 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100912 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200913 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200914static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau2019f952017-03-14 11:07:31 +0100915 int err, int finst, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200916{
Willy Tarreau2019f952017-03-14 11:07:31 +0100917 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100918 channel_auto_read(si_oc(si));
919 channel_abort(si_oc(si));
920 channel_auto_close(si_oc(si));
921 channel_erase(si_oc(si));
922 channel_auto_close(si_ic(si));
923 channel_auto_read(si_ic(si));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200924 if (msg)
Willy Tarreau06d80a92017-10-19 14:32:15 +0200925 co_inject(si_ic(si), msg->str, msg->len);
Willy Tarreaue7dff022015-04-03 01:14:29 +0200926 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200927 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200928 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200929 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200930}
931
Willy Tarreau87b09662015-04-03 00:22:06 +0200932/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100933 * and message.
934 */
935
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200936struct chunk *http_error_message(struct stream *s)
Willy Tarreau80587432006-12-24 17:47:20 +0100937{
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200938 const int msgnum = http_get_status_idx(s->txn->status);
939
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200940 if (s->be->errmsg[msgnum].str)
941 return &s->be->errmsg[msgnum];
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200942 else if (strm_fe(s)->errmsg[msgnum].str)
943 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100944 else
945 return &http_err_chunks[msgnum];
946}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200947
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100948void
949http_reply_and_close(struct stream *s, short status, struct chunk *msg)
950{
Christopher Fauletd7c91962015-04-30 11:48:27 +0200951 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Faulet3e344292015-11-24 16:24:13 +0100952 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100953 stream_int_retnclose(&s->si[0], msg);
954}
955
Willy Tarreau53b6c742006-12-17 13:37:46 +0100956/*
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200957 * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown
958 * ones.
Willy Tarreau53b6c742006-12-17 13:37:46 +0100959 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100960enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100961{
962 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100963 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100964
965 m = ((unsigned)*str - 'A');
966
967 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100968 for (h = http_methods[m]; h->len > 0; h++) {
969 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100970 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100971 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100972 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100973 };
Willy Tarreau53b6c742006-12-17 13:37:46 +0100974 }
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200975 return HTTP_METH_OTHER;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100976}
977
Willy Tarreau21d2af32008-02-14 20:25:24 +0100978/* Parse the URI from the given transaction (which is assumed to be in request
979 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
980 * It is returned otherwise.
981 */
Thierry FOURNIER3c331782015-09-17 19:33:35 +0200982char *http_get_path(struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +0100983{
984 char *ptr, *end;
985
Willy Tarreau9b28e032012-10-12 23:49:43 +0200986 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100987 end = ptr + txn->req.sl.rq.u_l;
988
989 if (ptr >= end)
990 return NULL;
991
Lukas Tribus23953682017-04-28 13:24:30 +0000992 /* RFC7230, par. 2.7 :
Willy Tarreau21d2af32008-02-14 20:25:24 +0100993 * Request-URI = "*" | absuri | abspath | authority
994 */
995
996 if (*ptr == '*')
997 return NULL;
998
999 if (isalpha((unsigned char)*ptr)) {
1000 /* this is a scheme as described by RFC3986, par. 3.1 */
1001 ptr++;
1002 while (ptr < end &&
1003 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
1004 ptr++;
1005 /* skip '://' */
1006 if (ptr == end || *ptr++ != ':')
1007 return NULL;
1008 if (ptr == end || *ptr++ != '/')
1009 return NULL;
1010 if (ptr == end || *ptr++ != '/')
1011 return NULL;
1012 }
1013 /* skip [user[:passwd]@]host[:[port]] */
1014
1015 while (ptr < end && *ptr != '/')
1016 ptr++;
1017
1018 if (ptr == end)
1019 return NULL;
1020
1021 /* OK, we got the '/' ! */
1022 return ptr;
1023}
1024
William Lallemand65ad6e12014-01-31 15:08:02 +01001025/* Parse the URI from the given string and look for the "/" beginning the PATH.
1026 * If not found, return NULL. It is returned otherwise.
1027 */
1028static char *
1029http_get_path_from_string(char *str)
1030{
1031 char *ptr = str;
1032
1033 /* RFC2616, par. 5.1.2 :
1034 * Request-URI = "*" | absuri | abspath | authority
1035 */
1036
1037 if (*ptr == '*')
1038 return NULL;
1039
1040 if (isalpha((unsigned char)*ptr)) {
1041 /* this is a scheme as described by RFC3986, par. 3.1 */
1042 ptr++;
1043 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
1044 ptr++;
1045 /* skip '://' */
1046 if (*ptr == '\0' || *ptr++ != ':')
1047 return NULL;
1048 if (*ptr == '\0' || *ptr++ != '/')
1049 return NULL;
1050 if (*ptr == '\0' || *ptr++ != '/')
1051 return NULL;
1052 }
1053 /* skip [user[:passwd]@]host[:[port]] */
1054
1055 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
1056 ptr++;
1057
1058 if (*ptr == '\0' || *ptr == ' ')
1059 return NULL;
1060
1061 /* OK, we got the '/' ! */
1062 return ptr;
1063}
1064
Willy Tarreau71241ab2012-12-27 11:30:54 +01001065/* Returns a 302 for a redirectable request that reaches a server working in
1066 * in redirect mode. This may only be called just after the stream interface
1067 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
1068 * follow normal proxy processing. NOTE: this function is designed to support
1069 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001070 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001071void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001072{
1073 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001074 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001075 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001076 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001077
1078 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001079 trash.len = strlen(HTTP_302);
1080 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001081
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001082 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001083
Willy Tarreauefb453c2008-10-26 20:49:47 +01001084 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001085 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001086 return;
1087
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001088 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +01001089 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001090 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
1091 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001092 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001093
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001094 /* 3: add the request URI. Since it was already forwarded, we need
1095 * to temporarily rewind the buffer.
1096 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001097 txn = s->txn;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001098 b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001099
Willy Tarreauefb453c2008-10-26 20:49:47 +01001100 path = http_get_path(txn);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001101 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 +02001102
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001103 b_adv(s->req.buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001104
Willy Tarreauefb453c2008-10-26 20:49:47 +01001105 if (!path)
1106 return;
1107
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001108 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001109 return;
1110
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001111 memcpy(trash.str + trash.len, path, len);
1112 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001113
1114 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001115 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
1116 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001117 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001118 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
1119 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001120 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001121
1122 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001123 si_shutr(si);
1124 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001125 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001126 si->state = SI_ST_CLO;
1127
1128 /* send the message */
Willy Tarreau2019f952017-03-14 11:07:31 +01001129 txn->status = 302;
1130 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001131
1132 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001133 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001134 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001135}
1136
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001137/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001138 * that the server side is closed. Note that err_type is actually a
1139 * bitmask, where almost only aborts may be cumulated with other
1140 * values. We consider that aborted operations are more important
1141 * than timeouts or errors due to the fact that nobody else in the
1142 * logs might explain incomplete retries. All others should avoid
1143 * being cumulated. It should normally not be possible to have multiple
1144 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001145 * Note that connection errors appearing on the second request of a keep-alive
1146 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001147 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001148void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001149{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001150 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001151
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001152 /* set s->txn->status for http_error_message(s) */
1153 s->txn->status = 503;
1154
Willy Tarreauefb453c2008-10-26 20:49:47 +01001155 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001156 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001157 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001158 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001159 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001160 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001161 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001162 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001163 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001164 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001165 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001166 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001167 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001168 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001169 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001170 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001171 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001172 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001173 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001174 (s->flags & SF_SRV_REUSED) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001175 http_error_message(s));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001176 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001177 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001178 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001179 http_error_message(s));
1180 else { /* SI_ET_CONN_OTHER and others */
1181 s->txn->status = 500;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001182 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001183 http_error_message(s));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001184 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001185}
1186
Willy Tarreau42250582007-04-01 01:30:43 +02001187extern const char sess_term_cond[8];
1188extern const char sess_fin_state[8];
1189extern const char *monthname[12];
Willy Tarreau63986c72015-04-03 22:55:33 +02001190struct pool_head *pool2_http_txn;
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001191struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001192struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001193struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001194
Willy Tarreau117f59e2007-03-04 18:17:17 +01001195/*
1196 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001197 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001198 */
1199void capture_headers(char *som, struct hdr_idx *idx,
1200 char **cap, struct cap_hdr *cap_hdr)
1201{
1202 char *eol, *sol, *col, *sov;
1203 int cur_idx;
1204 struct cap_hdr *h;
1205 int len;
1206
1207 sol = som + hdr_idx_first_pos(idx);
1208 cur_idx = hdr_idx_first_idx(idx);
1209
1210 while (cur_idx) {
1211 eol = sol + idx->v[cur_idx].len;
1212
1213 col = sol;
1214 while (col < eol && *col != ':')
1215 col++;
1216
1217 sov = col + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01001218 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001219 sov++;
1220
1221 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001222 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001223 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1224 if (cap[h->index] == NULL)
1225 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001226 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001227
1228 if (cap[h->index] == NULL) {
1229 Alert("HTTP capture : out of memory.\n");
1230 continue;
1231 }
1232
1233 len = eol - sov;
1234 if (len > h->len)
1235 len = h->len;
1236
1237 memcpy(cap[h->index], sov, len);
1238 cap[h->index][len]=0;
1239 }
1240 }
1241 sol = eol + idx->v[cur_idx].cr + 1;
1242 cur_idx = idx->v[cur_idx].next;
1243 }
1244}
1245
Willy Tarreaubaaee002006-06-26 02:48:02 +02001246/*
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001247 * Returns the data from Authorization header. Function may be called more
1248 * than once so data is stored in txn->auth_data. When no header is found
1249 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001250 * searching again for something we are unable to find anyway. However, if
1251 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +02001252 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001253 */
1254
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001255int
Willy Tarreau87b09662015-04-03 00:22:06 +02001256get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001257{
1258
Willy Tarreaueee5b512015-04-03 23:46:31 +02001259 struct http_txn *txn = s->txn;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001260 struct chunk auth_method;
1261 struct hdr_ctx ctx;
1262 char *h, *p;
1263 int len;
1264
1265#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +02001266 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001267#endif
1268
1269 if (txn->auth.method == HTTP_AUTH_WRONG)
1270 return 0;
1271
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001272 txn->auth.method = HTTP_AUTH_WRONG;
1273
1274 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001275
1276 if (txn->flags & TX_USE_PX_CONN) {
1277 h = "Proxy-Authorization";
1278 len = strlen(h);
1279 } else {
1280 h = "Authorization";
1281 len = strlen(h);
1282 }
1283
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001284 if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001285 return 0;
1286
1287 h = ctx.line + ctx.val;
1288
1289 p = memchr(h, ' ', ctx.vlen);
Willy Tarreau5c557d12016-03-13 08:17:02 +01001290 len = p - h;
1291 if (!p || len <= 0)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001292 return 0;
1293
David Carlier7365f7d2016-04-04 11:54:42 +01001294 if (chunk_initlen(&auth_method, h, 0, len) != 1)
1295 return 0;
1296
Willy Tarreau5c557d12016-03-13 08:17:02 +01001297 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001298
1299 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
Christopher Faulet6988f672017-07-27 15:18:52 +02001300 struct chunk *http_auth = get_trash_chunk();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001301
1302 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Christopher Faulet6988f672017-07-27 15:18:52 +02001303 http_auth->str, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001304
1305 if (len < 0)
1306 return 0;
1307
1308
Christopher Faulet6988f672017-07-27 15:18:52 +02001309 http_auth->str[len] = '\0';
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001310
Christopher Faulet6988f672017-07-27 15:18:52 +02001311 p = strchr(http_auth->str, ':');
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001312
1313 if (!p)
1314 return 0;
1315
Christopher Faulet6988f672017-07-27 15:18:52 +02001316 txn->auth.user = http_auth->str;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001317 *p = '\0';
1318 txn->auth.pass = p+1;
1319
1320 txn->auth.method = HTTP_AUTH_BASIC;
1321 return 1;
1322 }
1323
1324 return 0;
1325}
1326
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001327
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001328/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1329 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1330 * nothing is done and 1 is returned.
1331 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001332static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001333{
1334 int delta;
1335 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001336 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001337
1338 if (msg->sl.rq.v_l != 0)
1339 return 1;
1340
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001341 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1342 if (txn->meth != HTTP_METH_GET)
1343 return 0;
1344
Willy Tarreau9b28e032012-10-12 23:49:43 +02001345 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001346
1347 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001348 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1349 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001350 }
1351 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001352 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001353 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001354 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001355 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001356 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001357 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001358 NULL, NULL);
1359 if (unlikely(!cur_end))
1360 return 0;
1361
1362 /* we have a full HTTP/1.0 request now and we know that
1363 * we have either a CR or an LF at <ptr>.
1364 */
1365 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1366 return 1;
1367}
1368
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001369/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001370 * and "keep-alive" values. If we already know that some headers may safely
1371 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001372 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1373 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001374 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001375 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1376 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1377 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001378 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001379 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001380void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001381{
Willy Tarreau5b154472009-12-21 20:11:07 +01001382 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001383 const char *hdr_val = "Connection";
1384 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001385
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001386 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001387 return;
1388
Willy Tarreau88d349d2010-01-25 12:15:43 +01001389 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1390 hdr_val = "Proxy-Connection";
1391 hdr_len = 16;
1392 }
1393
Willy Tarreau5b154472009-12-21 20:11:07 +01001394 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001395 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001396 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001397 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1398 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001399 if (to_del & 2)
1400 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001401 else
1402 txn->flags |= TX_CON_KAL_SET;
1403 }
1404 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1405 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001406 if (to_del & 1)
1407 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001408 else
1409 txn->flags |= TX_CON_CLO_SET;
1410 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001411 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1412 txn->flags |= TX_HDR_CONN_UPG;
1413 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001414 }
1415
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001416 txn->flags |= TX_HDR_CONN_PRS;
1417 return;
1418}
Willy Tarreau5b154472009-12-21 20:11:07 +01001419
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001420/* Apply desired changes on the Connection: header. Values may be removed and/or
1421 * added depending on the <wanted> flags, which are exclusively composed of
1422 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1423 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1424 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001425void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001426{
1427 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001428 const char *hdr_val = "Connection";
1429 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001430
1431 ctx.idx = 0;
1432
Willy Tarreau88d349d2010-01-25 12:15:43 +01001433
1434 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1435 hdr_val = "Proxy-Connection";
1436 hdr_len = 16;
1437 }
1438
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001439 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001440 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001441 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1442 if (wanted & TX_CON_KAL_SET)
1443 txn->flags |= TX_CON_KAL_SET;
1444 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001445 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001446 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001447 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1448 if (wanted & TX_CON_CLO_SET)
1449 txn->flags |= TX_CON_CLO_SET;
1450 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001451 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001452 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001453 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001454
1455 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1456 return;
1457
1458 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1459 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001460 hdr_val = "Connection: close";
1461 hdr_len = 17;
1462 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1463 hdr_val = "Proxy-Connection: close";
1464 hdr_len = 23;
1465 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001466 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001467 }
1468
1469 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1470 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001471 hdr_val = "Connection: keep-alive";
1472 hdr_len = 22;
1473 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1474 hdr_val = "Proxy-Connection: keep-alive";
1475 hdr_len = 28;
1476 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001477 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001478 }
1479 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001480}
1481
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001482/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
1483 * value is larger than 1000, it is bound to 1000. The parser consumes up to
1484 * 1 digit, one dot and 3 digits and stops on the first invalid character.
1485 * Unparsable qvalues return 1000 as "q=1.000".
1486 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02001487int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001488{
1489 int q = 1000;
1490
Willy Tarreau506c69a2014-07-08 00:59:48 +02001491 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001492 goto out;
1493 q = (*qvalue++ - '0') * 1000;
1494
1495 if (*qvalue++ != '.')
1496 goto out;
1497
Willy Tarreau506c69a2014-07-08 00:59:48 +02001498 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001499 goto out;
1500 q += (*qvalue++ - '0') * 100;
1501
Willy Tarreau506c69a2014-07-08 00:59:48 +02001502 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001503 goto out;
1504 q += (*qvalue++ - '0') * 10;
1505
Willy Tarreau506c69a2014-07-08 00:59:48 +02001506 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001507 goto out;
1508 q += (*qvalue++ - '0') * 1;
1509 out:
1510 if (q > 1000)
1511 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02001512 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02001513 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001514 return q;
1515}
William Lallemand82fe75c2012-10-23 10:25:10 +02001516
Willy Tarreau87b09662015-04-03 00:22:06 +02001517void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001518{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001519 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001520 int tmp = TX_CON_WANT_KAL;
1521
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001522 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
1523 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001524 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
1525 tmp = TX_CON_WANT_TUN;
1526
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001527 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001528 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
1529 tmp = TX_CON_WANT_TUN;
1530 }
1531
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001532 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001533 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
1534 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001535 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001536 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
1537 tmp = TX_CON_WANT_CLO;
1538 else
1539 tmp = TX_CON_WANT_SCL;
1540 }
1541
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001542 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001543 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
1544 tmp = TX_CON_WANT_CLO;
1545
1546 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
1547 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
1548
1549 if (!(txn->flags & TX_HDR_CONN_PRS) &&
1550 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
1551 /* parse the Connection header and possibly clean it */
1552 int to_del = 0;
1553 if ((msg->flags & HTTP_MSGF_VER_11) ||
1554 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001555 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001556 to_del |= 2; /* remove "keep-alive" */
1557 if (!(msg->flags & HTTP_MSGF_VER_11))
1558 to_del |= 1; /* remove "close" */
1559 http_parse_connection_header(txn, msg, to_del);
1560 }
1561
1562 /* check if client or config asks for explicit close in KAL/SCL */
1563 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
1564 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
1565 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
1566 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
1567 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001568 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001569 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
1570}
William Lallemand82fe75c2012-10-23 10:25:10 +02001571
Willy Tarreaud787e662009-07-07 10:14:51 +02001572/* This stream analyser waits for a complete HTTP request. It returns 1 if the
1573 * processing can continue on next analysers, or zero if it either needs more
1574 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001575 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02001576 * when it has nothing left to do, and may remove any analyser when it wants to
1577 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001578 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001579int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001580{
Willy Tarreau59234e92008-11-30 23:51:27 +01001581 /*
1582 * We will parse the partial (or complete) lines.
1583 * We will check the request syntax, and also join multi-line
1584 * headers. An index of all the lines will be elaborated while
1585 * parsing.
1586 *
1587 * For the parsing, we use a 28 states FSM.
1588 *
1589 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02001590 * req->buf->p = beginning of request
1591 * req->buf->p + msg->eoh = end of processed headers / start of current one
1592 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02001593 * msg->eol = end of current header or line (LF or CRLF)
1594 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02001595 *
1596 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02001597 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02001598 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
1599 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01001600 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001601
Willy Tarreau59234e92008-11-30 23:51:27 +01001602 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001603 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001604 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01001605 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02001606 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001607
Willy Tarreau87b09662015-04-03 00:22:06 +02001608 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 +01001609 now_ms, __FUNCTION__,
1610 s,
1611 req,
1612 req->rex, req->wex,
1613 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001614 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01001615 req->analysers);
1616
Willy Tarreau52a0c602009-08-16 22:45:38 +02001617 /* we're speaking HTTP here, so let's speak HTTP to the client */
1618 s->srv_error = http_return_srv_error;
1619
Willy Tarreau83e3af02009-12-28 17:39:57 +01001620 /* There's a protected area at the end of the buffer for rewriting
1621 * purposes. We don't want to start to parse the request if the
1622 * protected area is affected, because we may have to move processed
1623 * data later, which is much more complicated.
1624 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001625 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001626
1627 /* This point is executed when some data is avalaible for analysis,
1628 * so we log the end of the idle time. */
1629 if (s->logs.t_idle == -1)
1630 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
1631
Willy Tarreau379357a2013-06-08 12:55:46 +02001632 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01001633 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001634 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01001635 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01001636 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001637 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001638 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01001639 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01001640 return 0;
1641 }
Willy Tarreau379357a2013-06-08 12:55:46 +02001642 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
1643 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
1644 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001645 }
1646
Willy Tarreau9b28e032012-10-12 23:49:43 +02001647 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01001648 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001649 }
1650
Willy Tarreau59234e92008-11-30 23:51:27 +01001651 /* 1: we might have to print this header in debug mode */
1652 if (unlikely((global.mode & MODE_DEBUG) &&
1653 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02001654 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001655 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001656
Willy Tarreau9b28e032012-10-12 23:49:43 +02001657 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001658 /* this is a bit complex : in case of error on the request line,
1659 * we know that rq.l is still zero, so we display only the part
1660 * up to the end of the line (truncated by debug_hdr).
1661 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001662 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01001663 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001664
Willy Tarreau59234e92008-11-30 23:51:27 +01001665 sol += hdr_idx_first_pos(&txn->hdr_idx);
1666 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001667
Willy Tarreau59234e92008-11-30 23:51:27 +01001668 while (cur_idx) {
1669 eol = sol + txn->hdr_idx.v[cur_idx].len;
1670 debug_hdr("clihdr", s, sol, eol);
1671 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1672 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001673 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001674 }
1675
Willy Tarreau58f10d72006-12-04 02:26:12 +01001676
Willy Tarreau59234e92008-11-30 23:51:27 +01001677 /*
1678 * Now we quickly check if we have found a full valid request.
1679 * If not so, we check the FD and buffer states before leaving.
1680 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01001681 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001682 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02001683 * on a keep-alive stream, if we encounter and error, close, t/o,
1684 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001685 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02001686 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02001687 * Last, we may increase some tracked counters' http request errors on
1688 * the cases that are deliberately the client's fault. For instance,
1689 * a timeout or connection reset is not counted as an error. However
1690 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01001691 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001692
Willy Tarreau655dce92009-11-08 13:10:58 +01001693 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001694 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001695 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001696 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01001697 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001698 stream_inc_http_req_ctr(s);
1699 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001700 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001701 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01001702 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001703
Willy Tarreau59234e92008-11-30 23:51:27 +01001704 /* 1: Since we are in header mode, if there's no space
1705 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02001706 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01001707 * must terminate it now.
1708 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001709 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001710 /* FIXME: check if URI is set and return Status
1711 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001712 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001713 stream_inc_http_req_ctr(s);
1714 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001715 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02001716 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02001717 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01001718 goto return_bad_req;
1719 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001720
Willy Tarreau59234e92008-11-30 23:51:27 +01001721 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001722 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001723 if (!(s->flags & SF_ERR_MASK))
1724 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001725
Willy Tarreaufcffa692010-01-10 14:21:19 +01001726 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001727 goto failed_keep_alive;
1728
Willy Tarreau0f228a02015-05-01 15:37:53 +02001729 if (sess->fe->options & PR_O_IGNORE_PRB)
1730 goto failed_keep_alive;
1731
Willy Tarreau59234e92008-11-30 23:51:27 +01001732 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001733 if (msg->err_pos >= 0) {
Emeric Brun8c1aaa22017-06-15 11:30:06 +02001734 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001735 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001736 }
1737
Willy Tarreaudc979f22012-12-04 10:39:01 +01001738 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001739 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001740 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001741 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01001742 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001743 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001744 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001745 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001746 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001747 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001748
Willy Tarreaue7dff022015-04-03 01:14:29 +02001749 if (!(s->flags & SF_FINST_MASK))
1750 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001751 return 0;
1752 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001753
Willy Tarreau59234e92008-11-30 23:51:27 +01001754 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001755 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001756 if (!(s->flags & SF_ERR_MASK))
1757 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001758
Willy Tarreaufcffa692010-01-10 14:21:19 +01001759 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001760 goto failed_keep_alive;
1761
Willy Tarreau0f228a02015-05-01 15:37:53 +02001762 if (sess->fe->options & PR_O_IGNORE_PRB)
1763 goto failed_keep_alive;
1764
Willy Tarreau59234e92008-11-30 23:51:27 +01001765 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001766 if (msg->err_pos >= 0) {
Emeric Brun8c1aaa22017-06-15 11:30:06 +02001767 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001768 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001769 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001770 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001771 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001772 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001773 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001774 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001775
Willy Tarreau87b09662015-04-03 00:22:06 +02001776 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001777 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001778 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001779 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001780 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001781
Willy Tarreaue7dff022015-04-03 01:14:29 +02001782 if (!(s->flags & SF_FINST_MASK))
1783 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001784 return 0;
1785 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001786
Willy Tarreau59234e92008-11-30 23:51:27 +01001787 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001788 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001789 if (!(s->flags & SF_ERR_MASK))
1790 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001791
Willy Tarreaufcffa692010-01-10 14:21:19 +01001792 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001793 goto failed_keep_alive;
1794
Willy Tarreau0f228a02015-05-01 15:37:53 +02001795 if (sess->fe->options & PR_O_IGNORE_PRB)
1796 goto failed_keep_alive;
1797
Willy Tarreau4076a152009-04-02 15:18:36 +02001798 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02001799 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001800 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001801 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001802 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001803 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001804 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001805 stream_inc_http_err_ctr(s);
1806 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001807 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001808 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001809 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001810 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001811
Willy Tarreaue7dff022015-04-03 01:14:29 +02001812 if (!(s->flags & SF_FINST_MASK))
1813 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001814 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001815 }
1816
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001817 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001818 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001819 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01001820#ifdef TCP_QUICKACK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001821 if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i &&
1822 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01001823 /* We need more data, we have to re-enable quick-ack in case we
1824 * previously disabled it, otherwise we might cause the client
1825 * to delay next data.
1826 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001827 setsockopt(__objt_conn(sess->origin)->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01001828 }
1829#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01001830
Willy Tarreaufcffa692010-01-10 14:21:19 +01001831 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
1832 /* If the client starts to talk, let's fall back to
1833 * request timeout processing.
1834 */
1835 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01001836 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01001837 }
1838
Willy Tarreau59234e92008-11-30 23:51:27 +01001839 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01001840 if (!tick_isset(req->analyse_exp)) {
1841 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
1842 (txn->flags & TX_WAIT_NEXT_RQ) &&
1843 tick_isset(s->be->timeout.httpka))
1844 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
1845 else
1846 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
1847 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001848
Willy Tarreau59234e92008-11-30 23:51:27 +01001849 /* we're not ready yet */
1850 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001851
1852 failed_keep_alive:
1853 /* Here we process low-level errors for keep-alive requests. In
1854 * short, if the request is not the first one and it experiences
1855 * a timeout, read error or shutdown, we just silently close so
1856 * that the client can try again.
1857 */
1858 txn->status = 0;
1859 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01001860 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001861 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02001862 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001863 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001864 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01001865 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01001866 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001867
Willy Tarreaud787e662009-07-07 10:14:51 +02001868 /* OK now we have a complete HTTP request with indexed headers. Let's
1869 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02001870 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01001871 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01001872 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001873 * byte after the last LF. msg->sov points to the first byte of data.
1874 * msg->eol cannot be trusted because it may have been left uninitialized
1875 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02001876 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02001877
Willy Tarreau87b09662015-04-03 00:22:06 +02001878 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001879 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001880
Willy Tarreaub16a5742010-01-10 14:46:16 +01001881 if (txn->flags & TX_WAIT_NEXT_RQ) {
1882 /* kill the pending keep-alive timeout */
1883 txn->flags &= ~TX_WAIT_NEXT_RQ;
1884 req->analyse_exp = TICK_ETERNITY;
1885 }
1886
1887
Willy Tarreaud787e662009-07-07 10:14:51 +02001888 /* Maybe we found in invalid header name while we were configured not
1889 * to block on that, so we have to capture it now.
1890 */
1891 if (unlikely(msg->err_pos >= 0))
Emeric Brun8c1aaa22017-06-15 11:30:06 +02001892 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02001893
Willy Tarreau59234e92008-11-30 23:51:27 +01001894 /*
1895 * 1: identify the method
1896 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001897 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01001898
1899 /* we can make use of server redirect on GET and HEAD */
1900 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001901 s->flags |= SF_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001902
Willy Tarreau59234e92008-11-30 23:51:27 +01001903 /*
1904 * 2: check if the URI matches the monitor_uri.
1905 * We have to do this for every request which gets in, because
1906 * the monitor-uri is defined by the frontend.
1907 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001908 if (unlikely((sess->fe->monitor_uri_len != 0) &&
1909 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02001910 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001911 sess->fe->monitor_uri,
1912 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001913 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001914 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001915 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001916 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001917
Willy Tarreaue7dff022015-04-03 01:14:29 +02001918 s->flags |= SF_MONITOR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001919 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreaub80c2302007-11-30 20:51:32 +01001920
Willy Tarreau59234e92008-11-30 23:51:27 +01001921 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001922 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001923 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02001924
Willy Tarreau59234e92008-11-30 23:51:27 +01001925 ret = acl_pass(ret);
1926 if (cond->pol == ACL_COND_UNLESS)
1927 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001928
Willy Tarreau59234e92008-11-30 23:51:27 +01001929 if (ret) {
1930 /* we fail this request, let's return 503 service unavail */
1931 txn->status = 503;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001932 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001933 if (!(s->flags & SF_ERR_MASK))
1934 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001935 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001936 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001937 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001938
Willy Tarreau59234e92008-11-30 23:51:27 +01001939 /* nothing to fail, let's reply normaly */
1940 txn->status = 200;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001941 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001942 if (!(s->flags & SF_ERR_MASK))
1943 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001944 goto return_prx_cond;
1945 }
1946
1947 /*
1948 * 3: Maybe we have to copy the original REQURI for the logs ?
1949 * Note: we cannot log anymore if the request has been
1950 * classified as invalid.
1951 */
1952 if (unlikely(s->logs.logwait & LW_REQ)) {
1953 /* we have a complete HTTP request that we must log */
1954 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
1955 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001956
Stéphane Cottin23e9e932017-05-18 08:58:41 +02001957 if (urilen >= global.tune.requri_len )
1958 urilen = global.tune.requri_len - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001959 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01001960 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001961
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001962 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01001963 s->do_log(s);
1964 } else {
1965 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001966 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001967 }
Willy Tarreau06619262006-12-17 08:37:22 +01001968
Willy Tarreau91852eb2015-05-01 13:26:00 +02001969 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
1970 * exactly one digit "." one digit. This check may be disabled using
1971 * option accept-invalid-http-request.
1972 */
1973 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
1974 if (msg->sl.rq.v_l != 8) {
1975 msg->err_pos = msg->sl.rq.v;
1976 goto return_bad_req;
1977 }
1978
1979 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
1980 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
1981 req->buf->p[msg->sl.rq.v + 6] != '.' ||
1982 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
1983 msg->err_pos = msg->sl.rq.v + 4;
1984 goto return_bad_req;
1985 }
1986 }
Willy Tarreau13317662015-05-01 13:47:08 +02001987 else {
1988 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1989 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
1990 goto return_bad_req;
1991 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02001992
Willy Tarreau5b154472009-12-21 20:11:07 +01001993 /* ... and check if the request is HTTP/1.1 or above */
1994 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02001995 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
1996 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
1997 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001998 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01001999
2000 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002001 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 +01002002
Willy Tarreau88d349d2010-01-25 12:15:43 +01002003 /* if the frontend has "option http-use-proxy-header", we'll check if
2004 * we have what looks like a proxied connection instead of a connection,
2005 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2006 * Note that this is *not* RFC-compliant, however browsers and proxies
2007 * happen to do that despite being non-standard :-(
2008 * We consider that a request not beginning with either '/' or '*' is
2009 * a proxied connection, which covers both "scheme://location" and
2010 * CONNECT ip:port.
2011 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002012 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002013 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002014 txn->flags |= TX_USE_PX_CONN;
2015
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002016 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002017 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002018
Willy Tarreau59234e92008-11-30 23:51:27 +01002019 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002020 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002021 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002022 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002023
Willy Tarreau557f1992015-05-01 10:05:17 +02002024 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
2025 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002026 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002027 * The length of a message body is determined by one of the following
2028 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02002029 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002030 * 1. Any response to a HEAD request and any response with a 1xx
2031 * (Informational), 204 (No Content), or 304 (Not Modified) status
2032 * code is always terminated by the first empty line after the
2033 * header fields, regardless of the header fields present in the
2034 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002035 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002036 * 2. Any 2xx (Successful) response to a CONNECT request implies that
2037 * the connection will become a tunnel immediately after the empty
2038 * line that concludes the header fields. A client MUST ignore any
2039 * Content-Length or Transfer-Encoding header fields received in
2040 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002041 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002042 * 3. If a Transfer-Encoding header field is present and the chunked
2043 * transfer coding (Section 4.1) is the final encoding, the message
2044 * body length is determined by reading and decoding the chunked
2045 * data until the transfer coding indicates the data is complete.
2046 *
2047 * If a Transfer-Encoding header field is present in a response and
2048 * the chunked transfer coding is not the final encoding, the
2049 * message body length is determined by reading the connection until
2050 * it is closed by the server. If a Transfer-Encoding header field
2051 * is present in a request and the chunked transfer coding is not
2052 * the final encoding, the message body length cannot be determined
2053 * reliably; the server MUST respond with the 400 (Bad Request)
2054 * status code and then close the connection.
2055 *
2056 * If a message is received with both a Transfer-Encoding and a
2057 * Content-Length header field, the Transfer-Encoding overrides the
2058 * Content-Length. Such a message might indicate an attempt to
2059 * perform request smuggling (Section 9.5) or response splitting
2060 * (Section 9.4) and ought to be handled as an error. A sender MUST
2061 * remove the received Content-Length field prior to forwarding such
2062 * a message downstream.
2063 *
2064 * 4. If a message is received without Transfer-Encoding and with
2065 * either multiple Content-Length header fields having differing
2066 * field-values or a single Content-Length header field having an
2067 * invalid value, then the message framing is invalid and the
2068 * recipient MUST treat it as an unrecoverable error. If this is a
2069 * request message, the server MUST respond with a 400 (Bad Request)
2070 * status code and then close the connection. If this is a response
2071 * message received by a proxy, the proxy MUST close the connection
2072 * to the server, discard the received response, and send a 502 (Bad
2073 * Gateway) response to the client. If this is a response message
2074 * received by a user agent, the user agent MUST close the
2075 * connection to the server and discard the received response.
2076 *
2077 * 5. If a valid Content-Length header field is present without
2078 * Transfer-Encoding, its decimal value defines the expected message
2079 * body length in octets. If the sender closes the connection or
2080 * the recipient times out before the indicated number of octets are
2081 * received, the recipient MUST consider the message to be
2082 * incomplete and close the connection.
2083 *
2084 * 6. If this is a request message and none of the above are true, then
2085 * the message body length is zero (no message body is present).
2086 *
2087 * 7. Otherwise, this is a response message without a declared message
2088 * body length, so the message body length is determined by the
2089 * number of octets received prior to the server closing the
2090 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002091 */
2092
Willy Tarreau32b47f42009-10-18 20:55:02 +02002093 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002094 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau4979d5c2015-05-01 10:06:30 +02002095 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002096 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Christopher Fauletbe821b92017-03-30 11:21:53 +02002097 msg->flags |= HTTP_MSGF_TE_CHNK;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002098 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02002099 /* chunked not last, return badreq */
2100 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002101 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002102 }
2103
Willy Tarreau1c913912015-04-30 10:57:51 +02002104 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02002105 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02002106 if (msg->flags & HTTP_MSGF_TE_CHNK) {
2107 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
2108 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2109 }
Willy Tarreau34dfc602015-05-01 10:09:49 +02002110 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002111 signed long long cl;
2112
Willy Tarreauad14f752011-09-02 20:33:27 +02002113 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002114 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002115 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002116 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002117
Willy Tarreauad14f752011-09-02 20:33:27 +02002118 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002119 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002120 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002121 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002122
Willy Tarreauad14f752011-09-02 20:33:27 +02002123 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002124 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002125 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002126 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002127
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002128 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002129 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002130 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002131 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002132
Christopher Fauletbe821b92017-03-30 11:21:53 +02002133 msg->flags |= HTTP_MSGF_CNT_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002134 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002135 }
2136
Willy Tarreau34dfc602015-05-01 10:09:49 +02002137 /* even bodyless requests have a known length */
2138 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002139
Willy Tarreau179085c2014-04-28 16:48:56 +02002140 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
2141 * only change if both the request and the config reference something else.
2142 * Option httpclose by itself sets tunnel mode where headers are mangled.
2143 * However, if another mode is set, it will affect it (eg: server-close/
2144 * keep-alive + httpclose = close). Note that we avoid to redo the same work
2145 * if FE and BE have the same settings (common). The method consists in
2146 * checking if options changed between the two calls (implying that either
2147 * one is non-null, or one of them is non-null and we are there for the first
2148 * time.
2149 */
2150 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002151 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002152 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02002153
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02002154 /* we may have to wait for the request's body */
2155 if ((s->be->options & PR_O_WREQ_BODY) &&
2156 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
2157 req->analysers |= AN_REQ_HTTP_BODY;
2158
Willy Tarreaud787e662009-07-07 10:14:51 +02002159 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002160 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002161 req->analyse_exp = TICK_ETERNITY;
2162 return 1;
2163
2164 return_bad_req:
2165 /* We centralize bad requests processing here */
2166 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2167 /* we detected a parsing error. We want to archive this request
2168 * in the dedicated proxy area for later troubleshooting.
2169 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02002170 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002171 }
2172
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002173 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02002174 txn->req.msg_state = HTTP_MSG_ERROR;
2175 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002176 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002177
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002178 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002179 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02002180 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaud787e662009-07-07 10:14:51 +02002181
2182 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02002183 if (!(s->flags & SF_ERR_MASK))
2184 s->flags |= SF_ERR_PRXCOND;
2185 if (!(s->flags & SF_FINST_MASK))
2186 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02002187
Christopher Faulet0184ea72017-01-05 14:06:34 +01002188 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02002189 req->analyse_exp = TICK_ETERNITY;
2190 return 0;
2191}
2192
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002193
Willy Tarreau347a35d2013-11-22 17:51:09 +01002194/* This function prepares an applet to handle the stats. It can deal with the
2195 * "100-continue" expectation, check that admin rules are met for POST requests,
2196 * and program a response message if something was unexpected. It cannot fail
2197 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002198 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002199 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02002200 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002201 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002202int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002203{
2204 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01002205 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02002206 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002207 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002208 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002209 struct uri_auth *uri_auth = s->be->uri_auth;
2210 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002211 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002212
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002213 appctx = si_appctx(si);
2214 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2215 appctx->st1 = appctx->st2 = 0;
2216 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
2217 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02002218 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02002219 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002220
2221 uri = msg->chn->buf->p + msg->sl.rq.u;
2222 lookup = uri + uri_auth->uri_len;
2223
2224 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
2225 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002226 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002227 break;
2228 }
2229 }
2230
2231 if (uri_auth->refresh) {
2232 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
2233 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002234 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002235 break;
2236 }
2237 }
2238 }
2239
2240 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
2241 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002242 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002243 break;
2244 }
2245 }
2246
Willy Tarreau1e62df92016-01-11 18:57:53 +01002247 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
2248 if (memcmp(h, ";typed", 6) == 0) {
2249 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
2250 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
2251 break;
2252 }
2253 }
2254
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002255 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2256 if (memcmp(h, ";st=", 4) == 0) {
2257 int i;
2258 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002259 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002260 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
2261 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002262 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002263 break;
2264 }
2265 }
2266 break;
2267 }
2268 }
2269
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002270 appctx->ctx.stats.scope_str = 0;
2271 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002272 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2273 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
2274 int itx = 0;
2275 const char *h2;
2276 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
2277 const char *err;
2278
2279 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
2280 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002281 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002282 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
2283 itx++;
2284 h++;
2285 }
2286
2287 if (itx > STAT_SCOPE_TXT_MAXLEN)
2288 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002289 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002290
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002291 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002292 memcpy(scope_txt, h2, itx);
2293 scope_txt[itx] = '\0';
2294 err = invalid_char(scope_txt);
2295 if (err) {
2296 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002297 appctx->ctx.stats.scope_str = 0;
2298 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002299 }
2300 break;
2301 }
2302 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002303
2304 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002305 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002306 int ret = 1;
2307
2308 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002309 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 +01002310 ret = acl_pass(ret);
2311 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2312 ret = !ret;
2313 }
2314
2315 if (ret) {
2316 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002317 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002318 break;
2319 }
2320 }
2321
2322 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02002323 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002324 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02002325 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02002326 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
2327 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002328 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002329 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002330 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002331 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
2332 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002333 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002334 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002335 else {
2336 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002337 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002338 }
2339
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002340 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002341 return 1;
2342}
2343
Lukas Tribus67db8df2013-06-23 17:37:13 +02002344/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
2345 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
2346 */
Vincent Bernat6e615892016-05-18 16:17:44 +02002347void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02002348{
2349#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02002350 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02002351 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2352#endif
2353#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02002354 if (from->ss_family == AF_INET6) {
2355 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02002356 /* v4-mapped addresses need IP_TOS */
2357 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2358 else
2359 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
2360 }
2361#endif
2362}
2363
Willy Tarreau87b09662015-04-03 00:22:06 +02002364int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002365 const char* name, unsigned int name_len,
2366 const char *str, struct my_regex *re,
2367 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06002368{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002369 struct hdr_ctx ctx;
2370 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002371 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002372 int (*http_find_hdr_func)(const char *name, int len, char *sol,
2373 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002374 struct chunk *output = get_trash_chunk();
2375
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002376 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06002377
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002378 /* Choose the header browsing function. */
2379 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002380 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002381 http_find_hdr_func = http_find_header2;
2382 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002383 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002384 http_find_hdr_func = http_find_full_header2;
2385 break;
2386 default: /* impossible */
2387 return -1;
2388 }
2389
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002390 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
2391 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06002392 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002393 char *val = ctx.line + ctx.val;
2394 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06002395
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002396 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
2397 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06002398
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002399 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002400 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06002401 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06002402
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002403 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06002404
2405 hdr->len += delta;
2406 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002407
2408 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002409 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06002410 }
2411
2412 return 0;
2413}
2414
Willy Tarreau87b09662015-04-03 00:22:06 +02002415static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002416 const char* name, unsigned int name_len,
2417 struct list *fmt, struct my_regex *re,
2418 int action)
2419{
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002420 struct chunk *replace;
2421 int ret = -1;
2422
2423 replace = alloc_trash_chunk();
2424 if (!replace)
2425 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002426
2427 replace->len = build_logline(s, replace->str, replace->size, fmt);
2428 if (replace->len >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002429 goto leave;
2430
2431 ret = http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002432
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002433 leave:
2434 free_trash_chunk(replace);
2435 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002436}
2437
Willy Tarreau87b09662015-04-03 00:22:06 +02002438/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02002439 * transaction <txn>. Returns the verdict of the first rule that prevents
2440 * further processing of the request (auth, deny, ...), and defaults to
2441 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2442 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02002443 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2444 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2445 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002446 */
Willy Tarreau0b748332014-04-29 00:13:29 +02002447enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02002448http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002449{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002450 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002451 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002452 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002453 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01002454 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02002455 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02002456 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02002457 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002458
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002459 /* If "the current_rule_list" match the executed rule list, we are in
2460 * resume condition. If a resume is needed it is always in the action
2461 * and never in the ACL or converters. In this case, we initialise the
2462 * current rule, and go to the action execution point.
2463 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002464 if (s->current_rule) {
2465 rule = s->current_rule;
2466 s->current_rule = NULL;
2467 if (s->current_rule_list == rules)
2468 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002469 }
2470 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002471
Willy Tarreauff011f22011-01-06 17:51:27 +01002472 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002473
Willy Tarreau96257ec2012-12-27 10:46:37 +01002474 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01002475 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01002476 int ret;
2477
Willy Tarreau192252e2015-04-04 01:47:55 +02002478 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002479 ret = acl_pass(ret);
2480
Willy Tarreauff011f22011-01-06 17:51:27 +01002481 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002482 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002483
2484 if (!ret) /* condition not matched */
2485 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002486 }
2487
Willy Tarreauacc98002015-09-27 23:34:39 +02002488 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002489resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01002490 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002491 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02002492 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002493
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002494 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02002495 if (deny_status)
2496 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02002497 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002498
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002499 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01002500 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002501 if (deny_status)
2502 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02002503 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01002504
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002505 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02002506 /* Auth might be performed on regular http-req rules as well as on stats */
2507 auth_realm = rule->arg.auth.realm;
2508 if (!auth_realm) {
2509 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
2510 auth_realm = STATS_DEFAULT_REALM;
2511 else
2512 auth_realm = px->id;
2513 }
2514 /* send 401/407 depending on whether we use a proxy or not. We still
2515 * count one error, because normal browsing won't significantly
2516 * increase the counter but brute force attempts will.
2517 */
2518 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
2519 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002520 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02002521 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02002522 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002523
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002524 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02002525 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
2526 return HTTP_RULE_RES_BADREQ;
2527 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01002528
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002529 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002530 s->task->nice = rule->arg.nice;
2531 break;
2532
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002533 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002534 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002535 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002536 break;
2537
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002538 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002539#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002540 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002541 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002542#endif
2543 break;
2544
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002545 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002546 s->logs.level = rule->arg.loglevel;
2547 break;
2548
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002549 case ACT_HTTP_REPLACE_HDR:
2550 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002551 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
2552 rule->arg.hdr_add.name_len,
2553 &rule->arg.hdr_add.fmt,
2554 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06002555 return HTTP_RULE_RES_BADREQ;
2556 break;
2557
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002558 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01002559 ctx.idx = 0;
2560 /* remove all occurrences of the header */
2561 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2562 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
2563 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01002564 }
Willy Tarreau85603282015-01-21 20:39:27 +01002565 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002566
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002567 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002568 case ACT_HTTP_ADD_HDR: {
Thierry Fournier4b788f72016-06-01 13:35:36 +02002569 /* The scope of the trash buffer must be limited to this function. The
2570 * build_logline() function can execute a lot of other function which
2571 * can use the trash buffer. So for limiting the scope of this global
2572 * buffer, we build first the header value using build_logline, and
2573 * after we store the header name.
2574 */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002575 struct chunk *replace;
2576
2577 replace = alloc_trash_chunk();
2578 if (!replace)
2579 return HTTP_RULE_RES_BADREQ;
2580
Thierry Fournier4b788f72016-06-01 13:35:36 +02002581 len = rule->arg.hdr_add.name_len + 2,
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002582 len += build_logline(s, replace->str + len, replace->size - len, &rule->arg.hdr_add.fmt);
2583 memcpy(replace->str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
2584 replace->str[rule->arg.hdr_add.name_len] = ':';
2585 replace->str[rule->arg.hdr_add.name_len + 1] = ' ';
2586 replace->len = len;
Willy Tarreau85603282015-01-21 20:39:27 +01002587
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002588 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002589 /* remove all occurrences of the header */
2590 ctx.idx = 0;
2591 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2592 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
2593 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
2594 }
2595 }
2596
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002597 http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->str, replace->len);
2598
2599 free_trash_chunk(replace);
Willy Tarreau96257ec2012-12-27 10:46:37 +01002600 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002601 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002602
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002603 case ACT_HTTP_DEL_ACL:
2604 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002605 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002606 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002607
2608 /* collect reference */
2609 ref = pat_ref_lookup(rule->arg.map.ref);
2610 if (!ref)
2611 continue;
2612
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002613 /* allocate key */
2614 key = alloc_trash_chunk();
2615 if (!key)
2616 return HTTP_RULE_RES_BADREQ;
2617
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002618 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002619 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2620 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002621
2622 /* perform update */
2623 /* returned code: 1=ok, 0=ko */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002624 pat_ref_delete(ref, key->str);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002625
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002626 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002627 break;
2628 }
2629
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002630 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002631 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002632 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002633
2634 /* collect reference */
2635 ref = pat_ref_lookup(rule->arg.map.ref);
2636 if (!ref)
2637 continue;
2638
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002639 /* allocate key */
2640 key = alloc_trash_chunk();
2641 if (!key)
2642 return HTTP_RULE_RES_BADREQ;
2643
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002644 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002645 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2646 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002647
2648 /* perform update */
2649 /* add entry only if it does not already exist */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002650 if (pat_ref_find_elt(ref, key->str) == NULL)
2651 pat_ref_add(ref, key->str, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002652
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002653 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002654 break;
2655 }
2656
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002657 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002658 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002659 struct chunk *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002660
2661 /* collect reference */
2662 ref = pat_ref_lookup(rule->arg.map.ref);
2663 if (!ref)
2664 continue;
2665
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002666 /* allocate key */
2667 key = alloc_trash_chunk();
2668 if (!key)
2669 return HTTP_RULE_RES_BADREQ;
2670
2671 /* allocate value */
2672 value = alloc_trash_chunk();
2673 if (!value) {
2674 free_trash_chunk(key);
2675 return HTTP_RULE_RES_BADREQ;
2676 }
2677
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002678 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002679 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2680 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002681
2682 /* collect value */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002683 value->len = build_logline(s, value->str, value->size, &rule->arg.map.value);
2684 value->str[value->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002685
2686 /* perform update */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002687 if (pat_ref_find_elt(ref, key->str) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002688 /* update entry if it exists */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002689 pat_ref_set(ref, key->str, value->str, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002690 else
2691 /* insert a new entry */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002692 pat_ref_add(ref, key->str, value->str, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002693
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002694 free_trash_chunk(key);
2695 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002696 break;
2697 }
William Lallemand73025dd2014-04-24 14:38:37 +02002698
Thierry FOURNIER42148732015-09-02 17:17:33 +02002699 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02002700 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
2701 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002702
Willy Tarreauacc98002015-09-27 23:34:39 +02002703 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002704 case ACT_RET_ERR:
2705 case ACT_RET_CONT:
2706 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002707 case ACT_RET_STOP:
2708 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002709 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002710 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002711 return HTTP_RULE_RES_YIELD;
2712 }
William Lallemand73025dd2014-04-24 14:38:37 +02002713 break;
2714
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002715 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02002716 /* Note: only the first valid tracking parameter of each
2717 * applies.
2718 */
2719
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002720 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02002721 struct stktable *t;
2722 struct stksess *ts;
2723 struct stktable_key *key;
Emeric Brun819fc6f2017-06-13 19:37:32 +02002724 void *ptr1, *ptr2;
Willy Tarreau09448f72014-06-25 18:12:15 +02002725
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02002726 t = rule->arg.trk_ctr.table.t;
2727 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 +02002728
2729 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002730 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02002731
2732 /* let's count a new HTTP request as it's the first time we do it */
Emeric Brun819fc6f2017-06-13 19:37:32 +02002733 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2734 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2735 if (ptr1 || ptr2) {
2736 RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreau09448f72014-06-25 18:12:15 +02002737
Emeric Brun819fc6f2017-06-13 19:37:32 +02002738 if (ptr1)
2739 stktable_data_cast(ptr1, http_req_cnt)++;
2740
2741 if (ptr2)
2742 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
2743 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2744
2745 RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
2746 }
Willy Tarreau09448f72014-06-25 18:12:15 +02002747
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002748 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002749 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002750 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02002751 }
2752 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02002753 break;
2754
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002755 /* other flags exists, but normaly, they never be matched. */
2756 default:
2757 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002758 }
2759 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01002760
2761 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02002762 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002763}
2764
Willy Tarreau71241ab2012-12-27 11:30:54 +01002765
Willy Tarreau51d861a2015-05-22 17:30:48 +02002766/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2767 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2768 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2769 * is returned, the process can continue the evaluation of next rule list. If
2770 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2771 * is returned, it means the operation could not be processed and a server error
2772 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
2773 * deny rule. If *YIELD is returned, the caller must call again the function
2774 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002775 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002776static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002777http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002778{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002779 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002780 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002781 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002782 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002783 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02002784 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002785
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002786 /* If "the current_rule_list" match the executed rule list, we are in
2787 * resume condition. If a resume is needed it is always in the action
2788 * and never in the ACL or converters. In this case, we initialise the
2789 * current rule, and go to the action execution point.
2790 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002791 if (s->current_rule) {
2792 rule = s->current_rule;
2793 s->current_rule = NULL;
2794 if (s->current_rule_list == rules)
2795 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002796 }
2797 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002798
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002799 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002800
2801 /* check optional condition */
2802 if (rule->cond) {
2803 int ret;
2804
Willy Tarreau192252e2015-04-04 01:47:55 +02002805 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002806 ret = acl_pass(ret);
2807
2808 if (rule->cond->pol == ACL_COND_UNLESS)
2809 ret = !ret;
2810
2811 if (!ret) /* condition not matched */
2812 continue;
2813 }
2814
Willy Tarreauacc98002015-09-27 23:34:39 +02002815 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002816resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002817 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002818 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002819 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002820
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002821 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002822 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002823 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002824
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002825 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002826 s->task->nice = rule->arg.nice;
2827 break;
2828
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002829 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002830 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002831 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002832 break;
2833
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002834 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002835#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002836 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002837 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002838#endif
2839 break;
2840
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002841 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002842 s->logs.level = rule->arg.loglevel;
2843 break;
2844
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002845 case ACT_HTTP_REPLACE_HDR:
2846 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002847 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
2848 rule->arg.hdr_add.name_len,
2849 &rule->arg.hdr_add.fmt,
2850 &rule->arg.hdr_add.re, rule->action))
Christopher Fauletcdade942017-02-08 12:41:31 +01002851 return HTTP_RULE_RES_BADREQ;
Sasha Pachev218f0642014-06-16 12:05:59 -06002852 break;
2853
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002854 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002855 ctx.idx = 0;
2856 /* remove all occurrences of the header */
2857 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2858 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
2859 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2860 }
Willy Tarreau85603282015-01-21 20:39:27 +01002861 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002862
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002863 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002864 case ACT_HTTP_ADD_HDR: {
2865 struct chunk *replace;
2866
2867 replace = alloc_trash_chunk();
2868 if (!replace)
2869 return HTTP_RULE_RES_BADREQ;
2870
2871 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
2872 memcpy(replace->str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
2873 replace->len = rule->arg.hdr_add.name_len;
2874 replace->str[replace->len++] = ':';
2875 replace->str[replace->len++] = ' ';
2876 replace->len += build_logline(s, replace->str + replace->len, replace->size - replace->len,
2877 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01002878
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002879 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002880 /* remove all occurrences of the header */
2881 ctx.idx = 0;
2882 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2883 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
2884 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2885 }
2886 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002887 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->str, replace->len);
2888
2889 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002890 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002891 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002892
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002893 case ACT_HTTP_DEL_ACL:
2894 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002895 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002896 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002897
2898 /* collect reference */
2899 ref = pat_ref_lookup(rule->arg.map.ref);
2900 if (!ref)
2901 continue;
2902
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002903 /* allocate key */
2904 key = alloc_trash_chunk();
2905 if (!key)
2906 return HTTP_RULE_RES_BADREQ;
2907
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002908 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002909 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2910 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002911
2912 /* perform update */
2913 /* returned code: 1=ok, 0=ko */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002914 pat_ref_delete(ref, key->str);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002915
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002916 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002917 break;
2918 }
2919
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002920 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002921 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002922 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002923
2924 /* collect reference */
2925 ref = pat_ref_lookup(rule->arg.map.ref);
2926 if (!ref)
2927 continue;
2928
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002929 /* allocate key */
2930 key = alloc_trash_chunk();
2931 if (!key)
2932 return HTTP_RULE_RES_BADREQ;
2933
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002934 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002935 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2936 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002937
2938 /* perform update */
2939 /* check if the entry already exists */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002940 if (pat_ref_find_elt(ref, key->str) == NULL)
2941 pat_ref_add(ref, key->str, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002942
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002943 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002944 break;
2945 }
2946
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002947 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002948 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002949 struct chunk *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002950
2951 /* collect reference */
2952 ref = pat_ref_lookup(rule->arg.map.ref);
2953 if (!ref)
2954 continue;
2955
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002956 /* allocate key */
2957 key = alloc_trash_chunk();
2958 if (!key)
2959 return HTTP_RULE_RES_BADREQ;
2960
2961 /* allocate value */
2962 value = alloc_trash_chunk();
2963 if (!value) {
2964 free_trash_chunk(key);
2965 return HTTP_RULE_RES_BADREQ;
2966 }
2967
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002968 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002969 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2970 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002971
2972 /* collect value */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002973 value->len = build_logline(s, value->str, value->size, &rule->arg.map.value);
2974 value->str[value->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002975
2976 /* perform update */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002977 if (pat_ref_find_elt(ref, key->str) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002978 /* update entry if it exists */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002979 pat_ref_set(ref, key->str, value->str, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002980 else
2981 /* insert a new entry */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002982 pat_ref_add(ref, key->str, value->str, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002983
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002984 free_trash_chunk(key);
2985 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002986 break;
2987 }
William Lallemand73025dd2014-04-24 14:38:37 +02002988
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002989 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02002990 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
2991 return HTTP_RULE_RES_BADREQ;
2992 return HTTP_RULE_RES_DONE;
2993
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002994 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
2995 /* Note: only the first valid tracking parameter of each
2996 * applies.
2997 */
2998
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002999 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003000 struct stktable *t;
3001 struct stksess *ts;
3002 struct stktable_key *key;
3003 void *ptr;
3004
3005 t = rule->arg.trk_ctr.table.t;
3006 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
3007
3008 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02003009 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003010
Emeric Brun819fc6f2017-06-13 19:37:32 +02003011 RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
3012
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003013 /* let's count a new HTTP request as it's the first time we do it */
3014 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3015 if (ptr)
3016 stktable_data_cast(ptr, http_req_cnt)++;
3017
3018 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3019 if (ptr)
3020 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3021 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3022
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003023 /* When the client triggers a 4xx from the server, it's most often due
3024 * to a missing object or permission. These events should be tracked
3025 * because if they happen often, it may indicate a brute force or a
3026 * vulnerability scan. Normally this is done when receiving the response
3027 * but here we're tracking after this ought to have been done so we have
3028 * to do it on purpose.
3029 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02003030 if ((unsigned)(txn->status - 400) < 100) {
3031 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3032 if (ptr)
3033 stktable_data_cast(ptr, http_err_cnt)++;
3034
3035 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3036 if (ptr)
3037 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3038 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3039 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02003040
3041 RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
3042
3043 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3044 if (sess->fe != s->be)
3045 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3046
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003047 }
3048 }
3049 break;
3050
Thierry FOURNIER42148732015-09-02 17:17:33 +02003051 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02003052 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
3053 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003054
Willy Tarreauacc98002015-09-27 23:34:39 +02003055 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003056 case ACT_RET_ERR:
3057 case ACT_RET_CONT:
3058 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003059 case ACT_RET_STOP:
3060 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003061 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003062 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003063 return HTTP_RULE_RES_YIELD;
3064 }
William Lallemand73025dd2014-04-24 14:38:37 +02003065 break;
3066
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003067 /* other flags exists, but normaly, they never be matched. */
3068 default:
3069 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003070 }
3071 }
3072
3073 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003074 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003075}
3076
3077
Willy Tarreau71241ab2012-12-27 11:30:54 +01003078/* Perform an HTTP redirect based on the information in <rule>. The function
3079 * returns non-zero on success, or zero in case of a, irrecoverable error such
3080 * as too large a request to build a valid response.
3081 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003082static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01003083{
Willy Tarreaub329a312015-05-22 16:27:37 +02003084 struct http_msg *req = &txn->req;
3085 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003086 const char *msg_fmt;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003087 struct chunk *chunk;
3088 int ret = 0;
3089
3090 chunk = alloc_trash_chunk();
3091 if (!chunk)
3092 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003093
3094 /* build redirect message */
3095 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003096 case 308:
3097 msg_fmt = HTTP_308;
3098 break;
3099 case 307:
3100 msg_fmt = HTTP_307;
3101 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003102 case 303:
3103 msg_fmt = HTTP_303;
3104 break;
3105 case 301:
3106 msg_fmt = HTTP_301;
3107 break;
3108 case 302:
3109 default:
3110 msg_fmt = HTTP_302;
3111 break;
3112 }
3113
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003114 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
3115 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003116
3117 switch(rule->type) {
3118 case REDIRECT_TYPE_SCHEME: {
3119 const char *path;
3120 const char *host;
3121 struct hdr_ctx ctx;
3122 int pathlen;
3123 int hostlen;
3124
3125 host = "";
3126 hostlen = 0;
3127 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02003128 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003129 host = ctx.line + ctx.val;
3130 hostlen = ctx.vlen;
3131 }
3132
3133 path = http_get_path(txn);
3134 /* build message using path */
3135 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02003136 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003137 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3138 int qs = 0;
3139 while (qs < pathlen) {
3140 if (path[qs] == '?') {
3141 pathlen = qs;
3142 break;
3143 }
3144 qs++;
3145 }
3146 }
3147 } else {
3148 path = "/";
3149 pathlen = 1;
3150 }
3151
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003152 if (rule->rdr_str) { /* this is an old "redirect" rule */
3153 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003154 if (chunk->len + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
3155 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003156
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003157 /* add scheme */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003158 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3159 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003160 }
3161 else {
3162 /* add scheme with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003163 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003164
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003165 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003166 if (chunk->len + 3 + hostlen + pathlen > chunk->size - 4)
3167 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003168 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003169 /* add "://" */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003170 memcpy(chunk->str + chunk->len, "://", 3);
3171 chunk->len += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003172
3173 /* add host */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003174 memcpy(chunk->str + chunk->len, host, hostlen);
3175 chunk->len += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003176
3177 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003178 memcpy(chunk->str + chunk->len, path, pathlen);
3179 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003180
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003181 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003182 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003183 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003184 if (chunk->len > chunk->size - 5)
3185 goto leave;
3186 chunk->str[chunk->len] = '/';
3187 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003188 }
3189
3190 break;
3191 }
3192 case REDIRECT_TYPE_PREFIX: {
3193 const char *path;
3194 int pathlen;
3195
3196 path = http_get_path(txn);
3197 /* build message using path */
3198 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02003199 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003200 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3201 int qs = 0;
3202 while (qs < pathlen) {
3203 if (path[qs] == '?') {
3204 pathlen = qs;
3205 break;
3206 }
3207 qs++;
3208 }
3209 }
3210 } else {
3211 path = "/";
3212 pathlen = 1;
3213 }
3214
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003215 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003216 if (chunk->len + rule->rdr_len + pathlen > chunk->size - 4)
3217 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003218
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003219 /* add prefix. Note that if prefix == "/", we don't want to
3220 * add anything, otherwise it makes it hard for the user to
3221 * configure a self-redirection.
3222 */
3223 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003224 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3225 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003226 }
3227 }
3228 else {
3229 /* add prefix with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003230 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003231
3232 /* Check length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003233 if (chunk->len + pathlen > chunk->size - 4)
3234 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003235 }
3236
3237 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003238 memcpy(chunk->str + chunk->len, path, pathlen);
3239 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003240
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003241 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003242 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003243 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003244 if (chunk->len > chunk->size - 5)
3245 goto leave;
3246 chunk->str[chunk->len] = '/';
3247 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003248 }
3249
3250 break;
3251 }
3252 case REDIRECT_TYPE_LOCATION:
3253 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003254 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003255 if (chunk->len + rule->rdr_len > chunk->size - 4)
3256 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003257
3258 /* add location */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003259 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3260 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003261 }
3262 else {
3263 /* add location with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003264 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003265
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003266 /* Check left length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003267 if (chunk->len > chunk->size - 4)
3268 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003269 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003270 break;
3271 }
3272
3273 if (rule->cookie_len) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003274 memcpy(chunk->str + chunk->len, "\r\nSet-Cookie: ", 14);
3275 chunk->len += 14;
3276 memcpy(chunk->str + chunk->len, rule->cookie_str, rule->cookie_len);
3277 chunk->len += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003278 }
3279
Willy Tarreau19b14122017-02-28 09:48:11 +01003280 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003281 txn->status = rule->code;
3282 /* let's log the request time */
3283 s->logs.tv_request = now;
3284
Christopher Fauletbe821b92017-03-30 11:21:53 +02003285 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003286 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3287 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3288 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02003289 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003290 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003291 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: keep-alive", 30);
3292 chunk->len += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003293 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003294 memcpy(chunk->str + chunk->len, "\r\nConnection: keep-alive", 24);
3295 chunk->len += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003296 }
3297 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003298 memcpy(chunk->str + chunk->len, "\r\n\r\n", 4);
3299 chunk->len += 4;
3300 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau06d80a92017-10-19 14:32:15 +02003301 co_inject(res->chn, chunk->str, chunk->len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003302 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02003303 bi_fast_delete(req->chn->buf, req->sov);
3304 req->next -= req->sov;
3305 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003306 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01003307 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02003308 req->msg_state = HTTP_MSG_CLOSED;
3309 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02003310 /* Trim any possible response */
3311 res->chn->buf->i = 0;
3312 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02003313 /* let the server side turn to SI_ST_CLO */
3314 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003315 } else {
3316 /* keep-alive not possible */
3317 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003318 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3319 chunk->len += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003320 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003321 memcpy(chunk->str + chunk->len, "\r\nConnection: close\r\n\r\n", 23);
3322 chunk->len += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003323 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003324 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01003325 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003326 }
3327
Willy Tarreaue7dff022015-04-03 01:14:29 +02003328 if (!(s->flags & SF_ERR_MASK))
3329 s->flags |= SF_ERR_LOCAL;
3330 if (!(s->flags & SF_FINST_MASK))
3331 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003332
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003333 ret = 1;
3334 leave:
3335 free_trash_chunk(chunk);
3336 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003337}
3338
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003339/* This stream analyser runs all HTTP request processing which is common to
3340 * frontends and backends, which means blocking ACLs, filters, connection-close,
3341 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003342 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003343 * either needs more data or wants to immediately abort the request (eg: deny,
3344 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003345 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003346int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003347{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003348 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003349 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02003350 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003351 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003352 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02003353 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003354 int deny_status = HTTP_ERR_403;
Olivier Houchardc2aae742017-09-22 18:26:28 +02003355 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaud787e662009-07-07 10:14:51 +02003356
Willy Tarreau655dce92009-11-08 13:10:58 +01003357 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003358 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003359 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02003360 }
3361
Willy Tarreau87b09662015-04-03 00:22:06 +02003362 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 +02003363 now_ms, __FUNCTION__,
3364 s,
3365 req,
3366 req->rex, req->wex,
3367 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003368 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003369 req->analysers);
3370
Willy Tarreau65410832014-04-28 21:25:43 +02003371 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02003372 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02003373
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003374 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02003375 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02003376 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01003377
Willy Tarreau0b748332014-04-29 00:13:29 +02003378 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003379 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
3380 goto return_prx_yield;
3381
Willy Tarreau0b748332014-04-29 00:13:29 +02003382 case HTTP_RULE_RES_CONT:
3383 case HTTP_RULE_RES_STOP: /* nothing to do */
3384 break;
Willy Tarreau52542592014-04-28 18:33:26 +02003385
Willy Tarreau0b748332014-04-29 00:13:29 +02003386 case HTTP_RULE_RES_DENY: /* deny or tarpit */
3387 if (txn->flags & TX_CLTARPIT)
3388 goto tarpit;
3389 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003390
Willy Tarreau0b748332014-04-29 00:13:29 +02003391 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
3392 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02003393
Willy Tarreau0b748332014-04-29 00:13:29 +02003394 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02003395 goto done;
3396
Willy Tarreau0b748332014-04-29 00:13:29 +02003397 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
3398 goto return_bad_req;
3399 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003400 }
3401
Olivier Houchardc2aae742017-09-22 18:26:28 +02003402 if (conn && conn->flags & CO_FL_EARLY_DATA) {
3403 struct hdr_ctx ctx;
3404
3405 ctx.idx = 0;
3406 if (!http_find_header2("Early-Data", strlen("Early-Data"),
3407 s->req.buf->p, &txn->hdr_idx, &ctx)) {
3408 if (unlikely(http_header_add_tail2(&txn->req,
3409 &txn->hdr_idx, "Early-Data: 1",
3410 strlen("Early-Data: 1"))) < 0) {
3411 goto return_bad_req;
3412 }
3413 }
3414
3415 }
3416
Willy Tarreau52542592014-04-28 18:33:26 +02003417 /* OK at this stage, we know that the request was accepted according to
3418 * the http-request rules, we can check for the stats. Note that the
3419 * URI is detected *before* the req* rules in order not to be affected
3420 * by a possible reqrep, while they are processed *after* so that a
3421 * reqdeny can still block them. This clearly needs to change in 1.6!
3422 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003423 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02003424 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01003425 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02003426 txn->status = 500;
3427 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003428 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003429
Willy Tarreaue7dff022015-04-03 01:14:29 +02003430 if (!(s->flags & SF_ERR_MASK))
3431 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02003432 goto return_prx_cond;
3433 }
3434
3435 /* parse the whole stats request and extract the relevant information */
3436 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02003437 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02003438 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003439
Willy Tarreau0b748332014-04-29 00:13:29 +02003440 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
3441 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003442
Willy Tarreau0b748332014-04-29 00:13:29 +02003443 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
3444 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003445 }
3446
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003447 /* evaluate the req* rules except reqadd */
3448 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003449 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003450 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003451
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003452 if (txn->flags & TX_CLDENY)
3453 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003454
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003455 if (txn->flags & TX_CLTARPIT) {
3456 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003457 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003458 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003459 }
Willy Tarreau06619262006-12-17 08:37:22 +01003460
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003461 /* add request headers from the rule sets in the same order */
3462 list_for_each_entry(wl, &px->req_add, list) {
3463 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003464 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003465 ret = acl_pass(ret);
3466 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3467 ret = !ret;
3468 if (!ret)
3469 continue;
3470 }
3471
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003472 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003473 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003474 }
3475
Willy Tarreau52542592014-04-28 18:33:26 +02003476
3477 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003478 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003479 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003480 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003481 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Willy Tarreau347a35d2013-11-22 17:51:09 +01003482
Willy Tarreaue7dff022015-04-03 01:14:29 +02003483 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
3484 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
3485 if (!(s->flags & SF_FINST_MASK))
3486 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003487
Willy Tarreau70730dd2014-04-24 18:06:27 +02003488 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01003489 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
3490 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003491 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003492 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003493 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003494
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003495 /* check whether we have some ACLs set to redirect this request */
3496 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003497 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003498 int ret;
3499
Willy Tarreau192252e2015-04-04 01:47:55 +02003500 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003501 ret = acl_pass(ret);
3502 if (rule->cond->pol == ACL_COND_UNLESS)
3503 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003504 if (!ret)
3505 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003506 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003507 if (!http_apply_redirect_rule(rule, s, txn))
3508 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003509 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003510 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003511
Willy Tarreau2be39392010-01-03 17:24:51 +01003512 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3513 * If this happens, then the data will not come immediately, so we must
3514 * send all what we have without waiting. Note that due to the small gain
3515 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003516 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003517 * itself once used.
3518 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003519 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003520
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003521 done: /* done with this analyser, continue with next ones that the calling
3522 * points will have set, if any.
3523 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003524 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003525 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
3526 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003527 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003528
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003529 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02003530 /* Allow cookie logging
3531 */
3532 if (s->be->cookie_name || sess->fe->capture_name)
3533 manage_client_side_cookies(s, req);
3534
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003535 /* When a connection is tarpitted, we use the tarpit timeout,
3536 * which may be the same as the connect timeout if unspecified.
3537 * If unset, then set it to zero because we really want it to
3538 * eventually expire. We build the tarpit as an analyser.
3539 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003540 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003541
3542 /* wipe the request out so that we can drop the connection early
3543 * if the client closes first.
3544 */
3545 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003546
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003547 txn->status = http_err_codes[deny_status];
3548
Christopher Faulet0184ea72017-01-05 14:06:34 +01003549 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003550 req->analysers |= AN_REQ_HTTP_TARPIT;
3551 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3552 if (!req->analyse_exp)
3553 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02003554 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003555 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003556 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003557 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003558 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003559 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003560 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003561
3562 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003563
3564 /* Allow cookie logging
3565 */
3566 if (s->be->cookie_name || sess->fe->capture_name)
3567 manage_client_side_cookies(s, req);
3568
Willy Tarreau0b748332014-04-29 00:13:29 +02003569 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003570 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003571 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003572 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02003573 stream_inc_http_err_ctr(s);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003574 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003575 if (sess->fe != s->be)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003576 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003577 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003578 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003579 goto return_prx_cond;
3580
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003581 return_bad_req:
3582 /* We centralize bad requests processing here */
3583 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3584 /* we detected a parsing error. We want to archive this request
3585 * in the dedicated proxy area for later troubleshooting.
3586 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02003587 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003588 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003589
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003590 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003591 txn->req.msg_state = HTTP_MSG_ERROR;
3592 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003593 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003594
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003595 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003596 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003597 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003598
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003599 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003600 if (!(s->flags & SF_ERR_MASK))
3601 s->flags |= SF_ERR_PRXCOND;
3602 if (!(s->flags & SF_FINST_MASK))
3603 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003604
Christopher Faulet0184ea72017-01-05 14:06:34 +01003605 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003606 req->analyse_exp = TICK_ETERNITY;
3607 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003608
3609 return_prx_yield:
3610 channel_dont_connect(req);
3611 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003612}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003613
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003614/* This function performs all the processing enabled for the current request.
3615 * It returns 1 if the processing can continue on next analysers, or zero if it
3616 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003617 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003618 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003619int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003620{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003621 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003622 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003623 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02003624 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003625
Willy Tarreau655dce92009-11-08 13:10:58 +01003626 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003627 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003628 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003629 return 0;
3630 }
3631
Willy Tarreau87b09662015-04-03 00:22:06 +02003632 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 +02003633 now_ms, __FUNCTION__,
3634 s,
3635 req,
3636 req->rex, req->wex,
3637 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003638 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003639 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003640
Willy Tarreau59234e92008-11-30 23:51:27 +01003641 /*
3642 * Right now, we know that we have processed the entire headers
3643 * and that unwanted requests have been filtered out. We can do
3644 * whatever we want with the remaining request. Also, now we
3645 * may have separate values for ->fe, ->be.
3646 */
Willy Tarreau06619262006-12-17 08:37:22 +01003647
Willy Tarreau59234e92008-11-30 23:51:27 +01003648 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003649 * If HTTP PROXY is set we simply get remote server address parsing
3650 * incoming request. Note that this requires that a connection is
3651 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003652 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003653 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003654 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003655 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003656
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003657 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau973a5422015-08-05 21:47:23 +02003658 if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003659 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003660 txn->req.msg_state = HTTP_MSG_ERROR;
3661 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003662 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003663 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003664
Willy Tarreaue7dff022015-04-03 01:14:29 +02003665 if (!(s->flags & SF_ERR_MASK))
3666 s->flags |= SF_ERR_RESOURCE;
3667 if (!(s->flags & SF_FINST_MASK))
3668 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003669
3670 return 0;
3671 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003672
3673 path = http_get_path(txn);
3674 url2sa(req->buf->p + msg->sl.rq.u,
3675 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01003676 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003677 /* if the path was found, we have to remove everything between
3678 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
3679 * found, we need to replace from req->buf->p + msg->sl.rq.u for
3680 * u_l characters by a single "/".
3681 */
3682 if (path) {
3683 char *cur_ptr = req->buf->p;
3684 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3685 int delta;
3686
3687 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
3688 http_msg_move_end(&txn->req, delta);
3689 cur_end += delta;
3690 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3691 goto return_bad_req;
3692 }
3693 else {
3694 char *cur_ptr = req->buf->p;
3695 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3696 int delta;
3697
3698 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
3699 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
3700 http_msg_move_end(&txn->req, delta);
3701 cur_end += delta;
3702 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3703 goto return_bad_req;
3704 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003705 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003706
Willy Tarreau59234e92008-11-30 23:51:27 +01003707 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003708 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003709 * Note that doing so might move headers in the request, but
3710 * the fields will stay coherent and the URI will not move.
3711 * This should only be performed in the backend.
3712 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003713 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003714 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003715
William Lallemanda73203e2012-03-12 12:48:57 +01003716 /* add unique-id if "header-unique-id" is specified */
3717
Thierry Fournierf4011dd2016-03-29 17:23:51 +02003718 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003719 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
3720 goto return_bad_req;
3721 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003722 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003723 }
William Lallemanda73203e2012-03-12 12:48:57 +01003724
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003725 if (sess->fe->header_unique_id && s->unique_id) {
3726 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003727 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003728 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003729 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003730 goto return_bad_req;
3731 }
3732
Cyril Bontéb21570a2009-11-29 20:04:48 +01003733 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003734 * 9: add X-Forwarded-For if either the frontend or the backend
3735 * asks for it.
3736 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003737 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003738 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003739 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
3740 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
3741 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003742 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003743 /* The header is set to be added only if none is present
3744 * and we found it, so don't do anything.
3745 */
3746 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003747 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003748 /* Add an X-Forwarded-For header unless the source IP is
3749 * in the 'except' network range.
3750 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003751 if ((!sess->fe->except_mask.s_addr ||
3752 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
3753 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01003754 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003755 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003756 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003757 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003758 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003759 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003760
3761 /* Note: we rely on the backend to get the header name to be used for
3762 * x-forwarded-for, because the header is really meant for the backends.
3763 * However, if the backend did not specify any option, we have to rely
3764 * on the frontend's header name.
3765 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003766 if (s->be->fwdfor_hdr_len) {
3767 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003768 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003769 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003770 len = sess->fe->fwdfor_hdr_len;
3771 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003772 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003773 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 +01003774
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003775 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003776 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003777 }
3778 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003779 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003780 /* FIXME: for the sake of completeness, we should also support
3781 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003782 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003783 int len;
3784 char pn[INET6_ADDRSTRLEN];
3785 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003786 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003787 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003788
Willy Tarreau59234e92008-11-30 23:51:27 +01003789 /* Note: we rely on the backend to get the header name to be used for
3790 * x-forwarded-for, because the header is really meant for the backends.
3791 * However, if the backend did not specify any option, we have to rely
3792 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003793 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003794 if (s->be->fwdfor_hdr_len) {
3795 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003796 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003797 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003798 len = sess->fe->fwdfor_hdr_len;
3799 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003800 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003801 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003802
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003803 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003804 goto return_bad_req;
3805 }
3806 }
3807
3808 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003809 * 10: add X-Original-To if either the frontend or the backend
3810 * asks for it.
3811 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003812 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003813
3814 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003815 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003816 /* Add an X-Original-To header unless the destination IP is
3817 * in the 'except' network range.
3818 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003819 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003820
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003821 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003822 ((!sess->fe->except_mask_to.s_addr ||
3823 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
3824 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003825 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003826 (((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 +02003827 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003828 int len;
3829 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003830 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003831
3832 /* Note: we rely on the backend to get the header name to be used for
3833 * x-original-to, because the header is really meant for the backends.
3834 * However, if the backend did not specify any option, we have to rely
3835 * on the frontend's header name.
3836 */
3837 if (s->be->orgto_hdr_len) {
3838 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003839 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003840 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003841 len = sess->fe->orgto_hdr_len;
3842 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003843 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003844 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 +02003845
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003846 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003847 goto return_bad_req;
3848 }
3849 }
3850 }
3851
Willy Tarreau50fc7772012-11-11 22:19:57 +01003852 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3853 * If an "Upgrade" token is found, the header is left untouched in order not to have
3854 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3855 * "Upgrade" is present in the Connection header.
3856 */
3857 if (!(txn->flags & TX_HDR_CONN_UPG) &&
3858 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003859 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003860 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003861 unsigned int want_flags = 0;
3862
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003863 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003864 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003865 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003866 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003867 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003868 want_flags |= TX_CON_CLO_SET;
3869 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003870 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003871 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003872 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003873 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003874 want_flags |= TX_CON_KAL_SET;
3875 }
3876
3877 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003878 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003879 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003880
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003881
Willy Tarreau522d6c02009-12-06 18:49:18 +01003882 /* If we have no server assigned yet and we're balancing on url_param
3883 * with a POST request, we may be interested in checking the body for
3884 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003885 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003886 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02003887 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003888 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003889 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003890 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003891 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003892
Christopher Fauletbe821b92017-03-30 11:21:53 +02003893 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3894 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003895#ifdef TCP_QUICKACK
Christopher Fauletbe821b92017-03-30 11:21:53 +02003896 /* We expect some data from the client. Unless we know for sure
3897 * we already have a full request, we have to re-enable quick-ack
3898 * in case we previously disabled it, otherwise we might cause
3899 * the client to delay further data.
3900 */
3901 if ((sess->listener->options & LI_O_NOQUICKACK) &&
3902 cli_conn && conn_ctrl_ready(cli_conn) &&
3903 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
3904 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreau585744b2017-08-24 14:31:19 +02003905 setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003906#endif
Willy Tarreau03945942009-12-22 16:50:27 +01003907
Willy Tarreau59234e92008-11-30 23:51:27 +01003908 /*************************************************************
3909 * OK, that's finished for the headers. We have done what we *
3910 * could. Let's switch to the DATA state. *
3911 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003912 req->analyse_exp = TICK_ETERNITY;
3913 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003914
Willy Tarreau59234e92008-11-30 23:51:27 +01003915 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003916 /* OK let's go on with the BODY now */
3917 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003918
Willy Tarreau59234e92008-11-30 23:51:27 +01003919 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003920 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003921 /* we detected a parsing error. We want to archive this request
3922 * in the dedicated proxy area for later troubleshooting.
3923 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02003924 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003925 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003926
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003927 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01003928 txn->req.msg_state = HTTP_MSG_ERROR;
3929 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003930 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003931 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003932
Christopher Fauletff8abcd2017-06-02 15:33:24 +02003933 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003934 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02003935 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003936
Willy Tarreaue7dff022015-04-03 01:14:29 +02003937 if (!(s->flags & SF_ERR_MASK))
3938 s->flags |= SF_ERR_PRXCOND;
3939 if (!(s->flags & SF_FINST_MASK))
3940 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003941 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003942}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003943
Willy Tarreau60b85b02008-11-30 23:28:40 +01003944/* This function is an analyser which processes the HTTP tarpit. It always
3945 * returns zero, at the beginning because it prevents any other processing
3946 * from occurring, and at the end because it terminates the request.
3947 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003948int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003949{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003950 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003951
3952 /* This connection is being tarpitted. The CLIENT side has
3953 * already set the connect expiration date to the right
3954 * timeout. We just have to check that the client is still
3955 * there and that the timeout has not expired.
3956 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003957 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003958 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003959 !tick_is_expired(req->analyse_exp, now_ms))
3960 return 0;
3961
3962 /* We will set the queue timer to the time spent, just for
3963 * logging purposes. We fake a 500 server error, so that the
3964 * attacker will not suspect his connection has been tarpitted.
3965 * It will not cause trouble to the logs because we can exclude
3966 * the tarpitted connections by filtering on the 'PT' status flags.
3967 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003968 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3969
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003970 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003971 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003972
Christopher Faulet0184ea72017-01-05 14:06:34 +01003973 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003974 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003975
Willy Tarreaue7dff022015-04-03 01:14:29 +02003976 if (!(s->flags & SF_ERR_MASK))
3977 s->flags |= SF_ERR_PRXCOND;
3978 if (!(s->flags & SF_FINST_MASK))
3979 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003980 return 0;
3981}
3982
Willy Tarreau5a8f9472014-04-10 11:16:06 +02003983/* This function is an analyser which waits for the HTTP request body. It waits
3984 * for either the buffer to be full, or the full advertised contents to have
3985 * reached the buffer. It must only be called after the standard HTTP request
3986 * processing has occurred, because it expects the request to be parsed and will
3987 * look for the Expect header. It may send a 100-Continue interim response. It
3988 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
3989 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
3990 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01003991 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003992int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003993{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003994 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003995 struct http_txn *txn = s->txn;
3996 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003997
3998 /* We have to parse the HTTP request body to find any required data.
3999 * "balance url_param check_post" should have been the only way to get
4000 * into this. We were brought here after HTTP header analysis, so all
4001 * related structures are ready.
4002 */
4003
Willy Tarreau890988f2014-04-10 11:59:33 +02004004 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4005 /* This is the first call */
4006 if (msg->msg_state < HTTP_MSG_BODY)
4007 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004008
Willy Tarreau890988f2014-04-10 11:59:33 +02004009 if (msg->msg_state < HTTP_MSG_100_SENT) {
4010 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4011 * send an HTTP/1.1 100 Continue intermediate response.
4012 */
4013 if (msg->flags & HTTP_MSGF_VER_11) {
4014 struct hdr_ctx ctx;
4015 ctx.idx = 0;
4016 /* Expect is allowed in 1.1, look for it */
4017 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4018 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02004019 co_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01004020 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02004021 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004022 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004023 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004024 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004025
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004026 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004027 * req->buf->p still points to the beginning of the message. We
4028 * must save the body in msg->next because it survives buffer
4029 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004030 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004031 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004032
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004033 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004034 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4035 else
4036 msg->msg_state = HTTP_MSG_DATA;
4037 }
4038
Willy Tarreau890988f2014-04-10 11:59:33 +02004039 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4040 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004041 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004042 goto missing_data;
4043
4044 /* OK we have everything we need now */
4045 goto http_end;
4046 }
4047
4048 /* OK here we're parsing a chunked-encoded message */
4049
Willy Tarreau522d6c02009-12-06 18:49:18 +01004050 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004051 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004052 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004053 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004054 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004055 unsigned int chunk;
4056 int ret = h1_parse_chunk_size(req->buf, msg->next, req->buf->i, &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01004057
Willy Tarreau115acb92009-12-26 13:56:06 +01004058 if (!ret)
4059 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004060 else if (ret < 0) {
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004061 msg->err_pos = req->buf->i + ret;
4062 if (msg->err_pos < 0)
4063 msg->err_pos += req->buf->size;
Willy Tarreau87b09662015-04-03 00:22:06 +02004064 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004065 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004066 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004067
4068 msg->chunk_len = chunk;
4069 msg->body_len += chunk;
4070
4071 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01004072 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004073 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01004074 }
4075
Willy Tarreaud98cf932009-12-27 22:54:55 +01004076 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02004077 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4078 * for at least a whole chunk or the whole content length bytes after
4079 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004080 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004081 if (msg->msg_state == HTTP_MSG_TRAILERS)
4082 goto http_end;
4083
Willy Tarreaue115b492015-05-01 23:05:14 +02004084 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004085 goto http_end;
4086
4087 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004088 /* we get here if we need to wait for more data. If the buffer is full,
4089 * we have the maximum we can expect.
4090 */
4091 if (buffer_full(req->buf, global.tune.maxrewrite))
4092 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004093
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004094 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004095 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004096 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004097
Willy Tarreaue7dff022015-04-03 01:14:29 +02004098 if (!(s->flags & SF_ERR_MASK))
4099 s->flags |= SF_ERR_CLITO;
4100 if (!(s->flags & SF_FINST_MASK))
4101 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004102 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004103 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004104
4105 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004106 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004107 /* Not enough data. We'll re-use the http-request
4108 * timeout here. Ideally, we should set the timeout
4109 * relative to the accept() date. We just set the
4110 * request timeout once at the beginning of the
4111 * request.
4112 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004113 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004114 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004115 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004116 return 0;
4117 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004118
4119 http_end:
4120 /* The situation will not evolve, so let's give up on the analysis. */
4121 s->logs.tv_request = now; /* update the request timer to reflect full request */
4122 req->analysers &= ~an_bit;
4123 req->analyse_exp = TICK_ETERNITY;
4124 return 1;
4125
4126 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004127 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004128 txn->req.msg_state = HTTP_MSG_ERROR;
4129 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004130 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004131
Willy Tarreaue7dff022015-04-03 01:14:29 +02004132 if (!(s->flags & SF_ERR_MASK))
4133 s->flags |= SF_ERR_PRXCOND;
4134 if (!(s->flags & SF_FINST_MASK))
4135 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004136
Willy Tarreau522d6c02009-12-06 18:49:18 +01004137 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01004138 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004139 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004140 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004141 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004142 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004143}
4144
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004145/* send a server's name with an outgoing request over an established connection.
4146 * Note: this function is designed to be called once the request has been scheduled
4147 * for being forwarded. This is the reason why it rewinds the buffer before
4148 * proceeding.
4149 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004150int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004151
4152 struct hdr_ctx ctx;
4153
Mark Lamourinec2247f02012-01-04 13:02:01 -05004154 char *hdr_name = be->server_id_hdr_name;
4155 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004156 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004157 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004158 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004159
William Lallemandd9e90662012-01-30 17:27:17 +01004160 ctx.idx = 0;
4161
Willy Tarreau211cdec2014-04-17 20:18:08 +02004162 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004163 if (old_o) {
4164 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004165 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004166 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004167 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004168 }
4169
Willy Tarreau9b28e032012-10-12 23:49:43 +02004170 old_i = chn->buf->i;
4171 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 -05004172 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004173 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004174 }
4175
4176 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004177 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004178 memcpy(hdr_val, hdr_name, hdr_name_len);
4179 hdr_val += hdr_name_len;
4180 *hdr_val++ = ':';
4181 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004182 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4183 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004184
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004185 if (old_o) {
4186 /* If this was a forwarded request, we must readjust the amount of
4187 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004188 * variations. Note that the current state is >= HTTP_MSG_BODY,
4189 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004190 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004191 old_o += chn->buf->i - old_i;
4192 b_adv(chn->buf, old_o);
4193 txn->req.next -= old_o;
4194 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004195 }
4196
Mark Lamourinec2247f02012-01-04 13:02:01 -05004197 return 0;
4198}
4199
Willy Tarreau610ecce2010-01-04 21:15:02 +01004200/* Terminate current transaction and prepare a new one. This is very tricky
4201 * right now but it works.
4202 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004203void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004204{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004205 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02004206 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01004207 struct proxy *be = s->be;
Willy Tarreau323a2d92015-08-04 19:00:17 +02004208 struct connection *srv_conn;
4209 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02004210 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01004211
Willy Tarreau610ecce2010-01-04 21:15:02 +01004212 /* FIXME: We need a more portable way of releasing a backend's and a
4213 * server's connections. We need a safer way to reinitialize buffer
4214 * flags. We also need a more accurate method for computing per-request
4215 * data.
4216 */
Willy Tarreau323a2d92015-08-04 19:00:17 +02004217 srv_conn = objt_conn(s->si[1].end);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004218
Willy Tarreau4213a112013-12-15 10:25:42 +01004219 /* unless we're doing keep-alive, we want to quickly close the connection
4220 * to the server.
4221 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004222 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004223 !si_conn_ready(&s->si[1])) {
4224 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4225 si_shutr(&s->si[1]);
4226 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01004227 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004228
Willy Tarreaue7dff022015-04-03 01:14:29 +02004229 if (s->flags & SF_BE_ASSIGNED) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004230 HA_ATOMIC_SUB(&be->beconn, 1);
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004231 if (unlikely(s->srv_conn))
4232 sess_change_server(s, NULL);
4233 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004234
4235 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02004236 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004237
Willy Tarreaueee5b512015-04-03 23:46:31 +02004238 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004239 int n;
4240
Willy Tarreaueee5b512015-04-03 23:46:31 +02004241 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004242 if (n < 1 || n > 5)
4243 n = 0;
4244
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004245 if (fe->mode == PR_MODE_HTTP) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004246 HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004247 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02004248 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01004249 (be->mode == PR_MODE_HTTP)) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004250 HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1);
4251 HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004252 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004253 }
4254
4255 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004256 s->logs.bytes_in -= s->req.buf->i;
4257 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004258
4259 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004260 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02004261 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004262 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004263 s->do_log(s);
4264 }
4265
Willy Tarreaud713bcc2014-06-25 15:36:04 +02004266 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02004267 stream_stop_content_counters(s);
4268 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004269
Willy Tarreau610ecce2010-01-04 21:15:02 +01004270 s->logs.accept_date = date; /* user-visible date for logging */
4271 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02004272 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
4273 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004274 tv_zero(&s->logs.tv_request);
4275 s->logs.t_queue = -1;
4276 s->logs.t_connect = -1;
4277 s->logs.t_data = -1;
4278 s->logs.t_close = 0;
4279 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4280 s->logs.srv_queue_size = 0; /* we will get this number soon */
4281
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004282 s->logs.bytes_in = s->req.total = s->req.buf->i;
4283 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004284
4285 if (s->pend_pos)
4286 pendconn_free(s->pend_pos);
4287
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004288 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004289 if (s->flags & SF_CURR_SESS) {
4290 s->flags &= ~SF_CURR_SESS;
Christopher Faulet29f77e82017-06-08 14:04:45 +02004291 HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004292 }
Willy Tarreau858b1032015-12-07 17:04:59 +01004293 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004294 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004295 }
4296
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004297 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004298
Willy Tarreau4213a112013-12-15 10:25:42 +01004299 /* only release our endpoint if we don't intend to reuse the
4300 * connection.
4301 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004302 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004303 !si_conn_ready(&s->si[1])) {
4304 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02004305 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004306 }
4307
Willy Tarreau350f4872014-11-28 14:42:25 +01004308 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
4309 s->si[1].err_type = SI_ET_NONE;
4310 s->si[1].conn_retries = 0; /* used for logging too */
4311 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02004312 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 +01004313 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);
4314 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 +02004315 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
4316 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
4317 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01004318
Willy Tarreaueee5b512015-04-03 23:46:31 +02004319 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004320 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02004321 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004322
4323 if (prev_status == 401 || prev_status == 407) {
4324 /* In HTTP keep-alive mode, if we receive a 401, we still have
4325 * a chance of being able to send the visitor again to the same
4326 * server over the same connection. This is required by some
4327 * broken protocols such as NTLM, and anyway whenever there is
4328 * an opportunity for sending the challenge to the proper place,
4329 * it's better to do it (at least it helps with debugging).
4330 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004331 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02004332 if (srv_conn)
4333 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01004334 }
4335
Willy Tarreau53f96852016-02-02 18:50:47 +01004336 /* Never ever allow to reuse a connection from a non-reuse backend */
4337 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
4338 srv_conn->flags |= CO_FL_PRIVATE;
4339
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004340 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01004341 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004342
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004343 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004344 s->req.flags |= CF_NEVER_WAIT;
4345 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004346 }
4347
Willy Tarreau714ea782015-11-25 20:11:11 +01004348 /* we're removing the analysers, we MUST re-enable events detection.
4349 * We don't enable close on the response channel since it's either
4350 * already closed, or in keep-alive with an idle connection handler.
4351 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004352 channel_auto_read(&s->req);
4353 channel_auto_close(&s->req);
4354 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004355
Willy Tarreau1c59bd52015-11-02 20:20:11 +01004356 /* we're in keep-alive with an idle connection, monitor it if not already done */
4357 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02004358 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02004359 if (!srv)
4360 si_idle_conn(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01004361 else if (srv_conn->flags & CO_FL_PRIVATE)
Christopher Faulet40a007c2017-07-03 15:41:01 +02004362 si_idle_conn(&s->si[1], (srv->priv_conns ? &srv->priv_conns[tid] : NULL));
Willy Tarreau449d74a2015-08-05 17:16:33 +02004363 else if (prev_flags & TX_NOT_FIRST)
4364 /* note: we check the request, not the connection, but
4365 * this is valid for strategies SAFE and AGGR, and in
4366 * case of ALWS, we don't care anyway.
4367 */
Christopher Faulet40a007c2017-07-03 15:41:01 +02004368 si_idle_conn(&s->si[1], (srv->safe_conns ? &srv->safe_conns[tid] : NULL));
Willy Tarreau8dff9982015-08-04 20:45:52 +02004369 else
Christopher Faulet40a007c2017-07-03 15:41:01 +02004370 si_idle_conn(&s->si[1], (srv->idle_conns ? &srv->idle_conns[tid] : NULL));
Willy Tarreau4320eaa2015-08-05 11:08:30 +02004371 }
Christopher Faulete6006242017-03-10 11:52:44 +01004372 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
4373 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004374}
4375
4376
4377/* This function updates the request state machine according to the response
4378 * state machine and buffer flags. It returns 1 if it changes anything (flag
4379 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4380 * it is only used to find when a request/response couple is complete. Both
4381 * this function and its equivalent should loop until both return zero. It
4382 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4383 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004384int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004385{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004386 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004387 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004388 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004389 unsigned int old_state = txn->req.msg_state;
4390
Christopher Faulet4be98032017-07-18 10:48:24 +02004391 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004392 return 0;
4393
4394 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004395 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004396 * We can shut the read side unless we want to abort_on_close,
4397 * or we have a POST request. The issue with POST requests is
4398 * that some browsers still send a CRLF after the request, and
4399 * this CRLF must be read so that it does not remain in the kernel
4400 * buffers, otherwise a close could cause an RST on some systems
4401 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004402 * Note that if we're using keep-alive on the client side, we'd
4403 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02004404 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01004405 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004406 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004407 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4408 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4409 !(s->be->options & PR_O_ABRT_CLOSE) &&
4410 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004411 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004412
Willy Tarreau40f151a2012-12-20 12:10:09 +01004413 /* if the server closes the connection, we want to immediately react
4414 * and close the socket to save packets and syscalls.
4415 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004416 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01004417
Willy Tarreau7f876a12015-11-18 11:59:55 +01004418 /* In any case we've finished parsing the request so we must
4419 * disable Nagle when sending data because 1) we're not going
4420 * to shut this side, and 2) the server is waiting for us to
4421 * send pending data.
4422 */
4423 chn->flags |= CF_NEVER_WAIT;
4424
Willy Tarreau610ecce2010-01-04 21:15:02 +01004425 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4426 goto wait_other_side;
4427
4428 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4429 /* The server has not finished to respond, so we
4430 * don't want to move in order not to upset it.
4431 */
4432 goto wait_other_side;
4433 }
4434
Willy Tarreau610ecce2010-01-04 21:15:02 +01004435 /* When we get here, it means that both the request and the
4436 * response have finished receiving. Depending on the connection
4437 * mode, we'll have to wait for the last bytes to leave in either
4438 * direction, and sometimes for a close to be effective.
4439 */
4440
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004441 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4442 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004443 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4444 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004445 }
4446 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4447 /* Option forceclose is set, or either side wants to close,
4448 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004449 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004450 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004451 *
4452 * However, there is an exception if the response
4453 * length is undefined. In this case, we need to wait
4454 * the close from the server. The response will be
4455 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004456 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004457 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4458 txn->rsp.msg_state != HTTP_MSG_CLOSED)
4459 goto check_channel_flags;
4460
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004461 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4462 channel_shutr_now(chn);
4463 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004464 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004465 }
4466 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004467 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4468 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004469 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004470 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4471 channel_auto_read(chn);
4472 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004473 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004474 }
4475
Christopher Faulet4be98032017-07-18 10:48:24 +02004476 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004477 }
4478
4479 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4480 http_msg_closing:
4481 /* nothing else to forward, just waiting for the output buffer
4482 * to be empty and for the shutw_now to take effect.
4483 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004484 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004485 txn->req.msg_state = HTTP_MSG_CLOSED;
4486 goto http_msg_closed;
4487 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004488 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004489 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004490 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004491 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004492 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004493 }
4494
4495 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4496 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01004497 /* see above in MSG_DONE why we only do this in these states */
4498 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4499 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4500 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004501 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004502 goto wait_other_side;
4503 }
4504
Christopher Faulet4be98032017-07-18 10:48:24 +02004505 check_channel_flags:
4506 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4507 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4508 /* if we've just closed an output, let's switch */
4509 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4510 txn->req.msg_state = HTTP_MSG_CLOSING;
4511 goto http_msg_closing;
4512 }
4513
4514
Willy Tarreau610ecce2010-01-04 21:15:02 +01004515 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004516 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004517}
4518
4519
4520/* This function updates the response state machine according to the request
4521 * state machine and buffer flags. It returns 1 if it changes anything (flag
4522 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4523 * it is only used to find when a request/response couple is complete. Both
4524 * this function and its equivalent should loop until both return zero. It
4525 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4526 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004527int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004528{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004529 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004530 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004531 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004532 unsigned int old_state = txn->rsp.msg_state;
4533
Christopher Faulet4be98032017-07-18 10:48:24 +02004534 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004535 return 0;
4536
4537 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4538 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004539 * still monitor the server connection for a possible close
4540 * while the request is being uploaded, so we don't disable
4541 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004542 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004543 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004544
4545 if (txn->req.msg_state == HTTP_MSG_ERROR)
4546 goto wait_other_side;
4547
4548 if (txn->req.msg_state < HTTP_MSG_DONE) {
4549 /* The client seems to still be sending data, probably
4550 * because we got an error response during an upload.
4551 * We have the choice of either breaking the connection
4552 * or letting it pass through. Let's do the later.
4553 */
4554 goto wait_other_side;
4555 }
4556
Willy Tarreau610ecce2010-01-04 21:15:02 +01004557 /* When we get here, it means that both the request and the
4558 * response have finished receiving. Depending on the connection
4559 * mode, we'll have to wait for the last bytes to leave in either
4560 * direction, and sometimes for a close to be effective.
4561 */
4562
4563 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4564 /* Server-close mode : shut read and wait for the request
4565 * side to close its output buffer. The caller will detect
4566 * when we're in DONE and the other is in CLOSED and will
4567 * catch that for the final cleanup.
4568 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004569 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4570 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004571 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004572 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4573 /* Option forceclose is set, or either side wants to close,
4574 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004575 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004576 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004577 *
4578 * However, there is an exception if the response length
4579 * is undefined. In this case, we switch in TUNNEL mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004580 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004581 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN)) {
4582 channel_auto_read(chn);
4583 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4584 chn->flags |= CF_NEVER_WAIT;
4585 }
4586 else if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004587 channel_shutr_now(chn);
4588 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004589 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004590 }
4591 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004592 /* The last possible modes are keep-alive and tunnel. Tunnel will
4593 * need to forward remaining data. Keep-alive will need to monitor
4594 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004595 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004596 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004597 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004598 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4599 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004600 }
4601
Christopher Faulet4be98032017-07-18 10:48:24 +02004602 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004603 }
4604
4605 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4606 http_msg_closing:
4607 /* nothing else to forward, just waiting for the output buffer
4608 * to be empty and for the shutw_now to take effect.
4609 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004610 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004611 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4612 goto http_msg_closed;
4613 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004614 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02004615 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004616 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004617 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004618 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004619 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004620 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004621 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004622 }
4623
4624 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4625 http_msg_closed:
4626 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01004627 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004628 channel_auto_close(chn);
4629 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004630 goto wait_other_side;
4631 }
4632
Christopher Faulet4be98032017-07-18 10:48:24 +02004633 check_channel_flags:
4634 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4635 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4636 /* if we've just closed an output, let's switch */
4637 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4638 goto http_msg_closing;
4639 }
4640
Willy Tarreau610ecce2010-01-04 21:15:02 +01004641 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004642 /* We force the response to leave immediately if we're waiting for the
4643 * other side, since there is no pending shutdown to push it out.
4644 */
4645 if (!channel_is_empty(chn))
4646 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004647 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004648}
4649
4650
Christopher Faulet894da4c2017-07-18 11:29:07 +02004651/* Resync the request and response state machines. */
4652void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004653{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004654 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004655#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01004656 int old_req_state = txn->req.msg_state;
4657 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004658#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01004659
Willy Tarreau610ecce2010-01-04 21:15:02 +01004660 http_sync_req_state(s);
4661 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004662 if (!http_sync_res_state(s))
4663 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004664 if (!http_sync_req_state(s))
4665 break;
4666 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004667
Christopher Faulet894da4c2017-07-18 11:29:07 +02004668 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
4669 "req->analysers=0x%08x res->analysers=0x%08x\n",
4670 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02004671 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
4672 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02004673 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02004674
4675
Willy Tarreau610ecce2010-01-04 21:15:02 +01004676 /* OK, both state machines agree on a compatible state.
4677 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01004678 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4679 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02004680 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
4681 * means we must abort the request.
4682 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
4683 * corresponding channel.
4684 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
4685 * on the response with server-close mode means we've completed one
4686 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004687 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02004688 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4689 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004690 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004691 channel_auto_close(&s->req);
4692 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004693 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004694 channel_auto_close(&s->res);
4695 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004696 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004697 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4698 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004699 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004700 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004701 channel_auto_close(&s->res);
4702 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004703 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004704 channel_abort(&s->req);
4705 channel_auto_close(&s->req);
4706 channel_auto_read(&s->req);
4707 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004708 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004709 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4710 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4711 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4712 s->req.analysers &= AN_REQ_FLT_END;
4713 if (HAS_REQ_DATA_FILTERS(s))
4714 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
4715 }
4716 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4717 s->res.analysers &= AN_RES_FLT_END;
4718 if (HAS_RSP_DATA_FILTERS(s))
4719 s->res.analysers |= AN_RES_FLT_XFER_DATA;
4720 }
4721 channel_auto_close(&s->req);
4722 channel_auto_read(&s->req);
4723 channel_auto_close(&s->res);
4724 channel_auto_read(&s->res);
4725 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004726 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4727 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004728 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004729 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4730 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4731 /* server-close/keep-alive: terminate this transaction,
4732 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004733 * a fresh-new transaction, but only once we're sure there's
4734 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02004735 * another request. They must not hold any pending output data
4736 * and the response buffer must realigned
4737 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01004738 */
Christopher Faulet894da4c2017-07-18 11:29:07 +02004739 if (s->req.buf->o)
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004740 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004741 else if (s->res.buf->o)
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004742 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02004743 else {
4744 s->req.analysers = AN_REQ_FLT_END;
4745 s->res.analysers = AN_RES_FLT_END;
4746 txn->flags |= TX_WAIT_CLEANUP;
4747 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004748 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004749}
4750
Willy Tarreaud98cf932009-12-27 22:54:55 +01004751/* This function is an analyser which forwards request body (including chunk
4752 * sizes if any). It is called as soon as we must forward, even if we forward
4753 * zero byte. The only situation where it must not be called is when we're in
4754 * tunnel mode and we want to forward till the close. It's used both to forward
4755 * remaining data and to resync after end of body. It expects the msg_state to
4756 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02004757 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01004758 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004759 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004760 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004761int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004762{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004763 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004764 struct http_txn *txn = s->txn;
4765 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01004766 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004767
Christopher Faulet814d2702017-03-30 11:33:44 +02004768 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
4769 now_ms, __FUNCTION__,
4770 s,
4771 req,
4772 req->rex, req->wex,
4773 req->flags,
4774 req->buf->i,
4775 req->analysers);
4776
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004777 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4778 return 0;
4779
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004780 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004781 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004782 /* Output closed while we were sending data. We must abort and
4783 * wake the other side up.
4784 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004785 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02004786 msg->msg_state = HTTP_MSG_ERROR;
4787 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004788 return 1;
4789 }
4790
Willy Tarreaud98cf932009-12-27 22:54:55 +01004791 /* Note that we don't have to send 100-continue back because we don't
4792 * need the data to complete our job, and it's up to the server to
4793 * decide whether to return 100, 417 or anything else in return of
4794 * an "Expect: 100-continue" header.
4795 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004796 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004797 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4798 ? HTTP_MSG_CHUNK_SIZE
4799 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004800
4801 /* TODO/filters: when http-buffer-request option is set or if a
4802 * rule on url_param exists, the first chunk size could be
4803 * already parsed. In that case, msg->next is after the chunk
4804 * size (including the CRLF after the size). So this case should
4805 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004806 }
4807
Willy Tarreau7ba23542014-04-17 21:50:00 +02004808 /* Some post-connect processing might want us to refrain from starting to
4809 * forward data. Currently, the only reason for this is "balance url_param"
4810 * whichs need to parse/process the request after we've enabled forwarding.
4811 */
4812 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004813 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004814 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004815 req->flags |= CF_WAKE_CONNECT;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004816 goto missing_data_or_waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004817 }
4818 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4819 }
4820
Willy Tarreau80a92c02014-03-12 10:41:13 +01004821 /* in most states, we should abort in case of early close */
4822 channel_auto_close(req);
4823
Willy Tarreauefdf0942014-04-24 20:08:57 +02004824 if (req->to_forward) {
4825 /* We can't process the buffer's contents yet */
4826 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004827 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004828 }
4829
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004830 if (msg->msg_state < HTTP_MSG_DONE) {
4831 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4832 ? http_msg_forward_chunked_body(s, msg)
4833 : http_msg_forward_body(s, msg));
4834 if (!ret)
4835 goto missing_data_or_waiting;
4836 if (ret < 0)
4837 goto return_bad_req;
4838 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004839
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004840 /* other states, DONE...TUNNEL */
4841 /* we don't want to forward closes on DONE except in tunnel mode. */
4842 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4843 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004844
Christopher Faulet894da4c2017-07-18 11:29:07 +02004845 http_resync_states(s);
4846 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004847 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4848 if (req->flags & CF_SHUTW) {
4849 /* request errors are most likely due to the
4850 * server aborting the transfer. */
4851 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004852 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004853 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02004854 http_capture_bad_message(sess->fe, &sess->fe->invalid_req, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004855 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004856 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004857 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004858 }
4859
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004860 /* If "option abortonclose" is set on the backend, we want to monitor
4861 * the client's connection and forward any shutdown notification to the
4862 * server, which will decide whether to close or to go on processing the
4863 * request. We only do that in tunnel mode, and not in other modes since
4864 * it can be abused to exhaust source ports. */
4865 if (s->be->options & PR_O_ABRT_CLOSE) {
4866 channel_auto_read(req);
4867 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4868 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4869 s->si[1].flags |= SI_FL_NOLINGER;
4870 channel_auto_close(req);
4871 }
4872 else if (s->txn->meth == HTTP_METH_POST) {
4873 /* POST requests may require to read extra CRLF sent by broken
4874 * browsers and which could cause an RST to be sent upon close
4875 * on some systems (eg: Linux). */
4876 channel_auto_read(req);
4877 }
4878 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004879
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004880 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004881 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02004882 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004883 if (!(s->flags & SF_ERR_MASK))
4884 s->flags |= SF_ERR_CLICL;
4885 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004886 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004887 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004888 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004889 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004890 }
4891
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004892 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
4893 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004894 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004895 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004896
4897 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004898 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004899
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004900 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004901 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004902 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004903
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004904 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004905 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004906 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004907 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004908 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004909
Willy Tarreau5c620922011-05-11 19:56:11 +02004910 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004911 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004912 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004913 * modes are already handled by the stream sock layer. We must not do
4914 * this in content-length mode because it could present the MSG_MORE
4915 * flag with the last block of forwarded data, which would cause an
4916 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004917 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004918 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004919 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004920
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004921 return 0;
4922
Willy Tarreaud98cf932009-12-27 22:54:55 +01004923 return_bad_req: /* let's centralize all bad requests */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004924 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004925 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02004926 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Willy Tarreaubed410e2014-04-22 08:19:34 +02004927
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004928 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004929 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004930 txn->req.msg_state = HTTP_MSG_ERROR;
4931 if (txn->status) {
4932 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004933 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004934 } else {
4935 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004936 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004937 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004938 req->analysers &= AN_REQ_FLT_END;
4939 s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004940
Willy Tarreaue7dff022015-04-03 01:14:29 +02004941 if (!(s->flags & SF_ERR_MASK))
4942 s->flags |= SF_ERR_PRXCOND;
4943 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004944 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004945 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004946 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004947 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004948 }
4949 return 0;
4950
4951 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004952 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004953 txn->req.msg_state = HTTP_MSG_ERROR;
4954 if (txn->status) {
4955 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004956 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004957 } else {
4958 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004959 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004960 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004961 req->analysers &= AN_REQ_FLT_END;
4962 s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004963
Christopher Fauletff8abcd2017-06-02 15:33:24 +02004964 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
4965 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004966 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02004967 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004968
Willy Tarreaue7dff022015-04-03 01:14:29 +02004969 if (!(s->flags & SF_ERR_MASK))
4970 s->flags |= SF_ERR_SRVCL;
4971 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004972 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004973 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004974 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004975 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004976 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004977 return 0;
4978}
4979
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004980/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4981 * processing can continue on next analysers, or zero if it either needs more
4982 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004983 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004984 * when it has nothing left to do, and may remove any analyser when it wants to
4985 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004986 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004987int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004988{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004989 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004990 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004991 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004992 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004993 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004994 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004995 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004996
Willy Tarreau87b09662015-04-03 00:22:06 +02004997 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 +02004998 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004999 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005000 rep,
5001 rep->rex, rep->wex,
5002 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005003 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005004 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005005
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005006 /*
5007 * Now parse the partial (or complete) lines.
5008 * We will check the response syntax, and also join multi-line
5009 * headers. An index of all the lines will be elaborated while
5010 * parsing.
5011 *
5012 * For the parsing, we use a 28 states FSM.
5013 *
5014 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005015 * rep->buf->p = beginning of response
5016 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5017 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005018 * msg->eol = end of current header or line (LF or CRLF)
5019 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005020 */
5021
Willy Tarreau628c40c2014-04-24 19:11:26 +02005022 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005023 /* There's a protected area at the end of the buffer for rewriting
5024 * purposes. We don't want to start to parse the request if the
5025 * protected area is affected, because we may have to move processed
5026 * data later, which is much more complicated.
5027 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005028 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005029 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005030 /* some data has still not left the buffer, wake us once that's done */
5031 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5032 goto abort_response;
5033 channel_dont_close(rep);
5034 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005035 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005036 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005037 }
5038
Willy Tarreau379357a2013-06-08 12:55:46 +02005039 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5040 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5041 buffer_slow_realign(rep->buf);
5042
Willy Tarreau9b28e032012-10-12 23:49:43 +02005043 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005044 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005045 }
5046
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005047 /* 1: we might have to print this header in debug mode */
5048 if (unlikely((global.mode & MODE_DEBUG) &&
5049 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005050 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005051 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005052
Willy Tarreau9b28e032012-10-12 23:49:43 +02005053 sol = rep->buf->p;
5054 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005055 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005056
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005057 sol += hdr_idx_first_pos(&txn->hdr_idx);
5058 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005059
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005060 while (cur_idx) {
5061 eol = sol + txn->hdr_idx.v[cur_idx].len;
5062 debug_hdr("srvhdr", s, sol, eol);
5063 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5064 cur_idx = txn->hdr_idx.v[cur_idx].next;
5065 }
5066 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005067
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005068 /*
5069 * Now we quickly check if we have found a full valid response.
5070 * If not so, we check the FD and buffer states before leaving.
5071 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005072 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005073 * responses are checked first.
5074 *
5075 * Depending on whether the client is still there or not, we
5076 * may send an error response back or not. Note that normally
5077 * we should only check for HTTP status there, and check I/O
5078 * errors somewhere else.
5079 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005080
Willy Tarreau655dce92009-11-08 13:10:58 +01005081 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005082 /* Invalid response */
5083 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5084 /* we detected a parsing error. We want to archive this response
5085 * in the dedicated proxy area for later troubleshooting.
5086 */
5087 hdr_response_bad:
5088 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005089 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005090
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005091 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005092 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005093 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005094 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005095 }
Willy Tarreau64648412010-03-05 10:41:54 +01005096 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005097 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005098 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005099 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005100 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005101 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005102 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005103
Willy Tarreaue7dff022015-04-03 01:14:29 +02005104 if (!(s->flags & SF_ERR_MASK))
5105 s->flags |= SF_ERR_PRXCOND;
5106 if (!(s->flags & SF_FINST_MASK))
5107 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005108
5109 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005110 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005111
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005112 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005113 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005114 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005115 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005116 goto hdr_response_bad;
5117 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005118
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005119 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005120 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005121 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005122 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005123 else if (txn->flags & TX_NOT_FIRST)
5124 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005125
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005126 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005127 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005128 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005129 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005130 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005131
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005132 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005133 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005134 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005135 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005136 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005137 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02005138
Willy Tarreaue7dff022015-04-03 01:14:29 +02005139 if (!(s->flags & SF_ERR_MASK))
5140 s->flags |= SF_ERR_SRVCL;
5141 if (!(s->flags & SF_FINST_MASK))
5142 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005143 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005144 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005145
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005146 /* read timeout : return a 504 to the client. */
5147 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005148 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005149 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005150
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005151 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005152 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005153 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005154 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005155 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005156
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005157 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005158 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005159 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01005160 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005161 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005162 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02005163
Willy Tarreaue7dff022015-04-03 01:14:29 +02005164 if (!(s->flags & SF_ERR_MASK))
5165 s->flags |= SF_ERR_SRVTO;
5166 if (!(s->flags & SF_FINST_MASK))
5167 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005168 return 0;
5169 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005170
Willy Tarreauf003d372012-11-26 13:35:37 +01005171 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005172 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005173 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
5174 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01005175 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005176 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreauf003d372012-11-26 13:35:37 +01005177
Christopher Faulet0184ea72017-01-05 14:06:34 +01005178 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01005179 channel_auto_close(rep);
5180
5181 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01005182 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005183 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01005184
Willy Tarreaue7dff022015-04-03 01:14:29 +02005185 if (!(s->flags & SF_ERR_MASK))
5186 s->flags |= SF_ERR_CLICL;
5187 if (!(s->flags & SF_FINST_MASK))
5188 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01005189
Willy Tarreau87b09662015-04-03 00:22:06 +02005190 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01005191 return 0;
5192 }
5193
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005194 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005195 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005196 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005197 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005198 else if (txn->flags & TX_NOT_FIRST)
5199 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005200
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005201 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005202 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005203 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005204 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005205 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005206
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005207 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005208 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005209 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005210 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005211 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005212 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005213
Willy Tarreaue7dff022015-04-03 01:14:29 +02005214 if (!(s->flags & SF_ERR_MASK))
5215 s->flags |= SF_ERR_SRVCL;
5216 if (!(s->flags & SF_FINST_MASK))
5217 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005218 return 0;
5219 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005220
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005221 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005222 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005223 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005224 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005225 else if (txn->flags & TX_NOT_FIRST)
5226 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005227
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005228 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005229 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005230 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005231
Willy Tarreaue7dff022015-04-03 01:14:29 +02005232 if (!(s->flags & SF_ERR_MASK))
5233 s->flags |= SF_ERR_CLICL;
5234 if (!(s->flags & SF_FINST_MASK))
5235 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005236
Willy Tarreau87b09662015-04-03 00:22:06 +02005237 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005238 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005239 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005240
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005241 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005242 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005243 return 0;
5244 }
5245
5246 /* More interesting part now : we know that we have a complete
5247 * response which at least looks like HTTP. We have an indicator
5248 * of each header's length, so we can parse them quickly.
5249 */
5250
5251 if (unlikely(msg->err_pos >= 0))
Emeric Brun8c1aaa22017-06-15 11:30:06 +02005252 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005253
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005254 /*
5255 * 1: get the status code
5256 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005257 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005258 if (n < 1 || n > 5)
5259 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005260 /* when the client triggers a 4xx from the server, it's most often due
5261 * to a missing object or permission. These events should be tracked
5262 * because if they happen often, it may indicate a brute force or a
5263 * vulnerability scan.
5264 */
5265 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02005266 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02005267
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005268 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005269 HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005270
Willy Tarreau91852eb2015-05-01 13:26:00 +02005271 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
5272 * exactly one digit "." one digit. This check may be disabled using
5273 * option accept-invalid-http-response.
5274 */
5275 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
5276 if (msg->sl.st.v_l != 8) {
5277 msg->err_pos = 0;
5278 goto hdr_response_bad;
5279 }
5280
5281 if (rep->buf->p[4] != '/' ||
5282 !isdigit((unsigned char)rep->buf->p[5]) ||
5283 rep->buf->p[6] != '.' ||
5284 !isdigit((unsigned char)rep->buf->p[7])) {
5285 msg->err_pos = 4;
5286 goto hdr_response_bad;
5287 }
5288 }
5289
Willy Tarreau5b154472009-12-21 20:11:07 +01005290 /* check if the response is HTTP/1.1 or above */
5291 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005292 ((rep->buf->p[5] > '1') ||
5293 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005294 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005295
5296 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005297 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 +01005298
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005299 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005300 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005301
Willy Tarreau9b28e032012-10-12 23:49:43 +02005302 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005303
Willy Tarreau39650402010-03-15 19:44:39 +01005304 /* Adjust server's health based on status code. Note: status codes 501
5305 * and 505 are triggered on demand by client request, so we must not
5306 * count them as server failures.
5307 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005308 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005309 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005310 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005311 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005312 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005313 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005314
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005315 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02005316 * We may be facing a 100-continue response, or any other informational
5317 * 1xx response which is non-final, in which case this is not the right
5318 * response, and we're waiting for the next one. Let's allow this response
5319 * to go to the client and wait for the next one. There's an exception for
5320 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005321 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02005322 if (txn->status < 200 &&
5323 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02005324 hdr_idx_init(&txn->hdr_idx);
5325 msg->next -= channel_forward(rep, msg->next);
5326 msg->msg_state = HTTP_MSG_RPBEFORE;
5327 txn->status = 0;
5328 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01005329 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02005330 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02005331 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02005332
Willy Tarreaua14ad722017-07-07 11:36:32 +02005333 /*
5334 * 2: check for cacheability.
5335 */
5336
5337 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005338 case 200:
5339 case 203:
5340 case 206:
5341 case 300:
5342 case 301:
5343 case 410:
5344 /* RFC2616 @13.4:
5345 * "A response received with a status code of
5346 * 200, 203, 206, 300, 301 or 410 MAY be stored
5347 * by a cache (...) unless a cache-control
5348 * directive prohibits caching."
5349 *
5350 * RFC2616 @9.5: POST method :
5351 * "Responses to this method are not cacheable,
5352 * unless the response includes appropriate
5353 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005354 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005355 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005356 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005357 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5358 break;
5359 default:
5360 break;
5361 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005362
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005363 /*
5364 * 3: we may need to capture headers
5365 */
5366 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005367 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005368 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005369 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005370
Willy Tarreau557f1992015-05-01 10:05:17 +02005371 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
5372 * by RFC7230#3.3.3 :
5373 *
5374 * The length of a message body is determined by one of the following
5375 * (in order of precedence):
5376 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005377 * 1. Any 2xx (Successful) response to a CONNECT request implies that
5378 * the connection will become a tunnel immediately after the empty
5379 * line that concludes the header fields. A client MUST ignore
5380 * any Content-Length or Transfer-Encoding header fields received
5381 * in such a message. Any 101 response (Switching Protocols) is
5382 * managed in the same manner.
5383 *
5384 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02005385 * (Informational), 204 (No Content), or 304 (Not Modified) status
5386 * code is always terminated by the first empty line after the
5387 * header fields, regardless of the header fields present in the
5388 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005389 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005390 * 3. If a Transfer-Encoding header field is present and the chunked
5391 * transfer coding (Section 4.1) is the final encoding, the message
5392 * body length is determined by reading and decoding the chunked
5393 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005394 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005395 * If a Transfer-Encoding header field is present in a response and
5396 * the chunked transfer coding is not the final encoding, the
5397 * message body length is determined by reading the connection until
5398 * it is closed by the server. If a Transfer-Encoding header field
5399 * is present in a request and the chunked transfer coding is not
5400 * the final encoding, the message body length cannot be determined
5401 * reliably; the server MUST respond with the 400 (Bad Request)
5402 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005403 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005404 * If a message is received with both a Transfer-Encoding and a
5405 * Content-Length header field, the Transfer-Encoding overrides the
5406 * Content-Length. Such a message might indicate an attempt to
5407 * perform request smuggling (Section 9.5) or response splitting
5408 * (Section 9.4) and ought to be handled as an error. A sender MUST
5409 * remove the received Content-Length field prior to forwarding such
5410 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005411 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005412 * 4. If a message is received without Transfer-Encoding and with
5413 * either multiple Content-Length header fields having differing
5414 * field-values or a single Content-Length header field having an
5415 * invalid value, then the message framing is invalid and the
5416 * recipient MUST treat it as an unrecoverable error. If this is a
5417 * request message, the server MUST respond with a 400 (Bad Request)
5418 * status code and then close the connection. If this is a response
5419 * message received by a proxy, the proxy MUST close the connection
5420 * to the server, discard the received response, and send a 502 (Bad
5421 * Gateway) response to the client. If this is a response message
5422 * received by a user agent, the user agent MUST close the
5423 * connection to the server and discard the received response.
5424 *
5425 * 5. If a valid Content-Length header field is present without
5426 * Transfer-Encoding, its decimal value defines the expected message
5427 * body length in octets. If the sender closes the connection or
5428 * the recipient times out before the indicated number of octets are
5429 * received, the recipient MUST consider the message to be
5430 * incomplete and close the connection.
5431 *
5432 * 6. If this is a request message and none of the above are true, then
5433 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005434 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005435 * 7. Otherwise, this is a response message without a declared message
5436 * body length, so the message body length is determined by the
5437 * number of octets received prior to the server closing the
5438 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005439 */
5440
5441 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005442 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005443 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005444 * FIXME: should we parse anyway and return an error on chunked encoding ?
5445 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005446 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5447 txn->status == 101)) {
5448 /* Either we've established an explicit tunnel, or we're
5449 * switching the protocol. In both cases, we're very unlikely
5450 * to understand the next protocols. We have to switch to tunnel
5451 * mode, so that we transfer the request and responses then let
5452 * this protocol pass unmodified. When we later implement specific
5453 * parsers for such protocols, we'll want to check the Upgrade
5454 * header which contains information about that protocol for
5455 * responses with status 101 (eg: see RFC2817 about TLS).
5456 */
5457 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5458 msg->flags |= HTTP_MSGF_XFER_LEN;
5459 goto end;
5460 }
5461
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005462 if (txn->meth == HTTP_METH_HEAD ||
5463 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005464 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005465 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005466 goto skip_content_length;
5467 }
5468
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005469 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005470 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02005471 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005472 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005473 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5474 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005475 /* bad transfer-encoding (chunked followed by something else) */
5476 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005477 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005478 break;
5479 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005480 }
5481
Willy Tarreau1c913912015-04-30 10:57:51 +02005482 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005483 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005484 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02005485 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
5486 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5487 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005488 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005489 signed long long cl;
5490
Willy Tarreauad14f752011-09-02 20:33:27 +02005491 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005492 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005493 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005494 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005495
Willy Tarreauad14f752011-09-02 20:33:27 +02005496 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005497 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005498 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005499 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005500
Willy Tarreauad14f752011-09-02 20:33:27 +02005501 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005502 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005503 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005504 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005505
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005506 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005507 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005508 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005509 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005510
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005511 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005512 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005513 }
5514
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005515 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01005516 /* Now we have to check if we need to modify the Connection header.
5517 * This is more difficult on the response than it is on the request,
5518 * because we can have two different HTTP versions and we don't know
5519 * how the client will interprete a response. For instance, let's say
5520 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5521 * HTTP/1.1 response without any header. Maybe it will bound itself to
5522 * HTTP/1.0 because it only knows about it, and will consider the lack
5523 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5524 * the lack of header as a keep-alive. Thus we will use two flags
5525 * indicating how a request MAY be understood by the client. In case
5526 * of multiple possibilities, we'll fix the header to be explicit. If
5527 * ambiguous cases such as both close and keepalive are seen, then we
5528 * will fall back to explicit close. Note that we won't take risks with
5529 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005530 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005531 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005532 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5533 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5534 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5535 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005536 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005537
Willy Tarreau70dffda2014-01-30 03:07:23 +01005538 /* this situation happens when combining pretend-keepalive with httpclose. */
5539 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005540 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005541 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01005542 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
5543
Willy Tarreau60466522010-01-18 19:08:45 +01005544 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005545 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005546 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5547 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005548
Willy Tarreau60466522010-01-18 19:08:45 +01005549 /* now adjust header transformations depending on current state */
5550 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5551 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5552 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005553 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005554 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005555 }
Willy Tarreau60466522010-01-18 19:08:45 +01005556 else { /* SCL / KAL */
5557 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005558 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005559 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005560 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005561
Willy Tarreau60466522010-01-18 19:08:45 +01005562 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005563 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005564
Willy Tarreau60466522010-01-18 19:08:45 +01005565 /* Some keep-alive responses are converted to Server-close if
5566 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005567 */
Willy Tarreau60466522010-01-18 19:08:45 +01005568 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5569 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005570 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005571 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005572 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005573 }
5574
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005575 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02005576 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005577 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005578
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005579 /* end of job, return OK */
5580 rep->analysers &= ~an_bit;
5581 rep->analyse_exp = TICK_ETERNITY;
5582 channel_auto_close(rep);
5583 return 1;
5584
5585 abort_keep_alive:
5586 /* A keep-alive request to the server failed on a network error.
5587 * The client is required to retry. We need to close without returning
5588 * any other information so that the client retries.
5589 */
5590 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005591 rep->analysers &= AN_RES_FLT_END;
5592 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005593 channel_auto_close(rep);
5594 s->logs.logwait = 0;
5595 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005596 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01005597 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005598 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005599 return 0;
5600}
5601
5602/* This function performs all the processing enabled for the current response.
5603 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005604 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005605 * other functions. It works like process_request (see indications above).
5606 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005607int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005608{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005609 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005610 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005611 struct http_msg *msg = &txn->rsp;
5612 struct proxy *cur_proxy;
5613 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01005614 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005615
Willy Tarreau87b09662015-04-03 00:22:06 +02005616 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 +02005617 now_ms, __FUNCTION__,
5618 s,
5619 rep,
5620 rep->rex, rep->wex,
5621 rep->flags,
5622 rep->buf->i,
5623 rep->analysers);
5624
5625 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
5626 return 0;
5627
Willy Tarreau70730dd2014-04-24 18:06:27 +02005628 /* The stats applet needs to adjust the Connection header but we don't
5629 * apply any filter there.
5630 */
Willy Tarreau612adb82015-03-10 15:25:54 +01005631 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
5632 rep->analysers &= ~an_bit;
5633 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02005634 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01005635 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02005636
Willy Tarreau58975672014-04-24 21:13:57 +02005637 /*
5638 * We will have to evaluate the filters.
5639 * As opposed to version 1.2, now they will be evaluated in the
5640 * filters order and not in the header order. This means that
5641 * each filter has to be validated among all headers.
5642 *
5643 * Filters are tried with ->be first, then with ->fe if it is
5644 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005645 *
5646 * Maybe we are in resume condiion. In this case I choose the
5647 * "struct proxy" which contains the rule list matching the resume
5648 * pointer. If none of theses "struct proxy" match, I initialise
5649 * the process with the first one.
5650 *
5651 * In fact, I check only correspondance betwwen the current list
5652 * pointer and the ->fe rule list. If it doesn't match, I initialize
5653 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02005654 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005655 if (s->current_rule_list == &sess->fe->http_res_rules)
5656 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005657 else
5658 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02005659 while (1) {
5660 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005661
Willy Tarreau58975672014-04-24 21:13:57 +02005662 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02005663 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005664 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005665
Willy Tarreau51d861a2015-05-22 17:30:48 +02005666 if (ret == HTTP_RULE_RES_BADREQ)
5667 goto return_srv_prx_502;
5668
5669 if (ret == HTTP_RULE_RES_DONE) {
5670 rep->analysers &= ~an_bit;
5671 rep->analyse_exp = TICK_ETERNITY;
5672 return 1;
5673 }
5674 }
5675
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005676 /* we need to be called again. */
5677 if (ret == HTTP_RULE_RES_YIELD) {
5678 channel_dont_close(rep);
5679 return 0;
5680 }
5681
Willy Tarreau58975672014-04-24 21:13:57 +02005682 /* try headers filters */
5683 if (rule_set->rsp_exp != NULL) {
5684 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5685 return_bad_resp:
5686 if (objt_server(s->target)) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02005687 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005688 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005689 }
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005690 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau58975672014-04-24 21:13:57 +02005691 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005692 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02005693 txn->status = 502;
5694 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01005695 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005696 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005697 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02005698 if (!(s->flags & SF_ERR_MASK))
5699 s->flags |= SF_ERR_PRXCOND;
5700 if (!(s->flags & SF_FINST_MASK))
5701 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02005702 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005703 }
Willy Tarreau58975672014-04-24 21:13:57 +02005704 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005705
Willy Tarreau58975672014-04-24 21:13:57 +02005706 /* has the response been denied ? */
5707 if (txn->flags & TX_SVDENY) {
5708 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005709 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005710
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005711 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5712 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005713 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005714 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005715
Willy Tarreau58975672014-04-24 21:13:57 +02005716 goto return_srv_prx_502;
5717 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005718
Willy Tarreau58975672014-04-24 21:13:57 +02005719 /* add response headers from the rule sets in the same order */
5720 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02005721 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005722 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005723 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005724 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02005725 ret = acl_pass(ret);
5726 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5727 ret = !ret;
5728 if (!ret)
5729 continue;
5730 }
5731 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
5732 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005733 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005734
Willy Tarreau58975672014-04-24 21:13:57 +02005735 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005736 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02005737 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005738 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02005739 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005740
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005741 /* After this point, this anayzer can't return yield, so we can
5742 * remove the bit corresponding to this analyzer from the list.
5743 *
5744 * Note that the intermediate returns and goto found previously
5745 * reset the analyzers.
5746 */
5747 rep->analysers &= ~an_bit;
5748 rep->analyse_exp = TICK_ETERNITY;
5749
Willy Tarreau58975672014-04-24 21:13:57 +02005750 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005751 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005752 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005753
Willy Tarreau58975672014-04-24 21:13:57 +02005754 /*
5755 * Now check for a server cookie.
5756 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005757 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005758 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005759
Willy Tarreau58975672014-04-24 21:13:57 +02005760 /*
5761 * Check for cache-control or pragma headers if required.
5762 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005763 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 +02005764 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005765
Willy Tarreau58975672014-04-24 21:13:57 +02005766 /*
5767 * Add server cookie in the response if needed
5768 */
5769 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5770 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005771 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005772 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5773 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5774 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5775 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5776 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005777 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005778 /* the server is known, it's not the one the client requested, or the
5779 * cookie's last seen date needs to be refreshed. We have to
5780 * insert a set-cookie here, except if we want to insert only on POST
5781 * requests and this one isn't. Note that servers which don't have cookies
5782 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005783 */
Willy Tarreau58975672014-04-24 21:13:57 +02005784 if (!objt_server(s->target)->cookie) {
5785 chunk_printf(&trash,
5786 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5787 s->be->cookie_name);
5788 }
5789 else {
5790 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005791
Willy Tarreau58975672014-04-24 21:13:57 +02005792 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5793 /* emit last_date, which is mandatory */
5794 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5795 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5796 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005797
Willy Tarreau58975672014-04-24 21:13:57 +02005798 if (s->be->cookie_maxlife) {
5799 /* emit first_date, which is either the original one or
5800 * the current date.
5801 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005802 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005803 s30tob64(txn->cookie_first_date ?
5804 txn->cookie_first_date >> 2 :
5805 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005806 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005807 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005808 }
Willy Tarreau58975672014-04-24 21:13:57 +02005809 chunk_appendf(&trash, "; path=/");
5810 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005811
Willy Tarreau58975672014-04-24 21:13:57 +02005812 if (s->be->cookie_domain)
5813 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005814
Willy Tarreau58975672014-04-24 21:13:57 +02005815 if (s->be->ck_opts & PR_CK_HTTPONLY)
5816 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005817
Willy Tarreau58975672014-04-24 21:13:57 +02005818 if (s->be->ck_opts & PR_CK_SECURE)
5819 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005820
Willy Tarreau58975672014-04-24 21:13:57 +02005821 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
5822 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005823
Willy Tarreau58975672014-04-24 21:13:57 +02005824 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005825 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005826 /* the server did not change, only the date was updated */
5827 txn->flags |= TX_SCK_UPDATED;
5828 else
5829 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005830
Willy Tarreau58975672014-04-24 21:13:57 +02005831 /* Here, we will tell an eventual cache on the client side that we don't
5832 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5833 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5834 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005835 */
Willy Tarreau58975672014-04-24 21:13:57 +02005836 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005837
Willy Tarreau58975672014-04-24 21:13:57 +02005838 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005839
Willy Tarreau58975672014-04-24 21:13:57 +02005840 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5841 "Cache-control: private", 22) < 0))
5842 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005843 }
Willy Tarreau58975672014-04-24 21:13:57 +02005844 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005845
Willy Tarreau58975672014-04-24 21:13:57 +02005846 /*
5847 * Check if result will be cacheable with a cookie.
5848 * We'll block the response if security checks have caught
5849 * nasty things such as a cacheable cookie.
5850 */
5851 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5852 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5853 (s->be->options & PR_O_CHK_CACHE)) {
5854 /* we're in presence of a cacheable response containing
5855 * a set-cookie header. We'll block it as requested by
5856 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005857 */
Willy Tarreau58975672014-04-24 21:13:57 +02005858 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02005859 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Willy Tarreau60466522010-01-18 19:08:45 +01005860
Christopher Fauletff8abcd2017-06-02 15:33:24 +02005861 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
5862 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005863 if (sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02005864 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005865
Willy Tarreau58975672014-04-24 21:13:57 +02005866 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5867 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5868 send_log(s->be, LOG_ALERT,
5869 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5870 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5871 goto return_srv_prx_502;
5872 }
Willy Tarreau03945942009-12-22 16:50:27 +01005873
Willy Tarreau70730dd2014-04-24 18:06:27 +02005874 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005875 /*
5876 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5877 * If an "Upgrade" token is found, the header is left untouched in order
5878 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005879 * if anything but "Upgrade" is present in the Connection header. We don't
5880 * want to touch any 101 response either since it's switching to another
5881 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005882 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005883 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02005884 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005885 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02005886 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
5887 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005888
Willy Tarreau58975672014-04-24 21:13:57 +02005889 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5890 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5891 /* we want a keep-alive response here. Keep-alive header
5892 * required if either side is not 1.1.
5893 */
5894 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5895 want_flags |= TX_CON_KAL_SET;
5896 }
5897 else {
5898 /* we want a close response here. Close header required if
5899 * the server is 1.1, regardless of the client.
5900 */
5901 if (msg->flags & HTTP_MSGF_VER_11)
5902 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005903 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005904
Willy Tarreau58975672014-04-24 21:13:57 +02005905 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5906 http_change_connection_header(txn, msg, want_flags);
5907 }
5908
5909 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02005910 /* Always enter in the body analyzer */
5911 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
5912 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005913
Willy Tarreau58975672014-04-24 21:13:57 +02005914 /* if the user wants to log as soon as possible, without counting
5915 * bytes from the server, then this is the right moment. We have
5916 * to temporarily assign bytes_out to log what we currently have.
5917 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005918 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005919 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
5920 s->logs.bytes_out = txn->rsp.eoh;
5921 s->do_log(s);
5922 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005923 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005924 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005925}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005926
Willy Tarreaud98cf932009-12-27 22:54:55 +01005927/* This function is an analyser which forwards response body (including chunk
5928 * sizes if any). It is called as soon as we must forward, even if we forward
5929 * zero byte. The only situation where it must not be called is when we're in
5930 * tunnel mode and we want to forward till the close. It's used both to forward
5931 * remaining data and to resync after end of body. It expects the msg_state to
5932 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005933 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02005934 *
5935 * It is capable of compressing response data both in content-length mode and
5936 * in chunked mode. The state machines follows different flows depending on
5937 * whether content-length and chunked modes are used, since there are no
5938 * trailers in content-length :
5939 *
5940 * chk-mode cl-mode
5941 * ,----- BODY -----.
5942 * / \
5943 * V size > 0 V chk-mode
5944 * .--> SIZE -------------> DATA -------------> CRLF
5945 * | | size == 0 | last byte |
5946 * | v final crlf v inspected |
5947 * | TRAILERS -----------> DONE |
5948 * | |
5949 * `----------------------------------------------'
5950 *
5951 * Compression only happens in the DATA state, and must be flushed in final
5952 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
5953 * is performed at once on final states for all bytes parsed, or when leaving
5954 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005955 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005956int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005957{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005958 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005959 struct http_txn *txn = s->txn;
5960 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01005961 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005962
Christopher Faulet814d2702017-03-30 11:33:44 +02005963 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
5964 now_ms, __FUNCTION__,
5965 s,
5966 res,
5967 res->rex, res->wex,
5968 res->flags,
5969 res->buf->i,
5970 res->analysers);
5971
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005972 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5973 return 0;
5974
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005975 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005976 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02005977 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005978 /* Output closed while we were sending data. We must abort and
5979 * wake the other side up.
5980 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005981 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005982 msg->msg_state = HTTP_MSG_ERROR;
5983 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005984 return 1;
5985 }
5986
Willy Tarreau4fe41902010-06-07 22:27:41 +02005987 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005988 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005989
Christopher Fauletd7c91962015-04-30 11:48:27 +02005990 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005991 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5992 ? HTTP_MSG_CHUNK_SIZE
5993 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005994 }
5995
Willy Tarreauefdf0942014-04-24 20:08:57 +02005996 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005997 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02005998 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005999 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02006000 }
6001
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006002 if (msg->msg_state < HTTP_MSG_DONE) {
6003 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
6004 ? http_msg_forward_chunked_body(s, msg)
6005 : http_msg_forward_body(s, msg));
6006 if (!ret)
6007 goto missing_data_or_waiting;
6008 if (ret < 0)
6009 goto return_bad_res;
6010 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02006011
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006012 /* other states, DONE...TUNNEL */
6013 /* for keep-alive we don't want to forward closes on DONE */
6014 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6015 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
6016 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006017
Christopher Faulet894da4c2017-07-18 11:29:07 +02006018 http_resync_states(s);
6019 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006020 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6021 if (res->flags & CF_SHUTW) {
6022 /* response errors are most likely due to the
6023 * client aborting the transfer. */
6024 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01006025 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006026 if (msg->err_pos >= 0)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006027 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006028 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006029 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006030 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006031 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02006032 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006033
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006034 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01006035 if (res->flags & CF_SHUTW)
6036 goto aborted_xfer;
6037
6038 /* stop waiting for data if the input is closed before the end. If the
6039 * client side was already closed, it means that the client has aborted,
6040 * so we don't want to count this as a server abort. Otherwise it's a
6041 * server abort.
6042 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02006043 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006044 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006045 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006046 /* If we have some pending data, we continue the processing */
6047 if (!buffer_pending(res->buf)) {
6048 if (!(s->flags & SF_ERR_MASK))
6049 s->flags |= SF_ERR_SRVCL;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006050 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006051 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006052 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006053 goto return_bad_res_stats_ok;
6054 }
Willy Tarreau40dba092010-03-04 18:14:51 +01006055 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006056
Willy Tarreau40dba092010-03-04 18:14:51 +01006057 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006058 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006059 goto return_bad_res;
6060
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006061 /* When TE: chunked is used, we need to get there again to parse
6062 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02006063 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
6064 * or if there are filters registered on the stream, we don't want to
6065 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006066 */
Christopher Faulet69744d92017-03-30 10:54:35 +02006067 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006068 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006069 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6070 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006071 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006072
Willy Tarreau5c620922011-05-11 19:56:11 +02006073 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006074 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006075 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006076 * modes are already handled by the stream sock layer. We must not do
6077 * this in content-length mode because it could present the MSG_MORE
6078 * flag with the last block of forwarded data, which would cause an
6079 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006080 */
Christopher Faulet92d36382015-11-05 13:35:03 +01006081 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006082 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006083
Willy Tarreau87b09662015-04-03 00:22:06 +02006084 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006085 return 0;
6086
Willy Tarreau40dba092010-03-04 18:14:51 +01006087 return_bad_res: /* let's centralize all bad responses */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006088 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006089 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006090 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006091
6092 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006093 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006094 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006095 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006096 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006097 res->analysers &= AN_RES_FLT_END;
6098 s->req.analysers &= AN_REQ_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006099 if (objt_server(s->target))
6100 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006101
Willy Tarreaue7dff022015-04-03 01:14:29 +02006102 if (!(s->flags & SF_ERR_MASK))
6103 s->flags |= SF_ERR_PRXCOND;
6104 if (!(s->flags & SF_FINST_MASK))
6105 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006106 return 0;
6107
6108 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006109 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006110 txn->rsp.msg_state = HTTP_MSG_ERROR;
6111 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006112 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006113 res->analysers &= AN_RES_FLT_END;
6114 s->req.analysers &= AN_REQ_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006115
Christopher Fauletff8abcd2017-06-02 15:33:24 +02006116 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
6117 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006118 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +02006119 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006120
Willy Tarreaue7dff022015-04-03 01:14:29 +02006121 if (!(s->flags & SF_ERR_MASK))
6122 s->flags |= SF_ERR_CLICL;
6123 if (!(s->flags & SF_FINST_MASK))
6124 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006125 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006126}
6127
6128
6129static inline int
6130http_msg_forward_body(struct stream *s, struct http_msg *msg)
6131{
6132 struct channel *chn = msg->chn;
6133 int ret;
6134
6135 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
6136
6137 if (msg->msg_state == HTTP_MSG_ENDING)
6138 goto ending;
6139
6140 /* Neither content-length, nor transfer-encoding was found, so we must
6141 * read the body until the server connection is closed. In that case, we
6142 * eat data as they come. Of course, this happens for response only. */
6143 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6144 unsigned long long len = (chn->buf->i - msg->next);
6145 msg->chunk_len += len;
6146 msg->body_len += len;
6147 }
Christopher Fauletda02e172015-12-04 09:25:05 +01006148 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
6149 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6150 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006151 msg->next += ret;
6152 msg->chunk_len -= ret;
6153 if (msg->chunk_len) {
6154 /* input empty or output full */
6155 if (chn->buf->i > msg->next)
6156 chn->flags |= CF_WAKE_WRITE;
6157 goto missing_data_or_waiting;
6158 }
6159
Christopher Faulet1486b0a2017-07-18 11:42:08 +02006160 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
6161 * always set for a request. */
6162 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6163 /* The server still sending data that should be filtered */
6164 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
6165 goto missing_data_or_waiting;
6166 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006167
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006168 msg->msg_state = HTTP_MSG_ENDING;
6169
6170 ending:
6171 /* we may have some pending data starting at res->buf->p such as a last
6172 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006173 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6174 /* default_ret */ msg->next,
6175 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006176 b_adv(chn->buf, ret);
6177 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006178 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6179 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006180 if (msg->next)
6181 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006182
Christopher Fauletda02e172015-12-04 09:25:05 +01006183 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
6184 /* default_ret */ 1,
6185 /* on_error */ goto error,
6186 /* on_wait */ goto waiting);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006187 msg->msg_state = HTTP_MSG_DONE;
6188 return 1;
6189
6190 missing_data_or_waiting:
6191 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006192 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6193 /* default_ret */ msg->next,
6194 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006195 b_adv(chn->buf, ret);
6196 msg->next -= ret;
6197 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6198 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006199 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006200 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006201 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006202 return 0;
6203 error:
6204 return -1;
6205}
6206
6207static inline int
6208http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
6209{
6210 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006211 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006212 int ret;
6213
6214 /* Here we have the guarantee to be in one of the following state:
6215 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
6216 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
6217
6218 switch_states:
6219 switch (msg->msg_state) {
6220 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01006221 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
6222 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6223 /* on_error */ goto error);
6224 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006225 msg->chunk_len -= ret;
6226 if (msg->chunk_len) {
6227 /* input empty or output full */
6228 if (chn->buf->i > msg->next)
6229 chn->flags |= CF_WAKE_WRITE;
6230 goto missing_data_or_waiting;
6231 }
6232
6233 /* nothing left to forward for this chunk*/
6234 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
6235 /* fall through for HTTP_MSG_CHUNK_CRLF */
6236
6237 case HTTP_MSG_CHUNK_CRLF:
6238 /* we want the CRLF after the data */
Willy Tarreaub2892562017-09-21 11:33:54 +02006239 ret = h1_skip_chunk_crlf(chn->buf, msg->next, chn->buf->i);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006240 if (ret == 0)
6241 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02006242 if (ret < 0) {
6243 msg->err_pos = chn->buf->i + ret;
6244 if (msg->err_pos < 0)
6245 msg->err_pos += chn->buf->size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006246 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02006247 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01006248 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006249 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6250 /* fall through for HTTP_MSG_CHUNK_SIZE */
6251
6252 case HTTP_MSG_CHUNK_SIZE:
6253 /* read the chunk size and assign it to ->chunk_len,
6254 * then set ->next to point to the body and switch to
6255 * DATA or TRAILERS state.
6256 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006257 ret = h1_parse_chunk_size(chn->buf, msg->next, chn->buf->i, &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006258 if (ret == 0)
6259 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006260 if (ret < 0) {
6261 msg->err_pos = chn->buf->i + ret;
6262 if (msg->err_pos < 0)
6263 msg->err_pos += chn->buf->size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006264 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006265 }
6266
6267 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01006268 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006269 msg->chunk_len = chunk;
6270 msg->body_len += chunk;
6271
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006272 if (msg->chunk_len) {
6273 msg->msg_state = HTTP_MSG_DATA;
6274 goto switch_states;
6275 }
6276 msg->msg_state = HTTP_MSG_TRAILERS;
6277 /* fall through for HTTP_MSG_TRAILERS */
6278
6279 case HTTP_MSG_TRAILERS:
6280 ret = http_forward_trailers(msg);
6281 if (ret < 0)
6282 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01006283 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
6284 /* default_ret */ 1,
6285 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006286 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006287 if (!ret)
6288 goto missing_data_or_waiting;
6289 break;
6290
6291 case HTTP_MSG_ENDING:
6292 goto ending;
6293
6294 default:
6295 /* This should no happen in this function */
6296 goto error;
6297 }
6298
6299 msg->msg_state = HTTP_MSG_ENDING;
6300 ending:
6301 /* we may have some pending data starting at res->buf->p such as a last
6302 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006303 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006304 /* default_ret */ msg->next,
6305 /* on_error */ goto error);
6306 b_adv(chn->buf, ret);
6307 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006308 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6309 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006310 if (msg->next)
6311 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006312
Christopher Fauletda02e172015-12-04 09:25:05 +01006313 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006314 /* default_ret */ 1,
6315 /* on_error */ goto error,
6316 /* on_wait */ goto waiting);
6317 msg->msg_state = HTTP_MSG_DONE;
6318 return 1;
6319
6320 missing_data_or_waiting:
6321 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006322 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006323 /* default_ret */ msg->next,
6324 /* on_error */ goto error);
6325 b_adv(chn->buf, ret);
6326 msg->next -= ret;
6327 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6328 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006329 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006330 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006331 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006332 return 0;
6333
6334 chunk_parsing_error:
6335 if (msg->err_pos >= 0) {
6336 if (chn->flags & CF_ISRESP)
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006337 http_capture_bad_message(s->be, &s->be->invalid_rep, s, msg,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006338 msg->msg_state, strm_fe(s));
6339 else
Emeric Brun8c1aaa22017-06-15 11:30:06 +02006340 http_capture_bad_message(strm_fe(s), &strm_fe(s)->invalid_req, s,
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006341 msg, msg->msg_state, s->be);
6342 }
6343 error:
6344 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006345}
6346
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006347
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006348/* Iterate the same filter through all request headers.
6349 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006350 * Since it can manage the switch to another backend, it updates the per-proxy
6351 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006352 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006353int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006354{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006355 char *cur_ptr, *cur_end, *cur_next;
6356 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006357 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006358 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006359 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006360
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006361 last_hdr = 0;
6362
Willy Tarreau9b28e032012-10-12 23:49:43 +02006363 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006364 old_idx = 0;
6365
6366 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006367 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006368 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006369 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006370 (exp->action == ACT_ALLOW ||
6371 exp->action == ACT_DENY ||
6372 exp->action == ACT_TARPIT))
6373 return 0;
6374
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006375 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006376 if (!cur_idx)
6377 break;
6378
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006379 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006380 cur_ptr = cur_next;
6381 cur_end = cur_ptr + cur_hdr->len;
6382 cur_next = cur_end + cur_hdr->cr + 1;
6383
6384 /* Now we have one header between cur_ptr and cur_end,
6385 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006386 */
6387
Willy Tarreau15a53a42015-01-21 13:39:42 +01006388 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006389 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006390 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006391 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006392 last_hdr = 1;
6393 break;
6394
6395 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006396 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006397 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006398 break;
6399
6400 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006401 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006402 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006403 break;
6404
6405 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006406 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6407 if (trash.len < 0)
6408 return -1;
6409
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006410 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006411 /* FIXME: if the user adds a newline in the replacement, the
6412 * index will not be recalculated for now, and the new line
6413 * will not be counted as a new header.
6414 */
6415
6416 cur_end += delta;
6417 cur_next += delta;
6418 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006419 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006420 break;
6421
6422 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006423 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006424 cur_next += delta;
6425
Willy Tarreaufa355d42009-11-29 18:12:29 +01006426 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006427 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6428 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006429 cur_hdr->len = 0;
6430 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006431 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006432 break;
6433
6434 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006435 }
6436
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006437 /* keep the link from this header to next one in case of later
6438 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006439 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006440 old_idx = cur_idx;
6441 }
6442 return 0;
6443}
6444
6445
6446/* Apply the filter to the request line.
6447 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6448 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006449 * Since it can manage the switch to another backend, it updates the per-proxy
6450 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006451 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006452int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006453{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006454 char *cur_ptr, *cur_end;
6455 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006456 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006457 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006458
Willy Tarreau3d300592007-03-18 18:34:41 +01006459 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006460 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006461 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006462 (exp->action == ACT_ALLOW ||
6463 exp->action == ACT_DENY ||
6464 exp->action == ACT_TARPIT))
6465 return 0;
6466 else if (exp->action == ACT_REMOVE)
6467 return 0;
6468
6469 done = 0;
6470
Willy Tarreau9b28e032012-10-12 23:49:43 +02006471 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006472 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006473
6474 /* Now we have the request line between cur_ptr and cur_end */
6475
Willy Tarreau15a53a42015-01-21 13:39:42 +01006476 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006477 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006478 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006479 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006480 done = 1;
6481 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006482
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006483 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006484 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006485 done = 1;
6486 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006487
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006488 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006489 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006490 done = 1;
6491 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006492
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006493 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006494 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6495 if (trash.len < 0)
6496 return -1;
6497
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006498 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006499 /* FIXME: if the user adds a newline in the replacement, the
6500 * index will not be recalculated for now, and the new line
6501 * will not be counted as a new header.
6502 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006503
Willy Tarreaufa355d42009-11-29 18:12:29 +01006504 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006505 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006506 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006507 HTTP_MSG_RQMETH,
6508 cur_ptr, cur_end + 1,
6509 NULL, NULL);
6510 if (unlikely(!cur_end))
6511 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006512
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006513 /* we have a full request and we know that we have either a CR
6514 * or an LF at <ptr>.
6515 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006516 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6517 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006518 /* there is no point trying this regex on headers */
6519 return 1;
6520 }
6521 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006522 return done;
6523}
Willy Tarreau97de6242006-12-27 17:18:38 +01006524
Willy Tarreau58f10d72006-12-04 02:26:12 +01006525
Willy Tarreau58f10d72006-12-04 02:26:12 +01006526
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006527/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006528 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006529 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006530 * unparsable request. Since it can manage the switch to another backend, it
6531 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006532 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006533int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006534{
Willy Tarreau192252e2015-04-04 01:47:55 +02006535 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006536 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006537 struct hdr_exp *exp;
6538
6539 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006540 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006541
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006542 /*
6543 * The interleaving of transformations and verdicts
6544 * makes it difficult to decide to continue or stop
6545 * the evaluation.
6546 */
6547
Willy Tarreau6c123b12010-01-28 20:22:06 +01006548 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6549 break;
6550
Willy Tarreau3d300592007-03-18 18:34:41 +01006551 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006552 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006553 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006554 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006555
6556 /* if this filter had a condition, evaluate it now and skip to
6557 * next filter if the condition does not match.
6558 */
6559 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006560 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006561 ret = acl_pass(ret);
6562 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6563 ret = !ret;
6564
6565 if (!ret)
6566 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006567 }
6568
6569 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006570 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006571 if (unlikely(ret < 0))
6572 return -1;
6573
6574 if (likely(ret == 0)) {
6575 /* The filter did not match the request, it can be
6576 * iterated through all headers.
6577 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01006578 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
6579 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006580 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006581 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006582 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006583}
6584
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006585
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006586/* Find the end of a cookie value contained between <s> and <e>. It works the
6587 * same way as with headers above except that the semi-colon also ends a token.
6588 * See RFC2965 for more information. Note that it requires a valid header to
6589 * return a valid result.
6590 */
6591char *find_cookie_value_end(char *s, const char *e)
6592{
6593 int quoted, qdpair;
6594
6595 quoted = qdpair = 0;
6596 for (; s < e; s++) {
6597 if (qdpair) qdpair = 0;
6598 else if (quoted) {
6599 if (*s == '\\') qdpair = 1;
6600 else if (*s == '"') quoted = 0;
6601 }
6602 else if (*s == '"') quoted = 1;
6603 else if (*s == ',' || *s == ';') return s;
6604 }
6605 return s;
6606}
6607
6608/* Delete a value in a header between delimiters <from> and <next> in buffer
6609 * <buf>. The number of characters displaced is returned, and the pointer to
6610 * the first delimiter is updated if required. The function tries as much as
6611 * possible to respect the following principles :
6612 * - replace <from> delimiter by the <next> one unless <from> points to a
6613 * colon, in which case <next> is simply removed
6614 * - set exactly one space character after the new first delimiter, unless
6615 * there are not enough characters in the block being moved to do so.
6616 * - remove unneeded spaces before the previous delimiter and after the new
6617 * one.
6618 *
6619 * It is the caller's responsibility to ensure that :
6620 * - <from> points to a valid delimiter or the colon ;
6621 * - <next> points to a valid delimiter or the final CR/LF ;
6622 * - there are non-space chars before <from> ;
6623 * - there is a CR/LF at or after <next>.
6624 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006625int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006626{
6627 char *prev = *from;
6628
6629 if (*prev == ':') {
6630 /* We're removing the first value, preserve the colon and add a
6631 * space if possible.
6632 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006633 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006634 next++;
6635 prev++;
6636 if (prev < next)
6637 *prev++ = ' ';
6638
Willy Tarreau2235b262016-11-05 15:50:20 +01006639 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006640 next++;
6641 } else {
6642 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01006643 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006644 prev--;
6645 *from = prev;
6646
6647 /* copy the delimiter and if possible a space if we're
6648 * not at the end of the line.
6649 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006650 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006651 *prev++ = *next++;
6652 if (prev + 1 < next)
6653 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01006654 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006655 next++;
6656 }
6657 }
6658 return buffer_replace2(buf, prev, next, NULL, 0);
6659}
6660
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006661/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006662 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006663 * desirable to call it only when needed. This code is quite complex because
6664 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6665 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006666 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006667void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006668{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006669 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006670 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006671 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006672 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006673 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6674 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006675
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006676 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006677 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006678 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006679
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006680 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006681 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006682 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006683
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006684 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006685 hdr_beg = hdr_next;
6686 hdr_end = hdr_beg + cur_hdr->len;
6687 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006688
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006689 /* We have one full header between hdr_beg and hdr_end, and the
6690 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006691 * "Cookie:" headers.
6692 */
6693
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006694 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006695 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006696 old_idx = cur_idx;
6697 continue;
6698 }
6699
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006700 del_from = NULL; /* nothing to be deleted */
6701 preserve_hdr = 0; /* assume we may kill the whole header */
6702
Willy Tarreau58f10d72006-12-04 02:26:12 +01006703 /* Now look for cookies. Conforming to RFC2109, we have to support
6704 * attributes whose name begin with a '$', and associate them with
6705 * the right cookie, if we want to delete this cookie.
6706 * So there are 3 cases for each cookie read :
6707 * 1) it's a special attribute, beginning with a '$' : ignore it.
6708 * 2) it's a server id cookie that we *MAY* want to delete : save
6709 * some pointers on it (last semi-colon, beginning of cookie...)
6710 * 3) it's an application cookie : we *MAY* have to delete a previous
6711 * "special" cookie.
6712 * At the end of loop, if a "special" cookie remains, we may have to
6713 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006714 * *MUST* delete it.
6715 *
6716 * Note: RFC2965 is unclear about the processing of spaces around
6717 * the equal sign in the ATTR=VALUE form. A careful inspection of
6718 * the RFC explicitly allows spaces before it, and not within the
6719 * tokens (attrs or values). An inspection of RFC2109 allows that
6720 * too but section 10.1.3 lets one think that spaces may be allowed
6721 * after the equal sign too, resulting in some (rare) buggy
6722 * implementations trying to do that. So let's do what servers do.
6723 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6724 * allowed quoted strings in values, with any possible character
6725 * after a backslash, including control chars and delimitors, which
6726 * causes parsing to become ambiguous. Browsers also allow spaces
6727 * within values even without quotes.
6728 *
6729 * We have to keep multiple pointers in order to support cookie
6730 * removal at the beginning, middle or end of header without
6731 * corrupting the header. All of these headers are valid :
6732 *
6733 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6734 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6735 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6736 * | | | | | | | | |
6737 * | | | | | | | | hdr_end <--+
6738 * | | | | | | | +--> next
6739 * | | | | | | +----> val_end
6740 * | | | | | +-----------> val_beg
6741 * | | | | +--------------> equal
6742 * | | | +----------------> att_end
6743 * | | +---------------------> att_beg
6744 * | +--------------------------> prev
6745 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006746 */
6747
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006748 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6749 /* Iterate through all cookies on this line */
6750
6751 /* find att_beg */
6752 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006753 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006754 att_beg++;
6755
6756 /* find att_end : this is the first character after the last non
6757 * space before the equal. It may be equal to hdr_end.
6758 */
6759 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006760
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006761 while (equal < hdr_end) {
6762 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006763 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006764 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006765 continue;
6766 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006767 }
6768
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006769 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6770 * is between <att_beg> and <equal>, both may be identical.
6771 */
6772
6773 /* look for end of cookie if there is an equal sign */
6774 if (equal < hdr_end && *equal == '=') {
6775 /* look for the beginning of the value */
6776 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006777 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006778 val_beg++;
6779
6780 /* find the end of the value, respecting quotes */
6781 next = find_cookie_value_end(val_beg, hdr_end);
6782
6783 /* make val_end point to the first white space or delimitor after the value */
6784 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006785 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006786 val_end--;
6787 } else {
6788 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006789 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006790
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006791 /* We have nothing to do with attributes beginning with '$'. However,
6792 * they will automatically be removed if a header before them is removed,
6793 * since they're supposed to be linked together.
6794 */
6795 if (*att_beg == '$')
6796 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006797
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006798 /* Ignore cookies with no equal sign */
6799 if (equal == next) {
6800 /* This is not our cookie, so we must preserve it. But if we already
6801 * scheduled another cookie for removal, we cannot remove the
6802 * complete header, but we can remove the previous block itself.
6803 */
6804 preserve_hdr = 1;
6805 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006806 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006807 val_end += delta;
6808 next += delta;
6809 hdr_end += delta;
6810 hdr_next += delta;
6811 cur_hdr->len += delta;
6812 http_msg_move_end(&txn->req, delta);
6813 prev = del_from;
6814 del_from = NULL;
6815 }
6816 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006817 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006818
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006819 /* if there are spaces around the equal sign, we need to
6820 * strip them otherwise we'll get trouble for cookie captures,
6821 * or even for rewrites. Since this happens extremely rarely,
6822 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006823 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006824 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6825 int stripped_before = 0;
6826 int stripped_after = 0;
6827
6828 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006829 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006830 equal += stripped_before;
6831 val_beg += stripped_before;
6832 }
6833
6834 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006835 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006836 val_beg += stripped_after;
6837 stripped_before += stripped_after;
6838 }
6839
6840 val_end += stripped_before;
6841 next += stripped_before;
6842 hdr_end += stripped_before;
6843 hdr_next += stripped_before;
6844 cur_hdr->len += stripped_before;
6845 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006846 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006847 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006848
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006849 /* First, let's see if we want to capture this cookie. We check
6850 * that we don't already have a client side cookie, because we
6851 * can only capture one. Also as an optimisation, we ignore
6852 * cookies shorter than the declared name.
6853 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006854 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6855 (val_end - att_beg >= sess->fe->capture_namelen) &&
6856 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006857 int log_len = val_end - att_beg;
6858
6859 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6860 Alert("HTTP logging : out of memory.\n");
6861 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006862 if (log_len > sess->fe->capture_len)
6863 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006864 memcpy(txn->cli_cookie, att_beg, log_len);
6865 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006866 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006867 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006868
Willy Tarreaubca99692010-10-06 19:25:55 +02006869 /* Persistence cookies in passive, rewrite or insert mode have the
6870 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006871 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006872 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006873 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006874 * For cookies in prefix mode, the form is :
6875 *
6876 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006877 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006878 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6879 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6880 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006881 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006882
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006883 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6884 * have the server ID between val_beg and delim, and the original cookie between
6885 * delim+1 and val_end. Otherwise, delim==val_end :
6886 *
6887 * Cookie: NAME=SRV; # in all but prefix modes
6888 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6889 * | || || | |+-> next
6890 * | || || | +--> val_end
6891 * | || || +---------> delim
6892 * | || |+------------> val_beg
6893 * | || +-------------> att_end = equal
6894 * | |+-----------------> att_beg
6895 * | +------------------> prev
6896 * +-------------------------> hdr_beg
6897 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006898
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006899 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006900 for (delim = val_beg; delim < val_end; delim++)
6901 if (*delim == COOKIE_DELIM)
6902 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006903 } else {
6904 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006905 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006906 /* Now check if the cookie contains a date field, which would
6907 * appear after a vertical bar ('|') just after the server name
6908 * and before the delimiter.
6909 */
6910 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6911 if (vbar1) {
6912 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006913 * right is the last seen date. It is a base64 encoded
6914 * 30-bit value representing the UNIX date since the
6915 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006916 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006917 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006918 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006919 if (val_end - vbar1 >= 5) {
6920 val = b64tos30(vbar1);
6921 if (val > 0)
6922 txn->cookie_last_date = val << 2;
6923 }
6924 /* look for a second vertical bar */
6925 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6926 if (vbar1 && (val_end - vbar1 > 5)) {
6927 val = b64tos30(vbar1 + 1);
6928 if (val > 0)
6929 txn->cookie_first_date = val << 2;
6930 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006931 }
6932 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006933
Willy Tarreauf64d1412010-10-07 20:06:11 +02006934 /* if the cookie has an expiration date and the proxy wants to check
6935 * it, then we do that now. We first check if the cookie is too old,
6936 * then only if it has expired. We detect strict overflow because the
6937 * time resolution here is not great (4 seconds). Cookies with dates
6938 * in the future are ignored if their offset is beyond one day. This
6939 * allows an admin to fix timezone issues without expiring everyone
6940 * and at the same time avoids keeping unwanted side effects for too
6941 * long.
6942 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006943 if (txn->cookie_first_date && s->be->cookie_maxlife &&
6944 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006945 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006946 txn->flags &= ~TX_CK_MASK;
6947 txn->flags |= TX_CK_OLD;
6948 delim = val_beg; // let's pretend we have not found the cookie
6949 txn->cookie_first_date = 0;
6950 txn->cookie_last_date = 0;
6951 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006952 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
6953 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006954 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006955 txn->flags &= ~TX_CK_MASK;
6956 txn->flags |= TX_CK_EXPIRED;
6957 delim = val_beg; // let's pretend we have not found the cookie
6958 txn->cookie_first_date = 0;
6959 txn->cookie_last_date = 0;
6960 }
6961
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006962 /* Here, we'll look for the first running server which supports the cookie.
6963 * This allows to share a same cookie between several servers, for example
6964 * to dedicate backup servers to specific servers only.
6965 * However, to prevent clients from sticking to cookie-less backup server
6966 * when they have incidentely learned an empty cookie, we simply ignore
6967 * empty cookies and mark them as invalid.
6968 * The same behaviour is applied when persistence must be ignored.
6969 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006970 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006971 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006972
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006973 while (srv) {
6974 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6975 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02006976 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006977 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006978 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006979 /* we found the server and we can use it */
6980 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02006981 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006982 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006983 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006984 break;
6985 } else {
6986 /* we found a server, but it's down,
6987 * mark it as such and go on in case
6988 * another one is available.
6989 */
6990 txn->flags &= ~TX_CK_MASK;
6991 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006992 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006993 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006994 srv = srv->next;
6995 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006996
Willy Tarreauf64d1412010-10-07 20:06:11 +02006997 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006998 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006999 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007000 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007001 txn->flags |= TX_CK_UNUSED;
7002 else
7003 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007004 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007005
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007006 /* depending on the cookie mode, we may have to either :
7007 * - delete the complete cookie if we're in insert+indirect mode, so that
7008 * the server never sees it ;
7009 * - remove the server id from the cookie value, and tag the cookie as an
7010 * application cookie so that it does not get accidentely removed later,
7011 * if we're in cookie prefix mode
7012 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007013 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007014 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007015
Willy Tarreau9b28e032012-10-12 23:49:43 +02007016 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007017 val_end += delta;
7018 next += delta;
7019 hdr_end += delta;
7020 hdr_next += delta;
7021 cur_hdr->len += delta;
7022 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007023
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007024 del_from = NULL;
7025 preserve_hdr = 1; /* we want to keep this cookie */
7026 }
7027 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007028 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007029 del_from = prev;
7030 }
7031 } else {
7032 /* This is not our cookie, so we must preserve it. But if we already
7033 * scheduled another cookie for removal, we cannot remove the
7034 * complete header, but we can remove the previous block itself.
7035 */
7036 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007037
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007038 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007039 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007040 if (att_beg >= del_from)
7041 att_beg += delta;
7042 if (att_end >= del_from)
7043 att_end += delta;
7044 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007045 val_end += delta;
7046 next += delta;
7047 hdr_end += delta;
7048 hdr_next += delta;
7049 cur_hdr->len += delta;
7050 http_msg_move_end(&txn->req, delta);
7051 prev = del_from;
7052 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007053 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007054 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007055
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007056 /* continue with next cookie on this header line */
7057 att_beg = next;
7058 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007059
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007060 /* There are no more cookies on this line.
7061 * We may still have one (or several) marked for deletion at the
7062 * end of the line. We must do this now in two ways :
7063 * - if some cookies must be preserved, we only delete from the
7064 * mark to the end of line ;
7065 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007066 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007067 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007068 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007069 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007070 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007071 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007072 cur_hdr->len += delta;
7073 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007074 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007075
7076 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007077 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7078 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007079 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007080 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007081 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007082 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007083 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007084 }
7085
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007086 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007087 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007088 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007089}
7090
7091
Willy Tarreaua15645d2007-03-18 16:22:39 +01007092/* Iterate the same filter through all response headers contained in <rtr>.
7093 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7094 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007095int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007096{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007097 char *cur_ptr, *cur_end, *cur_next;
7098 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007099 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007100 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007101 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007102
7103 last_hdr = 0;
7104
Willy Tarreau9b28e032012-10-12 23:49:43 +02007105 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007106 old_idx = 0;
7107
7108 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007109 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007110 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007111 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007112 (exp->action == ACT_ALLOW ||
7113 exp->action == ACT_DENY))
7114 return 0;
7115
7116 cur_idx = txn->hdr_idx.v[old_idx].next;
7117 if (!cur_idx)
7118 break;
7119
7120 cur_hdr = &txn->hdr_idx.v[cur_idx];
7121 cur_ptr = cur_next;
7122 cur_end = cur_ptr + cur_hdr->len;
7123 cur_next = cur_end + cur_hdr->cr + 1;
7124
7125 /* Now we have one header between cur_ptr and cur_end,
7126 * and the next header starts at cur_next.
7127 */
7128
Willy Tarreau15a53a42015-01-21 13:39:42 +01007129 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007130 switch (exp->action) {
7131 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007132 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007133 last_hdr = 1;
7134 break;
7135
7136 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007137 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007138 last_hdr = 1;
7139 break;
7140
7141 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007142 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7143 if (trash.len < 0)
7144 return -1;
7145
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007146 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007147 /* FIXME: if the user adds a newline in the replacement, the
7148 * index will not be recalculated for now, and the new line
7149 * will not be counted as a new header.
7150 */
7151
7152 cur_end += delta;
7153 cur_next += delta;
7154 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007155 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007156 break;
7157
7158 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007159 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007160 cur_next += delta;
7161
Willy Tarreaufa355d42009-11-29 18:12:29 +01007162 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007163 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7164 txn->hdr_idx.used--;
7165 cur_hdr->len = 0;
7166 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007167 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007168 break;
7169
7170 }
7171 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007172
7173 /* keep the link from this header to next one in case of later
7174 * removal of next header.
7175 */
7176 old_idx = cur_idx;
7177 }
7178 return 0;
7179}
7180
7181
7182/* Apply the filter to the status line in the response buffer <rtr>.
7183 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7184 * or -1 if a replacement resulted in an invalid status line.
7185 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007186int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007187{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007188 char *cur_ptr, *cur_end;
7189 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007190 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007191 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007192
7193
Willy Tarreau3d300592007-03-18 18:34:41 +01007194 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007195 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007196 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007197 (exp->action == ACT_ALLOW ||
7198 exp->action == ACT_DENY))
7199 return 0;
7200 else if (exp->action == ACT_REMOVE)
7201 return 0;
7202
7203 done = 0;
7204
Willy Tarreau9b28e032012-10-12 23:49:43 +02007205 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007206 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007207
7208 /* Now we have the status line between cur_ptr and cur_end */
7209
Willy Tarreau15a53a42015-01-21 13:39:42 +01007210 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007211 switch (exp->action) {
7212 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007213 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007214 done = 1;
7215 break;
7216
7217 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007218 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007219 done = 1;
7220 break;
7221
7222 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007223 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7224 if (trash.len < 0)
7225 return -1;
7226
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007227 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007228 /* FIXME: if the user adds a newline in the replacement, the
7229 * index will not be recalculated for now, and the new line
7230 * will not be counted as a new header.
7231 */
7232
Willy Tarreaufa355d42009-11-29 18:12:29 +01007233 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007234 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007235 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007236 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007237 cur_ptr, cur_end + 1,
7238 NULL, NULL);
7239 if (unlikely(!cur_end))
7240 return -1;
7241
7242 /* we have a full respnse and we know that we have either a CR
7243 * or an LF at <ptr>.
7244 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007245 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007246 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007247 /* there is no point trying this regex on headers */
7248 return 1;
7249 }
7250 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007251 return done;
7252}
7253
7254
7255
7256/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007257 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007258 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7259 * unparsable response.
7260 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007261int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007262{
Willy Tarreau192252e2015-04-04 01:47:55 +02007263 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007264 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007265 struct hdr_exp *exp;
7266
7267 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007268 int ret;
7269
7270 /*
7271 * The interleaving of transformations and verdicts
7272 * makes it difficult to decide to continue or stop
7273 * the evaluation.
7274 */
7275
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007276 if (txn->flags & TX_SVDENY)
7277 break;
7278
Willy Tarreau3d300592007-03-18 18:34:41 +01007279 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007280 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7281 exp->action == ACT_PASS)) {
7282 exp = exp->next;
7283 continue;
7284 }
7285
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007286 /* if this filter had a condition, evaluate it now and skip to
7287 * next filter if the condition does not match.
7288 */
7289 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007290 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007291 ret = acl_pass(ret);
7292 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7293 ret = !ret;
7294 if (!ret)
7295 continue;
7296 }
7297
Willy Tarreaua15645d2007-03-18 16:22:39 +01007298 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007299 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007300 if (unlikely(ret < 0))
7301 return -1;
7302
7303 if (likely(ret == 0)) {
7304 /* The filter did not match the response, it can be
7305 * iterated through all headers.
7306 */
Sasha Pachevc6002042014-05-26 12:33:48 -06007307 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
7308 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007309 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007310 }
7311 return 0;
7312}
7313
7314
Willy Tarreaua15645d2007-03-18 16:22:39 +01007315/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007316 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007317 * desirable to call it only when needed. This function is also used when we
7318 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007319 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007320void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007321{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007322 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007323 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01007324 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007325 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007326 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007327 char *hdr_beg, *hdr_end, *hdr_next;
7328 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007329
Willy Tarreaua15645d2007-03-18 16:22:39 +01007330 /* Iterate through the headers.
7331 * we start with the start line.
7332 */
7333 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007334 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007335
7336 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7337 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007338 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007339
7340 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007341 hdr_beg = hdr_next;
7342 hdr_end = hdr_beg + cur_hdr->len;
7343 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007344
Willy Tarreau24581ba2010-08-31 22:39:35 +02007345 /* We have one full header between hdr_beg and hdr_end, and the
7346 * next header starts at hdr_next. We're only interested in
7347 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007348 */
7349
Willy Tarreau24581ba2010-08-31 22:39:35 +02007350 is_cookie2 = 0;
7351 prev = hdr_beg + 10;
7352 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007353 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007354 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7355 if (!val) {
7356 old_idx = cur_idx;
7357 continue;
7358 }
7359 is_cookie2 = 1;
7360 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007361 }
7362
Willy Tarreau24581ba2010-08-31 22:39:35 +02007363 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7364 * <prev> points to the colon.
7365 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007366 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007367
Willy Tarreau24581ba2010-08-31 22:39:35 +02007368 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7369 * check-cache is enabled) and we are not interested in checking
7370 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007371 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02007372 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007373 return;
7374
Willy Tarreau24581ba2010-08-31 22:39:35 +02007375 /* OK so now we know we have to process this response cookie.
7376 * The format of the Set-Cookie header is slightly different
7377 * from the format of the Cookie header in that it does not
7378 * support the comma as a cookie delimiter (thus the header
7379 * cannot be folded) because the Expires attribute described in
7380 * the original Netscape's spec may contain an unquoted date
7381 * with a comma inside. We have to live with this because
7382 * many browsers don't support Max-Age and some browsers don't
7383 * support quoted strings. However the Set-Cookie2 header is
7384 * clean.
7385 *
7386 * We have to keep multiple pointers in order to support cookie
7387 * removal at the beginning, middle or end of header without
7388 * corrupting the header (in case of set-cookie2). A special
7389 * pointer, <scav> points to the beginning of the set-cookie-av
7390 * fields after the first semi-colon. The <next> pointer points
7391 * either to the end of line (set-cookie) or next unquoted comma
7392 * (set-cookie2). All of these headers are valid :
7393 *
7394 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7395 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7396 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7397 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7398 * | | | | | | | | | |
7399 * | | | | | | | | +-> next hdr_end <--+
7400 * | | | | | | | +------------> scav
7401 * | | | | | | +--------------> val_end
7402 * | | | | | +--------------------> val_beg
7403 * | | | | +----------------------> equal
7404 * | | | +------------------------> att_end
7405 * | | +----------------------------> att_beg
7406 * | +------------------------------> prev
7407 * +-----------------------------------------> hdr_beg
7408 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007409
Willy Tarreau24581ba2010-08-31 22:39:35 +02007410 for (; prev < hdr_end; prev = next) {
7411 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007412
Willy Tarreau24581ba2010-08-31 22:39:35 +02007413 /* find att_beg */
7414 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007415 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007416 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007417
Willy Tarreau24581ba2010-08-31 22:39:35 +02007418 /* find att_end : this is the first character after the last non
7419 * space before the equal. It may be equal to hdr_end.
7420 */
7421 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007422
Willy Tarreau24581ba2010-08-31 22:39:35 +02007423 while (equal < hdr_end) {
7424 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7425 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007426 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007427 continue;
7428 att_end = equal;
7429 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007430
Willy Tarreau24581ba2010-08-31 22:39:35 +02007431 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7432 * is between <att_beg> and <equal>, both may be identical.
7433 */
7434
7435 /* look for end of cookie if there is an equal sign */
7436 if (equal < hdr_end && *equal == '=') {
7437 /* look for the beginning of the value */
7438 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007439 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007440 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007441
Willy Tarreau24581ba2010-08-31 22:39:35 +02007442 /* find the end of the value, respecting quotes */
7443 next = find_cookie_value_end(val_beg, hdr_end);
7444
7445 /* make val_end point to the first white space or delimitor after the value */
7446 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007447 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007448 val_end--;
7449 } else {
7450 /* <equal> points to next comma, semi-colon or EOL */
7451 val_beg = val_end = next = equal;
7452 }
7453
7454 if (next < hdr_end) {
7455 /* Set-Cookie2 supports multiple cookies, and <next> points to
7456 * a colon or semi-colon before the end. So skip all attr-value
7457 * pairs and look for the next comma. For Set-Cookie, since
7458 * commas are permitted in values, skip to the end.
7459 */
7460 if (is_cookie2)
7461 next = find_hdr_value_end(next, hdr_end);
7462 else
7463 next = hdr_end;
7464 }
7465
7466 /* Now everything is as on the diagram above */
7467
7468 /* Ignore cookies with no equal sign */
7469 if (equal == val_end)
7470 continue;
7471
7472 /* If there are spaces around the equal sign, we need to
7473 * strip them otherwise we'll get trouble for cookie captures,
7474 * or even for rewrites. Since this happens extremely rarely,
7475 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007476 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007477 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7478 int stripped_before = 0;
7479 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007480
Willy Tarreau24581ba2010-08-31 22:39:35 +02007481 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007482 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007483 equal += stripped_before;
7484 val_beg += stripped_before;
7485 }
7486
7487 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007488 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007489 val_beg += stripped_after;
7490 stripped_before += stripped_after;
7491 }
7492
7493 val_end += stripped_before;
7494 next += stripped_before;
7495 hdr_end += stripped_before;
7496 hdr_next += stripped_before;
7497 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007498 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007499 }
7500
7501 /* First, let's see if we want to capture this cookie. We check
7502 * that we don't already have a server side cookie, because we
7503 * can only capture one. Also as an optimisation, we ignore
7504 * cookies shorter than the declared name.
7505 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007506 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007507 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007508 (val_end - att_beg >= sess->fe->capture_namelen) &&
7509 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007510 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007511 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007512 Alert("HTTP logging : out of memory.\n");
7513 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007514 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007515 if (log_len > sess->fe->capture_len)
7516 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01007517 memcpy(txn->srv_cookie, att_beg, log_len);
7518 txn->srv_cookie[log_len] = 0;
7519 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007520 }
7521
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007522 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007523 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007524 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007525 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7526 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007527 /* assume passive cookie by default */
7528 txn->flags &= ~TX_SCK_MASK;
7529 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007530
7531 /* If the cookie is in insert mode on a known server, we'll delete
7532 * this occurrence because we'll insert another one later.
7533 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007534 * a direct access.
7535 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007536 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007537 /* The "preserve" flag was set, we don't want to touch the
7538 * server's cookie.
7539 */
7540 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007541 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007542 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007543 /* this cookie must be deleted */
7544 if (*prev == ':' && next == hdr_end) {
7545 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007546 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007547 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7548 txn->hdr_idx.used--;
7549 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007550 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007551 hdr_next += delta;
7552 http_msg_move_end(&txn->rsp, delta);
7553 /* note: while both invalid now, <next> and <hdr_end>
7554 * are still equal, so the for() will stop as expected.
7555 */
7556 } else {
7557 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007558 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007559 next = prev;
7560 hdr_end += delta;
7561 hdr_next += delta;
7562 cur_hdr->len += delta;
7563 http_msg_move_end(&txn->rsp, delta);
7564 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007565 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007566 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007567 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007568 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007569 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007570 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007571 * with this server since we know it.
7572 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007573 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007574 next += delta;
7575 hdr_end += delta;
7576 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007577 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007578 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007579
Willy Tarreauf1348312010-10-07 15:54:11 +02007580 txn->flags &= ~TX_SCK_MASK;
7581 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007582 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007583 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007584 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007585 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007586 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007587 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007588 next += delta;
7589 hdr_end += delta;
7590 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007591 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007592 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007593
Willy Tarreau827aee92011-03-10 16:55:02 +01007594 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007595 txn->flags &= ~TX_SCK_MASK;
7596 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007597 }
7598 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007599 /* that's done for this cookie, check the next one on the same
7600 * line when next != hdr_end (only if is_cookie2).
7601 */
7602 }
7603 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007604 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007605 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007606}
7607
7608
Willy Tarreaua15645d2007-03-18 16:22:39 +01007609/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007610 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007611 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007612void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007613{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007614 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007615 char *p1, *p2;
7616
7617 char *cur_ptr, *cur_end, *cur_next;
7618 int cur_idx;
7619
Willy Tarreau5df51872007-11-25 16:20:08 +01007620 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007621 return;
7622
7623 /* Iterate through the headers.
7624 * we start with the start line.
7625 */
7626 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007627 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007628
7629 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7630 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007631 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007632
7633 cur_hdr = &txn->hdr_idx.v[cur_idx];
7634 cur_ptr = cur_next;
7635 cur_end = cur_ptr + cur_hdr->len;
7636 cur_next = cur_end + cur_hdr->cr + 1;
7637
7638 /* We have one full header between cur_ptr and cur_end, and the
7639 * next header starts at cur_next. We're only interested in
7640 * "Cookie:" headers.
7641 */
7642
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007643 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7644 if (val) {
7645 if ((cur_end - (cur_ptr + val) >= 8) &&
7646 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7647 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7648 return;
7649 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007650 }
7651
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007652 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7653 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007654 continue;
7655
7656 /* OK, right now we know we have a cache-control header at cur_ptr */
7657
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007658 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007659
7660 if (p1 >= cur_end) /* no more info */
7661 continue;
7662
7663 /* p1 is at the beginning of the value */
7664 p2 = p1;
7665
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007666 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007667 p2++;
7668
7669 /* we have a complete value between p1 and p2 */
7670 if (p2 < cur_end && *p2 == '=') {
7671 /* we have something of the form no-cache="set-cookie" */
7672 if ((cur_end - p1 >= 21) &&
7673 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7674 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007675 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007676 continue;
7677 }
7678
7679 /* OK, so we know that either p2 points to the end of string or to a comma */
7680 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007681 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01007682 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7683 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7684 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007685 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007686 return;
7687 }
7688
7689 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007690 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007691 continue;
7692 }
7693 }
7694}
7695
Willy Tarreau58f10d72006-12-04 02:26:12 +01007696
Willy Tarreaub2513902006-12-17 14:52:38 +01007697/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007698 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007699 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007700 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007701 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007702 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007703 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007704 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007705 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007706int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007707{
7708 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007709 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007710 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007711
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007712 if (!uri_auth)
7713 return 0;
7714
Cyril Bonté70be45d2010-10-12 00:14:35 +02007715 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007716 return 0;
7717
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007718 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007719 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007720 return 0;
7721
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007722 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007723 return 0;
7724
Willy Tarreaub2513902006-12-17 14:52:38 +01007725 return 1;
7726}
7727
Willy Tarreau4076a152009-04-02 15:18:36 +02007728/*
7729 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007730 * By default it tries to report the error position as msg->err_pos. However if
7731 * this one is not set, it will then report msg->next, which is the last known
7732 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007733 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007734 */
Emeric Brun8c1aaa22017-06-15 11:30:06 +02007735void http_capture_bad_message(struct proxy *proxy, struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007736 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007737 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007738{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007739 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007740 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007741 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007742
Emeric Brun8c1aaa22017-06-15 11:30:06 +02007743 SPIN_LOCK(PROXY_LOCK, &proxy->lock);
Willy Tarreauf3764b72016-03-31 13:45:10 +02007744 es->len = MIN(chn->buf->i, global.tune.bufsize);
Willy Tarreau9b28e032012-10-12 23:49:43 +02007745 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007746 len1 = MIN(len1, es->len);
7747 len2 = es->len - len1; /* remaining data if buffer wraps */
7748
Willy Tarreauf3764b72016-03-31 13:45:10 +02007749 if (!es->buf)
7750 es->buf = malloc(global.tune.bufsize);
7751
7752 if (es->buf) {
7753 memcpy(es->buf, chn->buf->p, len1);
7754 if (len2)
7755 memcpy(es->buf + len1, chn->buf->data, len2);
7756 }
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007757
Willy Tarreau4076a152009-04-02 15:18:36 +02007758 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007759 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007760 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007761 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007762
Willy Tarreau4076a152009-04-02 15:18:36 +02007763 es->when = date; // user-visible date
7764 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007765 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007766 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007767 if (objt_conn(sess->origin))
7768 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007769 else
7770 memset(&es->src, 0, sizeof(es->src));
7771
Willy Tarreau078272e2010-12-12 12:46:33 +01007772 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007773 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007774 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007775 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007776 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007777 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007778 es->b_out = chn->buf->o;
7779 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007780 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007781 es->m_clen = msg->chunk_len;
7782 es->m_blen = msg->body_len;
Emeric Brun8c1aaa22017-06-15 11:30:06 +02007783 SPIN_UNLOCK(PROXY_LOCK, &proxy->lock);
Willy Tarreau4076a152009-04-02 15:18:36 +02007784}
Willy Tarreaub2513902006-12-17 14:52:38 +01007785
Willy Tarreau294c4732011-12-16 21:35:50 +01007786/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7787 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7788 * performed over the whole headers. Otherwise it must contain a valid header
7789 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7790 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7791 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7792 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007793 * -1. The value fetch stops at commas, so this function is suited for use with
7794 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007795 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007796 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007797unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007798 struct hdr_idx *idx, int occ,
7799 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007800{
Willy Tarreau294c4732011-12-16 21:35:50 +01007801 struct hdr_ctx local_ctx;
7802 char *ptr_hist[MAX_HDR_HISTORY];
7803 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007804 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007805 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007806
Willy Tarreau294c4732011-12-16 21:35:50 +01007807 if (!ctx) {
7808 local_ctx.idx = 0;
7809 ctx = &local_ctx;
7810 }
7811
Willy Tarreaubce70882009-09-07 11:51:47 +02007812 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007813 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007814 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007815 occ--;
7816 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007817 *vptr = ctx->line + ctx->val;
7818 *vlen = ctx->vlen;
7819 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007820 }
7821 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007822 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007823 }
7824
7825 /* negative occurrence, we scan all the list then walk back */
7826 if (-occ > MAX_HDR_HISTORY)
7827 return 0;
7828
Willy Tarreau294c4732011-12-16 21:35:50 +01007829 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007830 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007831 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7832 len_hist[hist_ptr] = ctx->vlen;
7833 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007834 hist_ptr = 0;
7835 found++;
7836 }
7837 if (-occ > found)
7838 return 0;
7839 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02007840 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7841 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7842 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02007843 */
Willy Tarreau67dad272013-06-12 22:27:44 +02007844 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02007845 if (hist_ptr >= MAX_HDR_HISTORY)
7846 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007847 *vptr = ptr_hist[hist_ptr];
7848 *vlen = len_hist[hist_ptr];
7849 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007850}
7851
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007852/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7853 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7854 * performed over the whole headers. Otherwise it must contain a valid header
7855 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7856 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7857 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7858 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7859 * -1. This function differs from http_get_hdr() in that it only returns full
7860 * line header values and does not stop at commas.
7861 * The return value is 0 if nothing was found, or non-zero otherwise.
7862 */
7863unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
7864 struct hdr_idx *idx, int occ,
7865 struct hdr_ctx *ctx, char **vptr, int *vlen)
7866{
7867 struct hdr_ctx local_ctx;
7868 char *ptr_hist[MAX_HDR_HISTORY];
7869 int len_hist[MAX_HDR_HISTORY];
7870 unsigned int hist_ptr;
7871 int found;
7872
7873 if (!ctx) {
7874 local_ctx.idx = 0;
7875 ctx = &local_ctx;
7876 }
7877
7878 if (occ >= 0) {
7879 /* search from the beginning */
7880 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
7881 occ--;
7882 if (occ <= 0) {
7883 *vptr = ctx->line + ctx->val;
7884 *vlen = ctx->vlen;
7885 return 1;
7886 }
7887 }
7888 return 0;
7889 }
7890
7891 /* negative occurrence, we scan all the list then walk back */
7892 if (-occ > MAX_HDR_HISTORY)
7893 return 0;
7894
7895 found = hist_ptr = 0;
7896 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
7897 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7898 len_hist[hist_ptr] = ctx->vlen;
7899 if (++hist_ptr >= MAX_HDR_HISTORY)
7900 hist_ptr = 0;
7901 found++;
7902 }
7903 if (-occ > found)
7904 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007905
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007906 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007907 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7908 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7909 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007910 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007911 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007912 if (hist_ptr >= MAX_HDR_HISTORY)
7913 hist_ptr -= MAX_HDR_HISTORY;
7914 *vptr = ptr_hist[hist_ptr];
7915 *vlen = len_hist[hist_ptr];
7916 return 1;
7917}
7918
Willy Tarreaubaaee002006-06-26 02:48:02 +02007919/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007920 * Print a debug line with a header. Always stop at the first CR or LF char,
7921 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7922 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007923 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007924void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007925{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007926 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007927 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007928
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007929 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007930 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02007931 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
7932 objt_conn(s->si[1].end) ? (unsigned short)objt_conn(s->si[1].end)->handle.fd : -1);
Willy Tarreaue92693a2012-09-24 21:13:39 +02007933
7934 for (max = 0; start + max < end; max++)
7935 if (start[max] == '\r' || start[max] == '\n')
7936 break;
7937
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007938 UBOUND(max, trash.size - trash.len - 3);
7939 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
7940 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01007941 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007942}
7943
Willy Tarreaueee5b512015-04-03 23:46:31 +02007944
7945/* Allocate a new HTTP transaction for stream <s> unless there is one already.
7946 * The hdr_idx is allocated as well. In case of allocation failure, everything
7947 * allocated is freed and NULL is returned. Otherwise the new transaction is
7948 * assigned to the stream and returned.
7949 */
7950struct http_txn *http_alloc_txn(struct stream *s)
7951{
7952 struct http_txn *txn = s->txn;
7953
7954 if (txn)
7955 return txn;
7956
7957 txn = pool_alloc2(pool2_http_txn);
7958 if (!txn)
7959 return txn;
7960
7961 txn->hdr_idx.size = global.tune.max_http_hdr;
7962 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
7963 if (!txn->hdr_idx.v) {
7964 pool_free2(pool2_http_txn, txn);
7965 return NULL;
7966 }
7967
7968 s->txn = txn;
7969 return txn;
7970}
7971
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007972void http_txn_reset_req(struct http_txn *txn)
7973{
7974 txn->req.flags = 0;
7975 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
7976 txn->req.next = 0;
7977 txn->req.chunk_len = 0LL;
7978 txn->req.body_len = 0LL;
7979 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7980}
7981
7982void http_txn_reset_res(struct http_txn *txn)
7983{
7984 txn->rsp.flags = 0;
7985 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
7986 txn->rsp.next = 0;
7987 txn->rsp.chunk_len = 0LL;
7988 txn->rsp.body_len = 0LL;
7989 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
7990}
7991
Willy Tarreau0937bc42009-12-22 15:03:09 +01007992/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007993 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01007994 * the required fields are properly allocated and that we only need to (re)init
7995 * them. This should be used before processing any new request.
7996 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007997void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007998{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007999 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008000 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008001
8002 txn->flags = 0;
8003 txn->status = -1;
8004
Willy Tarreauf64d1412010-10-07 20:06:11 +02008005 txn->cookie_first_date = 0;
8006 txn->cookie_last_date = 0;
8007
Willy Tarreaueee5b512015-04-03 23:46:31 +02008008 txn->srv_cookie = NULL;
8009 txn->cli_cookie = NULL;
8010 txn->uri = NULL;
8011
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008012 http_txn_reset_req(txn);
8013 http_txn_reset_res(txn);
8014
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008015 txn->req.chn = &s->req;
8016 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008017
8018 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008019
8020 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8021 if (fe->options2 & PR_O2_REQBUG_OK)
8022 txn->req.err_pos = -1; /* let buggy requests pass */
8023
Willy Tarreau0937bc42009-12-22 15:03:09 +01008024 if (txn->hdr_idx.v)
8025 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008026
8027 vars_init(&s->vars_txn, SCOPE_TXN);
8028 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008029}
8030
8031/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008032void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008033{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008034 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008035 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008036
8037 /* these ones will have been dynamically allocated */
8038 pool_free2(pool2_requri, txn->uri);
8039 pool_free2(pool2_capture, txn->cli_cookie);
8040 pool_free2(pool2_capture, txn->srv_cookie);
William Lallemanda73203e2012-03-12 12:48:57 +01008041 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008042
William Lallemanda73203e2012-03-12 12:48:57 +01008043 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008044 txn->uri = NULL;
8045 txn->srv_cookie = NULL;
8046 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008047
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008048 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008049 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008050 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008051 pool_free2(h->pool, s->req_cap[h->index]);
8052 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008053 }
8054
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008055 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008056 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008057 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008058 pool_free2(h->pool, s->res_cap[h->index]);
8059 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008060 }
8061
Willy Tarreau6204cd92016-03-10 16:33:04 +01008062 vars_prune(&s->vars_txn, s->sess, s);
8063 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008064}
8065
8066/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008067void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008068{
8069 http_end_txn(s);
8070 http_init_txn(s);
8071
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008072 /* reinitialise the current rule list pointer to NULL. We are sure that
8073 * any rulelist match the NULL pointer.
8074 */
8075 s->current_rule_list = NULL;
8076
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008077 s->be = strm_fe(s);
8078 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008079 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008080 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008081 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008082 /* re-init store persistence */
8083 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008084 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008085
Willy Tarreau0937bc42009-12-22 15:03:09 +01008086 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008087
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008088 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008089
Willy Tarreau739cfba2010-01-25 23:11:14 +01008090 /* We must trim any excess data from the response buffer, because we
8091 * may have blocked an invalid response from a server that we don't
8092 * want to accidentely forward once we disable the analysers, nor do
8093 * we want those data to come along with next response. A typical
8094 * example of such data would be from a buggy server responding to
8095 * a HEAD with some data, or sending more than the advertised
8096 * content-length.
8097 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008098 if (unlikely(s->res.buf->i))
8099 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008100
Christopher Fauletc0c672a2017-03-28 11:51:33 +02008101 /* Now we can realign the response buffer */
8102 buffer_realign(s->res.buf);
8103
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008104 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008105 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008106
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008107 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008108 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008109
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008110 s->req.rex = TICK_ETERNITY;
8111 s->req.wex = TICK_ETERNITY;
8112 s->req.analyse_exp = TICK_ETERNITY;
8113 s->res.rex = TICK_ETERNITY;
8114 s->res.wex = TICK_ETERNITY;
8115 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01008116 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008117}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008118
Sasha Pachev218f0642014-06-16 12:05:59 -06008119void free_http_res_rules(struct list *r)
8120{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008121 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06008122
8123 list_for_each_entry_safe(pr, tr, r, list) {
8124 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008125 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008126 free(pr);
8127 }
8128}
8129
8130void free_http_req_rules(struct list *r)
8131{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008132 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01008133
8134 list_for_each_entry_safe(pr, tr, r, list) {
8135 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008136 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008137 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008138
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008139 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008140 free(pr);
8141 }
8142}
8143
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008144/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008145struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01008146{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008147 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008148 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008149 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008150 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008151
Vincent Bernat02779b62016-04-03 13:48:43 +02008152 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01008153 if (!rule) {
8154 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008155 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008156 }
8157
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008158 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008159 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01008160 cur_arg = 1;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008161 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block") || !strcmp(args[0], "tarpit")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04008162 int code;
8163 int hc;
8164
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008165 if (!strcmp(args[0], "tarpit")) {
8166 rule->action = ACT_HTTP_REQ_TARPIT;
8167 rule->deny_status = HTTP_ERR_500;
8168 }
8169 else {
8170 rule->action = ACT_ACTION_DENY;
8171 rule->deny_status = HTTP_ERR_403;
8172 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008173 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04008174 if (strcmp(args[cur_arg], "deny_status") == 0) {
8175 cur_arg++;
8176 if (!args[cur_arg]) {
8177 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
8178 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
8179 goto out_err;
8180 }
8181
8182 code = atol(args[cur_arg]);
8183 cur_arg++;
8184 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
8185 if (http_err_codes[hc] == code) {
8186 rule->deny_status = hc;
8187 break;
8188 }
8189 }
8190
8191 if (hc >= HTTP_ERR_SIZE) {
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008192 Warning("parsing [%s:%d] : status code %d not handled, using default code %d.\n",
8193 file, linenum, code, http_err_codes[rule->deny_status]);
CJ Ess108b1dd2015-04-07 12:03:37 -04008194 }
8195 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008196 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008197 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008198 cur_arg = 1;
8199
8200 while(*args[cur_arg]) {
8201 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008202 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008203 cur_arg+=2;
8204 continue;
8205 } else
8206 break;
8207 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008208 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008209 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008210 cur_arg = 1;
8211
8212 if (!*args[cur_arg] ||
8213 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8214 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8215 file, linenum, args[0]);
8216 goto out_err;
8217 }
8218 rule->arg.nice = atoi(args[cur_arg]);
8219 if (rule->arg.nice < -1024)
8220 rule->arg.nice = -1024;
8221 else if (rule->arg.nice > 1024)
8222 rule->arg.nice = 1024;
8223 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008224 } else if (!strcmp(args[0], "set-tos")) {
8225#ifdef IP_TOS
8226 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008227 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008228 cur_arg = 1;
8229
8230 if (!*args[cur_arg] ||
8231 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8232 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8233 file, linenum, args[0]);
8234 goto out_err;
8235 }
8236
8237 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8238 if (err && *err != '\0') {
8239 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8240 file, linenum, err, args[0]);
8241 goto out_err;
8242 }
8243 cur_arg++;
8244#else
8245 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8246 goto out_err;
8247#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008248 } else if (!strcmp(args[0], "set-mark")) {
8249#ifdef SO_MARK
8250 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008251 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008252 cur_arg = 1;
8253
8254 if (!*args[cur_arg] ||
8255 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8256 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8257 file, linenum, args[0]);
8258 goto out_err;
8259 }
8260
8261 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8262 if (err && *err != '\0') {
8263 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8264 file, linenum, err, args[0]);
8265 goto out_err;
8266 }
8267 cur_arg++;
8268 global.last_checks |= LSTCHK_NETADM;
8269#else
8270 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8271 goto out_err;
8272#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008273 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008274 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008275 cur_arg = 1;
8276
8277 if (!*args[cur_arg] ||
8278 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8279 bad_log_level:
8280 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8281 file, linenum, args[0]);
8282 goto out_err;
8283 }
8284 if (strcmp(args[cur_arg], "silent") == 0)
8285 rule->arg.loglevel = -1;
8286 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8287 goto bad_log_level;
8288 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008289 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008290 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008291 cur_arg = 1;
8292
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008293 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8294 (*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 +01008295 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8296 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008297 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008298 }
8299
8300 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8301 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8302 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008303
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008304 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008305 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008306 if (!parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008307 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8308 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8309 file, linenum, args[0], error);
8310 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008311 goto out_err;
8312 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008313 free(proxy->conf.lfs_file);
8314 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8315 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008316 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02008317 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008318 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008319 cur_arg = 1;
8320
8321 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02008322 (*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 -06008323 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
8324 file, linenum, args[0]);
8325 goto out_err;
8326 }
8327
8328 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8329 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8330 LIST_INIT(&rule->arg.hdr_add.fmt);
8331
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008332 error = NULL;
8333 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
8334 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8335 args[cur_arg + 1], error);
8336 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008337 goto out_err;
8338 }
8339
8340 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008341 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008342 if (!parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008343 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8344 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8345 file, linenum, args[0], error);
8346 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008347 goto out_err;
8348 }
Sasha Pachev218f0642014-06-16 12:05:59 -06008349
8350 free(proxy->conf.lfs_file);
8351 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8352 proxy->conf.lfs_line = proxy->conf.args.line;
8353 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008354 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008355 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008356 cur_arg = 1;
8357
8358 if (!*args[cur_arg] ||
8359 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8360 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8361 file, linenum, args[0]);
8362 goto out_err;
8363 }
8364
8365 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8366 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8367
8368 proxy->conf.args.ctx = ARGC_HRQ;
8369 free(proxy->conf.lfs_file);
8370 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8371 proxy->conf.lfs_line = proxy->conf.args.line;
8372 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02008373 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
8374 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02008375 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02008376 struct sample_expr *expr;
8377 unsigned int where;
8378 char *err = NULL;
8379
8380 cur_arg = 1;
8381 proxy->conf.args.ctx = ARGC_TRK;
8382
8383 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
8384 if (!expr) {
8385 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8386 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
8387 free(err);
8388 goto out_err;
8389 }
8390
8391 where = 0;
8392 if (proxy->cap & PR_CAP_FE)
8393 where |= SMP_VAL_FE_HRQ_HDR;
8394 if (proxy->cap & PR_CAP_BE)
8395 where |= SMP_VAL_BE_HRQ_HDR;
8396
8397 if (!(expr->fetch->val & where)) {
8398 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
8399 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
8400 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
8401 args[cur_arg-1], sample_src_names(expr->fetch->use));
8402 free(expr);
8403 goto out_err;
8404 }
8405
8406 if (strcmp(args[cur_arg], "table") == 0) {
8407 cur_arg++;
8408 if (!args[cur_arg]) {
8409 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
8410 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
8411 free(expr);
8412 goto out_err;
8413 }
8414 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008415 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02008416 cur_arg++;
8417 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008418 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008419 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Christopher Faulet78880fb2017-09-18 14:43:55 +02008420 rule->check_ptr = check_trk_action;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008421 } else if (strcmp(args[0], "redirect") == 0) {
8422 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008423 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008424
Willy Tarreaube4653b2015-05-28 15:26:58 +02008425 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01008426 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8427 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8428 goto out_err;
8429 }
8430
8431 /* this redirect rule might already contain a parsed condition which
8432 * we'll pass to the http-request rule.
8433 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008434 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008435 rule->arg.redir = redir;
8436 rule->cond = redir->cond;
8437 redir->cond = NULL;
8438 cur_arg = 2;
8439 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008440 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8441 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008442 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008443 /*
8444 * '+ 8' for 'add-acl('
8445 * '- 9' for 'add-acl(' + trailing ')'
8446 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008447 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008448
8449 cur_arg = 1;
8450
8451 if (!*args[cur_arg] ||
8452 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8453 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8454 file, linenum, args[0]);
8455 goto out_err;
8456 }
8457
8458 LIST_INIT(&rule->arg.map.key);
8459 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008460 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008461 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008462 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8463 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8464 file, linenum, args[0], error);
8465 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008466 goto out_err;
8467 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008468 free(proxy->conf.lfs_file);
8469 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8470 proxy->conf.lfs_line = proxy->conf.args.line;
8471 cur_arg += 1;
8472 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8473 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008474 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008475 /*
8476 * '+ 8' for 'del-acl('
8477 * '- 9' for 'del-acl(' + trailing ')'
8478 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008479 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008480
8481 cur_arg = 1;
8482
8483 if (!*args[cur_arg] ||
8484 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8485 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8486 file, linenum, args[0]);
8487 goto out_err;
8488 }
8489
8490 LIST_INIT(&rule->arg.map.key);
8491 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008492 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008493 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008494 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8495 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8496 file, linenum, args[0], error);
8497 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008498 goto out_err;
8499 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008500 free(proxy->conf.lfs_file);
8501 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8502 proxy->conf.lfs_line = proxy->conf.args.line;
8503 cur_arg += 1;
8504 } else if (strncmp(args[0], "del-map", 7) == 0) {
8505 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008506 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008507 /*
8508 * '+ 8' for 'del-map('
8509 * '- 9' for 'del-map(' + trailing ')'
8510 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008511 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008512
8513 cur_arg = 1;
8514
8515 if (!*args[cur_arg] ||
8516 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8517 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8518 file, linenum, args[0]);
8519 goto out_err;
8520 }
8521
8522 LIST_INIT(&rule->arg.map.key);
8523 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008524 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008525 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008526 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8527 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8528 file, linenum, args[0], error);
8529 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008530 goto out_err;
8531 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008532 free(proxy->conf.lfs_file);
8533 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8534 proxy->conf.lfs_line = proxy->conf.args.line;
8535 cur_arg += 1;
8536 } else if (strncmp(args[0], "set-map", 7) == 0) {
8537 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008538 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008539 /*
8540 * '+ 8' for 'set-map('
8541 * '- 9' for 'set-map(' + trailing ')'
8542 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008543 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008544
8545 cur_arg = 1;
8546
8547 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8548 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8549 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8550 file, linenum, args[0]);
8551 goto out_err;
8552 }
8553
8554 LIST_INIT(&rule->arg.map.key);
8555 LIST_INIT(&rule->arg.map.value);
8556 proxy->conf.args.ctx = ARGC_HRQ;
8557
8558 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008559 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008560 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008561 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8562 Alert("parsing [%s:%d]: 'http-request %s' key: %s.\n",
8563 file, linenum, args[0], error);
8564 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008565 goto out_err;
8566 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008567
8568 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008569 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008570 if (!parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008571 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8572 Alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n",
8573 file, linenum, args[0], error);
8574 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008575 goto out_err;
8576 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008577 free(proxy->conf.lfs_file);
8578 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8579 proxy->conf.lfs_line = proxy->conf.args.line;
8580
8581 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02008582 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
8583 char *errmsg = NULL;
8584 cur_arg = 1;
8585 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02008586 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02008587 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02008588 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02008589 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8590 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8591 free(errmsg);
8592 goto out_err;
8593 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008594 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02008595 action_build_list(&http_req_keywords.list, &trash);
8596 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
8597 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08008598 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02008599 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02008600 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008601 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008602 }
8603
8604 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8605 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008606 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008607
Christopher Faulet1b421ea2017-09-22 14:38:56 +02008608 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008609 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8610 file, linenum, args[0], errmsg);
8611 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008612 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008613 }
8614 rule->cond = cond;
8615 }
8616 else if (*args[cur_arg]) {
8617 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8618 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8619 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008620 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008621 }
8622
8623 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008624 out_err:
8625 free(rule);
8626 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008627}
8628
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008629/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008630struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008631{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008632 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008633 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008634 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008635 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008636
8637 rule = calloc(1, sizeof(*rule));
8638 if (!rule) {
8639 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
8640 goto out_err;
8641 }
8642
8643 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008644 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008645 cur_arg = 1;
8646 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008647 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008648 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008649 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008650 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008651 cur_arg = 1;
8652
8653 if (!*args[cur_arg] ||
8654 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8655 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8656 file, linenum, args[0]);
8657 goto out_err;
8658 }
8659 rule->arg.nice = atoi(args[cur_arg]);
8660 if (rule->arg.nice < -1024)
8661 rule->arg.nice = -1024;
8662 else if (rule->arg.nice > 1024)
8663 rule->arg.nice = 1024;
8664 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008665 } else if (!strcmp(args[0], "set-tos")) {
8666#ifdef IP_TOS
8667 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008668 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008669 cur_arg = 1;
8670
8671 if (!*args[cur_arg] ||
8672 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8673 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8674 file, linenum, args[0]);
8675 goto out_err;
8676 }
8677
8678 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8679 if (err && *err != '\0') {
8680 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8681 file, linenum, err, args[0]);
8682 goto out_err;
8683 }
8684 cur_arg++;
8685#else
8686 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8687 goto out_err;
8688#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008689 } else if (!strcmp(args[0], "set-mark")) {
8690#ifdef SO_MARK
8691 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008692 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008693 cur_arg = 1;
8694
8695 if (!*args[cur_arg] ||
8696 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8697 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8698 file, linenum, args[0]);
8699 goto out_err;
8700 }
8701
8702 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8703 if (err && *err != '\0') {
8704 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8705 file, linenum, err, args[0]);
8706 goto out_err;
8707 }
8708 cur_arg++;
8709 global.last_checks |= LSTCHK_NETADM;
8710#else
8711 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8712 goto out_err;
8713#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008714 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008715 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008716 cur_arg = 1;
8717
8718 if (!*args[cur_arg] ||
8719 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8720 bad_log_level:
8721 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8722 file, linenum, args[0]);
8723 goto out_err;
8724 }
8725 if (strcmp(args[cur_arg], "silent") == 0)
8726 rule->arg.loglevel = -1;
Ruoshan Huangdd016782016-06-15 22:16:03 +08008727 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008728 goto bad_log_level;
8729 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008730 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008731 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008732 cur_arg = 1;
8733
8734 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8735 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8736 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8737 file, linenum, args[0]);
8738 goto out_err;
8739 }
8740
8741 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8742 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8743 LIST_INIT(&rule->arg.hdr_add.fmt);
8744
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008745 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008746 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008747 if (!parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008748 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8749 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8750 file, linenum, args[0], error);
8751 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008752 goto out_err;
8753 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008754 free(proxy->conf.lfs_file);
8755 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8756 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008757 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06008758 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008759 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008760 cur_arg = 1;
8761
8762 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02008763 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
8764 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06008765 file, linenum, args[0]);
8766 goto out_err;
8767 }
8768
8769 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8770 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8771 LIST_INIT(&rule->arg.hdr_add.fmt);
8772
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008773 error = NULL;
8774 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
8775 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8776 args[cur_arg + 1], error);
8777 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008778 goto out_err;
8779 }
8780
8781 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008782 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008783 if (!parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008784 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8785 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8786 file, linenum, args[0], error);
8787 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008788 goto out_err;
8789 }
Sasha Pachev218f0642014-06-16 12:05:59 -06008790
8791 free(proxy->conf.lfs_file);
8792 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8793 proxy->conf.lfs_line = proxy->conf.args.line;
8794 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008795 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008796 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008797 cur_arg = 1;
8798
8799 if (!*args[cur_arg] ||
8800 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8801 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8802 file, linenum, args[0]);
8803 goto out_err;
8804 }
8805
8806 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8807 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8808
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008809 proxy->conf.args.ctx = ARGC_HRS;
8810 free(proxy->conf.lfs_file);
8811 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8812 proxy->conf.lfs_line = proxy->conf.args.line;
8813 cur_arg += 1;
8814 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8815 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008816 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008817 /*
8818 * '+ 8' for 'add-acl('
8819 * '- 9' for 'add-acl(' + trailing ')'
8820 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008821 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008822
8823 cur_arg = 1;
8824
8825 if (!*args[cur_arg] ||
8826 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8827 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8828 file, linenum, args[0]);
8829 goto out_err;
8830 }
8831
8832 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008833 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008834 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008835 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008836 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8837 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8838 file, linenum, args[0], error);
8839 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008840 goto out_err;
8841 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008842 free(proxy->conf.lfs_file);
8843 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8844 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008845
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008846 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008847 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8848 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008849 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008850 /*
8851 * '+ 8' for 'del-acl('
8852 * '- 9' for 'del-acl(' + trailing ')'
8853 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008854 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008855
8856 cur_arg = 1;
8857
8858 if (!*args[cur_arg] ||
8859 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8860 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8861 file, linenum, args[0]);
8862 goto out_err;
8863 }
8864
8865 LIST_INIT(&rule->arg.map.key);
8866 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008867 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008868 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008869 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8870 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8871 file, linenum, args[0], error);
8872 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008873 goto out_err;
8874 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008875 free(proxy->conf.lfs_file);
8876 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8877 proxy->conf.lfs_line = proxy->conf.args.line;
8878 cur_arg += 1;
8879 } else if (strncmp(args[0], "del-map", 7) == 0) {
8880 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008881 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008882 /*
8883 * '+ 8' for 'del-map('
8884 * '- 9' for 'del-map(' + trailing ')'
8885 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008886 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008887
8888 cur_arg = 1;
8889
8890 if (!*args[cur_arg] ||
8891 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8892 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8893 file, linenum, args[0]);
8894 goto out_err;
8895 }
8896
8897 LIST_INIT(&rule->arg.map.key);
8898 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008899 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008900 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008901 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8902 Alert("parsing [%s:%d]: 'http-response %s' %s.\n",
8903 file, linenum, args[0], error);
8904 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008905 goto out_err;
8906 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008907 free(proxy->conf.lfs_file);
8908 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8909 proxy->conf.lfs_line = proxy->conf.args.line;
8910 cur_arg += 1;
8911 } else if (strncmp(args[0], "set-map", 7) == 0) {
8912 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008913 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008914 /*
8915 * '+ 8' for 'set-map('
8916 * '- 9' for 'set-map(' + trailing ')'
8917 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008918 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008919
8920 cur_arg = 1;
8921
8922 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8923 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8924 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8925 file, linenum, args[0]);
8926 goto out_err;
8927 }
8928
8929 LIST_INIT(&rule->arg.map.key);
8930 LIST_INIT(&rule->arg.map.value);
8931
8932 proxy->conf.args.ctx = ARGC_HRS;
8933
8934 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008935 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008936 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008937 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8938 Alert("parsing [%s:%d]: 'http-response %s' name: %s.\n",
8939 file, linenum, args[0], error);
8940 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008941 goto out_err;
8942 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008943
8944 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008945 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008946 if (!parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008947 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8948 Alert("parsing [%s:%d]: 'http-response %s' value: %s.\n",
8949 file, linenum, args[0], error);
8950 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008951 goto out_err;
8952 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008953
8954 free(proxy->conf.lfs_file);
8955 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8956 proxy->conf.lfs_line = proxy->conf.args.line;
8957
8958 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02008959 } else if (strcmp(args[0], "redirect") == 0) {
8960 struct redirect_rule *redir;
8961 char *errmsg = NULL;
8962
8963 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
8964 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
8965 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8966 goto out_err;
8967 }
8968
8969 /* this redirect rule might already contain a parsed condition which
8970 * we'll pass to the http-request rule.
8971 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008972 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02008973 rule->arg.redir = redir;
8974 rule->cond = redir->cond;
8975 redir->cond = NULL;
8976 cur_arg = 2;
8977 return rule;
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08008978 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
8979 args[0][9] == '\0' && args[0][8] >= '0' &&
8980 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
8981 struct sample_expr *expr;
8982 unsigned int where;
8983 char *err = NULL;
8984
8985 cur_arg = 1;
8986 proxy->conf.args.ctx = ARGC_TRK;
8987
8988 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
8989 if (!expr) {
8990 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
8991 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
8992 free(err);
8993 goto out_err;
8994 }
8995
8996 where = 0;
8997 if (proxy->cap & PR_CAP_FE)
8998 where |= SMP_VAL_FE_HRS_HDR;
8999 if (proxy->cap & PR_CAP_BE)
9000 where |= SMP_VAL_BE_HRS_HDR;
9001
9002 if (!(expr->fetch->val & where)) {
9003 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :"
9004 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9005 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9006 args[cur_arg-1], sample_src_names(expr->fetch->use));
9007 free(expr);
9008 goto out_err;
9009 }
9010
9011 if (strcmp(args[cur_arg], "table") == 0) {
9012 cur_arg++;
9013 if (!args[cur_arg]) {
9014 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n",
9015 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9016 free(expr);
9017 goto out_err;
9018 }
9019 /* we copy the table name for now, it will be resolved later */
9020 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
9021 cur_arg++;
9022 }
9023 rule->arg.trk_ctr.expr = expr;
9024 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Christopher Faulet78880fb2017-09-18 14:43:55 +02009025 rule->check_ptr = check_trk_action;
William Lallemand73025dd2014-04-24 14:38:37 +02009026 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9027 char *errmsg = NULL;
9028 cur_arg = 1;
9029 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009030 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009031 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009032 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009033 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9034 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9035 free(errmsg);
9036 goto out_err;
9037 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009038 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009039 action_build_list(&http_res_keywords.list, &trash);
9040 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9041 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009042 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02009043 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009044 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009045 goto out_err;
9046 }
9047
9048 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9049 struct acl_cond *cond;
9050 char *errmsg = NULL;
9051
Christopher Faulet1b421ea2017-09-22 14:38:56 +02009052 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009053 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9054 file, linenum, args[0], errmsg);
9055 free(errmsg);
9056 goto out_err;
9057 }
9058 rule->cond = cond;
9059 }
9060 else if (*args[cur_arg]) {
9061 Alert("parsing [%s:%d]: 'http-response %s' expects"
9062 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9063 file, linenum, args[0], args[cur_arg]);
9064 goto out_err;
9065 }
9066
9067 return rule;
9068 out_err:
9069 free(rule);
9070 return NULL;
9071}
9072
Willy Tarreau4baae242012-12-27 12:00:31 +01009073/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009074 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009075 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9076 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009077 */
9078struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009079 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009080{
9081 struct redirect_rule *rule;
9082 int cur_arg;
9083 int type = REDIRECT_TYPE_NONE;
9084 int code = 302;
9085 const char *destination = NULL;
9086 const char *cookie = NULL;
9087 int cookie_set = 0;
9088 unsigned int flags = REDIRECT_FLAG_NONE;
9089 struct acl_cond *cond = NULL;
9090
9091 cur_arg = 0;
9092 while (*(args[cur_arg])) {
9093 if (strcmp(args[cur_arg], "location") == 0) {
9094 if (!*args[cur_arg + 1])
9095 goto missing_arg;
9096
9097 type = REDIRECT_TYPE_LOCATION;
9098 cur_arg++;
9099 destination = args[cur_arg];
9100 }
9101 else if (strcmp(args[cur_arg], "prefix") == 0) {
9102 if (!*args[cur_arg + 1])
9103 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009104 type = REDIRECT_TYPE_PREFIX;
9105 cur_arg++;
9106 destination = args[cur_arg];
9107 }
9108 else if (strcmp(args[cur_arg], "scheme") == 0) {
9109 if (!*args[cur_arg + 1])
9110 goto missing_arg;
9111
9112 type = REDIRECT_TYPE_SCHEME;
9113 cur_arg++;
9114 destination = args[cur_arg];
9115 }
9116 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9117 if (!*args[cur_arg + 1])
9118 goto missing_arg;
9119
9120 cur_arg++;
9121 cookie = args[cur_arg];
9122 cookie_set = 1;
9123 }
9124 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9125 if (!*args[cur_arg + 1])
9126 goto missing_arg;
9127
9128 cur_arg++;
9129 cookie = args[cur_arg];
9130 cookie_set = 0;
9131 }
9132 else if (strcmp(args[cur_arg], "code") == 0) {
9133 if (!*args[cur_arg + 1])
9134 goto missing_arg;
9135
9136 cur_arg++;
9137 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009138 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009139 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009140 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009141 args[cur_arg - 1], args[cur_arg]);
9142 return NULL;
9143 }
9144 }
9145 else if (!strcmp(args[cur_arg],"drop-query")) {
9146 flags |= REDIRECT_FLAG_DROP_QS;
9147 }
9148 else if (!strcmp(args[cur_arg],"append-slash")) {
9149 flags |= REDIRECT_FLAG_APPEND_SLASH;
9150 }
9151 else if (strcmp(args[cur_arg], "if") == 0 ||
9152 strcmp(args[cur_arg], "unless") == 0) {
Christopher Faulet1b421ea2017-09-22 14:38:56 +02009153 cond = build_acl_cond(file, linenum, &proxy->acl, curproxy, (const char **)args + cur_arg, errmsg);
Willy Tarreau4baae242012-12-27 12:00:31 +01009154 if (!cond) {
9155 memprintf(errmsg, "error in condition: %s", *errmsg);
9156 return NULL;
9157 }
9158 break;
9159 }
9160 else {
9161 memprintf(errmsg,
9162 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9163 args[cur_arg]);
9164 return NULL;
9165 }
9166 cur_arg++;
9167 }
9168
9169 if (type == REDIRECT_TYPE_NONE) {
9170 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9171 return NULL;
9172 }
9173
Willy Tarreaube4653b2015-05-28 15:26:58 +02009174 if (dir && type != REDIRECT_TYPE_LOCATION) {
9175 memprintf(errmsg, "response only supports redirect type 'location'");
9176 return NULL;
9177 }
9178
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02009179 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +01009180 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009181 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009182
9183 if (!use_fmt) {
9184 /* old-style static redirect rule */
9185 rule->rdr_str = strdup(destination);
9186 rule->rdr_len = strlen(destination);
9187 }
9188 else {
9189 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009190
9191 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9192 * if prefix == "/", we don't want to add anything, otherwise it
9193 * makes it hard for the user to configure a self-redirection.
9194 */
Godbachd9722032014-12-18 15:44:58 +08009195 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009196 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009197 if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
9198 dir ? (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRS_HDR : SMP_VAL_BE_HRS_HDR
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009199 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9200 errmsg)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009201 return NULL;
9202 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009203 free(curproxy->conf.lfs_file);
9204 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9205 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009206 }
9207 }
9208
Willy Tarreau4baae242012-12-27 12:00:31 +01009209 if (cookie) {
9210 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9211 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9212 */
9213 rule->cookie_len = strlen(cookie);
9214 if (cookie_set) {
9215 rule->cookie_str = malloc(rule->cookie_len + 10);
9216 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9217 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9218 rule->cookie_len += 9;
9219 } else {
9220 rule->cookie_str = malloc(rule->cookie_len + 21);
9221 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9222 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9223 rule->cookie_len += 20;
9224 }
9225 }
9226 rule->type = type;
9227 rule->code = code;
9228 rule->flags = flags;
9229 LIST_INIT(&rule->list);
9230 return rule;
9231
9232 missing_arg:
9233 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9234 return NULL;
9235}
9236
Willy Tarreau8797c062007-05-07 00:55:35 +02009237/************************************************************************/
9238/* The code below is dedicated to ACL parsing and matching */
9239/************************************************************************/
9240
9241
Willy Tarreau14174bc2012-04-16 14:34:04 +02009242/* This function ensures that the prerequisites for an L7 fetch are ready,
9243 * which means that a request or response is ready. If some data is missing,
9244 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009245 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9246 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009247 *
9248 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009249 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9250 * decide whether or not an HTTP message is present ;
9251 * 0 if the requested data cannot be fetched or if it is certain that
9252 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009253 * 1 if an HTTP message is ready
9254 */
James Rosewell91a41cb2015-09-18 17:11:16 +01009255int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009256 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009257{
Willy Tarreau192252e2015-04-04 01:47:55 +02009258 struct http_txn *txn;
9259 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009260
Willy Tarreaube508f12016-03-10 11:47:01 +01009261 /* Note: it is possible that <s> is NULL when called before stream
9262 * initialization (eg: tcp-request connection), so this function is the
9263 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009264 */
Willy Tarreau192252e2015-04-04 01:47:55 +02009265 if (!s)
9266 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +01009267
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +02009268 if (!s->txn) {
9269 if (unlikely(!http_alloc_txn(s)))
9270 return 0; /* not enough memory */
9271 http_init_txn(s);
9272 }
Willy Tarreau192252e2015-04-04 01:47:55 +02009273 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +02009274 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009275
9276 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009277 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009278
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009279 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009280 /* If the buffer does not leave enough free space at the end,
9281 * we must first realign it.
9282 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009283 if (s->req.buf->p > s->req.buf->data &&
9284 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
9285 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +01009286
Willy Tarreau14174bc2012-04-16 14:34:04 +02009287 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009288 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009289 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009290
9291 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009292 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009293 http_msg_analyzer(msg, &txn->hdr_idx);
9294
9295 /* Still no valid request ? */
9296 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009297 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009298 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009299 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009300 }
9301 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009302 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009303 return 0;
9304 }
9305
9306 /* OK we just got a valid HTTP request. We have some minor
9307 * preparation to perform so that further checks can rely
9308 * on HTTP tests.
9309 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009310
9311 /* If the request was parsed but was too large, we must absolutely
9312 * return an error so that it is not processed. At the moment this
9313 * cannot happen, but if the parsers are to change in the future,
9314 * we want this check to be maintained.
9315 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009316 if (unlikely(s->req.buf->i + s->req.buf->p >
9317 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01009318 msg->err_state = msg->msg_state;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009319 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009320 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009321 return 1;
9322 }
9323
Willy Tarreau9b28e032012-10-12 23:49:43 +02009324 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009325 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02009326 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009327
Willy Tarreau506d0502013-07-06 13:29:24 +02009328 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9329 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009330 }
9331
Willy Tarreau506d0502013-07-06 13:29:24 +02009332 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009333 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009334 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009335
9336 /* otherwise everything's ready for the request */
9337 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009338 else {
9339 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009340 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9341 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009342 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009343 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009344 }
9345
9346 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009347 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009348 return 1;
9349}
Willy Tarreau8797c062007-05-07 00:55:35 +02009350
Willy Tarreau8797c062007-05-07 00:55:35 +02009351/* 1. Check on METHOD
9352 * We use the pre-parsed method if it is known, and store its number as an
9353 * integer. If it is unknown, we use the pointer and the length.
9354 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009355static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009356{
9357 int len, meth;
9358
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009359 len = strlen(text);
9360 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009361
9362 pattern->val.i = meth;
9363 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +02009364 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009365 pattern->len = len;
9366 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009367 else {
9368 pattern->ptr.str = NULL;
9369 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009370 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009371 return 1;
9372}
9373
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009374/* This function fetches the method of current HTTP request and stores
9375 * it in the global pattern struct as a chunk. There are two possibilities :
9376 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9377 * in <len> and <ptr> is NULL ;
9378 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9379 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009380 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009381 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009382static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009383smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009384{
9385 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +01009386 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009387
Willy Tarreau24e32d82012-04-23 23:55:44 +02009388 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009389
Willy Tarreaube508f12016-03-10 11:47:01 +01009390 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009391 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009392 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009393 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009394 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009395 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9396 /* ensure the indexes are not affected */
9397 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009398 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009399 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
9400 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009401 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009402 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009403 return 1;
9404}
9405
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009406/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009407static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009408{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009409 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009410 struct pattern_list *lst;
9411 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009412
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009413 list_for_each_entry(lst, &expr->patterns, list) {
9414 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009415
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009416 /* well-known method */
9417 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009418 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009419 return pattern;
9420 else
9421 continue;
9422 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009423
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009424 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009425 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009426 continue;
9427
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009428 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009429 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
9430 (!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 +01009431 return pattern;
9432 }
9433 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009434}
9435
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009436static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009437smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009438{
Willy Tarreaube508f12016-03-10 11:47:01 +01009439 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009440 char *ptr;
9441 int len;
9442
Willy Tarreauc0239e02012-04-16 14:42:55 +02009443 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009444
Willy Tarreaube508f12016-03-10 11:47:01 +01009445 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009446 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009447 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009448
9449 while ((len-- > 0) && (*ptr++ != '/'));
9450 if (len <= 0)
9451 return 0;
9452
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009453 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009454 smp->data.u.str.str = ptr;
9455 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009456
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009457 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009458 return 1;
9459}
9460
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009461static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009462smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009463{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009464 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009465 char *ptr;
9466 int len;
9467
Willy Tarreauc0239e02012-04-16 14:42:55 +02009468 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009469
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009470 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009471 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9472 return 0;
9473
Willy Tarreau8797c062007-05-07 00:55:35 +02009474 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009475 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009476
9477 while ((len-- > 0) && (*ptr++ != '/'));
9478 if (len <= 0)
9479 return 0;
9480
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009481 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009482 smp->data.u.str.str = ptr;
9483 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009484
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009485 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009486 return 1;
9487}
9488
9489/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009490static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009491smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009492{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009493 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009494 char *ptr;
9495 int len;
9496
Willy Tarreauc0239e02012-04-16 14:42:55 +02009497 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009498
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009499 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009500 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9501 return 0;
9502
Willy Tarreau8797c062007-05-07 00:55:35 +02009503 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009504 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009505
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009506 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009507 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009508 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009509 return 1;
9510}
9511
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009512static int
9513smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
9514{
9515 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
9516 return 0;
9517
9518 if (!smp->strm->unique_id) {
9519 if ((smp->strm->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
9520 return 0;
9521 smp->strm->unique_id[0] = '\0';
9522 }
9523 smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id,
9524 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
9525
9526 smp->data.type = SMP_T_STR;
9527 smp->data.u.str.str = smp->strm->unique_id;
9528 smp->flags = SMP_F_CONST;
9529 return 1;
9530}
9531
Thierry FOURNIERd7d88812017-04-19 15:15:14 +02009532/* Returns a string block containing all headers including the
9533 * empty line wich separes headers from the body. This is useful
9534 * form some headers analysis.
9535 */
9536static int
9537smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
9538{
9539 struct http_msg *msg;
9540 struct hdr_idx *idx;
9541 struct http_txn *txn;
9542
9543 CHECK_HTTP_MESSAGE_FIRST();
9544
9545 txn = smp->strm->txn;
9546 idx = &txn->hdr_idx;
9547 msg = &txn->req;
9548
9549 smp->data.type = SMP_T_STR;
9550 smp->data.u.str.str = msg->chn->buf->p + hdr_idx_first_pos(idx);
9551 smp->data.u.str.len = msg->eoh - hdr_idx_first_pos(idx) + 1 +
9552 (msg->chn->buf->p[msg->eoh] == '\r');
9553
9554 return 1;
9555}
9556
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009557/* Returns the header request in a length/value encoded format.
9558 * This is useful for exchanges with the SPOE.
9559 *
9560 * A "length value" is a multibyte code encoding numbers. It uses the
9561 * SPOE format. The encoding is the following:
9562 *
9563 * Each couple "header name" / "header value" is composed
9564 * like this:
9565 * "length value" "header name bytes"
9566 * "length value" "header value bytes"
9567 * When the last header is reached, the header name and the header
9568 * value are empty. Their length are 0
9569 */
9570static int
9571smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
9572{
9573 struct http_msg *msg;
9574 struct chunk *temp;
9575 struct hdr_idx *idx;
9576 const char *cur_ptr, *cur_next, *p;
9577 int old_idx, cur_idx;
9578 struct hdr_idx_elem *cur_hdr;
9579 const char *hn, *hv;
9580 int hnl, hvl;
9581 int ret;
9582 struct http_txn *txn;
9583 char *buf;
9584 char *end;
9585
9586 CHECK_HTTP_MESSAGE_FIRST();
9587
9588 temp = get_trash_chunk();
9589 buf = temp->str;
9590 end = temp->str + temp->size;
9591
9592 txn = smp->strm->txn;
9593 idx = &txn->hdr_idx;
9594 msg = &txn->req;
9595
9596 /* Build array of headers. */
9597 old_idx = 0;
9598 cur_next = msg->chn->buf->p + hdr_idx_first_pos(idx);
9599 while (1) {
9600 cur_idx = idx->v[old_idx].next;
9601 if (!cur_idx)
9602 break;
9603 old_idx = cur_idx;
9604
9605 cur_hdr = &idx->v[cur_idx];
9606 cur_ptr = cur_next;
9607 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
9608
9609 /* Now we have one full header at cur_ptr of len cur_hdr->len,
9610 * and the next header starts at cur_next. We'll check
9611 * this header in the list as well as against the default
9612 * rule.
9613 */
9614
9615 /* look for ': *'. */
9616 hn = cur_ptr;
9617 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
9618 if (p >= cur_ptr+cur_hdr->len)
9619 continue;
9620 hnl = p - hn;
9621 p++;
9622 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
9623 p++;
9624 if (p >= cur_ptr + cur_hdr->len)
9625 continue;
9626 hv = p;
9627 hvl = cur_ptr + cur_hdr->len-p;
9628
9629 /* encode the header name. */
9630 ret = encode_varint(hnl, &buf, end);
9631 if (ret == -1)
9632 return 0;
9633 if (buf + hnl > end)
9634 return 0;
9635 memcpy(buf, hn, hnl);
9636 buf += hnl;
9637
9638 /* encode and copy the value. */
9639 ret = encode_varint(hvl, &buf, end);
9640 if (ret == -1)
9641 return 0;
9642 if (buf + hvl > end)
9643 return 0;
9644 memcpy(buf, hv, hvl);
9645 buf += hvl;
9646 }
9647
9648 /* encode the end of the header list with empty
9649 * header name and header value.
9650 */
9651 ret = encode_varint(0, &buf, end);
9652 if (ret == -1)
9653 return 0;
9654 ret = encode_varint(0, &buf, end);
9655 if (ret == -1)
9656 return 0;
9657
9658 /* Initialise sample data which will be filled. */
9659 smp->data.type = SMP_T_BIN;
9660 smp->data.u.str.str = temp->str;
9661 smp->data.u.str.len = buf - temp->str;
9662 smp->data.u.str.size = temp->size;
9663
9664 return 1;
9665}
9666
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009667/* returns the longest available part of the body. This requires that the body
9668 * has been waited for using http-buffer-request.
9669 */
9670static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009671smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009672{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009673 struct http_msg *msg;
9674 unsigned long len;
9675 unsigned long block1;
9676 char *body;
9677 struct chunk *temp;
9678
9679 CHECK_HTTP_MESSAGE_FIRST();
9680
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009681 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009682 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009683 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009684 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009685
9686 len = http_body_bytes(msg);
9687 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
9688
9689 block1 = len;
9690 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
9691 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
9692
9693 if (block1 == len) {
9694 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009695 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009696 smp->data.u.str.str = body;
9697 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009698 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
9699 }
9700 else {
9701 /* buffer is wrapped, we need to defragment it */
9702 temp = get_trash_chunk();
9703 memcpy(temp->str, body, block1);
9704 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009705 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009706 smp->data.u.str.str = temp->str;
9707 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009708 smp->flags = SMP_F_VOL_TEST;
9709 }
9710 return 1;
9711}
9712
9713
9714/* returns the available length of the body. This requires that the body
9715 * has been waited for using http-buffer-request.
9716 */
9717static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009718smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009719{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009720 struct http_msg *msg;
9721
9722 CHECK_HTTP_MESSAGE_FIRST();
9723
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009724 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009725 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009726 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009727 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009728
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009729 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009730 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009731
9732 smp->flags = SMP_F_VOL_TEST;
9733 return 1;
9734}
9735
9736
9737/* returns the advertised length of the body, or the advertised size of the
9738 * chunks available in the buffer. This requires that the body has been waited
9739 * for using http-buffer-request.
9740 */
9741static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009742smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009743{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009744 struct http_msg *msg;
9745
9746 CHECK_HTTP_MESSAGE_FIRST();
9747
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009748 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009749 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009750 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009751 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009752
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009753 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009754 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009755
9756 smp->flags = SMP_F_VOL_TEST;
9757 return 1;
9758}
9759
9760
Willy Tarreau8797c062007-05-07 00:55:35 +02009761/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009762static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009763smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009764{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009765 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009766
Willy Tarreauc0239e02012-04-16 14:42:55 +02009767 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009768
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009769 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009770 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009771 smp->data.u.str.len = txn->req.sl.rq.u_l;
9772 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009773 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009774 return 1;
9775}
9776
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009777static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009778smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009779{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009780 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009781 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009782
Willy Tarreauc0239e02012-04-16 14:42:55 +02009783 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009784
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009785 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009786 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 +02009787 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009788 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009789
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009790 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009791 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009792 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009793 return 1;
9794}
9795
9796static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009797smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009798{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009799 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009800 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009801
Willy Tarreauc0239e02012-04-16 14:42:55 +02009802 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009803
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009804 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009805 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 +02009806 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
9807 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009808
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009809 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009810 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009811 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009812 return 1;
9813}
9814
Willy Tarreau185b5c42012-04-26 15:11:51 +02009815/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9816 * Accepts an optional argument of type string containing the header field name,
9817 * and an optional argument of type signed or unsigned integer to request an
9818 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009819 * headers are considered from the first one. It does not stop on commas and
9820 * returns full lines instead (useful for User-Agent or Date for example).
9821 */
9822static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009823smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009824{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009825 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009826 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +02009827 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009828 int occ = 0;
9829 const char *name_str = NULL;
9830 int name_len = 0;
9831
9832 if (!ctx) {
9833 /* first call */
9834 ctx = &static_hdr_ctx;
9835 ctx->idx = 0;
9836 smp->ctx.a[0] = ctx;
9837 }
9838
9839 if (args) {
9840 if (args[0].type != ARGT_STR)
9841 return 0;
9842 name_str = args[0].data.str.str;
9843 name_len = args[0].data.str.len;
9844
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02009845 if (args[1].type == ARGT_SINT)
9846 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009847 }
9848
9849 CHECK_HTTP_MESSAGE_FIRST();
9850
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009851 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009852 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 +02009853
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009854 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9855 /* search for header from the beginning */
9856 ctx->idx = 0;
9857
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009858 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009859 /* no explicit occurrence and single fetch => last header by default */
9860 occ = -1;
9861
9862 if (!occ)
9863 /* prepare to report multiple occurrences for ACL fetches */
9864 smp->flags |= SMP_F_NOT_LAST;
9865
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009866 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009867 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009868 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 +02009869 return 1;
9870
9871 smp->flags &= ~SMP_F_NOT_LAST;
9872 return 0;
9873}
9874
9875/* 6. Check on HTTP header count. The number of occurrences is returned.
9876 * Accepts exactly 1 argument of type string. It does not stop on commas and
9877 * returns full lines instead (useful for User-Agent or Date for example).
9878 */
9879static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009880smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009881{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009882 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009883 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +02009884 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009885 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +02009886 const char *name = NULL;
9887 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009888
Willy Tarreau601a4d12015-04-01 19:16:09 +02009889 if (args && args->type == ARGT_STR) {
9890 name = args->data.str.str;
9891 len = args->data.str.len;
9892 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009893
9894 CHECK_HTTP_MESSAGE_FIRST();
9895
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009896 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009897 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 +02009898
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009899 ctx.idx = 0;
9900 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +02009901 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009902 cnt++;
9903
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009904 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009905 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009906 smp->flags = SMP_F_VOL_HDR;
9907 return 1;
9908}
9909
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009910static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009911smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009912{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009913 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009914 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +02009915 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009916 struct chunk *temp;
9917 char del = ',';
9918
9919 if (args && args->type == ARGT_STR)
9920 del = *args[0].data.str.str;
9921
9922 CHECK_HTTP_MESSAGE_FIRST();
9923
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009924 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009925 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 +02009926
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009927 temp = get_trash_chunk();
9928
9929 ctx.idx = 0;
9930 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
9931 if (temp->len)
9932 temp->str[temp->len++] = del;
9933 memcpy(temp->str + temp->len, ctx.line, ctx.del);
9934 temp->len += ctx.del;
9935 }
9936
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009937 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009938 smp->data.u.str.str = temp->str;
9939 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009940 smp->flags = SMP_F_VOL_HDR;
9941 return 1;
9942}
9943
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009944/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9945 * Accepts an optional argument of type string containing the header field name,
9946 * and an optional argument of type signed or unsigned integer to request an
9947 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009948 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009949 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009950static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009951smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009952{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009953 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009954 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +02009955 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009956 int occ = 0;
9957 const char *name_str = NULL;
9958 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009959
Willy Tarreaua890d072013-04-02 12:01:06 +02009960 if (!ctx) {
9961 /* first call */
9962 ctx = &static_hdr_ctx;
9963 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02009964 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009965 }
9966
Willy Tarreau185b5c42012-04-26 15:11:51 +02009967 if (args) {
9968 if (args[0].type != ARGT_STR)
9969 return 0;
9970 name_str = args[0].data.str.str;
9971 name_len = args[0].data.str.len;
9972
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02009973 if (args[1].type == ARGT_SINT)
9974 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009975 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009976
Willy Tarreaue333ec92012-04-16 16:26:40 +02009977 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02009978
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009979 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009980 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 +02009981
Willy Tarreau185b5c42012-04-26 15:11:51 +02009982 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009983 /* search for header from the beginning */
9984 ctx->idx = 0;
9985
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009986 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +02009987 /* no explicit occurrence and single fetch => last header by default */
9988 occ = -1;
9989
9990 if (!occ)
9991 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02009992 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01009993
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009994 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009995 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009996 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 +02009997 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009998
Willy Tarreau37406352012-04-23 16:16:37 +02009999 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010000 return 0;
10001}
10002
Willy Tarreauc11416f2007-06-17 16:58:38 +020010003/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010004 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010005 */
10006static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010007smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010008{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010009 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010010 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010011 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010012 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010013 const char *name = NULL;
10014 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010015
Willy Tarreau601a4d12015-04-01 19:16:09 +020010016 if (args && args->type == ARGT_STR) {
10017 name = args->data.str.str;
10018 len = args->data.str.len;
10019 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010020
Willy Tarreaue333ec92012-04-16 16:26:40 +020010021 CHECK_HTTP_MESSAGE_FIRST();
10022
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010023 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010024 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 +020010025
Willy Tarreau33a7e692007-06-10 19:45:56 +020010026 ctx.idx = 0;
10027 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010028 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010029 cnt++;
10030
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010031 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010032 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010033 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010034 return 1;
10035}
10036
Willy Tarreau185b5c42012-04-26 15:11:51 +020010037/* Fetch an HTTP header's integer value. The integer value is returned. It
10038 * takes a mandatory argument of type string and an optional one of type int
10039 * to designate a specific occurrence. It returns an unsigned integer, which
10040 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010041 */
10042static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010043smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010044{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010045 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010046
Willy Tarreauf853c462012-04-23 18:53:56 +020010047 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010048 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010049 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010050 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010051
Willy Tarreaud53e2422012-04-16 17:21:11 +020010052 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010053}
10054
Cyril Bonté69fa9922012-10-25 00:01:06 +020010055/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10056 * and an optional one of type int to designate a specific occurrence.
10057 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010058 */
10059static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010060smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010061{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010062 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010063
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010064 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010065 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010066 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010067 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010068 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010069 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010070 if (smp->data.u.str.len < temp->size - 1) {
10071 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10072 temp->str[smp->data.u.str.len] = '\0';
10073 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010074 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010075 break;
10076 }
10077 }
10078 }
10079
Willy Tarreaud53e2422012-04-16 17:21:11 +020010080 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010081 if (!(smp->flags & SMP_F_NOT_LAST))
10082 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010083 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010084 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010085}
10086
Willy Tarreau737b0c12007-06-10 21:28:46 +020010087/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10088 * the first '/' after the possible hostname, and ends before the possible '?'.
10089 */
10090static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010091smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010092{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010093 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010094 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010095
Willy Tarreauc0239e02012-04-16 14:42:55 +020010096 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010097
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010098 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010099 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010100 ptr = http_get_path(txn);
10101 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010102 return 0;
10103
10104 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010105 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010106 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010107
10108 while (ptr < end && *ptr != '?')
10109 ptr++;
10110
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010111 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010112 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010113 return 1;
10114}
10115
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010116/* This produces a concatenation of the first occurrence of the Host header
10117 * followed by the path component if it begins with a slash ('/'). This means
10118 * that '*' will not be added, resulting in exactly the first Host entry.
10119 * If no Host header is found, then the path is returned as-is. The returned
10120 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010121 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010122 */
10123static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010124smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010125{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010126 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010127 char *ptr, *end, *beg;
10128 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010129 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010130
10131 CHECK_HTTP_MESSAGE_FIRST();
10132
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010133 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010134 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010135 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010136 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010137
10138 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010139 temp = get_trash_chunk();
10140 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010141 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010142 smp->data.u.str.str = temp->str;
10143 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010144
10145 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010146 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010147 beg = http_get_path(txn);
10148 if (!beg)
10149 beg = end;
10150
10151 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10152
10153 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010154 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10155 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010156 }
10157
10158 smp->flags = SMP_F_VOL_1ST;
10159 return 1;
10160}
10161
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010162/* This produces a 32-bit hash of the concatenation of the first occurrence of
10163 * the Host header followed by the path component if it begins with a slash ('/').
10164 * This means that '*' will not be added, resulting in exactly the first Host
10165 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010166 * is hashed using the path hash followed by a full avalanche hash and provides a
10167 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010168 * high-traffic sites without having to store whole paths.
10169 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010170int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010171smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010172{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010173 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010174 struct hdr_ctx ctx;
10175 unsigned int hash = 0;
10176 char *ptr, *beg, *end;
10177 int len;
10178
10179 CHECK_HTTP_MESSAGE_FIRST();
10180
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010181 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010182 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010183 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010184 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10185 ptr = ctx.line + ctx.val;
10186 len = ctx.vlen;
10187 while (len--)
10188 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10189 }
10190
10191 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010192 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010193 beg = http_get_path(txn);
10194 if (!beg)
10195 beg = end;
10196
10197 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10198
10199 if (beg < ptr && *beg == '/') {
10200 while (beg < ptr)
10201 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10202 }
10203 hash = full_hash(hash);
10204
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010205 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010206 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010207 smp->flags = SMP_F_VOL_1ST;
10208 return 1;
10209}
10210
Willy Tarreau4a550602012-12-09 14:53:32 +010010211/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010212 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10213 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10214 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010215 * that in environments where IPv6 is insignificant, truncating the output to
10216 * 8 bytes would still work.
10217 */
10218static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010219smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010220{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010221 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010222 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010223
10224 if (!cli_conn)
10225 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010226
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010227 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010228 return 0;
10229
Willy Tarreau47ca5452012-12-23 20:22:19 +010010230 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010231 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010232 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010233
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010234 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010235 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010236 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010237 temp->len += 4;
10238 break;
10239 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010240 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010241 temp->len += 16;
10242 break;
10243 default:
10244 return 0;
10245 }
10246
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010247 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010248 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010249 return 1;
10250}
10251
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010252/* Extracts the query string, which comes after the question mark '?'. If no
10253 * question mark is found, nothing is returned. Otherwise it returns a sample
10254 * of type string carrying the whole query string.
10255 */
10256static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010257smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010258{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010259 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010260 char *ptr, *end;
10261
10262 CHECK_HTTP_MESSAGE_FIRST();
10263
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010264 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010265 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10266 end = ptr + txn->req.sl.rq.u_l;
10267
10268 /* look up the '?' */
10269 do {
10270 if (ptr == end)
10271 return 0;
10272 } while (*ptr++ != '?');
10273
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010274 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010275 smp->data.u.str.str = ptr;
10276 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010277 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10278 return 1;
10279}
10280
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010281static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010282smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010283{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010284 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10285 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10286 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010287
Willy Tarreau24e32d82012-04-23 23:55:44 +020010288 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010289
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010290 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010291 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010292 return 1;
10293}
10294
Willy Tarreau7f18e522010-10-22 20:04:13 +020010295/* return a valid test if the current request is the first one on the connection */
10296static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010297smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010298{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010299 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010300 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010301 return 1;
10302}
10303
Willy Tarreau34db1082012-04-19 17:16:54 +020010304/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010305static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010306smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010307{
10308
Willy Tarreau24e32d82012-04-23 23:55:44 +020010309 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010310 return 0;
10311
Willy Tarreauc0239e02012-04-16 14:42:55 +020010312 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010313
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010314 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010315 return 0;
10316
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010317 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010318 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010319 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010320 return 1;
10321}
Willy Tarreau8797c062007-05-07 00:55:35 +020010322
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010323/* Accepts exactly 1 argument of type userlist */
10324static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010325smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010326{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010327 if (!args || args->type != ARGT_USR)
10328 return 0;
10329
10330 CHECK_HTTP_MESSAGE_FIRST();
10331
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010332 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010333 return 0;
10334
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010335 /* if the user does not belong to the userlist or has a wrong password,
10336 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010337 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010338 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010339 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10340 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010341 return 0;
10342
10343 /* pat_match_auth() will need the user list */
10344 smp->ctx.a[0] = args->data.usr;
10345
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010346 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010347 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010348 smp->data.u.str.str = smp->strm->txn->auth.user;
10349 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010350
10351 return 1;
10352}
10353
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010354/* Try to find the next occurrence of a cookie name in a cookie header value.
10355 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10356 * the cookie value is returned into *value and *value_l, and the function
10357 * returns a pointer to the next pointer to search from if the value was found.
10358 * Otherwise if the cookie was not found, NULL is returned and neither value
10359 * nor value_l are touched. The input <hdr> string should first point to the
10360 * header's value, and the <hdr_end> pointer must point to the first character
10361 * not part of the value. <list> must be non-zero if value may represent a list
10362 * of values (cookie headers). This makes it faster to abort parsing when no
10363 * list is expected.
10364 */
David Carlier4686f792015-09-25 14:10:50 +010010365char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010366extract_cookie_value(char *hdr, const char *hdr_end,
10367 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010368 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010369{
10370 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10371 char *next;
10372
10373 /* we search at least a cookie name followed by an equal, and more
10374 * generally something like this :
10375 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10376 */
10377 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10378 /* Iterate through all cookies on this line */
10379
Willy Tarreau2235b262016-11-05 15:50:20 +010010380 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010381 att_beg++;
10382
10383 /* find att_end : this is the first character after the last non
10384 * space before the equal. It may be equal to hdr_end.
10385 */
10386 equal = att_end = att_beg;
10387
10388 while (equal < hdr_end) {
10389 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10390 break;
Willy Tarreau2235b262016-11-05 15:50:20 +010010391 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010392 continue;
10393 att_end = equal;
10394 }
10395
10396 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10397 * is between <att_beg> and <equal>, both may be identical.
10398 */
10399
10400 /* look for end of cookie if there is an equal sign */
10401 if (equal < hdr_end && *equal == '=') {
10402 /* look for the beginning of the value */
10403 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +010010404 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010405 val_beg++;
10406
10407 /* find the end of the value, respecting quotes */
10408 next = find_cookie_value_end(val_beg, hdr_end);
10409
10410 /* make val_end point to the first white space or delimitor after the value */
10411 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +010010412 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010413 val_end--;
10414 } else {
10415 val_beg = val_end = next = equal;
10416 }
10417
10418 /* We have nothing to do with attributes beginning with '$'. However,
10419 * they will automatically be removed if a header before them is removed,
10420 * since they're supposed to be linked together.
10421 */
10422 if (*att_beg == '$')
10423 continue;
10424
10425 /* Ignore cookies with no equal sign */
10426 if (equal == next)
10427 continue;
10428
10429 /* Now we have the cookie name between att_beg and att_end, and
10430 * its value between val_beg and val_end.
10431 */
10432
10433 if (att_end - att_beg == cookie_name_l &&
10434 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10435 /* let's return this value and indicate where to go on from */
10436 *value = val_beg;
10437 *value_l = val_end - val_beg;
10438 return next + 1;
10439 }
10440
10441 /* Set-Cookie headers only have the name in the first attr=value part */
10442 if (!list)
10443 break;
10444 }
10445
10446 return NULL;
10447}
10448
William Lallemanda43ba4e2014-01-28 18:14:25 +010010449/* Fetch a captured HTTP request header. The index is the position of
10450 * the "capture" option in the configuration file
10451 */
10452static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010453smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010454{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010455 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010456 int idx;
10457
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010458 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010459 return 0;
10460
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010461 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010462
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010463 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 +010010464 return 0;
10465
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010466 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010467 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010468 smp->data.u.str.str = smp->strm->req_cap[idx];
10469 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010470
10471 return 1;
10472}
10473
10474/* Fetch a captured HTTP response header. The index is the position of
10475 * the "capture" option in the configuration file
10476 */
10477static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010478smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010479{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010480 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010481 int idx;
10482
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010483 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010484 return 0;
10485
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010486 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010487
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010488 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 +010010489 return 0;
10490
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010491 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010492 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010493 smp->data.u.str.str = smp->strm->res_cap[idx];
10494 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010495
10496 return 1;
10497}
10498
William Lallemand65ad6e12014-01-31 15:08:02 +010010499/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10500static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010501smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010502{
10503 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010504 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010010505 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010506
Willy Tarreau15e91e12015-04-04 00:52:09 +020010507 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010508 return 0;
10509
William Lallemand96a77852014-02-05 00:30:02 +010010510 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010511
William Lallemand96a77852014-02-05 00:30:02 +010010512 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10513 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010514
William Lallemand96a77852014-02-05 00:30:02 +010010515 temp = get_trash_chunk();
10516 temp->str = txn->uri;
10517 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010518 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010519 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010520 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010521
10522 return 1;
10523
10524}
10525
10526/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10527static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010528smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010529{
10530 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010531 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010010532 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010533
Willy Tarreau15e91e12015-04-04 00:52:09 +020010534 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010535 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010536
William Lallemand65ad6e12014-01-31 15:08:02 +010010537 ptr = txn->uri;
10538
10539 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10540 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010541
William Lallemand65ad6e12014-01-31 15:08:02 +010010542 if (!*ptr)
10543 return 0;
10544
10545 ptr++; /* skip the space */
10546
10547 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010548 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010549 if (!ptr)
10550 return 0;
10551 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10552 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010553
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010554 smp->data.u.str = *temp;
10555 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010556 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010557 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010558
10559 return 1;
10560}
10561
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010562/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10563 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10564 */
10565static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010566smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010567{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010568 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010569
Willy Tarreau15e91e12015-04-04 00:52:09 +020010570 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010571 return 0;
10572
10573 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010574 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010575 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010576 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010577
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010578 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010579 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010580 smp->flags = SMP_F_CONST;
10581 return 1;
10582
10583}
10584
10585/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10586 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10587 */
10588static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010589smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010590{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010591 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010592
Willy Tarreau15e91e12015-04-04 00:52:09 +020010593 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010594 return 0;
10595
10596 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010597 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010598 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010599 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010600
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010601 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010602 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010603 smp->flags = SMP_F_CONST;
10604 return 1;
10605
10606}
10607
William Lallemand65ad6e12014-01-31 15:08:02 +010010608
Willy Tarreaue333ec92012-04-16 16:26:40 +020010609/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010610 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010611 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010612 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010613 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010614 * Accepts exactly 1 argument of type string. If the input options indicate
10615 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010616 * The returned sample is of type CSTR. Can be used to parse cookies in other
10617 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010618 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010619int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010620{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010621 struct http_txn *txn;
10622 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010623 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010624 const struct http_msg *msg;
10625 const char *hdr_name;
10626 int hdr_name_len;
10627 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010628 int occ = 0;
10629 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010630
Willy Tarreau24e32d82012-04-23 23:55:44 +020010631 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010632 return 0;
10633
Willy Tarreaua890d072013-04-02 12:01:06 +020010634 if (!ctx) {
10635 /* first call */
10636 ctx = &static_hdr_ctx;
10637 ctx->idx = 0;
10638 smp->ctx.a[2] = ctx;
10639 }
10640
Willy Tarreaue333ec92012-04-16 16:26:40 +020010641 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010642
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010643 txn = smp->strm->txn;
10644 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010645
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010646 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010647 msg = &txn->req;
10648 hdr_name = "Cookie";
10649 hdr_name_len = 6;
10650 } else {
10651 msg = &txn->rsp;
10652 hdr_name = "Set-Cookie";
10653 hdr_name_len = 10;
10654 }
10655
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010656 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020010657 /* no explicit occurrence and single fetch => last cookie by default */
10658 occ = -1;
10659
10660 /* OK so basically here, either we want only one value and it's the
10661 * last one, or we want to iterate over all of them and we fetch the
10662 * next one.
10663 */
10664
Willy Tarreau9b28e032012-10-12 23:49:43 +020010665 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010666 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010667 /* search for the header from the beginning, we must first initialize
10668 * the search parameters.
10669 */
Willy Tarreau37406352012-04-23 16:16:37 +020010670 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010671 ctx->idx = 0;
10672 }
10673
Willy Tarreau28376d62012-04-26 21:26:10 +020010674 smp->flags |= SMP_F_VOL_HDR;
10675
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010676 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010677 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10678 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010679 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10680 goto out;
10681
Willy Tarreau24e32d82012-04-23 23:55:44 +020010682 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010683 continue;
10684
Willy Tarreau37406352012-04-23 16:16:37 +020010685 smp->ctx.a[0] = ctx->line + ctx->val;
10686 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010687 }
10688
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010689 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010690 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010691 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010692 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010693 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010694 &smp->data.u.str.str,
10695 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010696 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010697 found = 1;
10698 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010699 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020010700 smp->flags |= SMP_F_NOT_LAST;
10701 return 1;
10702 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010703 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010704 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010705 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010706 /* all cookie headers and values were scanned. If we're looking for the
10707 * last occurrence, we may return it now.
10708 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010709 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010710 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010711 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010712}
10713
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010714/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010715 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010716 * multiple cookies may be parsed on the same line. The returned sample is of
10717 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010718 */
10719static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010720smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010721{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010722 struct http_txn *txn;
10723 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010724 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010725 const struct http_msg *msg;
10726 const char *hdr_name;
10727 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010728 int cnt;
10729 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010730 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010731
Willy Tarreau24e32d82012-04-23 23:55:44 +020010732 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010733 return 0;
10734
Willy Tarreaue333ec92012-04-16 16:26:40 +020010735 CHECK_HTTP_MESSAGE_FIRST();
10736
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010737 txn = smp->strm->txn;
10738 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010739
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010740 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010741 msg = &txn->req;
10742 hdr_name = "Cookie";
10743 hdr_name_len = 6;
10744 } else {
10745 msg = &txn->rsp;
10746 hdr_name = "Set-Cookie";
10747 hdr_name_len = 10;
10748 }
10749
Willy Tarreau9b28e032012-10-12 23:49:43 +020010750 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010751 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010752 ctx.idx = 0;
10753 cnt = 0;
10754
10755 while (1) {
10756 /* Note: val_beg == NULL every time we need to fetch a new header */
10757 if (!val_beg) {
10758 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10759 break;
10760
Willy Tarreau24e32d82012-04-23 23:55:44 +020010761 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010762 continue;
10763
10764 val_beg = ctx.line + ctx.val;
10765 val_end = val_beg + ctx.vlen;
10766 }
10767
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010768 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010769 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010770 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010771 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010772 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010773 &smp->data.u.str.str,
10774 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010775 cnt++;
10776 }
10777 }
10778
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010779 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010780 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010781 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010782 return 1;
10783}
10784
Willy Tarreau51539362012-05-08 12:46:28 +020010785/* Fetch an cookie's integer value. The integer value is returned. It
10786 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10787 */
10788static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010789smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020010790{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010791 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020010792
10793 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010794 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010795 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020010796 }
10797
10798 return ret;
10799}
10800
Willy Tarreau8797c062007-05-07 00:55:35 +020010801/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010802/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010803/************************************************************************/
10804
David Cournapeau16023ee2010-12-23 20:55:41 +090010805/*
10806 * Given a path string and its length, find the position of beginning of the
10807 * query string. Returns NULL if no query string is found in the path.
10808 *
10809 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
10810 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010811 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090010812 */
bedis4c75cca2012-10-05 08:38:24 +020010813static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010814{
10815 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020010816
bedis4c75cca2012-10-05 08:38:24 +020010817 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090010818 return p ? p + 1 : NULL;
10819}
10820
bedis4c75cca2012-10-05 08:38:24 +020010821static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010822{
bedis4c75cca2012-10-05 08:38:24 +020010823 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090010824}
10825
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010826/* after increasing a pointer value, it can exceed the first buffer
10827 * size. This function transform the value of <ptr> according with
10828 * the expected position. <chunks> is an array of the one or two
10829 * avalaible chunks. The first value is the start of the first chunk,
10830 * the second value if the end+1 of the first chunks. The third value
10831 * is NULL or the start of the second chunk and the fourth value is
10832 * the end+1 of the second chunk. The function returns 1 if does a
10833 * wrap, else returns 0.
10834 */
10835static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
10836{
10837 if (*ptr < chunks[1])
10838 return 0;
10839 if (!chunks[2])
10840 return 0;
10841 *ptr = chunks[2] + ( *ptr - chunks[1] );
10842 return 1;
10843}
10844
David Cournapeau16023ee2010-12-23 20:55:41 +090010845/*
10846 * Given a url parameter, find the starting position of the first occurence,
10847 * or NULL if the parameter is not found.
10848 *
10849 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
10850 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010851 *
Willy Tarreauf6625822015-12-27 14:51:01 +010010852 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010853 * or the second chunk. The caller must be check the position before using the
10854 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090010855 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010856static const char *
10857find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010858 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020010859 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010860{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010861 const char *pos, *last, *equal;
10862 const char **bufs = chunks;
10863 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090010864
David Cournapeau16023ee2010-12-23 20:55:41 +090010865
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010866 pos = bufs[0];
10867 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010010868 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010869 /* Check the equal. */
10870 equal = pos + url_param_name_l;
10871 if (fix_pointer_if_wrap(chunks, &equal)) {
10872 if (equal >= chunks[3])
10873 return NULL;
10874 } else {
10875 if (equal >= chunks[1])
10876 return NULL;
10877 }
10878 if (*equal == '=') {
10879 if (pos + url_param_name_l > last) {
10880 /* process wrap case, we detect a wrap. In this case, the
10881 * comparison is performed in two parts.
10882 */
10883
10884 /* This is the end, we dont have any other chunk. */
10885 if (bufs != chunks || !bufs[2])
10886 return NULL;
10887
10888 /* Compute the length of each part of the comparison. */
10889 l1 = last - pos;
10890 l2 = url_param_name_l - l1;
10891
10892 /* The second buffer is too short to contain the compared string. */
10893 if (bufs[2] + l2 > bufs[3])
10894 return NULL;
10895
10896 if (memcmp(pos, url_param_name, l1) == 0 &&
10897 memcmp(bufs[2], url_param_name+l1, l2) == 0)
10898 return pos;
10899
10900 /* Perform wrapping and jump the string who fail the comparison. */
10901 bufs += 2;
10902 pos = bufs[0] + l2;
10903 last = bufs[1];
10904
10905 } else {
10906 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010010907 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
10908 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010909 pos += url_param_name_l + 1;
10910 if (fix_pointer_if_wrap(chunks, &pos))
10911 last = bufs[2];
10912 }
10913 }
10914
10915 while (1) {
10916 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010010917 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010918 pos++;
10919 if (pos < last)
10920 break;
10921 /* process buffer wrapping. */
10922 if (bufs != chunks || !bufs[2])
10923 return NULL;
10924 bufs += 2;
10925 pos = bufs[0];
10926 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090010927 }
David Cournapeau16023ee2010-12-23 20:55:41 +090010928 pos++;
10929 }
10930 return NULL;
10931}
10932
10933/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010010934 * Given a url parameter name and a query string, find the next value.
10935 * An empty url_param_name matches the first available parameter.
10936 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
10937 * respectively provide a pointer to the value and its end.
10938 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090010939 */
10940static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010941find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010942 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010943 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010944{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010945 const char *arg_start, *qs_end;
10946 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090010947
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010948 arg_start = chunks[0];
10949 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010950 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010951 /* Looks for an argument name. */
10952 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010953 url_param_name, url_param_name_l,
10954 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010955 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010010956 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010957 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010958 }
David Cournapeau16023ee2010-12-23 20:55:41 +090010959 if (!arg_start)
10960 return 0;
10961
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010962 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010963 while (1) {
10964 /* looks for the first argument. */
10965 value_start = memchr(arg_start, '=', qs_end - arg_start);
10966 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010967 /* Check for wrapping. */
10968 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010010969 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010970 chunks[2]) {
10971 arg_start = chunks[2];
10972 qs_end = chunks[3];
10973 continue;
10974 }
10975 return 0;
10976 }
10977 break;
10978 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010979 value_start++;
10980 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010981 else {
10982 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010983 value_start = arg_start + url_param_name_l + 1;
10984
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010985 /* Check for pointer wrapping. */
10986 if (fix_pointer_if_wrap(chunks, &value_start)) {
10987 /* Update the end pointer. */
10988 qs_end = chunks[3];
10989
10990 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010010991 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010992 return 0;
10993 }
10994 }
10995
David Cournapeau16023ee2010-12-23 20:55:41 +090010996 value_end = value_start;
10997
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010998 while (1) {
10999 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11000 value_end++;
11001 if (value_end < qs_end)
11002 break;
11003 /* process buffer wrapping. */
11004 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011005 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011006 chunks[2]) {
11007 value_end = chunks[2];
11008 qs_end = chunks[3];
11009 continue;
11010 }
11011 break;
11012 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011013
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011014 *vstart = value_start;
11015 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011016 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011017}
11018
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011019/* This scans a URL-encoded query string. It takes an optionally wrapping
11020 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11021 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11022 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011023 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011024static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011025smp_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 +090011026{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011027 const char *vstart, *vend;
11028 struct chunk *temp;
11029 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011030
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011031 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011032 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011033 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011034 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011035 return 0;
11036
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011037 /* Create sample. If the value is contiguous, return the pointer as CONST,
11038 * if the value is wrapped, copy-it in a buffer.
11039 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011040 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011041 if (chunks[2] &&
11042 vstart >= chunks[0] && vstart <= chunks[1] &&
11043 vend >= chunks[2] && vend <= chunks[3]) {
11044 /* Wrapped case. */
11045 temp = get_trash_chunk();
11046 memcpy(temp->str, vstart, chunks[1] - vstart);
11047 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011048 smp->data.u.str.str = temp->str;
11049 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011050 } else {
11051 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011052 smp->data.u.str.str = (char *)vstart;
11053 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011054 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11055 }
11056
11057 /* Update context, check wrapping. */
11058 chunks[0] = vend;
11059 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11060 chunks[1] = chunks[3];
11061 chunks[2] = NULL;
11062 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011063
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011064 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011065 smp->flags |= SMP_F_NOT_LAST;
11066
David Cournapeau16023ee2010-12-23 20:55:41 +090011067 return 1;
11068}
11069
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011070/* This function iterates over each parameter of the query string. It uses
11071 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011072 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11073 * An optional parameter name is passed in args[0], otherwise any parameter is
11074 * considered. It supports an optional delimiter argument for the beginning of
11075 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011076 */
11077static int
11078smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11079{
11080 struct http_msg *msg;
11081 char delim = '?';
11082 const char *name;
11083 int name_len;
11084
Dragan Dosen26f77e52015-05-25 10:02:11 +020011085 if (!args ||
11086 (args[0].type && args[0].type != ARGT_STR) ||
11087 (args[1].type && args[1].type != ARGT_STR))
11088 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011089
Dragan Dosen26f77e52015-05-25 10:02:11 +020011090 name = "";
11091 name_len = 0;
11092 if (args->type == ARGT_STR) {
11093 name = args->data.str.str;
11094 name_len = args->data.str.len;
11095 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011096
Dragan Dosen26f77e52015-05-25 10:02:11 +020011097 if (args[1].type)
11098 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011099
Dragan Dosen26f77e52015-05-25 10:02:11 +020011100 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011101 CHECK_HTTP_MESSAGE_FIRST();
11102
11103 msg = &smp->strm->txn->req;
11104
11105 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11106 msg->sl.rq.u_l, delim);
11107 if (!smp->ctx.a[0])
11108 return 0;
11109
11110 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011111
11112 /* Assume that the context is filled with NULL pointer
11113 * before the first call.
11114 * smp->ctx.a[2] = NULL;
11115 * smp->ctx.a[3] = NULL;
11116 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011117 }
11118
11119 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11120}
11121
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011122/* This function iterates over each parameter of the body. This requires
11123 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011124 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11125 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11126 * optional second part if the body wraps at the end of the buffer. An optional
11127 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011128 */
11129static int
11130smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11131{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011132 struct http_msg *msg;
11133 unsigned long len;
11134 unsigned long block1;
11135 char *body;
11136 const char *name;
11137 int name_len;
11138
11139 if (!args || (args[0].type && args[0].type != ARGT_STR))
11140 return 0;
11141
11142 name = "";
11143 name_len = 0;
11144 if (args[0].type == ARGT_STR) {
11145 name = args[0].data.str.str;
11146 name_len = args[0].data.str.len;
11147 }
11148
11149 if (!smp->ctx.a[0]) { // first call, find the query string
11150 CHECK_HTTP_MESSAGE_FIRST();
11151
11152 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010011153 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011154 else
Willy Tarreaube508f12016-03-10 11:47:01 +010011155 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011156
11157 len = http_body_bytes(msg);
11158 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11159
11160 block1 = len;
11161 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11162 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11163
11164 if (block1 == len) {
11165 /* buffer is not wrapped (or empty) */
11166 smp->ctx.a[0] = body;
11167 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011168
11169 /* Assume that the context is filled with NULL pointer
11170 * before the first call.
11171 * smp->ctx.a[2] = NULL;
11172 * smp->ctx.a[3] = NULL;
11173 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011174 }
11175 else {
11176 /* buffer is wrapped, we need to defragment it */
11177 smp->ctx.a[0] = body;
11178 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011179 smp->ctx.a[2] = msg->chn->buf->data;
11180 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011181 }
11182 }
11183 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11184}
11185
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011186/* Return the signed integer value for the specified url parameter (see url_param
11187 * above).
11188 */
11189static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011190smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011191{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011192 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011193
11194 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011195 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011196 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011197 }
11198
11199 return ret;
11200}
11201
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011202/* This produces a 32-bit hash of the concatenation of the first occurrence of
11203 * the Host header followed by the path component if it begins with a slash ('/').
11204 * This means that '*' will not be added, resulting in exactly the first Host
11205 * entry. If no Host header is found, then the path is used. The resulting value
11206 * is hashed using the url hash followed by a full avalanche hash and provides a
11207 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11208 * high-traffic sites without having to store whole paths.
11209 * this differs from the base32 functions in that it includes the url parameters
11210 * as well as the path
11211 */
11212static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011213smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011214{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011215 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011216 struct hdr_ctx ctx;
11217 unsigned int hash = 0;
11218 char *ptr, *beg, *end;
11219 int len;
11220
11221 CHECK_HTTP_MESSAGE_FIRST();
11222
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011223 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011224 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011225 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011226 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11227 ptr = ctx.line + ctx.val;
11228 len = ctx.vlen;
11229 while (len--)
11230 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11231 }
11232
11233 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011234 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 +000011235 beg = http_get_path(txn);
11236 if (!beg)
11237 beg = end;
11238
11239 for (ptr = beg; ptr < end ; ptr++);
11240
11241 if (beg < ptr && *beg == '/') {
11242 while (beg < ptr)
11243 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11244 }
11245 hash = full_hash(hash);
11246
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011247 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011248 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011249 smp->flags = SMP_F_VOL_1ST;
11250 return 1;
11251}
11252
11253/* This concatenates the source address with the 32-bit hash of the Host and
11254 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11255 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11256 * on the source address length. The URL hash is stored before the address so
11257 * that in environments where IPv6 is insignificant, truncating the output to
11258 * 8 bytes would still work.
11259 */
11260static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011261smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011262{
11263 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011264 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011265
Dragan Dosendb5af612016-06-16 11:23:01 +020011266 if (!cli_conn)
11267 return 0;
11268
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011269 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011270 return 0;
11271
11272 temp = get_trash_chunk();
Dragan Dosene5f41332016-06-16 11:08:08 +020011273 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
11274 temp->len += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011275
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011276 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011277 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011278 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011279 temp->len += 4;
11280 break;
11281 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011282 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011283 temp->len += 16;
11284 break;
11285 default:
11286 return 0;
11287 }
11288
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011289 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011290 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011291 return 1;
11292}
11293
Willy Tarreau185b5c42012-04-26 15:11:51 +020011294/* This function is used to validate the arguments passed to any "hdr" fetch
11295 * keyword. These keywords support an optional positive or negative occurrence
11296 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11297 * is assumed that the types are already the correct ones. Returns 0 on error,
11298 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11299 * error message in case of error, that the caller is responsible for freeing.
11300 * The initial location must either be freeable or NULL.
11301 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011302int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011303{
11304 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011305 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011306 return 0;
11307 }
11308 return 1;
11309}
11310
Willy Tarreau276fae92013-07-25 14:36:01 +020011311/* takes an UINT value on input supposed to represent the time since EPOCH,
11312 * adds an optional offset found in args[0] and emits a string representing
11313 * the date in RFC-1123/5322 format.
11314 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011315static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011316{
Cyril Bontéf78d8962016-01-22 19:40:28 +010011317 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020011318 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11319 struct chunk *temp;
11320 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011321 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011322 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020011323
11324 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011325 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020011326 curr_date += args[0].data.sint;
11327
11328 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020011329 if (!tm)
11330 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011331
11332 temp = get_trash_chunk();
11333 temp->len = snprintf(temp->str, temp->size - temp->len,
11334 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11335 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11336 tm->tm_hour, tm->tm_min, tm->tm_sec);
11337
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011338 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011339 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020011340 return 1;
11341}
11342
Thierry FOURNIERad903512014-04-11 17:51:01 +020011343/* Match language range with language tag. RFC2616 14.4:
11344 *
11345 * A language-range matches a language-tag if it exactly equals
11346 * the tag, or if it exactly equals a prefix of the tag such
11347 * that the first tag character following the prefix is "-".
11348 *
11349 * Return 1 if the strings match, else return 0.
11350 */
11351static inline int language_range_match(const char *range, int range_len,
11352 const char *tag, int tag_len)
11353{
11354 const char *end = range + range_len;
11355 const char *tend = tag + tag_len;
11356 while (range < end) {
11357 if (*range == '-' && tag == tend)
11358 return 1;
11359 if (*range != *tag || tag == tend)
11360 return 0;
11361 range++;
11362 tag++;
11363 }
11364 /* Return true only if the last char of the tag is matched. */
11365 return tag == tend;
11366}
11367
11368/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011369static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011370{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011371 const char *al = smp->data.u.str.str;
11372 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011373 const char *token;
11374 int toklen;
11375 int qvalue;
11376 const char *str;
11377 const char *w;
11378 int best_q = 0;
11379
11380 /* Set the constant to the sample, because the output of the
11381 * function will be peek in the constant configuration string.
11382 */
11383 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011384 smp->data.u.str.size = 0;
11385 smp->data.u.str.str = "";
11386 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011387
11388 /* Parse the accept language */
11389 while (1) {
11390
11391 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011392 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011393 al++;
11394 if (al >= end)
11395 break;
11396
11397 /* Start of the fisrt word. */
11398 token = al;
11399
11400 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011401 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011402 al++;
11403 if (al == token)
11404 goto expect_comma;
11405
11406 /* Length of the token. */
11407 toklen = al - token;
11408 qvalue = 1000;
11409
11410 /* Check if the token exists in the list. If the token not exists,
11411 * jump to the next token.
11412 */
11413 str = args[0].data.str.str;
11414 w = str;
11415 while (1) {
11416 if (*str == ';' || *str == '\0') {
11417 if (language_range_match(token, toklen, w, str-w))
11418 goto look_for_q;
11419 if (*str == '\0')
11420 goto expect_comma;
11421 w = str + 1;
11422 }
11423 str++;
11424 }
11425 goto expect_comma;
11426
11427look_for_q:
11428
11429 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011430 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011431 al++;
11432 if (al >= end)
11433 goto process_value;
11434
11435 /* If ',' is found, process the result */
11436 if (*al == ',')
11437 goto process_value;
11438
11439 /* If the character is different from ';', look
11440 * for the end of the header part in best effort.
11441 */
11442 if (*al != ';')
11443 goto expect_comma;
11444
11445 /* Assumes that the char is ';', now expect "q=". */
11446 al++;
11447
11448 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011449 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011450 al++;
11451 if (al >= end)
11452 goto process_value;
11453
11454 /* Expect 'q'. If no 'q', continue in best effort */
11455 if (*al != 'q')
11456 goto process_value;
11457 al++;
11458
11459 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011460 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011461 al++;
11462 if (al >= end)
11463 goto process_value;
11464
11465 /* Expect '='. If no '=', continue in best effort */
11466 if (*al != '=')
11467 goto process_value;
11468 al++;
11469
11470 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011471 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011472 al++;
11473 if (al >= end)
11474 goto process_value;
11475
11476 /* Parse the q value. */
11477 qvalue = parse_qvalue(al, &al);
11478
11479process_value:
11480
11481 /* If the new q value is the best q value, then store the associated
11482 * language in the response. If qvalue is the biggest value (1000),
11483 * break the process.
11484 */
11485 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011486 smp->data.u.str.str = (char *)w;
11487 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011488 if (qvalue >= 1000)
11489 break;
11490 best_q = qvalue;
11491 }
11492
11493expect_comma:
11494
11495 /* Expect comma or end. If the end is detected, quit the loop. */
11496 while (al < end && *al != ',')
11497 al++;
11498 if (al >= end)
11499 break;
11500
11501 /* Comma is found, jump it and restart the analyzer. */
11502 al++;
11503 }
11504
11505 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011506 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
11507 smp->data.u.str.str = args[1].data.str.str;
11508 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011509 }
11510
11511 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011512 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011513}
11514
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011515/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011516static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011517{
11518 /* If the constant flag is set or if not size is avalaible at
11519 * the end of the buffer, copy the string in other buffer
11520 * before decoding.
11521 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011522 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011523 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011524 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
11525 smp->data.u.str.str = str->str;
11526 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011527 smp->flags &= ~SMP_F_CONST;
11528 }
11529
11530 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011531 smp->data.u.str.str[smp->data.u.str.len] = '\0';
11532 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Christopher Fauleta2584792017-10-05 10:03:12 +020011533 return (smp->data.u.str.len >= 0);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011534}
11535
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011536static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
11537{
11538 struct proxy *fe = strm_fe(smp->strm);
11539 int idx, i;
11540 struct cap_hdr *hdr;
11541 int len;
11542
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011543 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011544 return 0;
11545
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011546 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011547
11548 /* Check the availibity of the capture id. */
11549 if (idx > fe->nb_req_cap - 1)
11550 return 0;
11551
11552 /* Look for the original configuration. */
11553 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
11554 hdr != NULL && i != idx ;
11555 i--, hdr = hdr->next);
11556 if (!hdr)
11557 return 0;
11558
11559 /* check for the memory allocation */
11560 if (smp->strm->req_cap[hdr->index] == NULL)
11561 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
11562 if (smp->strm->req_cap[hdr->index] == NULL)
11563 return 0;
11564
11565 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011566 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011567 if (len > hdr->len)
11568 len = hdr->len;
11569
11570 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011571 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011572 smp->strm->req_cap[idx][len] = '\0';
11573
11574 return 1;
11575}
11576
11577static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
11578{
11579 struct proxy *fe = strm_fe(smp->strm);
11580 int idx, i;
11581 struct cap_hdr *hdr;
11582 int len;
11583
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011584 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011585 return 0;
11586
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011587 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011588
11589 /* Check the availibity of the capture id. */
11590 if (idx > fe->nb_rsp_cap - 1)
11591 return 0;
11592
11593 /* Look for the original configuration. */
11594 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
11595 hdr != NULL && i != idx ;
11596 i--, hdr = hdr->next);
11597 if (!hdr)
11598 return 0;
11599
11600 /* check for the memory allocation */
11601 if (smp->strm->res_cap[hdr->index] == NULL)
11602 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
11603 if (smp->strm->res_cap[hdr->index] == NULL)
11604 return 0;
11605
11606 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011607 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011608 if (len > hdr->len)
11609 len = hdr->len;
11610
11611 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011612 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011613 smp->strm->res_cap[idx][len] = '\0';
11614
11615 return 1;
11616}
11617
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011618/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011619 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011620 * the relevant part of the request line accordingly. Then it updates various
11621 * pointers to the next elements which were moved, and the total buffer length.
11622 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011623 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
11624 * error, though this can be revisited when this code is finally exploited.
11625 *
11626 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
11627 * query string and 3 to replace uri.
11628 *
11629 * In query string case, the mark question '?' must be set at the start of the
11630 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011631 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011632int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011633 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011634{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011635 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011636 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011637 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011638 int delta;
11639
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011640 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011641 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011642 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011643 cur_end = cur_ptr + txn->req.sl.rq.m_l;
11644
11645 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011646 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011647 txn->req.sl.rq.m_l += delta;
11648 txn->req.sl.rq.u += delta;
11649 txn->req.sl.rq.v += delta;
11650 break;
11651
11652 case 1: // path
11653 cur_ptr = http_get_path(txn);
11654 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011655 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011656
11657 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011658 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 +010011659 cur_end++;
11660
11661 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011662 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011663 txn->req.sl.rq.u_l += delta;
11664 txn->req.sl.rq.v += delta;
11665 break;
11666
11667 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011668 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011669 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011670 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11671 while (cur_ptr < cur_end && *cur_ptr != '?')
11672 cur_ptr++;
11673
11674 /* skip the question mark or indicate that we must insert it
11675 * (but only if the format string is not empty then).
11676 */
11677 if (cur_ptr < cur_end)
11678 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011679 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011680 offset = 0;
11681
11682 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011683 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011684 txn->req.sl.rq.u_l += delta;
11685 txn->req.sl.rq.v += delta;
11686 break;
11687
11688 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011689 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011690 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11691
11692 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011693 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011694 txn->req.sl.rq.u_l += delta;
11695 txn->req.sl.rq.v += delta;
11696 break;
11697
11698 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011699 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011700 }
11701
11702 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011703 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020011704 txn->req.sl.rq.l += delta;
11705 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011706 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011707 return 0;
11708}
11709
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011710/* This function replace the HTTP status code and the associated message. The
11711 * variable <status> contains the new status code. This function never fails.
11712 */
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011713void http_set_status(unsigned int status, const char *reason, struct stream *s)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011714{
11715 struct http_txn *txn = s->txn;
11716 char *cur_ptr, *cur_end;
11717 int delta;
11718 char *res;
11719 int c_l;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011720 const char *msg = reason;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011721 int msg_len;
11722
11723 chunk_reset(&trash);
11724
11725 res = ultoa_o(status, trash.str, trash.size);
11726 c_l = res - trash.str;
11727
11728 trash.str[c_l] = ' ';
11729 trash.len = c_l + 1;
11730
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011731 /* Do we have a custom reason format string? */
11732 if (msg == NULL)
11733 msg = get_reason(status);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011734 msg_len = strlen(msg);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011735 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
11736 trash.len += msg_len;
11737
11738 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
11739 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
11740
11741 /* commit changes and adjust message */
11742 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
11743
11744 /* adjust res line offsets and lengths */
11745 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
11746 txn->rsp.sl.st.c_l = c_l;
11747 txn->rsp.sl.st.r_l = msg_len;
11748
11749 delta = trash.len - (cur_end - cur_ptr);
11750 txn->rsp.sl.st.l += delta;
11751 txn->hdr_idx.v[0].len += delta;
11752 http_msg_move_end(&txn->rsp, delta);
11753}
11754
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011755/* This function executes one of the set-{method,path,query,uri} actions. It
11756 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020011757 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011758 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020011759 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
11760 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011761 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011762enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011763 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011764{
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011765 struct chunk *replace;
11766 enum act_return ret = ACT_RET_ERR;
11767
11768 replace = alloc_trash_chunk();
11769 if (!replace)
11770 goto leave;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011771
11772 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011773 if (rule->arg.http.action == 2)
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011774 replace->str[replace->len++] = '?';
11775 replace->len += build_logline(s, replace->str + replace->len, replace->size - replace->len,
11776 &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011777
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011778 http_replace_req_line(rule->arg.http.action, replace->str, replace->len, px, s);
11779
11780 ret = ACT_RET_CONT;
11781
11782leave:
11783 free_trash_chunk(replace);
11784 return ret;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011785}
11786
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011787/* This function is just a compliant action wrapper for "set-status". */
11788enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011789 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011790{
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011791 http_set_status(rule->arg.status.code, rule->arg.status.reason, s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011792 return ACT_RET_CONT;
11793}
11794
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011795/* parse an http-request action among :
11796 * set-method
11797 * set-path
11798 * set-query
11799 * set-uri
11800 *
11801 * All of them accept a single argument of type string representing a log-format.
11802 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
11803 * 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 +020011804 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011805 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011806enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
11807 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011808{
11809 int cur_arg = *orig_arg;
11810
Thierry FOURNIER42148732015-09-02 17:17:33 +020011811 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011812
11813 switch (args[0][4]) {
11814 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011815 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011816 rule->action_ptr = http_action_set_req_line;
11817 break;
11818 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011819 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011820 rule->action_ptr = http_action_set_req_line;
11821 break;
11822 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011823 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011824 rule->action_ptr = http_action_set_req_line;
11825 break;
11826 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011827 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011828 rule->action_ptr = http_action_set_req_line;
11829 break;
11830 default:
11831 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011832 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011833 }
11834
11835 if (!*args[cur_arg] ||
11836 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
11837 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011838 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011839 }
11840
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011841 LIST_INIT(&rule->arg.http.logfmt);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011842 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010011843 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +010011844 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, err)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010011845 return ACT_RET_PRS_ERR;
11846 }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011847
11848 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011849 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011850}
11851
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011852/* parse set-status action:
11853 * This action accepts a single argument of type int representing
11854 * an http status code. It returns ACT_RET_PRS_OK on success,
11855 * ACT_RET_PRS_ERR on error.
11856 */
11857enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
11858 struct act_rule *rule, char **err)
11859{
11860 char *error;
11861
Thierry FOURNIER42148732015-09-02 17:17:33 +020011862 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011863 rule->action_ptr = action_http_set_status;
11864
11865 /* Check if an argument is available */
11866 if (!*args[*orig_arg]) {
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011867 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011868 return ACT_RET_PRS_ERR;
11869 }
11870
11871 /* convert status code as integer */
11872 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
11873 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
11874 memprintf(err, "expects an integer status code between 100 and 999");
11875 return ACT_RET_PRS_ERR;
11876 }
11877
11878 (*orig_arg)++;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011879
11880 /* set custom reason string */
11881 rule->arg.status.reason = NULL; // If null, we use the default reason for the status code.
11882 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
11883 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
11884 (*orig_arg)++;
11885 rule->arg.status.reason = strdup(args[*orig_arg]);
11886 (*orig_arg)++;
11887 }
11888
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011889 return ACT_RET_PRS_OK;
11890}
11891
Willy Tarreaua9083d02015-05-08 15:27:59 +020011892/* This function executes the "capture" action. It executes a fetch expression,
11893 * turns the result into a string and puts it in a capture slot. It always
11894 * returns 1. If an error occurs the action is cancelled, but the rule
11895 * processing continues.
11896 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011897enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011898 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020011899{
Willy Tarreaua9083d02015-05-08 15:27:59 +020011900 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020011901 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011902 char **cap = s->req_cap;
11903 int len;
11904
Thierry FOURNIER32b15002015-07-31 08:56:16 +020011905 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 +020011906 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011907 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011908
11909 if (cap[h->index] == NULL)
11910 cap[h->index] = pool_alloc2(h->pool);
11911
11912 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011913 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011914
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011915 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011916 if (len > h->len)
11917 len = h->len;
11918
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011919 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020011920 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011921 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011922}
11923
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011924/* This function executes the "capture" action and store the result in a
11925 * capture slot if exists. It executes a fetch expression, turns the result
11926 * into a string and puts it in a capture slot. It always returns 1. If an
11927 * error occurs the action is cancelled, but the rule processing continues.
11928 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011929enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011930 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011931{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011932 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011933 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011934 char **cap = s->req_cap;
11935 struct proxy *fe = strm_fe(s);
11936 int len;
11937 int i;
11938
11939 /* Look for the original configuration. */
11940 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020011941 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011942 i--, h = h->next);
11943 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011944 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011945
Thierry FOURNIERe2097972015-07-31 08:56:35 +020011946 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 +020011947 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011948 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011949
11950 if (cap[h->index] == NULL)
11951 cap[h->index] = pool_alloc2(h->pool);
11952
11953 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011954 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011955
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011956 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011957 if (len > h->len)
11958 len = h->len;
11959
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011960 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011961 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011962 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011963}
11964
Christopher Faulet29730ba2017-09-18 15:26:32 +020011965/* Check an "http-request capture" action.
11966 *
11967 * The function returns 1 in success case, otherwise, it returns 0 and err is
11968 * filled.
11969 */
11970int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err)
11971{
11972 if (rule->arg.capid.idx >= px->nb_req_cap) {
11973 memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule",
11974 rule->arg.capid.idx);
11975 return 0;
11976 }
11977
11978 return 1;
11979}
11980
Willy Tarreaua9083d02015-05-08 15:27:59 +020011981/* parse an "http-request capture" action. It takes a single argument which is
11982 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011983 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011984 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020011985 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011986enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
11987 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020011988{
11989 struct sample_expr *expr;
11990 struct cap_hdr *hdr;
11991 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020011992 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011993
11994 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
11995 if (strcmp(args[cur_arg], "if") == 0 ||
11996 strcmp(args[cur_arg], "unless") == 0)
11997 break;
11998
11999 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012000 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012001 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012002 }
12003
Willy Tarreaua9083d02015-05-08 15:27:59 +020012004 cur_arg = *orig_arg;
12005 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12006 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012007 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012008
12009 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12010 memprintf(err,
12011 "fetch method '%s' extracts information from '%s', none of which is available here",
12012 args[cur_arg-1], sample_src_names(expr->fetch->use));
12013 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012014 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012015 }
12016
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012017 if (!args[cur_arg] || !*args[cur_arg]) {
12018 memprintf(err, "expects 'len or 'id'");
12019 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012020 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012021 }
12022
Willy Tarreaua9083d02015-05-08 15:27:59 +020012023 if (strcmp(args[cur_arg], "len") == 0) {
12024 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012025
12026 if (!(px->cap & PR_CAP_FE)) {
12027 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012028 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012029 }
12030
12031 proxy->conf.args.ctx = ARGC_CAP;
12032
Willy Tarreaua9083d02015-05-08 15:27:59 +020012033 if (!args[cur_arg]) {
12034 memprintf(err, "missing length value");
12035 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012036 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012037 }
12038 /* we copy the table name for now, it will be resolved later */
12039 len = atoi(args[cur_arg]);
12040 if (len <= 0) {
12041 memprintf(err, "length must be > 0");
12042 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012043 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012044 }
12045 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012046
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012047 if (!len) {
12048 memprintf(err, "a positive 'len' argument is mandatory");
12049 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012050 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012051 }
12052
Vincent Bernat02779b62016-04-03 13:48:43 +020012053 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012054 hdr->next = px->req_cap;
12055 hdr->name = NULL; /* not a header capture */
12056 hdr->namelen = 0;
12057 hdr->len = len;
12058 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12059 hdr->index = px->nb_req_cap++;
12060
12061 px->req_cap = hdr;
12062 px->to_log |= LW_REQHDR;
12063
Thierry FOURNIER42148732015-09-02 17:17:33 +020012064 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012065 rule->action_ptr = http_action_req_capture;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012066 rule->check_ptr = check_http_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012067 rule->arg.cap.expr = expr;
12068 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012069 }
12070
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012071 else if (strcmp(args[cur_arg], "id") == 0) {
12072 int id;
12073 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012074
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012075 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012076
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012077 if (!args[cur_arg]) {
12078 memprintf(err, "missing id value");
12079 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012080 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012081 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012082
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012083 id = strtol(args[cur_arg], &error, 10);
12084 if (*error != '\0') {
12085 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12086 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012087 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012088 }
12089 cur_arg++;
12090
12091 proxy->conf.args.ctx = ARGC_CAP;
12092
Thierry FOURNIER42148732015-09-02 17:17:33 +020012093 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012094 rule->action_ptr = http_action_req_capture_by_id;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012095 rule->check_ptr = check_http_req_capture;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012096 rule->arg.capid.expr = expr;
12097 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012098 }
12099
12100 else {
12101 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12102 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012103 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012104 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012105
12106 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012107 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012108}
12109
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012110/* This function executes the "capture" action and store the result in a
12111 * capture slot if exists. It executes a fetch expression, turns the result
12112 * into a string and puts it in a capture slot. It always returns 1. If an
12113 * error occurs the action is cancelled, but the rule processing continues.
12114 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012115enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012116 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012117{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012118 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012119 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012120 char **cap = s->res_cap;
12121 struct proxy *fe = strm_fe(s);
12122 int len;
12123 int i;
12124
12125 /* Look for the original configuration. */
12126 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012127 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012128 i--, h = h->next);
12129 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012130 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012131
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012132 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 +020012133 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012134 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012135
12136 if (cap[h->index] == NULL)
12137 cap[h->index] = pool_alloc2(h->pool);
12138
12139 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012140 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012141
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012142 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012143 if (len > h->len)
12144 len = h->len;
12145
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012146 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012147 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012148 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012149}
12150
Christopher Faulet29730ba2017-09-18 15:26:32 +020012151/* Check an "http-response capture" action.
12152 *
12153 * The function returns 1 in success case, otherwise, it returns 0 and err is
12154 * filled.
12155 */
12156int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err)
12157{
12158 if (rule->arg.capid.idx >= px->nb_rsp_cap) {
12159 memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule",
12160 rule->arg.capid.idx);
12161 return 0;
12162 }
12163
12164 return 1;
12165}
12166
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012167/* parse an "http-response capture" action. It takes a single argument which is
12168 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12169 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012170 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012171 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012172enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12173 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012174{
12175 struct sample_expr *expr;
12176 int cur_arg;
12177 int id;
12178 char *error;
12179
12180 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12181 if (strcmp(args[cur_arg], "if") == 0 ||
12182 strcmp(args[cur_arg], "unless") == 0)
12183 break;
12184
12185 if (cur_arg < *orig_arg + 3) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012186 memprintf(err, "expects <expression> id <idx>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012187 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012188 }
12189
12190 cur_arg = *orig_arg;
12191 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12192 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012193 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012194
12195 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12196 memprintf(err,
12197 "fetch method '%s' extracts information from '%s', none of which is available here",
12198 args[cur_arg-1], sample_src_names(expr->fetch->use));
12199 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012200 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012201 }
12202
12203 if (!args[cur_arg] || !*args[cur_arg]) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012204 memprintf(err, "expects 'id'");
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012205 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012206 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012207 }
12208
12209 if (strcmp(args[cur_arg], "id") != 0) {
12210 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12211 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012212 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012213 }
12214
12215 cur_arg++;
12216
12217 if (!args[cur_arg]) {
12218 memprintf(err, "missing id value");
12219 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012220 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012221 }
12222
12223 id = strtol(args[cur_arg], &error, 10);
12224 if (*error != '\0') {
12225 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12226 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012227 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012228 }
12229 cur_arg++;
12230
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012231 proxy->conf.args.ctx = ARGC_CAP;
12232
Thierry FOURNIER42148732015-09-02 17:17:33 +020012233 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012234 rule->action_ptr = http_action_res_capture_by_id;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012235 rule->check_ptr = check_http_res_capture;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012236 rule->arg.capid.expr = expr;
12237 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012238
12239 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012240 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012241}
12242
William Lallemand73025dd2014-04-24 14:38:37 +020012243/*
12244 * Return the struct http_req_action_kw associated to a keyword.
12245 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012246struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012247{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012248 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012249}
12250
12251/*
12252 * Return the struct http_res_action_kw associated to a keyword.
12253 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012254struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012255{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012256 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012257}
12258
Willy Tarreau12207b32016-11-22 19:48:51 +010012259
12260/* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop
12261 * now.
12262 */
12263static int cli_parse_show_errors(char **args, struct appctx *appctx, void *private)
12264{
12265 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
12266 return 1;
12267
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012268 if (*args[2]) {
12269 struct proxy *px;
12270
12271 px = proxy_find_by_name(args[2], 0, 0);
12272 if (px)
12273 appctx->ctx.errors.iid = px->uuid;
12274 else
12275 appctx->ctx.errors.iid = atoi(args[2]);
12276
12277 if (!appctx->ctx.errors.iid) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +020012278 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012279 appctx->ctx.cli.msg = "No such proxy.\n";
12280 appctx->st0 = CLI_ST_PRINT;
12281 return 1;
12282 }
12283 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012284 else
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012285 appctx->ctx.errors.iid = -1; // dump all proxies
12286
Willy Tarreau35069f82016-11-25 09:16:37 +010012287 appctx->ctx.errors.flag = 0;
12288 if (strcmp(args[3], "request") == 0)
12289 appctx->ctx.errors.flag |= 4; // ignore response
12290 else if (strcmp(args[3], "response") == 0)
12291 appctx->ctx.errors.flag |= 2; // ignore request
Willy Tarreau12207b32016-11-22 19:48:51 +010012292 appctx->ctx.errors.px = NULL;
Willy Tarreau12207b32016-11-22 19:48:51 +010012293 return 0;
12294}
12295
12296/* This function dumps all captured errors onto the stream interface's
12297 * read buffer. It returns 0 if the output buffer is full and it needs
12298 * to be called again, otherwise non-zero.
12299 */
12300static int cli_io_handler_show_errors(struct appctx *appctx)
12301{
12302 struct stream_interface *si = appctx->owner;
12303 extern const char *monthname[12];
12304
12305 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
12306 return 1;
12307
12308 chunk_reset(&trash);
12309
12310 if (!appctx->ctx.errors.px) {
12311 /* the function had not been called yet, let's prepare the
12312 * buffer for a response.
12313 */
12314 struct tm tm;
12315
12316 get_localtime(date.tv_sec, &tm);
12317 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
12318 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12319 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
12320 error_snapshot_id);
12321
Willy Tarreau06d80a92017-10-19 14:32:15 +020012322 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012323 /* Socket buffer full. Let's try again later from the same point */
12324 si_applet_cant_put(si);
12325 return 0;
12326 }
12327
12328 appctx->ctx.errors.px = proxy;
Willy Tarreau12207b32016-11-22 19:48:51 +010012329 appctx->ctx.errors.bol = 0;
12330 appctx->ctx.errors.ptr = -1;
12331 }
12332
12333 /* we have two inner loops here, one for the proxy, the other one for
12334 * the buffer.
12335 */
12336 while (appctx->ctx.errors.px) {
12337 struct error_snapshot *es;
12338
Willy Tarreau35069f82016-11-25 09:16:37 +010012339 if ((appctx->ctx.errors.flag & 1) == 0) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012340 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau35069f82016-11-25 09:16:37 +010012341 if (appctx->ctx.errors.flag & 2) // skip req
12342 goto next;
12343 }
12344 else {
Willy Tarreau12207b32016-11-22 19:48:51 +010012345 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau35069f82016-11-25 09:16:37 +010012346 if (appctx->ctx.errors.flag & 4) // skip resp
12347 goto next;
12348 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012349
12350 if (!es->when.tv_sec)
12351 goto next;
12352
12353 if (appctx->ctx.errors.iid >= 0 &&
12354 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
12355 es->oe->uuid != appctx->ctx.errors.iid)
12356 goto next;
12357
12358 if (appctx->ctx.errors.ptr < 0) {
12359 /* just print headers now */
12360
12361 char pn[INET6_ADDRSTRLEN];
12362 struct tm tm;
12363 int port;
12364
12365 get_localtime(es->when.tv_sec, &tm);
12366 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
12367 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12368 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
12369
12370 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
12371 case AF_INET:
12372 case AF_INET6:
12373 port = get_host_port(&es->src);
12374 break;
12375 default:
12376 port = 0;
12377 }
12378
Willy Tarreau35069f82016-11-25 09:16:37 +010012379 switch (appctx->ctx.errors.flag & 1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012380 case 0:
12381 chunk_appendf(&trash,
12382 " frontend %s (#%d): invalid request\n"
12383 " backend %s (#%d)",
12384 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
12385 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
12386 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
12387 break;
12388 case 1:
12389 chunk_appendf(&trash,
12390 " backend %s (#%d): invalid response\n"
12391 " frontend %s (#%d)",
12392 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
12393 es->oe->id, es->oe->uuid);
12394 break;
12395 }
12396
12397 chunk_appendf(&trash,
12398 ", server %s (#%d), event #%u\n"
12399 " src %s:%d, session #%d, session flags 0x%08x\n"
Willy Tarreau10e61cb2017-01-04 14:51:22 +010012400 " HTTP msg state %s(%d), msg flags 0x%08x, tx flags 0x%08x\n"
Willy Tarreau12207b32016-11-22 19:48:51 +010012401 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
12402 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
12403 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
12404 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
12405 es->ev_id,
12406 pn, port, es->sid, es->s_flags,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020012407 h1_msg_state_str(es->state), es->state, es->m_flags, es->t_flags,
Willy Tarreau12207b32016-11-22 19:48:51 +010012408 es->m_clen, es->m_blen,
12409 es->b_flags, es->b_out, es->b_tot,
12410 es->len, es->b_wrap, es->pos);
12411
Willy Tarreau06d80a92017-10-19 14:32:15 +020012412 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012413 /* Socket buffer full. Let's try again later from the same point */
12414 si_applet_cant_put(si);
12415 return 0;
12416 }
12417 appctx->ctx.errors.ptr = 0;
12418 appctx->ctx.errors.sid = es->sid;
12419 }
12420
12421 if (appctx->ctx.errors.sid != es->sid) {
12422 /* the snapshot changed while we were dumping it */
12423 chunk_appendf(&trash,
12424 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +020012425 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012426 si_applet_cant_put(si);
12427 return 0;
12428 }
12429 goto next;
12430 }
12431
12432 /* OK, ptr >= 0, so we have to dump the current line */
12433 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
12434 int newptr;
12435 int newline;
12436
12437 newline = appctx->ctx.errors.bol;
12438 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
12439 if (newptr == appctx->ctx.errors.ptr)
12440 return 0;
12441
Willy Tarreau06d80a92017-10-19 14:32:15 +020012442 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012443 /* Socket buffer full. Let's try again later from the same point */
12444 si_applet_cant_put(si);
12445 return 0;
12446 }
12447 appctx->ctx.errors.ptr = newptr;
12448 appctx->ctx.errors.bol = newline;
12449 };
12450 next:
12451 appctx->ctx.errors.bol = 0;
12452 appctx->ctx.errors.ptr = -1;
Willy Tarreau35069f82016-11-25 09:16:37 +010012453 appctx->ctx.errors.flag ^= 1;
12454 if (!(appctx->ctx.errors.flag & 1))
Willy Tarreau12207b32016-11-22 19:48:51 +010012455 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau12207b32016-11-22 19:48:51 +010012456 }
12457
12458 /* dump complete */
12459 return 1;
12460}
12461
12462/* register cli keywords */
12463static struct cli_kw_list cli_kws = {{ },{
12464 { { "show", "errors", NULL },
12465 "show errors : report last request and response errors for each proxy",
12466 cli_parse_show_errors, cli_io_handler_show_errors, NULL,
12467 },
12468 {{},}
12469}};
12470
Willy Tarreau4a568972010-05-12 08:08:50 +020012471/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012472/* All supported ACL keywords must be declared here. */
12473/************************************************************************/
12474
12475/* Note: must not be declared <const> as its list will be overwritten.
12476 * Please take care of keeping this list alphabetically sorted.
12477 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012478static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012479 { "base", "base", PAT_MATCH_STR },
12480 { "base_beg", "base", PAT_MATCH_BEG },
12481 { "base_dir", "base", PAT_MATCH_DIR },
12482 { "base_dom", "base", PAT_MATCH_DOM },
12483 { "base_end", "base", PAT_MATCH_END },
12484 { "base_len", "base", PAT_MATCH_LEN },
12485 { "base_reg", "base", PAT_MATCH_REG },
12486 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012487
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012488 { "cook", "req.cook", PAT_MATCH_STR },
12489 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12490 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12491 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12492 { "cook_end", "req.cook", PAT_MATCH_END },
12493 { "cook_len", "req.cook", PAT_MATCH_LEN },
12494 { "cook_reg", "req.cook", PAT_MATCH_REG },
12495 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012496
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012497 { "hdr", "req.hdr", PAT_MATCH_STR },
12498 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12499 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12500 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12501 { "hdr_end", "req.hdr", PAT_MATCH_END },
12502 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12503 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12504 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012505
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012506 /* these two declarations uses strings with list storage (in place
12507 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12508 * and delete functions are relative to the list management. The parse
12509 * and match method are related to the corresponding fetch methods. This
12510 * is very particular ACL declaration mode.
12511 */
12512 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12513 { "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 +020012514
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012515 { "path", "path", PAT_MATCH_STR },
12516 { "path_beg", "path", PAT_MATCH_BEG },
12517 { "path_dir", "path", PAT_MATCH_DIR },
12518 { "path_dom", "path", PAT_MATCH_DOM },
12519 { "path_end", "path", PAT_MATCH_END },
12520 { "path_len", "path", PAT_MATCH_LEN },
12521 { "path_reg", "path", PAT_MATCH_REG },
12522 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012523
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012524 { "req_ver", "req.ver", PAT_MATCH_STR },
12525 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012526
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012527 { "scook", "res.cook", PAT_MATCH_STR },
12528 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12529 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12530 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12531 { "scook_end", "res.cook", PAT_MATCH_END },
12532 { "scook_len", "res.cook", PAT_MATCH_LEN },
12533 { "scook_reg", "res.cook", PAT_MATCH_REG },
12534 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012535
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012536 { "shdr", "res.hdr", PAT_MATCH_STR },
12537 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12538 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12539 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12540 { "shdr_end", "res.hdr", PAT_MATCH_END },
12541 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12542 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12543 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012544
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012545 { "url", "url", PAT_MATCH_STR },
12546 { "url_beg", "url", PAT_MATCH_BEG },
12547 { "url_dir", "url", PAT_MATCH_DIR },
12548 { "url_dom", "url", PAT_MATCH_DOM },
12549 { "url_end", "url", PAT_MATCH_END },
12550 { "url_len", "url", PAT_MATCH_LEN },
12551 { "url_reg", "url", PAT_MATCH_REG },
12552 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012553
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012554 { "urlp", "urlp", PAT_MATCH_STR },
12555 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12556 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12557 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12558 { "urlp_end", "urlp", PAT_MATCH_END },
12559 { "urlp_len", "urlp", PAT_MATCH_LEN },
12560 { "urlp_reg", "urlp", PAT_MATCH_REG },
12561 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012562
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012563 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012564}};
12565
12566/************************************************************************/
12567/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012568/************************************************************************/
12569/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012570static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012571 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012572 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012573 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12574
Willy Tarreau87b09662015-04-03 00:22:06 +020012575 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012576 { "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 +020012577
12578 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012579 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12580 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12581 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012582
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012583 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
12584 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012585
Willy Tarreau409bcde2013-01-08 00:31:00 +010012586 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12587 * are only here to match the ACL's name, are request-only and are used
12588 * for ACL compatibility only.
12589 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012590 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12591 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012592 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12593 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012594
12595 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12596 * only here to match the ACL's name, are request-only and are used for
12597 * ACL compatibility only.
12598 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012599 { "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 +020012600 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012601 { "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 +020012602 { "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 +010012603
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012604 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012605 { "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 +010012606 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012607 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012608 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012609 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012610
12611 /* HTTP protocol on the request path */
12612 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012613 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012614
12615 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012616 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12617 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012618
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012619 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012620 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
12621 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012622 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012623
Thierry FOURNIERd7d88812017-04-19 15:15:14 +020012624 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER5617dce2017-04-09 05:38:19 +020012625 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12626
Willy Tarreau18ed2562013-01-14 15:56:36 +010012627 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012628 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12629 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012630
Willy Tarreau18ed2562013-01-14 15:56:36 +010012631 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012632 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012633 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12634 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012635
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012636 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012637 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012638 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012639 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012640 { "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 +010012641 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012642 { "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 +010012643
12644 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012645 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012646 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12647 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012648
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012649 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012650 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012651 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012652 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012653 { "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 +010012654 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012655 { "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 +010012656
Willy Tarreau409bcde2013-01-08 00:31:00 +010012657 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012658 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012659 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12660 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012661 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010012662
12663 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012664 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012665 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012666 { "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 +020012667 { "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 +010012668
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012669 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020012670 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012671 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012672 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012673 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012674 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012675 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020012676 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12677 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012678 { "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 +010012679 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020012680}};
12681
Willy Tarreau8797c062007-05-07 00:55:35 +020012682
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012683/************************************************************************/
12684/* All supported converter keywords must be declared here. */
12685/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020012686/* Note: must not be declared <const> as its list will be overwritten */
12687static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012688 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020012689 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012690 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
12691 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012692 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020012693 { NULL, NULL, 0, 0, 0 },
12694}};
12695
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012696
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012697/************************************************************************/
12698/* All supported http-request action keywords must be declared here. */
12699/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012700struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012701 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020012702 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012703 { "set-method", parse_set_req_line },
12704 { "set-path", parse_set_req_line },
12705 { "set-query", parse_set_req_line },
12706 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020012707 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012708 }
12709};
12710
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012711struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012712 .kw = {
12713 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012714 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012715 { NULL, NULL }
12716 }
12717};
12718
Willy Tarreau8797c062007-05-07 00:55:35 +020012719__attribute__((constructor))
12720static void __http_protocol_init(void)
12721{
12722 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020012723 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020012724 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012725 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012726 http_res_keywords_register(&http_res_actions);
Willy Tarreau12207b32016-11-22 19:48:51 +010012727 cli_register_kw(&cli_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020012728}
12729
12730
Willy Tarreau58f10d72006-12-04 02:26:12 +010012731/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020012732 * Local variables:
12733 * c-indent-level: 8
12734 * c-basic-offset: 8
12735 * End:
12736 */