blob: 939a7a137f786ede31447796ffc39b0bb9a3dc3b [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) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001734 http_capture_bad_message(&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);
1745 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001746 if (sess->listener->counters)
1747 sess->listener->counters->failed_req++;
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) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001767 http_capture_bad_message(&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);
1778 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001779 if (sess->listener->counters)
1780 sess->listener->counters->failed_req++;
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)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001799 http_capture_bad_message(&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);
1808 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001809 if (sess->listener->counters)
1810 sess->listener->counters->failed_req++;
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))
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001892 http_capture_bad_message(&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;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001919 sess->fe->fe_counters.intercepted_req++;
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 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002170 http_capture_bad_message(&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
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002178 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002179 if (sess->listener->counters)
2180 sess->listener->counters->failed_req++;
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
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002720 if (stkctr_entry(&s->stkctr[http_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;
2724 void *ptr;
2725
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))) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002730 stream_track_stkctr(&s->stkctr[http_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 */
2733 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2734 if (ptr)
2735 stktable_data_cast(ptr, http_req_cnt)++;
2736
2737 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2738 if (ptr)
2739 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2740 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2741
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002742 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002743 if (sess->fe != s->be)
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002744 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02002745 }
2746 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02002747 break;
2748
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002749 /* other flags exists, but normaly, they never be matched. */
2750 default:
2751 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002752 }
2753 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01002754
2755 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02002756 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002757}
2758
Willy Tarreau71241ab2012-12-27 11:30:54 +01002759
Willy Tarreau51d861a2015-05-22 17:30:48 +02002760/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2761 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2762 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2763 * is returned, the process can continue the evaluation of next rule list. If
2764 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2765 * is returned, it means the operation could not be processed and a server error
2766 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
2767 * deny rule. If *YIELD is returned, the caller must call again the function
2768 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002769 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002770static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002771http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002772{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002773 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002774 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002775 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002776 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002777 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02002778 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002779
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002780 /* If "the current_rule_list" match the executed rule list, we are in
2781 * resume condition. If a resume is needed it is always in the action
2782 * and never in the ACL or converters. In this case, we initialise the
2783 * current rule, and go to the action execution point.
2784 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002785 if (s->current_rule) {
2786 rule = s->current_rule;
2787 s->current_rule = NULL;
2788 if (s->current_rule_list == rules)
2789 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002790 }
2791 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002792
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002793 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002794
2795 /* check optional condition */
2796 if (rule->cond) {
2797 int ret;
2798
Willy Tarreau192252e2015-04-04 01:47:55 +02002799 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002800 ret = acl_pass(ret);
2801
2802 if (rule->cond->pol == ACL_COND_UNLESS)
2803 ret = !ret;
2804
2805 if (!ret) /* condition not matched */
2806 continue;
2807 }
2808
Willy Tarreauacc98002015-09-27 23:34:39 +02002809 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002810resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002811 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002812 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002813 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002814
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002815 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002816 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002817 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002818
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002819 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002820 s->task->nice = rule->arg.nice;
2821 break;
2822
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002823 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002824 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002825 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002826 break;
2827
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002828 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002829#ifdef SO_MARK
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 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002832#endif
2833 break;
2834
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002835 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002836 s->logs.level = rule->arg.loglevel;
2837 break;
2838
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002839 case ACT_HTTP_REPLACE_HDR:
2840 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002841 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
2842 rule->arg.hdr_add.name_len,
2843 &rule->arg.hdr_add.fmt,
2844 &rule->arg.hdr_add.re, rule->action))
Christopher Fauletcdade942017-02-08 12:41:31 +01002845 return HTTP_RULE_RES_BADREQ;
Sasha Pachev218f0642014-06-16 12:05:59 -06002846 break;
2847
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002848 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002849 ctx.idx = 0;
2850 /* remove all occurrences of the header */
2851 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2852 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
2853 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2854 }
Willy Tarreau85603282015-01-21 20:39:27 +01002855 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002856
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002857 case ACT_HTTP_SET_HDR:
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002858 case ACT_HTTP_ADD_HDR: {
2859 struct chunk *replace;
2860
2861 replace = alloc_trash_chunk();
2862 if (!replace)
2863 return HTTP_RULE_RES_BADREQ;
2864
2865 chunk_printf(replace, "%s: ", rule->arg.hdr_add.name);
2866 memcpy(replace->str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
2867 replace->len = rule->arg.hdr_add.name_len;
2868 replace->str[replace->len++] = ':';
2869 replace->str[replace->len++] = ' ';
2870 replace->len += build_logline(s, replace->str + replace->len, replace->size - replace->len,
2871 &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01002872
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002873 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002874 /* remove all occurrences of the header */
2875 ctx.idx = 0;
2876 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2877 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
2878 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2879 }
2880 }
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002881 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->str, replace->len);
2882
2883 free_trash_chunk(replace);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002884 break;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002885 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002886
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002887 case ACT_HTTP_DEL_ACL:
2888 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002889 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002890 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002891
2892 /* collect reference */
2893 ref = pat_ref_lookup(rule->arg.map.ref);
2894 if (!ref)
2895 continue;
2896
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002897 /* allocate key */
2898 key = alloc_trash_chunk();
2899 if (!key)
2900 return HTTP_RULE_RES_BADREQ;
2901
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002902 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002903 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2904 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002905
2906 /* perform update */
2907 /* returned code: 1=ok, 0=ko */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002908 pat_ref_delete(ref, key->str);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002909
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002910 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002911 break;
2912 }
2913
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002914 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002915 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002916 struct chunk *key;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002917
2918 /* collect reference */
2919 ref = pat_ref_lookup(rule->arg.map.ref);
2920 if (!ref)
2921 continue;
2922
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002923 /* allocate key */
2924 key = alloc_trash_chunk();
2925 if (!key)
2926 return HTTP_RULE_RES_BADREQ;
2927
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002928 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002929 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2930 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002931
2932 /* perform update */
2933 /* check if the entry already exists */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002934 if (pat_ref_find_elt(ref, key->str) == NULL)
2935 pat_ref_add(ref, key->str, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002936
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002937 free_trash_chunk(key);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002938 break;
2939 }
2940
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002941 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002942 struct pat_ref *ref;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002943 struct chunk *key, *value;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002944
2945 /* collect reference */
2946 ref = pat_ref_lookup(rule->arg.map.ref);
2947 if (!ref)
2948 continue;
2949
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002950 /* allocate key */
2951 key = alloc_trash_chunk();
2952 if (!key)
2953 return HTTP_RULE_RES_BADREQ;
2954
2955 /* allocate value */
2956 value = alloc_trash_chunk();
2957 if (!value) {
2958 free_trash_chunk(key);
2959 return HTTP_RULE_RES_BADREQ;
2960 }
2961
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002962 /* collect key */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002963 key->len = build_logline(s, key->str, key->size, &rule->arg.map.key);
2964 key->str[key->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002965
2966 /* collect value */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002967 value->len = build_logline(s, value->str, value->size, &rule->arg.map.value);
2968 value->str[value->len] = '\0';
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002969
2970 /* perform update */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002971 if (pat_ref_find_elt(ref, key->str) != NULL)
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002972 /* update entry if it exists */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002973 pat_ref_set(ref, key->str, value->str, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002974 else
2975 /* insert a new entry */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002976 pat_ref_add(ref, key->str, value->str, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002977
Dragan Dosen2ae327e2017-10-26 11:25:10 +02002978 free_trash_chunk(key);
2979 free_trash_chunk(value);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002980 break;
2981 }
William Lallemand73025dd2014-04-24 14:38:37 +02002982
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002983 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02002984 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
2985 return HTTP_RULE_RES_BADREQ;
2986 return HTTP_RULE_RES_DONE;
2987
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002988 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
2989 /* Note: only the first valid tracking parameter of each
2990 * applies.
2991 */
2992
2993 if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) {
2994 struct stktable *t;
2995 struct stksess *ts;
2996 struct stktable_key *key;
2997 void *ptr;
2998
2999 t = rule->arg.trk_ctr.table.t;
3000 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
3001
3002 if (key && (ts = stktable_get_entry(t, key))) {
3003 stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts);
3004
3005 /* let's count a new HTTP request as it's the first time we do it */
3006 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3007 if (ptr)
3008 stktable_data_cast(ptr, http_req_cnt)++;
3009
3010 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3011 if (ptr)
3012 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3013 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3014
3015 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3016 if (sess->fe != s->be)
3017 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3018
3019 /* When the client triggers a 4xx from the server, it's most often due
3020 * to a missing object or permission. These events should be tracked
3021 * because if they happen often, it may indicate a brute force or a
3022 * vulnerability scan. Normally this is done when receiving the response
3023 * but here we're tracking after this ought to have been done so we have
3024 * to do it on purpose.
3025 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02003026 if ((unsigned)(txn->status - 400) < 100) {
3027 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3028 if (ptr)
3029 stktable_data_cast(ptr, http_err_cnt)++;
3030
3031 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3032 if (ptr)
3033 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3034 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3035 }
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003036 }
3037 }
3038 break;
3039
Thierry FOURNIER42148732015-09-02 17:17:33 +02003040 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02003041 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
3042 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003043
Willy Tarreauacc98002015-09-27 23:34:39 +02003044 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003045 case ACT_RET_ERR:
3046 case ACT_RET_CONT:
3047 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003048 case ACT_RET_STOP:
3049 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003050 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003051 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003052 return HTTP_RULE_RES_YIELD;
3053 }
William Lallemand73025dd2014-04-24 14:38:37 +02003054 break;
3055
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003056 /* other flags exists, but normaly, they never be matched. */
3057 default:
3058 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003059 }
3060 }
3061
3062 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003063 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003064}
3065
3066
Willy Tarreau71241ab2012-12-27 11:30:54 +01003067/* Perform an HTTP redirect based on the information in <rule>. The function
3068 * returns non-zero on success, or zero in case of a, irrecoverable error such
3069 * as too large a request to build a valid response.
3070 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003071static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01003072{
Willy Tarreaub329a312015-05-22 16:27:37 +02003073 struct http_msg *req = &txn->req;
3074 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003075 const char *msg_fmt;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003076 struct chunk *chunk;
3077 int ret = 0;
3078
3079 chunk = alloc_trash_chunk();
3080 if (!chunk)
3081 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003082
3083 /* build redirect message */
3084 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003085 case 308:
3086 msg_fmt = HTTP_308;
3087 break;
3088 case 307:
3089 msg_fmt = HTTP_307;
3090 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003091 case 303:
3092 msg_fmt = HTTP_303;
3093 break;
3094 case 301:
3095 msg_fmt = HTTP_301;
3096 break;
3097 case 302:
3098 default:
3099 msg_fmt = HTTP_302;
3100 break;
3101 }
3102
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003103 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
3104 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003105
3106 switch(rule->type) {
3107 case REDIRECT_TYPE_SCHEME: {
3108 const char *path;
3109 const char *host;
3110 struct hdr_ctx ctx;
3111 int pathlen;
3112 int hostlen;
3113
3114 host = "";
3115 hostlen = 0;
3116 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02003117 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003118 host = ctx.line + ctx.val;
3119 hostlen = ctx.vlen;
3120 }
3121
3122 path = http_get_path(txn);
3123 /* build message using path */
3124 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02003125 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003126 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3127 int qs = 0;
3128 while (qs < pathlen) {
3129 if (path[qs] == '?') {
3130 pathlen = qs;
3131 break;
3132 }
3133 qs++;
3134 }
3135 }
3136 } else {
3137 path = "/";
3138 pathlen = 1;
3139 }
3140
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003141 if (rule->rdr_str) { /* this is an old "redirect" rule */
3142 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003143 if (chunk->len + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
3144 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003145
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003146 /* add scheme */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003147 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3148 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003149 }
3150 else {
3151 /* add scheme with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003152 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003153
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003154 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003155 if (chunk->len + 3 + hostlen + pathlen > chunk->size - 4)
3156 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003157 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003158 /* add "://" */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003159 memcpy(chunk->str + chunk->len, "://", 3);
3160 chunk->len += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003161
3162 /* add host */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003163 memcpy(chunk->str + chunk->len, host, hostlen);
3164 chunk->len += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003165
3166 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003167 memcpy(chunk->str + chunk->len, path, pathlen);
3168 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003169
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003170 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003171 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003172 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003173 if (chunk->len > chunk->size - 5)
3174 goto leave;
3175 chunk->str[chunk->len] = '/';
3176 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003177 }
3178
3179 break;
3180 }
3181 case REDIRECT_TYPE_PREFIX: {
3182 const char *path;
3183 int pathlen;
3184
3185 path = http_get_path(txn);
3186 /* build message using path */
3187 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02003188 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003189 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3190 int qs = 0;
3191 while (qs < pathlen) {
3192 if (path[qs] == '?') {
3193 pathlen = qs;
3194 break;
3195 }
3196 qs++;
3197 }
3198 }
3199 } else {
3200 path = "/";
3201 pathlen = 1;
3202 }
3203
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003204 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003205 if (chunk->len + rule->rdr_len + pathlen > chunk->size - 4)
3206 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003207
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003208 /* add prefix. Note that if prefix == "/", we don't want to
3209 * add anything, otherwise it makes it hard for the user to
3210 * configure a self-redirection.
3211 */
3212 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003213 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3214 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003215 }
3216 }
3217 else {
3218 /* add prefix with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003219 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003220
3221 /* Check length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003222 if (chunk->len + pathlen > chunk->size - 4)
3223 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003224 }
3225
3226 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003227 memcpy(chunk->str + chunk->len, path, pathlen);
3228 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003229
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003230 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003231 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003232 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003233 if (chunk->len > chunk->size - 5)
3234 goto leave;
3235 chunk->str[chunk->len] = '/';
3236 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003237 }
3238
3239 break;
3240 }
3241 case REDIRECT_TYPE_LOCATION:
3242 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003243 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003244 if (chunk->len + rule->rdr_len > chunk->size - 4)
3245 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003246
3247 /* add location */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003248 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3249 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003250 }
3251 else {
3252 /* add location with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003253 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003254
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003255 /* Check left length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003256 if (chunk->len > chunk->size - 4)
3257 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003258 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003259 break;
3260 }
3261
3262 if (rule->cookie_len) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003263 memcpy(chunk->str + chunk->len, "\r\nSet-Cookie: ", 14);
3264 chunk->len += 14;
3265 memcpy(chunk->str + chunk->len, rule->cookie_str, rule->cookie_len);
3266 chunk->len += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003267 }
3268
Willy Tarreau19b14122017-02-28 09:48:11 +01003269 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003270 txn->status = rule->code;
3271 /* let's log the request time */
3272 s->logs.tv_request = now;
3273
Christopher Fauletbe821b92017-03-30 11:21:53 +02003274 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003275 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3276 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3277 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02003278 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003279 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003280 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: keep-alive", 30);
3281 chunk->len += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003282 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003283 memcpy(chunk->str + chunk->len, "\r\nConnection: keep-alive", 24);
3284 chunk->len += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003285 }
3286 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003287 memcpy(chunk->str + chunk->len, "\r\n\r\n", 4);
3288 chunk->len += 4;
3289 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau06d80a92017-10-19 14:32:15 +02003290 co_inject(res->chn, chunk->str, chunk->len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003291 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02003292 bi_fast_delete(req->chn->buf, req->sov);
3293 req->next -= req->sov;
3294 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003295 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01003296 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02003297 req->msg_state = HTTP_MSG_CLOSED;
3298 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02003299 /* Trim any possible response */
3300 res->chn->buf->i = 0;
3301 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02003302 /* let the server side turn to SI_ST_CLO */
3303 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003304 } else {
3305 /* keep-alive not possible */
3306 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003307 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3308 chunk->len += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003309 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003310 memcpy(chunk->str + chunk->len, "\r\nConnection: close\r\n\r\n", 23);
3311 chunk->len += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003312 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003313 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01003314 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003315 }
3316
Willy Tarreaue7dff022015-04-03 01:14:29 +02003317 if (!(s->flags & SF_ERR_MASK))
3318 s->flags |= SF_ERR_LOCAL;
3319 if (!(s->flags & SF_FINST_MASK))
3320 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003321
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003322 ret = 1;
3323 leave:
3324 free_trash_chunk(chunk);
3325 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003326}
3327
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003328/* This stream analyser runs all HTTP request processing which is common to
3329 * frontends and backends, which means blocking ACLs, filters, connection-close,
3330 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003331 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003332 * either needs more data or wants to immediately abort the request (eg: deny,
3333 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003334 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003335int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003336{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003337 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003338 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02003339 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003340 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003341 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02003342 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003343 int deny_status = HTTP_ERR_403;
Willy Tarreaud787e662009-07-07 10:14:51 +02003344
Willy Tarreau655dce92009-11-08 13:10:58 +01003345 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003346 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003347 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02003348 }
3349
Willy Tarreau87b09662015-04-03 00:22:06 +02003350 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 +02003351 now_ms, __FUNCTION__,
3352 s,
3353 req,
3354 req->rex, req->wex,
3355 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003356 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003357 req->analysers);
3358
Willy Tarreau65410832014-04-28 21:25:43 +02003359 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02003360 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02003361
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003362 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02003363 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02003364 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01003365
Willy Tarreau0b748332014-04-29 00:13:29 +02003366 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003367 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
3368 goto return_prx_yield;
3369
Willy Tarreau0b748332014-04-29 00:13:29 +02003370 case HTTP_RULE_RES_CONT:
3371 case HTTP_RULE_RES_STOP: /* nothing to do */
3372 break;
Willy Tarreau52542592014-04-28 18:33:26 +02003373
Willy Tarreau0b748332014-04-29 00:13:29 +02003374 case HTTP_RULE_RES_DENY: /* deny or tarpit */
3375 if (txn->flags & TX_CLTARPIT)
3376 goto tarpit;
3377 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003378
Willy Tarreau0b748332014-04-29 00:13:29 +02003379 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
3380 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02003381
Willy Tarreau0b748332014-04-29 00:13:29 +02003382 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02003383 goto done;
3384
Willy Tarreau0b748332014-04-29 00:13:29 +02003385 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
3386 goto return_bad_req;
3387 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003388 }
3389
Willy Tarreau52542592014-04-28 18:33:26 +02003390 /* OK at this stage, we know that the request was accepted according to
3391 * the http-request rules, we can check for the stats. Note that the
3392 * URI is detected *before* the req* rules in order not to be affected
3393 * by a possible reqrep, while they are processed *after* so that a
3394 * reqdeny can still block them. This clearly needs to change in 1.6!
3395 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003396 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02003397 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01003398 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02003399 txn->status = 500;
3400 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003401 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003402
Willy Tarreaue7dff022015-04-03 01:14:29 +02003403 if (!(s->flags & SF_ERR_MASK))
3404 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02003405 goto return_prx_cond;
3406 }
3407
3408 /* parse the whole stats request and extract the relevant information */
3409 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02003410 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02003411 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003412
Willy Tarreau0b748332014-04-29 00:13:29 +02003413 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
3414 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003415
Willy Tarreau0b748332014-04-29 00:13:29 +02003416 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
3417 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003418 }
3419
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003420 /* evaluate the req* rules except reqadd */
3421 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003422 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003423 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003424
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003425 if (txn->flags & TX_CLDENY)
3426 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003427
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003428 if (txn->flags & TX_CLTARPIT) {
3429 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003430 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003431 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003432 }
Willy Tarreau06619262006-12-17 08:37:22 +01003433
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003434 /* add request headers from the rule sets in the same order */
3435 list_for_each_entry(wl, &px->req_add, list) {
3436 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003437 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003438 ret = acl_pass(ret);
3439 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3440 ret = !ret;
3441 if (!ret)
3442 continue;
3443 }
3444
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003445 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003446 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003447 }
3448
Willy Tarreau52542592014-04-28 18:33:26 +02003449
3450 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003451 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003452 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003453 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
3454 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003455
Willy Tarreaue7dff022015-04-03 01:14:29 +02003456 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
3457 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
3458 if (!(s->flags & SF_FINST_MASK))
3459 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003460
Willy Tarreau70730dd2014-04-24 18:06:27 +02003461 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01003462 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
3463 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003464 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003465 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003466 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003467
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003468 /* check whether we have some ACLs set to redirect this request */
3469 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003470 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003471 int ret;
3472
Willy Tarreau192252e2015-04-04 01:47:55 +02003473 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003474 ret = acl_pass(ret);
3475 if (rule->cond->pol == ACL_COND_UNLESS)
3476 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003477 if (!ret)
3478 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003479 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003480 if (!http_apply_redirect_rule(rule, s, txn))
3481 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003482 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003483 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003484
Willy Tarreau2be39392010-01-03 17:24:51 +01003485 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3486 * If this happens, then the data will not come immediately, so we must
3487 * send all what we have without waiting. Note that due to the small gain
3488 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003489 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003490 * itself once used.
3491 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003492 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003493
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003494 done: /* done with this analyser, continue with next ones that the calling
3495 * points will have set, if any.
3496 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003497 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003498 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
3499 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003500 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003501
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003502 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02003503 /* Allow cookie logging
3504 */
3505 if (s->be->cookie_name || sess->fe->capture_name)
3506 manage_client_side_cookies(s, req);
3507
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003508 /* When a connection is tarpitted, we use the tarpit timeout,
3509 * which may be the same as the connect timeout if unspecified.
3510 * If unset, then set it to zero because we really want it to
3511 * eventually expire. We build the tarpit as an analyser.
3512 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003513 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003514
3515 /* wipe the request out so that we can drop the connection early
3516 * if the client closes first.
3517 */
3518 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003519
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003520 txn->status = http_err_codes[deny_status];
3521
Christopher Faulet0184ea72017-01-05 14:06:34 +01003522 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003523 req->analysers |= AN_REQ_HTTP_TARPIT;
3524 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3525 if (!req->analyse_exp)
3526 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02003527 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003528 sess->fe->fe_counters.denied_req++;
3529 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003530 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003531 if (sess->listener->counters)
3532 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003533 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003534
3535 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003536
3537 /* Allow cookie logging
3538 */
3539 if (s->be->cookie_name || sess->fe->capture_name)
3540 manage_client_side_cookies(s, req);
3541
Willy Tarreau0b748332014-04-29 00:13:29 +02003542 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003543 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003544 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003545 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02003546 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003547 sess->fe->fe_counters.denied_req++;
3548 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003549 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003550 if (sess->listener->counters)
3551 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003552 goto return_prx_cond;
3553
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003554 return_bad_req:
3555 /* We centralize bad requests processing here */
3556 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3557 /* we detected a parsing error. We want to archive this request
3558 * in the dedicated proxy area for later troubleshooting.
3559 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003560 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003561 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003562
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003563 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003564 txn->req.msg_state = HTTP_MSG_ERROR;
3565 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003566 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003567
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003568 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003569 if (sess->listener->counters)
3570 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003571
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003572 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003573 if (!(s->flags & SF_ERR_MASK))
3574 s->flags |= SF_ERR_PRXCOND;
3575 if (!(s->flags & SF_FINST_MASK))
3576 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003577
Christopher Faulet0184ea72017-01-05 14:06:34 +01003578 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003579 req->analyse_exp = TICK_ETERNITY;
3580 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003581
3582 return_prx_yield:
3583 channel_dont_connect(req);
3584 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003585}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003586
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003587/* This function performs all the processing enabled for the current request.
3588 * It returns 1 if the processing can continue on next analysers, or zero if it
3589 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003590 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003591 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003592int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003593{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003594 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003595 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003596 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02003597 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003598
Willy Tarreau655dce92009-11-08 13:10:58 +01003599 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003600 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003601 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003602 return 0;
3603 }
3604
Willy Tarreau87b09662015-04-03 00:22:06 +02003605 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 +02003606 now_ms, __FUNCTION__,
3607 s,
3608 req,
3609 req->rex, req->wex,
3610 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003611 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003612 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003613
Willy Tarreau59234e92008-11-30 23:51:27 +01003614 /*
3615 * Right now, we know that we have processed the entire headers
3616 * and that unwanted requests have been filtered out. We can do
3617 * whatever we want with the remaining request. Also, now we
3618 * may have separate values for ->fe, ->be.
3619 */
Willy Tarreau06619262006-12-17 08:37:22 +01003620
Willy Tarreau59234e92008-11-30 23:51:27 +01003621 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003622 * If HTTP PROXY is set we simply get remote server address parsing
3623 * incoming request. Note that this requires that a connection is
3624 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003625 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003626 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003627 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003628 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003629
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003630 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau973a5422015-08-05 21:47:23 +02003631 if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003632 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003633 txn->req.msg_state = HTTP_MSG_ERROR;
3634 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003635 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003636 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003637
Willy Tarreaue7dff022015-04-03 01:14:29 +02003638 if (!(s->flags & SF_ERR_MASK))
3639 s->flags |= SF_ERR_RESOURCE;
3640 if (!(s->flags & SF_FINST_MASK))
3641 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003642
3643 return 0;
3644 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003645
3646 path = http_get_path(txn);
3647 url2sa(req->buf->p + msg->sl.rq.u,
3648 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01003649 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003650 /* if the path was found, we have to remove everything between
3651 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
3652 * found, we need to replace from req->buf->p + msg->sl.rq.u for
3653 * u_l characters by a single "/".
3654 */
3655 if (path) {
3656 char *cur_ptr = req->buf->p;
3657 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3658 int delta;
3659
3660 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
3661 http_msg_move_end(&txn->req, delta);
3662 cur_end += delta;
3663 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3664 goto return_bad_req;
3665 }
3666 else {
3667 char *cur_ptr = req->buf->p;
3668 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3669 int delta;
3670
3671 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
3672 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
3673 http_msg_move_end(&txn->req, delta);
3674 cur_end += delta;
3675 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3676 goto return_bad_req;
3677 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003678 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003679
Willy Tarreau59234e92008-11-30 23:51:27 +01003680 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003681 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003682 * Note that doing so might move headers in the request, but
3683 * the fields will stay coherent and the URI will not move.
3684 * This should only be performed in the backend.
3685 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003686 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003687 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003688
William Lallemanda73203e2012-03-12 12:48:57 +01003689 /* add unique-id if "header-unique-id" is specified */
3690
Thierry Fournierf4011dd2016-03-29 17:23:51 +02003691 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003692 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
3693 goto return_bad_req;
3694 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003695 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003696 }
William Lallemanda73203e2012-03-12 12:48:57 +01003697
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003698 if (sess->fe->header_unique_id && s->unique_id) {
3699 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003700 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003701 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003702 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003703 goto return_bad_req;
3704 }
3705
Cyril Bontéb21570a2009-11-29 20:04:48 +01003706 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003707 * 9: add X-Forwarded-For if either the frontend or the backend
3708 * asks for it.
3709 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003710 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003711 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003712 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
3713 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
3714 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003715 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003716 /* The header is set to be added only if none is present
3717 * and we found it, so don't do anything.
3718 */
3719 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003720 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003721 /* Add an X-Forwarded-For header unless the source IP is
3722 * in the 'except' network range.
3723 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003724 if ((!sess->fe->except_mask.s_addr ||
3725 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
3726 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01003727 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003728 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003729 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003730 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003731 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003732 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003733
3734 /* Note: we rely on the backend to get the header name to be used for
3735 * x-forwarded-for, because the header is really meant for the backends.
3736 * However, if the backend did not specify any option, we have to rely
3737 * on the frontend's header name.
3738 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003739 if (s->be->fwdfor_hdr_len) {
3740 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003741 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003742 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003743 len = sess->fe->fwdfor_hdr_len;
3744 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003745 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003746 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 +01003747
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003748 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003749 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003750 }
3751 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003752 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003753 /* FIXME: for the sake of completeness, we should also support
3754 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003755 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003756 int len;
3757 char pn[INET6_ADDRSTRLEN];
3758 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003759 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003760 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003761
Willy Tarreau59234e92008-11-30 23:51:27 +01003762 /* Note: we rely on the backend to get the header name to be used for
3763 * x-forwarded-for, because the header is really meant for the backends.
3764 * However, if the backend did not specify any option, we have to rely
3765 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003766 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003767 if (s->be->fwdfor_hdr_len) {
3768 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003769 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003770 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003771 len = sess->fe->fwdfor_hdr_len;
3772 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003773 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003774 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003775
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003776 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003777 goto return_bad_req;
3778 }
3779 }
3780
3781 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003782 * 10: add X-Original-To if either the frontend or the backend
3783 * asks for it.
3784 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003785 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003786
3787 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003788 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003789 /* Add an X-Original-To header unless the destination IP is
3790 * in the 'except' network range.
3791 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003792 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003793
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003794 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003795 ((!sess->fe->except_mask_to.s_addr ||
3796 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
3797 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003798 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003799 (((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 +02003800 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003801 int len;
3802 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003803 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003804
3805 /* Note: we rely on the backend to get the header name to be used for
3806 * x-original-to, because the header is really meant for the backends.
3807 * However, if the backend did not specify any option, we have to rely
3808 * on the frontend's header name.
3809 */
3810 if (s->be->orgto_hdr_len) {
3811 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003812 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003813 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003814 len = sess->fe->orgto_hdr_len;
3815 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003816 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003817 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 +02003818
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003819 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003820 goto return_bad_req;
3821 }
3822 }
3823 }
3824
Willy Tarreau50fc7772012-11-11 22:19:57 +01003825 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3826 * If an "Upgrade" token is found, the header is left untouched in order not to have
3827 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3828 * "Upgrade" is present in the Connection header.
3829 */
3830 if (!(txn->flags & TX_HDR_CONN_UPG) &&
3831 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003832 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003833 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003834 unsigned int want_flags = 0;
3835
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003836 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003837 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003838 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003839 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003840 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003841 want_flags |= TX_CON_CLO_SET;
3842 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003843 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003844 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003845 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003846 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003847 want_flags |= TX_CON_KAL_SET;
3848 }
3849
3850 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003851 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003852 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003853
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003854
Willy Tarreau522d6c02009-12-06 18:49:18 +01003855 /* If we have no server assigned yet and we're balancing on url_param
3856 * with a POST request, we may be interested in checking the body for
3857 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003858 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003859 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02003860 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003861 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003862 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003863 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003864 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003865
Christopher Fauletbe821b92017-03-30 11:21:53 +02003866 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3867 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003868#ifdef TCP_QUICKACK
Christopher Fauletbe821b92017-03-30 11:21:53 +02003869 /* We expect some data from the client. Unless we know for sure
3870 * we already have a full request, we have to re-enable quick-ack
3871 * in case we previously disabled it, otherwise we might cause
3872 * the client to delay further data.
3873 */
3874 if ((sess->listener->options & LI_O_NOQUICKACK) &&
3875 cli_conn && conn_ctrl_ready(cli_conn) &&
3876 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
3877 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreau585744b2017-08-24 14:31:19 +02003878 setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003879#endif
Willy Tarreau03945942009-12-22 16:50:27 +01003880
Willy Tarreau59234e92008-11-30 23:51:27 +01003881 /*************************************************************
3882 * OK, that's finished for the headers. We have done what we *
3883 * could. Let's switch to the DATA state. *
3884 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003885 req->analyse_exp = TICK_ETERNITY;
3886 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003887
Willy Tarreau59234e92008-11-30 23:51:27 +01003888 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003889 /* OK let's go on with the BODY now */
3890 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003891
Willy Tarreau59234e92008-11-30 23:51:27 +01003892 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003893 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003894 /* we detected a parsing error. We want to archive this request
3895 * in the dedicated proxy area for later troubleshooting.
3896 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003897 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003898 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003899
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003900 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01003901 txn->req.msg_state = HTTP_MSG_ERROR;
3902 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003903 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003904 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003905
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003906 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003907 if (sess->listener->counters)
3908 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003909
Willy Tarreaue7dff022015-04-03 01:14:29 +02003910 if (!(s->flags & SF_ERR_MASK))
3911 s->flags |= SF_ERR_PRXCOND;
3912 if (!(s->flags & SF_FINST_MASK))
3913 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003914 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003915}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003916
Willy Tarreau60b85b02008-11-30 23:28:40 +01003917/* This function is an analyser which processes the HTTP tarpit. It always
3918 * returns zero, at the beginning because it prevents any other processing
3919 * from occurring, and at the end because it terminates the request.
3920 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003921int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003922{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003923 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003924
3925 /* This connection is being tarpitted. The CLIENT side has
3926 * already set the connect expiration date to the right
3927 * timeout. We just have to check that the client is still
3928 * there and that the timeout has not expired.
3929 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003930 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003931 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003932 !tick_is_expired(req->analyse_exp, now_ms))
3933 return 0;
3934
3935 /* We will set the queue timer to the time spent, just for
3936 * logging purposes. We fake a 500 server error, so that the
3937 * attacker will not suspect his connection has been tarpitted.
3938 * It will not cause trouble to the logs because we can exclude
3939 * the tarpitted connections by filtering on the 'PT' status flags.
3940 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003941 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3942
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003943 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003944 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003945
Christopher Faulet0184ea72017-01-05 14:06:34 +01003946 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003947 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003948
Willy Tarreaue7dff022015-04-03 01:14:29 +02003949 if (!(s->flags & SF_ERR_MASK))
3950 s->flags |= SF_ERR_PRXCOND;
3951 if (!(s->flags & SF_FINST_MASK))
3952 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003953 return 0;
3954}
3955
Willy Tarreau5a8f9472014-04-10 11:16:06 +02003956/* This function is an analyser which waits for the HTTP request body. It waits
3957 * for either the buffer to be full, or the full advertised contents to have
3958 * reached the buffer. It must only be called after the standard HTTP request
3959 * processing has occurred, because it expects the request to be parsed and will
3960 * look for the Expect header. It may send a 100-Continue interim response. It
3961 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
3962 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
3963 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01003964 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003965int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003966{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003967 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003968 struct http_txn *txn = s->txn;
3969 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003970
3971 /* We have to parse the HTTP request body to find any required data.
3972 * "balance url_param check_post" should have been the only way to get
3973 * into this. We were brought here after HTTP header analysis, so all
3974 * related structures are ready.
3975 */
3976
Willy Tarreau890988f2014-04-10 11:59:33 +02003977 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3978 /* This is the first call */
3979 if (msg->msg_state < HTTP_MSG_BODY)
3980 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003981
Willy Tarreau890988f2014-04-10 11:59:33 +02003982 if (msg->msg_state < HTTP_MSG_100_SENT) {
3983 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3984 * send an HTTP/1.1 100 Continue intermediate response.
3985 */
3986 if (msg->flags & HTTP_MSGF_VER_11) {
3987 struct hdr_ctx ctx;
3988 ctx.idx = 0;
3989 /* Expect is allowed in 1.1, look for it */
3990 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
3991 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02003992 co_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01003993 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02003994 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003995 }
Willy Tarreau890988f2014-04-10 11:59:33 +02003996 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003997 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003998
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003999 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004000 * req->buf->p still points to the beginning of the message. We
4001 * must save the body in msg->next because it survives buffer
4002 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004003 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004004 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004005
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004006 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004007 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4008 else
4009 msg->msg_state = HTTP_MSG_DATA;
4010 }
4011
Willy Tarreau890988f2014-04-10 11:59:33 +02004012 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4013 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004014 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004015 goto missing_data;
4016
4017 /* OK we have everything we need now */
4018 goto http_end;
4019 }
4020
4021 /* OK here we're parsing a chunked-encoded message */
4022
Willy Tarreau522d6c02009-12-06 18:49:18 +01004023 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004024 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004025 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004026 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004027 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004028 unsigned int chunk;
4029 int ret = h1_parse_chunk_size(req->buf, msg->next, req->buf->i, &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01004030
Willy Tarreau115acb92009-12-26 13:56:06 +01004031 if (!ret)
4032 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004033 else if (ret < 0) {
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004034 msg->err_pos = req->buf->i + ret;
4035 if (msg->err_pos < 0)
4036 msg->err_pos += req->buf->size;
Willy Tarreau87b09662015-04-03 00:22:06 +02004037 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004038 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004039 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004040
4041 msg->chunk_len = chunk;
4042 msg->body_len += chunk;
4043
4044 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01004045 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004046 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01004047 }
4048
Willy Tarreaud98cf932009-12-27 22:54:55 +01004049 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02004050 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4051 * for at least a whole chunk or the whole content length bytes after
4052 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004053 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004054 if (msg->msg_state == HTTP_MSG_TRAILERS)
4055 goto http_end;
4056
Willy Tarreaue115b492015-05-01 23:05:14 +02004057 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004058 goto http_end;
4059
4060 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004061 /* we get here if we need to wait for more data. If the buffer is full,
4062 * we have the maximum we can expect.
4063 */
4064 if (buffer_full(req->buf, global.tune.maxrewrite))
4065 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004066
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004067 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004068 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004069 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004070
Willy Tarreaue7dff022015-04-03 01:14:29 +02004071 if (!(s->flags & SF_ERR_MASK))
4072 s->flags |= SF_ERR_CLITO;
4073 if (!(s->flags & SF_FINST_MASK))
4074 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004075 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004076 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004077
4078 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004079 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004080 /* Not enough data. We'll re-use the http-request
4081 * timeout here. Ideally, we should set the timeout
4082 * relative to the accept() date. We just set the
4083 * request timeout once at the beginning of the
4084 * request.
4085 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004086 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004087 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004088 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004089 return 0;
4090 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004091
4092 http_end:
4093 /* The situation will not evolve, so let's give up on the analysis. */
4094 s->logs.tv_request = now; /* update the request timer to reflect full request */
4095 req->analysers &= ~an_bit;
4096 req->analyse_exp = TICK_ETERNITY;
4097 return 1;
4098
4099 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004100 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004101 txn->req.msg_state = HTTP_MSG_ERROR;
4102 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004103 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004104
Willy Tarreaue7dff022015-04-03 01:14:29 +02004105 if (!(s->flags & SF_ERR_MASK))
4106 s->flags |= SF_ERR_PRXCOND;
4107 if (!(s->flags & SF_FINST_MASK))
4108 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004109
Willy Tarreau522d6c02009-12-06 18:49:18 +01004110 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01004111 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004112 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004113 if (sess->listener->counters)
4114 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004115 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004116}
4117
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004118/* send a server's name with an outgoing request over an established connection.
4119 * Note: this function is designed to be called once the request has been scheduled
4120 * for being forwarded. This is the reason why it rewinds the buffer before
4121 * proceeding.
4122 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004123int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004124
4125 struct hdr_ctx ctx;
4126
Mark Lamourinec2247f02012-01-04 13:02:01 -05004127 char *hdr_name = be->server_id_hdr_name;
4128 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004129 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004130 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004131 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004132
William Lallemandd9e90662012-01-30 17:27:17 +01004133 ctx.idx = 0;
4134
Willy Tarreau211cdec2014-04-17 20:18:08 +02004135 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004136 if (old_o) {
4137 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004138 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004139 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004140 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004141 }
4142
Willy Tarreau9b28e032012-10-12 23:49:43 +02004143 old_i = chn->buf->i;
4144 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 -05004145 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004146 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004147 }
4148
4149 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004150 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004151 memcpy(hdr_val, hdr_name, hdr_name_len);
4152 hdr_val += hdr_name_len;
4153 *hdr_val++ = ':';
4154 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004155 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4156 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004157
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004158 if (old_o) {
4159 /* If this was a forwarded request, we must readjust the amount of
4160 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004161 * variations. Note that the current state is >= HTTP_MSG_BODY,
4162 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004163 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004164 old_o += chn->buf->i - old_i;
4165 b_adv(chn->buf, old_o);
4166 txn->req.next -= old_o;
4167 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004168 }
4169
Mark Lamourinec2247f02012-01-04 13:02:01 -05004170 return 0;
4171}
4172
Willy Tarreau610ecce2010-01-04 21:15:02 +01004173/* Terminate current transaction and prepare a new one. This is very tricky
4174 * right now but it works.
4175 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004176void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004177{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004178 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02004179 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01004180 struct proxy *be = s->be;
Willy Tarreau323a2d92015-08-04 19:00:17 +02004181 struct connection *srv_conn;
4182 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02004183 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01004184
Willy Tarreau610ecce2010-01-04 21:15:02 +01004185 /* FIXME: We need a more portable way of releasing a backend's and a
4186 * server's connections. We need a safer way to reinitialize buffer
4187 * flags. We also need a more accurate method for computing per-request
4188 * data.
4189 */
Willy Tarreau323a2d92015-08-04 19:00:17 +02004190 srv_conn = objt_conn(s->si[1].end);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004191
Willy Tarreau4213a112013-12-15 10:25:42 +01004192 /* unless we're doing keep-alive, we want to quickly close the connection
4193 * to the server.
4194 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004195 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004196 !si_conn_ready(&s->si[1])) {
4197 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4198 si_shutr(&s->si[1]);
4199 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01004200 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004201
Willy Tarreaue7dff022015-04-03 01:14:29 +02004202 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau858b1032015-12-07 17:04:59 +01004203 be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004204 if (unlikely(s->srv_conn))
4205 sess_change_server(s, NULL);
4206 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004207
4208 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02004209 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004210
Willy Tarreaueee5b512015-04-03 23:46:31 +02004211 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004212 int n;
4213
Willy Tarreaueee5b512015-04-03 23:46:31 +02004214 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004215 if (n < 1 || n > 5)
4216 n = 0;
4217
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004218 if (fe->mode == PR_MODE_HTTP) {
4219 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004220 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02004221 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01004222 (be->mode == PR_MODE_HTTP)) {
4223 be->be_counters.p.http.rsp[n]++;
4224 be->be_counters.p.http.cum_req++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004225 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004226 }
4227
4228 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004229 s->logs.bytes_in -= s->req.buf->i;
4230 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004231
4232 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004233 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02004234 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004235 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004236 s->do_log(s);
4237 }
4238
Willy Tarreaud713bcc2014-06-25 15:36:04 +02004239 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02004240 stream_stop_content_counters(s);
4241 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004242
Willy Tarreau610ecce2010-01-04 21:15:02 +01004243 s->logs.accept_date = date; /* user-visible date for logging */
4244 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02004245 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
4246 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004247 tv_zero(&s->logs.tv_request);
4248 s->logs.t_queue = -1;
4249 s->logs.t_connect = -1;
4250 s->logs.t_data = -1;
4251 s->logs.t_close = 0;
4252 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4253 s->logs.srv_queue_size = 0; /* we will get this number soon */
4254
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004255 s->logs.bytes_in = s->req.total = s->req.buf->i;
4256 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004257
4258 if (s->pend_pos)
4259 pendconn_free(s->pend_pos);
4260
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004261 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004262 if (s->flags & SF_CURR_SESS) {
4263 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004264 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004265 }
Willy Tarreau858b1032015-12-07 17:04:59 +01004266 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004267 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004268 }
4269
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004270 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004271
Willy Tarreau4213a112013-12-15 10:25:42 +01004272 /* only release our endpoint if we don't intend to reuse the
4273 * connection.
4274 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004275 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004276 !si_conn_ready(&s->si[1])) {
4277 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02004278 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004279 }
4280
Willy Tarreau350f4872014-11-28 14:42:25 +01004281 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
4282 s->si[1].err_type = SI_ET_NONE;
4283 s->si[1].conn_retries = 0; /* used for logging too */
4284 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02004285 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 +01004286 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);
4287 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 +02004288 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
4289 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
4290 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01004291
Willy Tarreaueee5b512015-04-03 23:46:31 +02004292 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004293 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02004294 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004295
4296 if (prev_status == 401 || prev_status == 407) {
4297 /* In HTTP keep-alive mode, if we receive a 401, we still have
4298 * a chance of being able to send the visitor again to the same
4299 * server over the same connection. This is required by some
4300 * broken protocols such as NTLM, and anyway whenever there is
4301 * an opportunity for sending the challenge to the proper place,
4302 * it's better to do it (at least it helps with debugging).
4303 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004304 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02004305 if (srv_conn)
4306 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01004307 }
4308
Willy Tarreau53f96852016-02-02 18:50:47 +01004309 /* Never ever allow to reuse a connection from a non-reuse backend */
4310 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
4311 srv_conn->flags |= CO_FL_PRIVATE;
4312
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004313 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01004314 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004315
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004316 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004317 s->req.flags |= CF_NEVER_WAIT;
4318 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004319 }
4320
Willy Tarreau714ea782015-11-25 20:11:11 +01004321 /* we're removing the analysers, we MUST re-enable events detection.
4322 * We don't enable close on the response channel since it's either
4323 * already closed, or in keep-alive with an idle connection handler.
4324 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004325 channel_auto_read(&s->req);
4326 channel_auto_close(&s->req);
4327 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004328
Willy Tarreau1c59bd52015-11-02 20:20:11 +01004329 /* we're in keep-alive with an idle connection, monitor it if not already done */
4330 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02004331 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02004332 if (!srv)
4333 si_idle_conn(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01004334 else if (srv_conn->flags & CO_FL_PRIVATE)
Willy Tarreau8dff9982015-08-04 20:45:52 +02004335 si_idle_conn(&s->si[1], &srv->priv_conns);
Willy Tarreau449d74a2015-08-05 17:16:33 +02004336 else if (prev_flags & TX_NOT_FIRST)
4337 /* note: we check the request, not the connection, but
4338 * this is valid for strategies SAFE and AGGR, and in
4339 * case of ALWS, we don't care anyway.
4340 */
4341 si_idle_conn(&s->si[1], &srv->safe_conns);
Willy Tarreau8dff9982015-08-04 20:45:52 +02004342 else
4343 si_idle_conn(&s->si[1], &srv->idle_conns);
Willy Tarreau4320eaa2015-08-05 11:08:30 +02004344 }
Christopher Faulete6006242017-03-10 11:52:44 +01004345 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
4346 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004347}
4348
4349
4350/* This function updates the request state machine according to the response
4351 * state machine and buffer flags. It returns 1 if it changes anything (flag
4352 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4353 * it is only used to find when a request/response couple is complete. Both
4354 * this function and its equivalent should loop until both return zero. It
4355 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4356 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004357int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004358{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004359 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004360 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004361 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004362 unsigned int old_state = txn->req.msg_state;
4363
Christopher Faulet4be98032017-07-18 10:48:24 +02004364 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004365 return 0;
4366
4367 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004368 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004369 * We can shut the read side unless we want to abort_on_close,
4370 * or we have a POST request. The issue with POST requests is
4371 * that some browsers still send a CRLF after the request, and
4372 * this CRLF must be read so that it does not remain in the kernel
4373 * buffers, otherwise a close could cause an RST on some systems
4374 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004375 * Note that if we're using keep-alive on the client side, we'd
4376 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02004377 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01004378 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004379 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004380 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4381 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4382 !(s->be->options & PR_O_ABRT_CLOSE) &&
4383 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004384 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004385
Willy Tarreau40f151a2012-12-20 12:10:09 +01004386 /* if the server closes the connection, we want to immediately react
4387 * and close the socket to save packets and syscalls.
4388 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004389 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01004390
Willy Tarreau7f876a12015-11-18 11:59:55 +01004391 /* In any case we've finished parsing the request so we must
4392 * disable Nagle when sending data because 1) we're not going
4393 * to shut this side, and 2) the server is waiting for us to
4394 * send pending data.
4395 */
4396 chn->flags |= CF_NEVER_WAIT;
4397
Willy Tarreau610ecce2010-01-04 21:15:02 +01004398 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4399 goto wait_other_side;
4400
4401 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4402 /* The server has not finished to respond, so we
4403 * don't want to move in order not to upset it.
4404 */
4405 goto wait_other_side;
4406 }
4407
Willy Tarreau610ecce2010-01-04 21:15:02 +01004408 /* When we get here, it means that both the request and the
4409 * response have finished receiving. Depending on the connection
4410 * mode, we'll have to wait for the last bytes to leave in either
4411 * direction, and sometimes for a close to be effective.
4412 */
4413
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004414 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4415 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004416 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4417 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004418 }
4419 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4420 /* Option forceclose is set, or either side wants to close,
4421 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004422 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004423 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004424 *
4425 * However, there is an exception if the response
4426 * length is undefined. In this case, we need to wait
4427 * the close from the server. The response will be
4428 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004429 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004430 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4431 txn->rsp.msg_state != HTTP_MSG_CLOSED)
4432 goto check_channel_flags;
4433
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004434 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4435 channel_shutr_now(chn);
4436 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004437 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004438 }
4439 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004440 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4441 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004442 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004443 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4444 channel_auto_read(chn);
4445 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004446 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004447 }
4448
Christopher Faulet4be98032017-07-18 10:48:24 +02004449 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004450 }
4451
4452 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4453 http_msg_closing:
4454 /* nothing else to forward, just waiting for the output buffer
4455 * to be empty and for the shutw_now to take effect.
4456 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004457 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004458 txn->req.msg_state = HTTP_MSG_CLOSED;
4459 goto http_msg_closed;
4460 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004461 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004462 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004463 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004464 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004465 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004466 }
4467
4468 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4469 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01004470 /* see above in MSG_DONE why we only do this in these states */
4471 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4472 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4473 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004474 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004475 goto wait_other_side;
4476 }
4477
Christopher Faulet4be98032017-07-18 10:48:24 +02004478 check_channel_flags:
4479 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4480 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4481 /* if we've just closed an output, let's switch */
4482 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4483 txn->req.msg_state = HTTP_MSG_CLOSING;
4484 goto http_msg_closing;
4485 }
4486
4487
Willy Tarreau610ecce2010-01-04 21:15:02 +01004488 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004489 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004490}
4491
4492
4493/* This function updates the response state machine according to the request
4494 * state machine and buffer flags. It returns 1 if it changes anything (flag
4495 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4496 * it is only used to find when a request/response couple is complete. Both
4497 * this function and its equivalent should loop until both return zero. It
4498 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4499 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004500int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004501{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004502 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004503 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004504 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004505 unsigned int old_state = txn->rsp.msg_state;
4506
Christopher Faulet4be98032017-07-18 10:48:24 +02004507 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004508 return 0;
4509
4510 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4511 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004512 * still monitor the server connection for a possible close
4513 * while the request is being uploaded, so we don't disable
4514 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004515 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004516 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004517
4518 if (txn->req.msg_state == HTTP_MSG_ERROR)
4519 goto wait_other_side;
4520
4521 if (txn->req.msg_state < HTTP_MSG_DONE) {
4522 /* The client seems to still be sending data, probably
4523 * because we got an error response during an upload.
4524 * We have the choice of either breaking the connection
4525 * or letting it pass through. Let's do the later.
4526 */
4527 goto wait_other_side;
4528 }
4529
Willy Tarreau610ecce2010-01-04 21:15:02 +01004530 /* When we get here, it means that both the request and the
4531 * response have finished receiving. Depending on the connection
4532 * mode, we'll have to wait for the last bytes to leave in either
4533 * direction, and sometimes for a close to be effective.
4534 */
4535
4536 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4537 /* Server-close mode : shut read and wait for the request
4538 * side to close its output buffer. The caller will detect
4539 * when we're in DONE and the other is in CLOSED and will
4540 * catch that for the final cleanup.
4541 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004542 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4543 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004544 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004545 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4546 /* Option forceclose is set, or either side wants to close,
4547 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004548 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004549 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004550 *
4551 * However, there is an exception if the response length
4552 * is undefined. In this case, we switch in TUNNEL mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004553 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004554 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN)) {
4555 channel_auto_read(chn);
4556 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4557 chn->flags |= CF_NEVER_WAIT;
4558 }
4559 else if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004560 channel_shutr_now(chn);
4561 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004562 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004563 }
4564 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004565 /* The last possible modes are keep-alive and tunnel. Tunnel will
4566 * need to forward remaining data. Keep-alive will need to monitor
4567 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004568 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004569 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004570 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004571 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4572 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004573 }
4574
Christopher Faulet4be98032017-07-18 10:48:24 +02004575 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004576 }
4577
4578 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4579 http_msg_closing:
4580 /* nothing else to forward, just waiting for the output buffer
4581 * to be empty and for the shutw_now to take effect.
4582 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004583 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004584 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4585 goto http_msg_closed;
4586 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004587 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02004588 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004589 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004590 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004591 if (objt_server(s->target))
4592 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004593 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004594 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004595 }
4596
4597 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4598 http_msg_closed:
4599 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01004600 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004601 channel_auto_close(chn);
4602 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004603 goto wait_other_side;
4604 }
4605
Christopher Faulet4be98032017-07-18 10:48:24 +02004606 check_channel_flags:
4607 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4608 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4609 /* if we've just closed an output, let's switch */
4610 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4611 goto http_msg_closing;
4612 }
4613
Willy Tarreau610ecce2010-01-04 21:15:02 +01004614 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004615 /* We force the response to leave immediately if we're waiting for the
4616 * other side, since there is no pending shutdown to push it out.
4617 */
4618 if (!channel_is_empty(chn))
4619 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004620 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004621}
4622
4623
Christopher Faulet894da4c2017-07-18 11:29:07 +02004624/* Resync the request and response state machines. */
4625void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004626{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004627 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004628#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01004629 int old_req_state = txn->req.msg_state;
4630 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004631#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01004632
Willy Tarreau610ecce2010-01-04 21:15:02 +01004633 http_sync_req_state(s);
4634 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004635 if (!http_sync_res_state(s))
4636 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004637 if (!http_sync_req_state(s))
4638 break;
4639 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004640
Christopher Faulet894da4c2017-07-18 11:29:07 +02004641 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
4642 "req->analysers=0x%08x res->analysers=0x%08x\n",
4643 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02004644 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
4645 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02004646 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02004647
4648
Willy Tarreau610ecce2010-01-04 21:15:02 +01004649 /* OK, both state machines agree on a compatible state.
4650 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01004651 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4652 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02004653 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
4654 * means we must abort the request.
4655 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
4656 * corresponding channel.
4657 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
4658 * on the response with server-close mode means we've completed one
4659 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004660 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02004661 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4662 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004663 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004664 channel_auto_close(&s->req);
4665 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004666 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004667 channel_auto_close(&s->res);
4668 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004669 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004670 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4671 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004672 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004673 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004674 channel_auto_close(&s->res);
4675 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004676 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004677 channel_abort(&s->req);
4678 channel_auto_close(&s->req);
4679 channel_auto_read(&s->req);
4680 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004681 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004682 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4683 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4684 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4685 s->req.analysers &= AN_REQ_FLT_END;
4686 if (HAS_REQ_DATA_FILTERS(s))
4687 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
4688 }
4689 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4690 s->res.analysers &= AN_RES_FLT_END;
4691 if (HAS_RSP_DATA_FILTERS(s))
4692 s->res.analysers |= AN_RES_FLT_XFER_DATA;
4693 }
4694 channel_auto_close(&s->req);
4695 channel_auto_read(&s->req);
4696 channel_auto_close(&s->res);
4697 channel_auto_read(&s->res);
4698 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004699 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4700 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004701 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004702 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4703 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4704 /* server-close/keep-alive: terminate this transaction,
4705 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004706 * a fresh-new transaction, but only once we're sure there's
4707 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02004708 * another request. They must not hold any pending output data
4709 * and the response buffer must realigned
4710 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01004711 */
Christopher Faulet894da4c2017-07-18 11:29:07 +02004712 if (s->req.buf->o)
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004713 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004714 else if (s->res.buf->o)
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004715 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02004716 else {
4717 s->req.analysers = AN_REQ_FLT_END;
4718 s->res.analysers = AN_RES_FLT_END;
4719 txn->flags |= TX_WAIT_CLEANUP;
4720 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004721 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004722}
4723
Willy Tarreaud98cf932009-12-27 22:54:55 +01004724/* This function is an analyser which forwards request body (including chunk
4725 * sizes if any). It is called as soon as we must forward, even if we forward
4726 * zero byte. The only situation where it must not be called is when we're in
4727 * tunnel mode and we want to forward till the close. It's used both to forward
4728 * remaining data and to resync after end of body. It expects the msg_state to
4729 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02004730 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01004731 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004732 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004733 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004734int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004735{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004736 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004737 struct http_txn *txn = s->txn;
4738 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01004739 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004740
Christopher Faulet814d2702017-03-30 11:33:44 +02004741 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
4742 now_ms, __FUNCTION__,
4743 s,
4744 req,
4745 req->rex, req->wex,
4746 req->flags,
4747 req->buf->i,
4748 req->analysers);
4749
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004750 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4751 return 0;
4752
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004753 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004754 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004755 /* Output closed while we were sending data. We must abort and
4756 * wake the other side up.
4757 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004758 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02004759 msg->msg_state = HTTP_MSG_ERROR;
4760 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004761 return 1;
4762 }
4763
Willy Tarreaud98cf932009-12-27 22:54:55 +01004764 /* Note that we don't have to send 100-continue back because we don't
4765 * need the data to complete our job, and it's up to the server to
4766 * decide whether to return 100, 417 or anything else in return of
4767 * an "Expect: 100-continue" header.
4768 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004769 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004770 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4771 ? HTTP_MSG_CHUNK_SIZE
4772 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004773
4774 /* TODO/filters: when http-buffer-request option is set or if a
4775 * rule on url_param exists, the first chunk size could be
4776 * already parsed. In that case, msg->next is after the chunk
4777 * size (including the CRLF after the size). So this case should
4778 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004779 }
4780
Willy Tarreau7ba23542014-04-17 21:50:00 +02004781 /* Some post-connect processing might want us to refrain from starting to
4782 * forward data. Currently, the only reason for this is "balance url_param"
4783 * whichs need to parse/process the request after we've enabled forwarding.
4784 */
4785 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004786 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004787 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004788 req->flags |= CF_WAKE_CONNECT;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004789 goto missing_data_or_waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004790 }
4791 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4792 }
4793
Willy Tarreau80a92c02014-03-12 10:41:13 +01004794 /* in most states, we should abort in case of early close */
4795 channel_auto_close(req);
4796
Willy Tarreauefdf0942014-04-24 20:08:57 +02004797 if (req->to_forward) {
4798 /* We can't process the buffer's contents yet */
4799 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004800 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004801 }
4802
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004803 if (msg->msg_state < HTTP_MSG_DONE) {
4804 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4805 ? http_msg_forward_chunked_body(s, msg)
4806 : http_msg_forward_body(s, msg));
4807 if (!ret)
4808 goto missing_data_or_waiting;
4809 if (ret < 0)
4810 goto return_bad_req;
4811 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004812
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004813 /* other states, DONE...TUNNEL */
4814 /* we don't want to forward closes on DONE except in tunnel mode. */
4815 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4816 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004817
Christopher Faulet894da4c2017-07-18 11:29:07 +02004818 http_resync_states(s);
4819 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004820 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4821 if (req->flags & CF_SHUTW) {
4822 /* request errors are most likely due to the
4823 * server aborting the transfer. */
4824 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004825 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004826 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004827 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004828 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004829 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004830 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004831 }
4832
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004833 /* If "option abortonclose" is set on the backend, we want to monitor
4834 * the client's connection and forward any shutdown notification to the
4835 * server, which will decide whether to close or to go on processing the
4836 * request. We only do that in tunnel mode, and not in other modes since
4837 * it can be abused to exhaust source ports. */
4838 if (s->be->options & PR_O_ABRT_CLOSE) {
4839 channel_auto_read(req);
4840 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4841 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4842 s->si[1].flags |= SI_FL_NOLINGER;
4843 channel_auto_close(req);
4844 }
4845 else if (s->txn->meth == HTTP_METH_POST) {
4846 /* POST requests may require to read extra CRLF sent by broken
4847 * browsers and which could cause an RST to be sent upon close
4848 * on some systems (eg: Linux). */
4849 channel_auto_read(req);
4850 }
4851 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004852
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004853 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004854 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02004855 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004856 if (!(s->flags & SF_ERR_MASK))
4857 s->flags |= SF_ERR_CLICL;
4858 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004859 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004860 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004861 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004862 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004863 }
4864
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004865 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004866 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004867 if (objt_server(s->target))
4868 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004869
4870 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004871 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004872
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004873 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004874 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004875 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004876
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004877 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004878 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004879 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004880 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004881 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004882
Willy Tarreau5c620922011-05-11 19:56:11 +02004883 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004884 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004885 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004886 * modes are already handled by the stream sock layer. We must not do
4887 * this in content-length mode because it could present the MSG_MORE
4888 * flag with the last block of forwarded data, which would cause an
4889 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004890 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004891 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004892 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004893
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004894 return 0;
4895
Willy Tarreaud98cf932009-12-27 22:54:55 +01004896 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004897 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004898 if (sess->listener->counters)
4899 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004900
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004901 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004902 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004903 txn->req.msg_state = HTTP_MSG_ERROR;
4904 if (txn->status) {
4905 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004906 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004907 } else {
4908 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004909 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004910 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004911 req->analysers &= AN_REQ_FLT_END;
4912 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 +01004913
Willy Tarreaue7dff022015-04-03 01:14:29 +02004914 if (!(s->flags & SF_ERR_MASK))
4915 s->flags |= SF_ERR_PRXCOND;
4916 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004917 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004918 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004919 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004920 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004921 }
4922 return 0;
4923
4924 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004925 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004926 txn->req.msg_state = HTTP_MSG_ERROR;
4927 if (txn->status) {
4928 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004929 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004930 } else {
4931 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004932 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004933 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004934 req->analysers &= AN_REQ_FLT_END;
4935 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 +01004936
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004937 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004938 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004939 if (objt_server(s->target))
4940 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004941
Willy Tarreaue7dff022015-04-03 01:14:29 +02004942 if (!(s->flags & SF_ERR_MASK))
4943 s->flags |= SF_ERR_SRVCL;
4944 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004945 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004946 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004947 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004948 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004949 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004950 return 0;
4951}
4952
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004953/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4954 * processing can continue on next analysers, or zero if it either needs more
4955 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004956 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004957 * when it has nothing left to do, and may remove any analyser when it wants to
4958 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004959 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004960int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004961{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004962 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004963 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004964 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004965 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004966 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004967 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004968 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004969
Willy Tarreau87b09662015-04-03 00:22:06 +02004970 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 +02004971 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004972 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004973 rep,
4974 rep->rex, rep->wex,
4975 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004976 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004977 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004978
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004979 /*
4980 * Now parse the partial (or complete) lines.
4981 * We will check the response syntax, and also join multi-line
4982 * headers. An index of all the lines will be elaborated while
4983 * parsing.
4984 *
4985 * For the parsing, we use a 28 states FSM.
4986 *
4987 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02004988 * rep->buf->p = beginning of response
4989 * rep->buf->p + msg->eoh = end of processed headers / start of current one
4990 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02004991 * msg->eol = end of current header or line (LF or CRLF)
4992 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004993 */
4994
Willy Tarreau628c40c2014-04-24 19:11:26 +02004995 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01004996 /* There's a protected area at the end of the buffer for rewriting
4997 * purposes. We don't want to start to parse the request if the
4998 * protected area is affected, because we may have to move processed
4999 * data later, which is much more complicated.
5000 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005001 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005002 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005003 /* some data has still not left the buffer, wake us once that's done */
5004 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5005 goto abort_response;
5006 channel_dont_close(rep);
5007 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005008 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005009 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005010 }
5011
Willy Tarreau379357a2013-06-08 12:55:46 +02005012 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5013 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5014 buffer_slow_realign(rep->buf);
5015
Willy Tarreau9b28e032012-10-12 23:49:43 +02005016 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005017 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005018 }
5019
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005020 /* 1: we might have to print this header in debug mode */
5021 if (unlikely((global.mode & MODE_DEBUG) &&
5022 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005023 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005024 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005025
Willy Tarreau9b28e032012-10-12 23:49:43 +02005026 sol = rep->buf->p;
5027 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005028 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005029
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005030 sol += hdr_idx_first_pos(&txn->hdr_idx);
5031 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005032
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005033 while (cur_idx) {
5034 eol = sol + txn->hdr_idx.v[cur_idx].len;
5035 debug_hdr("srvhdr", s, sol, eol);
5036 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5037 cur_idx = txn->hdr_idx.v[cur_idx].next;
5038 }
5039 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005040
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005041 /*
5042 * Now we quickly check if we have found a full valid response.
5043 * If not so, we check the FD and buffer states before leaving.
5044 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005045 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005046 * responses are checked first.
5047 *
5048 * Depending on whether the client is still there or not, we
5049 * may send an error response back or not. Note that normally
5050 * we should only check for HTTP status there, and check I/O
5051 * errors somewhere else.
5052 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005053
Willy Tarreau655dce92009-11-08 13:10:58 +01005054 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005055 /* Invalid response */
5056 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5057 /* we detected a parsing error. We want to archive this response
5058 * in the dedicated proxy area for later troubleshooting.
5059 */
5060 hdr_response_bad:
5061 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005062 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005063
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005064 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005065 if (objt_server(s->target)) {
5066 objt_server(s->target)->counters.failed_resp++;
5067 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005068 }
Willy Tarreau64648412010-03-05 10:41:54 +01005069 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005070 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005071 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005072 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005073 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005074 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005075 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005076
Willy Tarreaue7dff022015-04-03 01:14:29 +02005077 if (!(s->flags & SF_ERR_MASK))
5078 s->flags |= SF_ERR_PRXCOND;
5079 if (!(s->flags & SF_FINST_MASK))
5080 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005081
5082 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005083 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005084
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005085 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005086 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005087 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005088 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005089 goto hdr_response_bad;
5090 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005091
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005092 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005093 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005094 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005095 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005096 else if (txn->flags & TX_NOT_FIRST)
5097 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005098
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005099 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005100 if (objt_server(s->target)) {
5101 objt_server(s->target)->counters.failed_resp++;
5102 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005103 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005104
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005105 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005106 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005107 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005108 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005109 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005110 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02005111
Willy Tarreaue7dff022015-04-03 01:14:29 +02005112 if (!(s->flags & SF_ERR_MASK))
5113 s->flags |= SF_ERR_SRVCL;
5114 if (!(s->flags & SF_FINST_MASK))
5115 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005116 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005117 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005118
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005119 /* read timeout : return a 504 to the client. */
5120 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005121 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005122 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005123
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005124 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005125 if (objt_server(s->target)) {
5126 objt_server(s->target)->counters.failed_resp++;
5127 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005128 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005129
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005130 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005131 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005132 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01005133 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005134 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005135 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02005136
Willy Tarreaue7dff022015-04-03 01:14:29 +02005137 if (!(s->flags & SF_ERR_MASK))
5138 s->flags |= SF_ERR_SRVTO;
5139 if (!(s->flags & SF_FINST_MASK))
5140 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005141 return 0;
5142 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005143
Willy Tarreauf003d372012-11-26 13:35:37 +01005144 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005145 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005146 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01005147 s->be->be_counters.cli_aborts++;
5148 if (objt_server(s->target))
5149 objt_server(s->target)->counters.cli_aborts++;
5150
Christopher Faulet0184ea72017-01-05 14:06:34 +01005151 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01005152 channel_auto_close(rep);
5153
5154 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01005155 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005156 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01005157
Willy Tarreaue7dff022015-04-03 01:14:29 +02005158 if (!(s->flags & SF_ERR_MASK))
5159 s->flags |= SF_ERR_CLICL;
5160 if (!(s->flags & SF_FINST_MASK))
5161 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01005162
Willy Tarreau87b09662015-04-03 00:22:06 +02005163 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01005164 return 0;
5165 }
5166
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005167 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005168 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005169 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005170 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005171 else if (txn->flags & TX_NOT_FIRST)
5172 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005173
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005174 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005175 if (objt_server(s->target)) {
5176 objt_server(s->target)->counters.failed_resp++;
5177 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005178 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005179
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005180 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005181 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005182 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005183 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005184 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005185 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005186
Willy Tarreaue7dff022015-04-03 01:14:29 +02005187 if (!(s->flags & SF_ERR_MASK))
5188 s->flags |= SF_ERR_SRVCL;
5189 if (!(s->flags & SF_FINST_MASK))
5190 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005191 return 0;
5192 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005193
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005194 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005195 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005196 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005197 http_capture_bad_message(&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;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005200
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005201 s->be->be_counters.failed_resp++;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005202 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005203 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005204
Willy Tarreaue7dff022015-04-03 01:14:29 +02005205 if (!(s->flags & SF_ERR_MASK))
5206 s->flags |= SF_ERR_CLICL;
5207 if (!(s->flags & SF_FINST_MASK))
5208 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005209
Willy Tarreau87b09662015-04-03 00:22:06 +02005210 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005211 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005212 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005213
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005214 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005215 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005216 return 0;
5217 }
5218
5219 /* More interesting part now : we know that we have a complete
5220 * response which at least looks like HTTP. We have an indicator
5221 * of each header's length, so we can parse them quickly.
5222 */
5223
5224 if (unlikely(msg->err_pos >= 0))
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005225 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005226
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005227 /*
5228 * 1: get the status code
5229 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005230 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005231 if (n < 1 || n > 5)
5232 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005233 /* when the client triggers a 4xx from the server, it's most often due
5234 * to a missing object or permission. These events should be tracked
5235 * because if they happen often, it may indicate a brute force or a
5236 * vulnerability scan.
5237 */
5238 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02005239 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02005240
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005241 if (objt_server(s->target))
5242 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005243
Willy Tarreau91852eb2015-05-01 13:26:00 +02005244 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
5245 * exactly one digit "." one digit. This check may be disabled using
5246 * option accept-invalid-http-response.
5247 */
5248 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
5249 if (msg->sl.st.v_l != 8) {
5250 msg->err_pos = 0;
5251 goto hdr_response_bad;
5252 }
5253
5254 if (rep->buf->p[4] != '/' ||
5255 !isdigit((unsigned char)rep->buf->p[5]) ||
5256 rep->buf->p[6] != '.' ||
5257 !isdigit((unsigned char)rep->buf->p[7])) {
5258 msg->err_pos = 4;
5259 goto hdr_response_bad;
5260 }
5261 }
5262
Willy Tarreau5b154472009-12-21 20:11:07 +01005263 /* check if the response is HTTP/1.1 or above */
5264 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005265 ((rep->buf->p[5] > '1') ||
5266 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005267 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005268
5269 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005270 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 +01005271
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005272 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005273 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005274
Willy Tarreau9b28e032012-10-12 23:49:43 +02005275 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005276
Willy Tarreau39650402010-03-15 19:44:39 +01005277 /* Adjust server's health based on status code. Note: status codes 501
5278 * and 505 are triggered on demand by client request, so we must not
5279 * count them as server failures.
5280 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005281 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005282 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005283 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005284 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005285 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005286 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005287
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005288 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02005289 * We may be facing a 100-continue response, or any other informational
5290 * 1xx response which is non-final, in which case this is not the right
5291 * response, and we're waiting for the next one. Let's allow this response
5292 * to go to the client and wait for the next one. There's an exception for
5293 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005294 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02005295 if (txn->status < 200 &&
5296 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02005297 hdr_idx_init(&txn->hdr_idx);
5298 msg->next -= channel_forward(rep, msg->next);
5299 msg->msg_state = HTTP_MSG_RPBEFORE;
5300 txn->status = 0;
5301 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01005302 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02005303 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02005304 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02005305
Willy Tarreaua14ad722017-07-07 11:36:32 +02005306 /*
5307 * 2: check for cacheability.
5308 */
5309
5310 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005311 case 200:
5312 case 203:
5313 case 206:
5314 case 300:
5315 case 301:
5316 case 410:
5317 /* RFC2616 @13.4:
5318 * "A response received with a status code of
5319 * 200, 203, 206, 300, 301 or 410 MAY be stored
5320 * by a cache (...) unless a cache-control
5321 * directive prohibits caching."
5322 *
5323 * RFC2616 @9.5: POST method :
5324 * "Responses to this method are not cacheable,
5325 * unless the response includes appropriate
5326 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005327 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005328 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005329 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005330 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5331 break;
5332 default:
5333 break;
5334 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005335
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005336 /*
5337 * 3: we may need to capture headers
5338 */
5339 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005340 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005341 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005342 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005343
Willy Tarreau557f1992015-05-01 10:05:17 +02005344 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
5345 * by RFC7230#3.3.3 :
5346 *
5347 * The length of a message body is determined by one of the following
5348 * (in order of precedence):
5349 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005350 * 1. Any 2xx (Successful) response to a CONNECT request implies that
5351 * the connection will become a tunnel immediately after the empty
5352 * line that concludes the header fields. A client MUST ignore
5353 * any Content-Length or Transfer-Encoding header fields received
5354 * in such a message. Any 101 response (Switching Protocols) is
5355 * managed in the same manner.
5356 *
5357 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02005358 * (Informational), 204 (No Content), or 304 (Not Modified) status
5359 * code is always terminated by the first empty line after the
5360 * header fields, regardless of the header fields present in the
5361 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005362 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005363 * 3. If a Transfer-Encoding header field is present and the chunked
5364 * transfer coding (Section 4.1) is the final encoding, the message
5365 * body length is determined by reading and decoding the chunked
5366 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005367 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005368 * If a Transfer-Encoding header field is present in a response and
5369 * the chunked transfer coding is not the final encoding, the
5370 * message body length is determined by reading the connection until
5371 * it is closed by the server. If a Transfer-Encoding header field
5372 * is present in a request and the chunked transfer coding is not
5373 * the final encoding, the message body length cannot be determined
5374 * reliably; the server MUST respond with the 400 (Bad Request)
5375 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005376 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005377 * If a message is received with both a Transfer-Encoding and a
5378 * Content-Length header field, the Transfer-Encoding overrides the
5379 * Content-Length. Such a message might indicate an attempt to
5380 * perform request smuggling (Section 9.5) or response splitting
5381 * (Section 9.4) and ought to be handled as an error. A sender MUST
5382 * remove the received Content-Length field prior to forwarding such
5383 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005384 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005385 * 4. If a message is received without Transfer-Encoding and with
5386 * either multiple Content-Length header fields having differing
5387 * field-values or a single Content-Length header field having an
5388 * invalid value, then the message framing is invalid and the
5389 * recipient MUST treat it as an unrecoverable error. If this is a
5390 * request message, the server MUST respond with a 400 (Bad Request)
5391 * status code and then close the connection. If this is a response
5392 * message received by a proxy, the proxy MUST close the connection
5393 * to the server, discard the received response, and send a 502 (Bad
5394 * Gateway) response to the client. If this is a response message
5395 * received by a user agent, the user agent MUST close the
5396 * connection to the server and discard the received response.
5397 *
5398 * 5. If a valid Content-Length header field is present without
5399 * Transfer-Encoding, its decimal value defines the expected message
5400 * body length in octets. If the sender closes the connection or
5401 * the recipient times out before the indicated number of octets are
5402 * received, the recipient MUST consider the message to be
5403 * incomplete and close the connection.
5404 *
5405 * 6. If this is a request message and none of the above are true, then
5406 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005407 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005408 * 7. Otherwise, this is a response message without a declared message
5409 * body length, so the message body length is determined by the
5410 * number of octets received prior to the server closing the
5411 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005412 */
5413
5414 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005415 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005416 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005417 * FIXME: should we parse anyway and return an error on chunked encoding ?
5418 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005419 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5420 txn->status == 101)) {
5421 /* Either we've established an explicit tunnel, or we're
5422 * switching the protocol. In both cases, we're very unlikely
5423 * to understand the next protocols. We have to switch to tunnel
5424 * mode, so that we transfer the request and responses then let
5425 * this protocol pass unmodified. When we later implement specific
5426 * parsers for such protocols, we'll want to check the Upgrade
5427 * header which contains information about that protocol for
5428 * responses with status 101 (eg: see RFC2817 about TLS).
5429 */
5430 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5431 msg->flags |= HTTP_MSGF_XFER_LEN;
5432 goto end;
5433 }
5434
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005435 if (txn->meth == HTTP_METH_HEAD ||
5436 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005437 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005438 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005439 goto skip_content_length;
5440 }
5441
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005442 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005443 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02005444 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005445 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005446 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5447 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005448 /* bad transfer-encoding (chunked followed by something else) */
5449 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005450 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005451 break;
5452 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005453 }
5454
Willy Tarreau1c913912015-04-30 10:57:51 +02005455 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005456 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005457 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02005458 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
5459 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5460 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005461 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005462 signed long long cl;
5463
Willy Tarreauad14f752011-09-02 20:33:27 +02005464 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005465 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005466 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005467 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005468
Willy Tarreauad14f752011-09-02 20:33:27 +02005469 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005470 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005471 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005472 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005473
Willy Tarreauad14f752011-09-02 20:33:27 +02005474 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005475 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005476 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005477 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005478
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005479 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005480 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005481 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005482 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005483
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005484 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005485 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005486 }
5487
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005488 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01005489 /* Now we have to check if we need to modify the Connection header.
5490 * This is more difficult on the response than it is on the request,
5491 * because we can have two different HTTP versions and we don't know
5492 * how the client will interprete a response. For instance, let's say
5493 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5494 * HTTP/1.1 response without any header. Maybe it will bound itself to
5495 * HTTP/1.0 because it only knows about it, and will consider the lack
5496 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5497 * the lack of header as a keep-alive. Thus we will use two flags
5498 * indicating how a request MAY be understood by the client. In case
5499 * of multiple possibilities, we'll fix the header to be explicit. If
5500 * ambiguous cases such as both close and keepalive are seen, then we
5501 * will fall back to explicit close. Note that we won't take risks with
5502 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005503 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005504 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005505 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5506 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5507 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5508 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005509 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005510
Willy Tarreau70dffda2014-01-30 03:07:23 +01005511 /* this situation happens when combining pretend-keepalive with httpclose. */
5512 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005513 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005514 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01005515 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
5516
Willy Tarreau60466522010-01-18 19:08:45 +01005517 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005518 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005519 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5520 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005521
Willy Tarreau60466522010-01-18 19:08:45 +01005522 /* now adjust header transformations depending on current state */
5523 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5524 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5525 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005526 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005527 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005528 }
Willy Tarreau60466522010-01-18 19:08:45 +01005529 else { /* SCL / KAL */
5530 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005531 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005532 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005533 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005534
Willy Tarreau60466522010-01-18 19:08:45 +01005535 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005536 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005537
Willy Tarreau60466522010-01-18 19:08:45 +01005538 /* Some keep-alive responses are converted to Server-close if
5539 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005540 */
Willy Tarreau60466522010-01-18 19:08:45 +01005541 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5542 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005543 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005544 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005545 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005546 }
5547
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005548 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02005549 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005550 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005551
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005552 /* end of job, return OK */
5553 rep->analysers &= ~an_bit;
5554 rep->analyse_exp = TICK_ETERNITY;
5555 channel_auto_close(rep);
5556 return 1;
5557
5558 abort_keep_alive:
5559 /* A keep-alive request to the server failed on a network error.
5560 * The client is required to retry. We need to close without returning
5561 * any other information so that the client retries.
5562 */
5563 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005564 rep->analysers &= AN_RES_FLT_END;
5565 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005566 channel_auto_close(rep);
5567 s->logs.logwait = 0;
5568 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005569 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01005570 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005571 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005572 return 0;
5573}
5574
5575/* This function performs all the processing enabled for the current response.
5576 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005577 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005578 * other functions. It works like process_request (see indications above).
5579 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005580int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005581{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005582 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005583 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005584 struct http_msg *msg = &txn->rsp;
5585 struct proxy *cur_proxy;
5586 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01005587 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005588
Willy Tarreau87b09662015-04-03 00:22:06 +02005589 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 +02005590 now_ms, __FUNCTION__,
5591 s,
5592 rep,
5593 rep->rex, rep->wex,
5594 rep->flags,
5595 rep->buf->i,
5596 rep->analysers);
5597
5598 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
5599 return 0;
5600
Willy Tarreau70730dd2014-04-24 18:06:27 +02005601 /* The stats applet needs to adjust the Connection header but we don't
5602 * apply any filter there.
5603 */
Willy Tarreau612adb82015-03-10 15:25:54 +01005604 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
5605 rep->analysers &= ~an_bit;
5606 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02005607 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01005608 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02005609
Willy Tarreau58975672014-04-24 21:13:57 +02005610 /*
5611 * We will have to evaluate the filters.
5612 * As opposed to version 1.2, now they will be evaluated in the
5613 * filters order and not in the header order. This means that
5614 * each filter has to be validated among all headers.
5615 *
5616 * Filters are tried with ->be first, then with ->fe if it is
5617 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005618 *
5619 * Maybe we are in resume condiion. In this case I choose the
5620 * "struct proxy" which contains the rule list matching the resume
5621 * pointer. If none of theses "struct proxy" match, I initialise
5622 * the process with the first one.
5623 *
5624 * In fact, I check only correspondance betwwen the current list
5625 * pointer and the ->fe rule list. If it doesn't match, I initialize
5626 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02005627 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005628 if (s->current_rule_list == &sess->fe->http_res_rules)
5629 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005630 else
5631 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02005632 while (1) {
5633 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005634
Willy Tarreau58975672014-04-24 21:13:57 +02005635 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02005636 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005637 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005638
Willy Tarreau51d861a2015-05-22 17:30:48 +02005639 if (ret == HTTP_RULE_RES_BADREQ)
5640 goto return_srv_prx_502;
5641
5642 if (ret == HTTP_RULE_RES_DONE) {
5643 rep->analysers &= ~an_bit;
5644 rep->analyse_exp = TICK_ETERNITY;
5645 return 1;
5646 }
5647 }
5648
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005649 /* we need to be called again. */
5650 if (ret == HTTP_RULE_RES_YIELD) {
5651 channel_dont_close(rep);
5652 return 0;
5653 }
5654
Willy Tarreau58975672014-04-24 21:13:57 +02005655 /* try headers filters */
5656 if (rule_set->rsp_exp != NULL) {
5657 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5658 return_bad_resp:
5659 if (objt_server(s->target)) {
5660 objt_server(s->target)->counters.failed_resp++;
5661 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005662 }
Willy Tarreau58975672014-04-24 21:13:57 +02005663 s->be->be_counters.failed_resp++;
5664 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005665 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02005666 txn->status = 502;
5667 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01005668 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005669 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005670 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02005671 if (!(s->flags & SF_ERR_MASK))
5672 s->flags |= SF_ERR_PRXCOND;
5673 if (!(s->flags & SF_FINST_MASK))
5674 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02005675 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005676 }
Willy Tarreau58975672014-04-24 21:13:57 +02005677 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005678
Willy Tarreau58975672014-04-24 21:13:57 +02005679 /* has the response been denied ? */
5680 if (txn->flags & TX_SVDENY) {
5681 if (objt_server(s->target))
5682 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005683
Willy Tarreau58975672014-04-24 21:13:57 +02005684 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005685 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005686 if (sess->listener->counters)
5687 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005688
Willy Tarreau58975672014-04-24 21:13:57 +02005689 goto return_srv_prx_502;
5690 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005691
Willy Tarreau58975672014-04-24 21:13:57 +02005692 /* add response headers from the rule sets in the same order */
5693 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02005694 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005695 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005696 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005697 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02005698 ret = acl_pass(ret);
5699 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5700 ret = !ret;
5701 if (!ret)
5702 continue;
5703 }
5704 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
5705 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005706 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005707
Willy Tarreau58975672014-04-24 21:13:57 +02005708 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005709 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02005710 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005711 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02005712 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005713
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005714 /* After this point, this anayzer can't return yield, so we can
5715 * remove the bit corresponding to this analyzer from the list.
5716 *
5717 * Note that the intermediate returns and goto found previously
5718 * reset the analyzers.
5719 */
5720 rep->analysers &= ~an_bit;
5721 rep->analyse_exp = TICK_ETERNITY;
5722
Willy Tarreau58975672014-04-24 21:13:57 +02005723 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005724 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005725 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005726
Willy Tarreau58975672014-04-24 21:13:57 +02005727 /*
5728 * Now check for a server cookie.
5729 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005730 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005731 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005732
Willy Tarreau58975672014-04-24 21:13:57 +02005733 /*
5734 * Check for cache-control or pragma headers if required.
5735 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005736 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 +02005737 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005738
Willy Tarreau58975672014-04-24 21:13:57 +02005739 /*
5740 * Add server cookie in the response if needed
5741 */
5742 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5743 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005744 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005745 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5746 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5747 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5748 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5749 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005750 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005751 /* the server is known, it's not the one the client requested, or the
5752 * cookie's last seen date needs to be refreshed. We have to
5753 * insert a set-cookie here, except if we want to insert only on POST
5754 * requests and this one isn't. Note that servers which don't have cookies
5755 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005756 */
Willy Tarreau58975672014-04-24 21:13:57 +02005757 if (!objt_server(s->target)->cookie) {
5758 chunk_printf(&trash,
5759 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5760 s->be->cookie_name);
5761 }
5762 else {
5763 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005764
Willy Tarreau58975672014-04-24 21:13:57 +02005765 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5766 /* emit last_date, which is mandatory */
5767 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5768 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5769 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005770
Willy Tarreau58975672014-04-24 21:13:57 +02005771 if (s->be->cookie_maxlife) {
5772 /* emit first_date, which is either the original one or
5773 * the current date.
5774 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005775 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005776 s30tob64(txn->cookie_first_date ?
5777 txn->cookie_first_date >> 2 :
5778 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005779 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005780 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005781 }
Willy Tarreau58975672014-04-24 21:13:57 +02005782 chunk_appendf(&trash, "; path=/");
5783 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005784
Willy Tarreau58975672014-04-24 21:13:57 +02005785 if (s->be->cookie_domain)
5786 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005787
Willy Tarreau58975672014-04-24 21:13:57 +02005788 if (s->be->ck_opts & PR_CK_HTTPONLY)
5789 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005790
Willy Tarreau58975672014-04-24 21:13:57 +02005791 if (s->be->ck_opts & PR_CK_SECURE)
5792 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005793
Willy Tarreau58975672014-04-24 21:13:57 +02005794 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
5795 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005796
Willy Tarreau58975672014-04-24 21:13:57 +02005797 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005798 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005799 /* the server did not change, only the date was updated */
5800 txn->flags |= TX_SCK_UPDATED;
5801 else
5802 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005803
Willy Tarreau58975672014-04-24 21:13:57 +02005804 /* Here, we will tell an eventual cache on the client side that we don't
5805 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5806 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5807 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005808 */
Willy Tarreau58975672014-04-24 21:13:57 +02005809 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005810
Willy Tarreau58975672014-04-24 21:13:57 +02005811 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005812
Willy Tarreau58975672014-04-24 21:13:57 +02005813 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5814 "Cache-control: private", 22) < 0))
5815 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005816 }
Willy Tarreau58975672014-04-24 21:13:57 +02005817 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005818
Willy Tarreau58975672014-04-24 21:13:57 +02005819 /*
5820 * Check if result will be cacheable with a cookie.
5821 * We'll block the response if security checks have caught
5822 * nasty things such as a cacheable cookie.
5823 */
5824 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5825 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5826 (s->be->options & PR_O_CHK_CACHE)) {
5827 /* we're in presence of a cacheable response containing
5828 * a set-cookie header. We'll block it as requested by
5829 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005830 */
Willy Tarreau58975672014-04-24 21:13:57 +02005831 if (objt_server(s->target))
5832 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01005833
Willy Tarreau58975672014-04-24 21:13:57 +02005834 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005835 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005836 if (sess->listener->counters)
5837 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005838
Willy Tarreau58975672014-04-24 21:13:57 +02005839 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5840 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5841 send_log(s->be, LOG_ALERT,
5842 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5843 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5844 goto return_srv_prx_502;
5845 }
Willy Tarreau03945942009-12-22 16:50:27 +01005846
Willy Tarreau70730dd2014-04-24 18:06:27 +02005847 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005848 /*
5849 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5850 * If an "Upgrade" token is found, the header is left untouched in order
5851 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005852 * if anything but "Upgrade" is present in the Connection header. We don't
5853 * want to touch any 101 response either since it's switching to another
5854 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005855 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005856 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02005857 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005858 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02005859 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
5860 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005861
Willy Tarreau58975672014-04-24 21:13:57 +02005862 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5863 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5864 /* we want a keep-alive response here. Keep-alive header
5865 * required if either side is not 1.1.
5866 */
5867 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5868 want_flags |= TX_CON_KAL_SET;
5869 }
5870 else {
5871 /* we want a close response here. Close header required if
5872 * the server is 1.1, regardless of the client.
5873 */
5874 if (msg->flags & HTTP_MSGF_VER_11)
5875 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005876 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005877
Willy Tarreau58975672014-04-24 21:13:57 +02005878 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5879 http_change_connection_header(txn, msg, want_flags);
5880 }
5881
5882 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02005883 /* Always enter in the body analyzer */
5884 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
5885 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005886
Willy Tarreau58975672014-04-24 21:13:57 +02005887 /* if the user wants to log as soon as possible, without counting
5888 * bytes from the server, then this is the right moment. We have
5889 * to temporarily assign bytes_out to log what we currently have.
5890 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005891 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005892 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
5893 s->logs.bytes_out = txn->rsp.eoh;
5894 s->do_log(s);
5895 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005896 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005897 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005898}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005899
Willy Tarreaud98cf932009-12-27 22:54:55 +01005900/* This function is an analyser which forwards response body (including chunk
5901 * sizes if any). It is called as soon as we must forward, even if we forward
5902 * zero byte. The only situation where it must not be called is when we're in
5903 * tunnel mode and we want to forward till the close. It's used both to forward
5904 * remaining data and to resync after end of body. It expects the msg_state to
5905 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005906 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02005907 *
5908 * It is capable of compressing response data both in content-length mode and
5909 * in chunked mode. The state machines follows different flows depending on
5910 * whether content-length and chunked modes are used, since there are no
5911 * trailers in content-length :
5912 *
5913 * chk-mode cl-mode
5914 * ,----- BODY -----.
5915 * / \
5916 * V size > 0 V chk-mode
5917 * .--> SIZE -------------> DATA -------------> CRLF
5918 * | | size == 0 | last byte |
5919 * | v final crlf v inspected |
5920 * | TRAILERS -----------> DONE |
5921 * | |
5922 * `----------------------------------------------'
5923 *
5924 * Compression only happens in the DATA state, and must be flushed in final
5925 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
5926 * is performed at once on final states for all bytes parsed, or when leaving
5927 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005928 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005929int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005930{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005931 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005932 struct http_txn *txn = s->txn;
5933 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01005934 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005935
Christopher Faulet814d2702017-03-30 11:33:44 +02005936 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
5937 now_ms, __FUNCTION__,
5938 s,
5939 res,
5940 res->rex, res->wex,
5941 res->flags,
5942 res->buf->i,
5943 res->analysers);
5944
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005945 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5946 return 0;
5947
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005948 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005949 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02005950 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005951 /* Output closed while we were sending data. We must abort and
5952 * wake the other side up.
5953 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005954 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005955 msg->msg_state = HTTP_MSG_ERROR;
5956 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005957 return 1;
5958 }
5959
Willy Tarreau4fe41902010-06-07 22:27:41 +02005960 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005961 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005962
Christopher Fauletd7c91962015-04-30 11:48:27 +02005963 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005964 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5965 ? HTTP_MSG_CHUNK_SIZE
5966 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005967 }
5968
Willy Tarreauefdf0942014-04-24 20:08:57 +02005969 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005970 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02005971 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005972 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005973 }
5974
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005975 if (msg->msg_state < HTTP_MSG_DONE) {
5976 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5977 ? http_msg_forward_chunked_body(s, msg)
5978 : http_msg_forward_body(s, msg));
5979 if (!ret)
5980 goto missing_data_or_waiting;
5981 if (ret < 0)
5982 goto return_bad_res;
5983 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005984
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005985 /* other states, DONE...TUNNEL */
5986 /* for keep-alive we don't want to forward closes on DONE */
5987 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5988 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5989 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005990
Christopher Faulet894da4c2017-07-18 11:29:07 +02005991 http_resync_states(s);
5992 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005993 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5994 if (res->flags & CF_SHUTW) {
5995 /* response errors are most likely due to the
5996 * client aborting the transfer. */
5997 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01005998 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005999 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006000 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006001 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006002 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006003 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006004 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02006005 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006006
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006007 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01006008 if (res->flags & CF_SHUTW)
6009 goto aborted_xfer;
6010
6011 /* stop waiting for data if the input is closed before the end. If the
6012 * client side was already closed, it means that the client has aborted,
6013 * so we don't want to count this as a server abort. Otherwise it's a
6014 * server abort.
6015 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02006016 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006017 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006018 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006019 /* If we have some pending data, we continue the processing */
6020 if (!buffer_pending(res->buf)) {
6021 if (!(s->flags & SF_ERR_MASK))
6022 s->flags |= SF_ERR_SRVCL;
6023 s->be->be_counters.srv_aborts++;
6024 if (objt_server(s->target))
6025 objt_server(s->target)->counters.srv_aborts++;
6026 goto return_bad_res_stats_ok;
6027 }
Willy Tarreau40dba092010-03-04 18:14:51 +01006028 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006029
Willy Tarreau40dba092010-03-04 18:14:51 +01006030 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006031 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006032 goto return_bad_res;
6033
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006034 /* When TE: chunked is used, we need to get there again to parse
6035 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02006036 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
6037 * or if there are filters registered on the stream, we don't want to
6038 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006039 */
Christopher Faulet69744d92017-03-30 10:54:35 +02006040 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006041 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006042 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6043 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006044 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006045
Willy Tarreau5c620922011-05-11 19:56:11 +02006046 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006047 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006048 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006049 * modes are already handled by the stream sock layer. We must not do
6050 * this in content-length mode because it could present the MSG_MORE
6051 * flag with the last block of forwarded data, which would cause an
6052 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006053 */
Christopher Faulet92d36382015-11-05 13:35:03 +01006054 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006055 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006056
Willy Tarreau87b09662015-04-03 00:22:06 +02006057 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006058 return 0;
6059
Willy Tarreau40dba092010-03-04 18:14:51 +01006060 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006061 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006062 if (objt_server(s->target))
6063 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006064
6065 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006066 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006067 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006068 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006069 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006070 res->analysers &= AN_RES_FLT_END;
6071 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 +01006072 if (objt_server(s->target))
6073 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006074
Willy Tarreaue7dff022015-04-03 01:14:29 +02006075 if (!(s->flags & SF_ERR_MASK))
6076 s->flags |= SF_ERR_PRXCOND;
6077 if (!(s->flags & SF_FINST_MASK))
6078 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006079 return 0;
6080
6081 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006082 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006083 txn->rsp.msg_state = HTTP_MSG_ERROR;
6084 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006085 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006086 res->analysers &= AN_RES_FLT_END;
6087 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 +01006088
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006089 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006090 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006091 if (objt_server(s->target))
6092 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006093
Willy Tarreaue7dff022015-04-03 01:14:29 +02006094 if (!(s->flags & SF_ERR_MASK))
6095 s->flags |= SF_ERR_CLICL;
6096 if (!(s->flags & SF_FINST_MASK))
6097 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006098 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006099}
6100
6101
6102static inline int
6103http_msg_forward_body(struct stream *s, struct http_msg *msg)
6104{
6105 struct channel *chn = msg->chn;
6106 int ret;
6107
6108 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
6109
6110 if (msg->msg_state == HTTP_MSG_ENDING)
6111 goto ending;
6112
6113 /* Neither content-length, nor transfer-encoding was found, so we must
6114 * read the body until the server connection is closed. In that case, we
6115 * eat data as they come. Of course, this happens for response only. */
6116 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6117 unsigned long long len = (chn->buf->i - msg->next);
6118 msg->chunk_len += len;
6119 msg->body_len += len;
6120 }
Christopher Fauletda02e172015-12-04 09:25:05 +01006121 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
6122 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6123 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006124 msg->next += ret;
6125 msg->chunk_len -= ret;
6126 if (msg->chunk_len) {
6127 /* input empty or output full */
6128 if (chn->buf->i > msg->next)
6129 chn->flags |= CF_WAKE_WRITE;
6130 goto missing_data_or_waiting;
6131 }
6132
Christopher Faulet1486b0a2017-07-18 11:42:08 +02006133 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
6134 * always set for a request. */
6135 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6136 /* The server still sending data that should be filtered */
6137 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
6138 goto missing_data_or_waiting;
6139 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006140
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006141 msg->msg_state = HTTP_MSG_ENDING;
6142
6143 ending:
6144 /* we may have some pending data starting at res->buf->p such as a last
6145 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006146 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6147 /* default_ret */ msg->next,
6148 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006149 b_adv(chn->buf, ret);
6150 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006151 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6152 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006153 if (msg->next)
6154 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006155
Christopher Fauletda02e172015-12-04 09:25:05 +01006156 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
6157 /* default_ret */ 1,
6158 /* on_error */ goto error,
6159 /* on_wait */ goto waiting);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006160 msg->msg_state = HTTP_MSG_DONE;
6161 return 1;
6162
6163 missing_data_or_waiting:
6164 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006165 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6166 /* default_ret */ msg->next,
6167 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006168 b_adv(chn->buf, ret);
6169 msg->next -= ret;
6170 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6171 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006172 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006173 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006174 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006175 return 0;
6176 error:
6177 return -1;
6178}
6179
6180static inline int
6181http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
6182{
6183 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006184 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006185 int ret;
6186
6187 /* Here we have the guarantee to be in one of the following state:
6188 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
6189 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
6190
6191 switch_states:
6192 switch (msg->msg_state) {
6193 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01006194 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
6195 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6196 /* on_error */ goto error);
6197 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006198 msg->chunk_len -= ret;
6199 if (msg->chunk_len) {
6200 /* input empty or output full */
6201 if (chn->buf->i > msg->next)
6202 chn->flags |= CF_WAKE_WRITE;
6203 goto missing_data_or_waiting;
6204 }
6205
6206 /* nothing left to forward for this chunk*/
6207 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
6208 /* fall through for HTTP_MSG_CHUNK_CRLF */
6209
6210 case HTTP_MSG_CHUNK_CRLF:
6211 /* we want the CRLF after the data */
Willy Tarreaub2892562017-09-21 11:33:54 +02006212 ret = h1_skip_chunk_crlf(chn->buf, msg->next, chn->buf->i);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006213 if (ret == 0)
6214 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02006215 if (ret < 0) {
6216 msg->err_pos = chn->buf->i + ret;
6217 if (msg->err_pos < 0)
6218 msg->err_pos += chn->buf->size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006219 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02006220 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01006221 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006222 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6223 /* fall through for HTTP_MSG_CHUNK_SIZE */
6224
6225 case HTTP_MSG_CHUNK_SIZE:
6226 /* read the chunk size and assign it to ->chunk_len,
6227 * then set ->next to point to the body and switch to
6228 * DATA or TRAILERS state.
6229 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006230 ret = h1_parse_chunk_size(chn->buf, msg->next, chn->buf->i, &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006231 if (ret == 0)
6232 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006233 if (ret < 0) {
6234 msg->err_pos = chn->buf->i + ret;
6235 if (msg->err_pos < 0)
6236 msg->err_pos += chn->buf->size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006237 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006238 }
6239
6240 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01006241 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006242 msg->chunk_len = chunk;
6243 msg->body_len += chunk;
6244
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006245 if (msg->chunk_len) {
6246 msg->msg_state = HTTP_MSG_DATA;
6247 goto switch_states;
6248 }
6249 msg->msg_state = HTTP_MSG_TRAILERS;
6250 /* fall through for HTTP_MSG_TRAILERS */
6251
6252 case HTTP_MSG_TRAILERS:
6253 ret = http_forward_trailers(msg);
6254 if (ret < 0)
6255 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01006256 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
6257 /* default_ret */ 1,
6258 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006259 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006260 if (!ret)
6261 goto missing_data_or_waiting;
6262 break;
6263
6264 case HTTP_MSG_ENDING:
6265 goto ending;
6266
6267 default:
6268 /* This should no happen in this function */
6269 goto error;
6270 }
6271
6272 msg->msg_state = HTTP_MSG_ENDING;
6273 ending:
6274 /* we may have some pending data starting at res->buf->p such as a last
6275 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006276 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006277 /* default_ret */ msg->next,
6278 /* on_error */ goto error);
6279 b_adv(chn->buf, ret);
6280 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006281 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6282 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006283 if (msg->next)
6284 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006285
Christopher Fauletda02e172015-12-04 09:25:05 +01006286 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006287 /* default_ret */ 1,
6288 /* on_error */ goto error,
6289 /* on_wait */ goto waiting);
6290 msg->msg_state = HTTP_MSG_DONE;
6291 return 1;
6292
6293 missing_data_or_waiting:
6294 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006295 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006296 /* default_ret */ msg->next,
6297 /* on_error */ goto error);
6298 b_adv(chn->buf, ret);
6299 msg->next -= ret;
6300 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6301 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006302 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006303 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006304 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006305 return 0;
6306
6307 chunk_parsing_error:
6308 if (msg->err_pos >= 0) {
6309 if (chn->flags & CF_ISRESP)
6310 http_capture_bad_message(&s->be->invalid_rep, s, msg,
6311 msg->msg_state, strm_fe(s));
6312 else
6313 http_capture_bad_message(&strm_fe(s)->invalid_req, s,
6314 msg, msg->msg_state, s->be);
6315 }
6316 error:
6317 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006318}
6319
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006320
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006321/* Iterate the same filter through all request headers.
6322 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006323 * Since it can manage the switch to another backend, it updates the per-proxy
6324 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006325 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006326int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006327{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006328 char *cur_ptr, *cur_end, *cur_next;
6329 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006330 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006331 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006332 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006333
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006334 last_hdr = 0;
6335
Willy Tarreau9b28e032012-10-12 23:49:43 +02006336 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006337 old_idx = 0;
6338
6339 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006340 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006341 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006342 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006343 (exp->action == ACT_ALLOW ||
6344 exp->action == ACT_DENY ||
6345 exp->action == ACT_TARPIT))
6346 return 0;
6347
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006348 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006349 if (!cur_idx)
6350 break;
6351
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006352 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006353 cur_ptr = cur_next;
6354 cur_end = cur_ptr + cur_hdr->len;
6355 cur_next = cur_end + cur_hdr->cr + 1;
6356
6357 /* Now we have one header between cur_ptr and cur_end,
6358 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006359 */
6360
Willy Tarreau15a53a42015-01-21 13:39:42 +01006361 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006362 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006363 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006364 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006365 last_hdr = 1;
6366 break;
6367
6368 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006369 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006370 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006371 break;
6372
6373 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006374 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006375 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006376 break;
6377
6378 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006379 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6380 if (trash.len < 0)
6381 return -1;
6382
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006383 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006384 /* FIXME: if the user adds a newline in the replacement, the
6385 * index will not be recalculated for now, and the new line
6386 * will not be counted as a new header.
6387 */
6388
6389 cur_end += delta;
6390 cur_next += delta;
6391 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006392 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006393 break;
6394
6395 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006396 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006397 cur_next += delta;
6398
Willy Tarreaufa355d42009-11-29 18:12:29 +01006399 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006400 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6401 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006402 cur_hdr->len = 0;
6403 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006404 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006405 break;
6406
6407 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006408 }
6409
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006410 /* keep the link from this header to next one in case of later
6411 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006412 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006413 old_idx = cur_idx;
6414 }
6415 return 0;
6416}
6417
6418
6419/* Apply the filter to the request line.
6420 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6421 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006422 * Since it can manage the switch to another backend, it updates the per-proxy
6423 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006424 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006425int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006426{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006427 char *cur_ptr, *cur_end;
6428 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006429 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006430 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006431
Willy Tarreau3d300592007-03-18 18:34:41 +01006432 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006433 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006434 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006435 (exp->action == ACT_ALLOW ||
6436 exp->action == ACT_DENY ||
6437 exp->action == ACT_TARPIT))
6438 return 0;
6439 else if (exp->action == ACT_REMOVE)
6440 return 0;
6441
6442 done = 0;
6443
Willy Tarreau9b28e032012-10-12 23:49:43 +02006444 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006445 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006446
6447 /* Now we have the request line between cur_ptr and cur_end */
6448
Willy Tarreau15a53a42015-01-21 13:39:42 +01006449 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006450 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006451 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006452 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006453 done = 1;
6454 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006455
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006456 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006457 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006458 done = 1;
6459 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006460
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006461 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006462 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006463 done = 1;
6464 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006465
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006466 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006467 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6468 if (trash.len < 0)
6469 return -1;
6470
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006471 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006472 /* FIXME: if the user adds a newline in the replacement, the
6473 * index will not be recalculated for now, and the new line
6474 * will not be counted as a new header.
6475 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006476
Willy Tarreaufa355d42009-11-29 18:12:29 +01006477 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006478 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006479 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006480 HTTP_MSG_RQMETH,
6481 cur_ptr, cur_end + 1,
6482 NULL, NULL);
6483 if (unlikely(!cur_end))
6484 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006485
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006486 /* we have a full request and we know that we have either a CR
6487 * or an LF at <ptr>.
6488 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006489 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6490 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006491 /* there is no point trying this regex on headers */
6492 return 1;
6493 }
6494 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006495 return done;
6496}
Willy Tarreau97de6242006-12-27 17:18:38 +01006497
Willy Tarreau58f10d72006-12-04 02:26:12 +01006498
Willy Tarreau58f10d72006-12-04 02:26:12 +01006499
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006500/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006501 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006502 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006503 * unparsable request. Since it can manage the switch to another backend, it
6504 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006505 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006506int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006507{
Willy Tarreau192252e2015-04-04 01:47:55 +02006508 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006509 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006510 struct hdr_exp *exp;
6511
6512 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006513 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006514
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006515 /*
6516 * The interleaving of transformations and verdicts
6517 * makes it difficult to decide to continue or stop
6518 * the evaluation.
6519 */
6520
Willy Tarreau6c123b12010-01-28 20:22:06 +01006521 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6522 break;
6523
Willy Tarreau3d300592007-03-18 18:34:41 +01006524 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006525 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006526 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006527 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006528
6529 /* if this filter had a condition, evaluate it now and skip to
6530 * next filter if the condition does not match.
6531 */
6532 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006533 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006534 ret = acl_pass(ret);
6535 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6536 ret = !ret;
6537
6538 if (!ret)
6539 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006540 }
6541
6542 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006543 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006544 if (unlikely(ret < 0))
6545 return -1;
6546
6547 if (likely(ret == 0)) {
6548 /* The filter did not match the request, it can be
6549 * iterated through all headers.
6550 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01006551 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
6552 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006553 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006554 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006555 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006556}
6557
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006558
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006559/* Find the end of a cookie value contained between <s> and <e>. It works the
6560 * same way as with headers above except that the semi-colon also ends a token.
6561 * See RFC2965 for more information. Note that it requires a valid header to
6562 * return a valid result.
6563 */
6564char *find_cookie_value_end(char *s, const char *e)
6565{
6566 int quoted, qdpair;
6567
6568 quoted = qdpair = 0;
6569 for (; s < e; s++) {
6570 if (qdpair) qdpair = 0;
6571 else if (quoted) {
6572 if (*s == '\\') qdpair = 1;
6573 else if (*s == '"') quoted = 0;
6574 }
6575 else if (*s == '"') quoted = 1;
6576 else if (*s == ',' || *s == ';') return s;
6577 }
6578 return s;
6579}
6580
6581/* Delete a value in a header between delimiters <from> and <next> in buffer
6582 * <buf>. The number of characters displaced is returned, and the pointer to
6583 * the first delimiter is updated if required. The function tries as much as
6584 * possible to respect the following principles :
6585 * - replace <from> delimiter by the <next> one unless <from> points to a
6586 * colon, in which case <next> is simply removed
6587 * - set exactly one space character after the new first delimiter, unless
6588 * there are not enough characters in the block being moved to do so.
6589 * - remove unneeded spaces before the previous delimiter and after the new
6590 * one.
6591 *
6592 * It is the caller's responsibility to ensure that :
6593 * - <from> points to a valid delimiter or the colon ;
6594 * - <next> points to a valid delimiter or the final CR/LF ;
6595 * - there are non-space chars before <from> ;
6596 * - there is a CR/LF at or after <next>.
6597 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006598int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006599{
6600 char *prev = *from;
6601
6602 if (*prev == ':') {
6603 /* We're removing the first value, preserve the colon and add a
6604 * space if possible.
6605 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006606 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006607 next++;
6608 prev++;
6609 if (prev < next)
6610 *prev++ = ' ';
6611
Willy Tarreau2235b262016-11-05 15:50:20 +01006612 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006613 next++;
6614 } else {
6615 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01006616 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006617 prev--;
6618 *from = prev;
6619
6620 /* copy the delimiter and if possible a space if we're
6621 * not at the end of the line.
6622 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006623 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006624 *prev++ = *next++;
6625 if (prev + 1 < next)
6626 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01006627 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006628 next++;
6629 }
6630 }
6631 return buffer_replace2(buf, prev, next, NULL, 0);
6632}
6633
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006634/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006635 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006636 * desirable to call it only when needed. This code is quite complex because
6637 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6638 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006639 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006640void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006641{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006642 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006643 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006644 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006645 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006646 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6647 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006648
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006649 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006650 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006651 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006652
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006653 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006654 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006655 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006656
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006657 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006658 hdr_beg = hdr_next;
6659 hdr_end = hdr_beg + cur_hdr->len;
6660 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006661
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006662 /* We have one full header between hdr_beg and hdr_end, and the
6663 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006664 * "Cookie:" headers.
6665 */
6666
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006667 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006668 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006669 old_idx = cur_idx;
6670 continue;
6671 }
6672
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006673 del_from = NULL; /* nothing to be deleted */
6674 preserve_hdr = 0; /* assume we may kill the whole header */
6675
Willy Tarreau58f10d72006-12-04 02:26:12 +01006676 /* Now look for cookies. Conforming to RFC2109, we have to support
6677 * attributes whose name begin with a '$', and associate them with
6678 * the right cookie, if we want to delete this cookie.
6679 * So there are 3 cases for each cookie read :
6680 * 1) it's a special attribute, beginning with a '$' : ignore it.
6681 * 2) it's a server id cookie that we *MAY* want to delete : save
6682 * some pointers on it (last semi-colon, beginning of cookie...)
6683 * 3) it's an application cookie : we *MAY* have to delete a previous
6684 * "special" cookie.
6685 * At the end of loop, if a "special" cookie remains, we may have to
6686 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006687 * *MUST* delete it.
6688 *
6689 * Note: RFC2965 is unclear about the processing of spaces around
6690 * the equal sign in the ATTR=VALUE form. A careful inspection of
6691 * the RFC explicitly allows spaces before it, and not within the
6692 * tokens (attrs or values). An inspection of RFC2109 allows that
6693 * too but section 10.1.3 lets one think that spaces may be allowed
6694 * after the equal sign too, resulting in some (rare) buggy
6695 * implementations trying to do that. So let's do what servers do.
6696 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6697 * allowed quoted strings in values, with any possible character
6698 * after a backslash, including control chars and delimitors, which
6699 * causes parsing to become ambiguous. Browsers also allow spaces
6700 * within values even without quotes.
6701 *
6702 * We have to keep multiple pointers in order to support cookie
6703 * removal at the beginning, middle or end of header without
6704 * corrupting the header. All of these headers are valid :
6705 *
6706 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6707 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6708 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6709 * | | | | | | | | |
6710 * | | | | | | | | hdr_end <--+
6711 * | | | | | | | +--> next
6712 * | | | | | | +----> val_end
6713 * | | | | | +-----------> val_beg
6714 * | | | | +--------------> equal
6715 * | | | +----------------> att_end
6716 * | | +---------------------> att_beg
6717 * | +--------------------------> prev
6718 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006719 */
6720
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006721 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6722 /* Iterate through all cookies on this line */
6723
6724 /* find att_beg */
6725 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006726 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006727 att_beg++;
6728
6729 /* find att_end : this is the first character after the last non
6730 * space before the equal. It may be equal to hdr_end.
6731 */
6732 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006733
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006734 while (equal < hdr_end) {
6735 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006736 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006737 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006738 continue;
6739 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006740 }
6741
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006742 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6743 * is between <att_beg> and <equal>, both may be identical.
6744 */
6745
6746 /* look for end of cookie if there is an equal sign */
6747 if (equal < hdr_end && *equal == '=') {
6748 /* look for the beginning of the value */
6749 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006750 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006751 val_beg++;
6752
6753 /* find the end of the value, respecting quotes */
6754 next = find_cookie_value_end(val_beg, hdr_end);
6755
6756 /* make val_end point to the first white space or delimitor after the value */
6757 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006758 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006759 val_end--;
6760 } else {
6761 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006762 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006763
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006764 /* We have nothing to do with attributes beginning with '$'. However,
6765 * they will automatically be removed if a header before them is removed,
6766 * since they're supposed to be linked together.
6767 */
6768 if (*att_beg == '$')
6769 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006770
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006771 /* Ignore cookies with no equal sign */
6772 if (equal == next) {
6773 /* This is not our cookie, so we must preserve it. But if we already
6774 * scheduled another cookie for removal, we cannot remove the
6775 * complete header, but we can remove the previous block itself.
6776 */
6777 preserve_hdr = 1;
6778 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006779 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006780 val_end += delta;
6781 next += delta;
6782 hdr_end += delta;
6783 hdr_next += delta;
6784 cur_hdr->len += delta;
6785 http_msg_move_end(&txn->req, delta);
6786 prev = del_from;
6787 del_from = NULL;
6788 }
6789 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006790 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006791
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006792 /* if there are spaces around the equal sign, we need to
6793 * strip them otherwise we'll get trouble for cookie captures,
6794 * or even for rewrites. Since this happens extremely rarely,
6795 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006796 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006797 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6798 int stripped_before = 0;
6799 int stripped_after = 0;
6800
6801 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006802 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006803 equal += stripped_before;
6804 val_beg += stripped_before;
6805 }
6806
6807 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006808 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006809 val_beg += stripped_after;
6810 stripped_before += stripped_after;
6811 }
6812
6813 val_end += stripped_before;
6814 next += stripped_before;
6815 hdr_end += stripped_before;
6816 hdr_next += stripped_before;
6817 cur_hdr->len += stripped_before;
6818 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006819 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006820 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006821
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006822 /* First, let's see if we want to capture this cookie. We check
6823 * that we don't already have a client side cookie, because we
6824 * can only capture one. Also as an optimisation, we ignore
6825 * cookies shorter than the declared name.
6826 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006827 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6828 (val_end - att_beg >= sess->fe->capture_namelen) &&
6829 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006830 int log_len = val_end - att_beg;
6831
6832 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6833 Alert("HTTP logging : out of memory.\n");
6834 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006835 if (log_len > sess->fe->capture_len)
6836 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006837 memcpy(txn->cli_cookie, att_beg, log_len);
6838 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006839 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006840 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006841
Willy Tarreaubca99692010-10-06 19:25:55 +02006842 /* Persistence cookies in passive, rewrite or insert mode have the
6843 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006844 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006845 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006846 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006847 * For cookies in prefix mode, the form is :
6848 *
6849 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006850 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006851 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6852 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6853 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006854 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006855
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006856 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6857 * have the server ID between val_beg and delim, and the original cookie between
6858 * delim+1 and val_end. Otherwise, delim==val_end :
6859 *
6860 * Cookie: NAME=SRV; # in all but prefix modes
6861 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6862 * | || || | |+-> next
6863 * | || || | +--> val_end
6864 * | || || +---------> delim
6865 * | || |+------------> val_beg
6866 * | || +-------------> att_end = equal
6867 * | |+-----------------> att_beg
6868 * | +------------------> prev
6869 * +-------------------------> hdr_beg
6870 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006871
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006872 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006873 for (delim = val_beg; delim < val_end; delim++)
6874 if (*delim == COOKIE_DELIM)
6875 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006876 } else {
6877 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006878 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006879 /* Now check if the cookie contains a date field, which would
6880 * appear after a vertical bar ('|') just after the server name
6881 * and before the delimiter.
6882 */
6883 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6884 if (vbar1) {
6885 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006886 * right is the last seen date. It is a base64 encoded
6887 * 30-bit value representing the UNIX date since the
6888 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006889 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006890 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006891 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006892 if (val_end - vbar1 >= 5) {
6893 val = b64tos30(vbar1);
6894 if (val > 0)
6895 txn->cookie_last_date = val << 2;
6896 }
6897 /* look for a second vertical bar */
6898 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6899 if (vbar1 && (val_end - vbar1 > 5)) {
6900 val = b64tos30(vbar1 + 1);
6901 if (val > 0)
6902 txn->cookie_first_date = val << 2;
6903 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006904 }
6905 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006906
Willy Tarreauf64d1412010-10-07 20:06:11 +02006907 /* if the cookie has an expiration date and the proxy wants to check
6908 * it, then we do that now. We first check if the cookie is too old,
6909 * then only if it has expired. We detect strict overflow because the
6910 * time resolution here is not great (4 seconds). Cookies with dates
6911 * in the future are ignored if their offset is beyond one day. This
6912 * allows an admin to fix timezone issues without expiring everyone
6913 * and at the same time avoids keeping unwanted side effects for too
6914 * long.
6915 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006916 if (txn->cookie_first_date && s->be->cookie_maxlife &&
6917 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006918 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006919 txn->flags &= ~TX_CK_MASK;
6920 txn->flags |= TX_CK_OLD;
6921 delim = val_beg; // let's pretend we have not found the cookie
6922 txn->cookie_first_date = 0;
6923 txn->cookie_last_date = 0;
6924 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006925 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
6926 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006927 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006928 txn->flags &= ~TX_CK_MASK;
6929 txn->flags |= TX_CK_EXPIRED;
6930 delim = val_beg; // let's pretend we have not found the cookie
6931 txn->cookie_first_date = 0;
6932 txn->cookie_last_date = 0;
6933 }
6934
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006935 /* Here, we'll look for the first running server which supports the cookie.
6936 * This allows to share a same cookie between several servers, for example
6937 * to dedicate backup servers to specific servers only.
6938 * However, to prevent clients from sticking to cookie-less backup server
6939 * when they have incidentely learned an empty cookie, we simply ignore
6940 * empty cookies and mark them as invalid.
6941 * The same behaviour is applied when persistence must be ignored.
6942 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006943 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006944 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006945
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006946 while (srv) {
6947 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6948 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02006949 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006950 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006951 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006952 /* we found the server and we can use it */
6953 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02006954 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006955 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006956 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006957 break;
6958 } else {
6959 /* we found a server, but it's down,
6960 * mark it as such and go on in case
6961 * another one is available.
6962 */
6963 txn->flags &= ~TX_CK_MASK;
6964 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006965 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006966 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006967 srv = srv->next;
6968 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006969
Willy Tarreauf64d1412010-10-07 20:06:11 +02006970 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006971 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006972 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006973 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006974 txn->flags |= TX_CK_UNUSED;
6975 else
6976 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006977 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006978
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006979 /* depending on the cookie mode, we may have to either :
6980 * - delete the complete cookie if we're in insert+indirect mode, so that
6981 * the server never sees it ;
6982 * - remove the server id from the cookie value, and tag the cookie as an
6983 * application cookie so that it does not get accidentely removed later,
6984 * if we're in cookie prefix mode
6985 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006986 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006987 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006988
Willy Tarreau9b28e032012-10-12 23:49:43 +02006989 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006990 val_end += delta;
6991 next += delta;
6992 hdr_end += delta;
6993 hdr_next += delta;
6994 cur_hdr->len += delta;
6995 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006996
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006997 del_from = NULL;
6998 preserve_hdr = 1; /* we want to keep this cookie */
6999 }
7000 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007001 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007002 del_from = prev;
7003 }
7004 } else {
7005 /* This is not our cookie, so we must preserve it. But if we already
7006 * scheduled another cookie for removal, we cannot remove the
7007 * complete header, but we can remove the previous block itself.
7008 */
7009 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007010
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007011 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007012 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007013 if (att_beg >= del_from)
7014 att_beg += delta;
7015 if (att_end >= del_from)
7016 att_end += delta;
7017 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007018 val_end += delta;
7019 next += delta;
7020 hdr_end += delta;
7021 hdr_next += delta;
7022 cur_hdr->len += delta;
7023 http_msg_move_end(&txn->req, delta);
7024 prev = del_from;
7025 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007026 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007027 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007028
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007029 /* continue with next cookie on this header line */
7030 att_beg = next;
7031 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007032
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007033 /* There are no more cookies on this line.
7034 * We may still have one (or several) marked for deletion at the
7035 * end of the line. We must do this now in two ways :
7036 * - if some cookies must be preserved, we only delete from the
7037 * mark to the end of line ;
7038 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007039 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007040 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007041 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007042 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007043 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007044 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007045 cur_hdr->len += delta;
7046 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007047 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007048
7049 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007050 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7051 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007052 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007053 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007054 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007055 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007056 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007057 }
7058
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007059 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007060 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007061 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007062}
7063
7064
Willy Tarreaua15645d2007-03-18 16:22:39 +01007065/* Iterate the same filter through all response headers contained in <rtr>.
7066 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7067 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007068int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007069{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007070 char *cur_ptr, *cur_end, *cur_next;
7071 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007072 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007073 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007074 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007075
7076 last_hdr = 0;
7077
Willy Tarreau9b28e032012-10-12 23:49:43 +02007078 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007079 old_idx = 0;
7080
7081 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007082 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007083 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007084 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007085 (exp->action == ACT_ALLOW ||
7086 exp->action == ACT_DENY))
7087 return 0;
7088
7089 cur_idx = txn->hdr_idx.v[old_idx].next;
7090 if (!cur_idx)
7091 break;
7092
7093 cur_hdr = &txn->hdr_idx.v[cur_idx];
7094 cur_ptr = cur_next;
7095 cur_end = cur_ptr + cur_hdr->len;
7096 cur_next = cur_end + cur_hdr->cr + 1;
7097
7098 /* Now we have one header between cur_ptr and cur_end,
7099 * and the next header starts at cur_next.
7100 */
7101
Willy Tarreau15a53a42015-01-21 13:39:42 +01007102 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007103 switch (exp->action) {
7104 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007105 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007106 last_hdr = 1;
7107 break;
7108
7109 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007110 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007111 last_hdr = 1;
7112 break;
7113
7114 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007115 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7116 if (trash.len < 0)
7117 return -1;
7118
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007119 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007120 /* FIXME: if the user adds a newline in the replacement, the
7121 * index will not be recalculated for now, and the new line
7122 * will not be counted as a new header.
7123 */
7124
7125 cur_end += delta;
7126 cur_next += delta;
7127 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007128 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007129 break;
7130
7131 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007132 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007133 cur_next += delta;
7134
Willy Tarreaufa355d42009-11-29 18:12:29 +01007135 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007136 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7137 txn->hdr_idx.used--;
7138 cur_hdr->len = 0;
7139 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007140 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007141 break;
7142
7143 }
7144 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007145
7146 /* keep the link from this header to next one in case of later
7147 * removal of next header.
7148 */
7149 old_idx = cur_idx;
7150 }
7151 return 0;
7152}
7153
7154
7155/* Apply the filter to the status line in the response buffer <rtr>.
7156 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7157 * or -1 if a replacement resulted in an invalid status line.
7158 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007159int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007160{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007161 char *cur_ptr, *cur_end;
7162 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007163 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007164 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007165
7166
Willy Tarreau3d300592007-03-18 18:34:41 +01007167 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007168 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007169 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007170 (exp->action == ACT_ALLOW ||
7171 exp->action == ACT_DENY))
7172 return 0;
7173 else if (exp->action == ACT_REMOVE)
7174 return 0;
7175
7176 done = 0;
7177
Willy Tarreau9b28e032012-10-12 23:49:43 +02007178 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007179 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007180
7181 /* Now we have the status line between cur_ptr and cur_end */
7182
Willy Tarreau15a53a42015-01-21 13:39:42 +01007183 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007184 switch (exp->action) {
7185 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007186 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007187 done = 1;
7188 break;
7189
7190 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007191 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007192 done = 1;
7193 break;
7194
7195 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007196 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7197 if (trash.len < 0)
7198 return -1;
7199
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007200 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007201 /* FIXME: if the user adds a newline in the replacement, the
7202 * index will not be recalculated for now, and the new line
7203 * will not be counted as a new header.
7204 */
7205
Willy Tarreaufa355d42009-11-29 18:12:29 +01007206 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007207 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007208 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007209 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007210 cur_ptr, cur_end + 1,
7211 NULL, NULL);
7212 if (unlikely(!cur_end))
7213 return -1;
7214
7215 /* we have a full respnse and we know that we have either a CR
7216 * or an LF at <ptr>.
7217 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007218 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007219 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007220 /* there is no point trying this regex on headers */
7221 return 1;
7222 }
7223 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007224 return done;
7225}
7226
7227
7228
7229/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007230 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007231 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7232 * unparsable response.
7233 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007234int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007235{
Willy Tarreau192252e2015-04-04 01:47:55 +02007236 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007237 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007238 struct hdr_exp *exp;
7239
7240 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007241 int ret;
7242
7243 /*
7244 * The interleaving of transformations and verdicts
7245 * makes it difficult to decide to continue or stop
7246 * the evaluation.
7247 */
7248
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007249 if (txn->flags & TX_SVDENY)
7250 break;
7251
Willy Tarreau3d300592007-03-18 18:34:41 +01007252 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007253 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7254 exp->action == ACT_PASS)) {
7255 exp = exp->next;
7256 continue;
7257 }
7258
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007259 /* if this filter had a condition, evaluate it now and skip to
7260 * next filter if the condition does not match.
7261 */
7262 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007263 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007264 ret = acl_pass(ret);
7265 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7266 ret = !ret;
7267 if (!ret)
7268 continue;
7269 }
7270
Willy Tarreaua15645d2007-03-18 16:22:39 +01007271 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007272 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007273 if (unlikely(ret < 0))
7274 return -1;
7275
7276 if (likely(ret == 0)) {
7277 /* The filter did not match the response, it can be
7278 * iterated through all headers.
7279 */
Sasha Pachevc6002042014-05-26 12:33:48 -06007280 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
7281 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007282 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007283 }
7284 return 0;
7285}
7286
7287
Willy Tarreaua15645d2007-03-18 16:22:39 +01007288/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007289 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007290 * desirable to call it only when needed. This function is also used when we
7291 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007292 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007293void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007294{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007295 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007296 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01007297 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007298 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007299 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007300 char *hdr_beg, *hdr_end, *hdr_next;
7301 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007302
Willy Tarreaua15645d2007-03-18 16:22:39 +01007303 /* Iterate through the headers.
7304 * we start with the start line.
7305 */
7306 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007307 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007308
7309 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7310 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007311 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007312
7313 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007314 hdr_beg = hdr_next;
7315 hdr_end = hdr_beg + cur_hdr->len;
7316 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007317
Willy Tarreau24581ba2010-08-31 22:39:35 +02007318 /* We have one full header between hdr_beg and hdr_end, and the
7319 * next header starts at hdr_next. We're only interested in
7320 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007321 */
7322
Willy Tarreau24581ba2010-08-31 22:39:35 +02007323 is_cookie2 = 0;
7324 prev = hdr_beg + 10;
7325 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007326 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007327 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7328 if (!val) {
7329 old_idx = cur_idx;
7330 continue;
7331 }
7332 is_cookie2 = 1;
7333 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007334 }
7335
Willy Tarreau24581ba2010-08-31 22:39:35 +02007336 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7337 * <prev> points to the colon.
7338 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007339 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007340
Willy Tarreau24581ba2010-08-31 22:39:35 +02007341 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7342 * check-cache is enabled) and we are not interested in checking
7343 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007344 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02007345 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007346 return;
7347
Willy Tarreau24581ba2010-08-31 22:39:35 +02007348 /* OK so now we know we have to process this response cookie.
7349 * The format of the Set-Cookie header is slightly different
7350 * from the format of the Cookie header in that it does not
7351 * support the comma as a cookie delimiter (thus the header
7352 * cannot be folded) because the Expires attribute described in
7353 * the original Netscape's spec may contain an unquoted date
7354 * with a comma inside. We have to live with this because
7355 * many browsers don't support Max-Age and some browsers don't
7356 * support quoted strings. However the Set-Cookie2 header is
7357 * clean.
7358 *
7359 * We have to keep multiple pointers in order to support cookie
7360 * removal at the beginning, middle or end of header without
7361 * corrupting the header (in case of set-cookie2). A special
7362 * pointer, <scav> points to the beginning of the set-cookie-av
7363 * fields after the first semi-colon. The <next> pointer points
7364 * either to the end of line (set-cookie) or next unquoted comma
7365 * (set-cookie2). All of these headers are valid :
7366 *
7367 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7368 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7369 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7370 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7371 * | | | | | | | | | |
7372 * | | | | | | | | +-> next hdr_end <--+
7373 * | | | | | | | +------------> scav
7374 * | | | | | | +--------------> val_end
7375 * | | | | | +--------------------> val_beg
7376 * | | | | +----------------------> equal
7377 * | | | +------------------------> att_end
7378 * | | +----------------------------> att_beg
7379 * | +------------------------------> prev
7380 * +-----------------------------------------> hdr_beg
7381 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007382
Willy Tarreau24581ba2010-08-31 22:39:35 +02007383 for (; prev < hdr_end; prev = next) {
7384 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007385
Willy Tarreau24581ba2010-08-31 22:39:35 +02007386 /* find att_beg */
7387 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007388 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007389 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007390
Willy Tarreau24581ba2010-08-31 22:39:35 +02007391 /* find att_end : this is the first character after the last non
7392 * space before the equal. It may be equal to hdr_end.
7393 */
7394 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007395
Willy Tarreau24581ba2010-08-31 22:39:35 +02007396 while (equal < hdr_end) {
7397 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7398 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007399 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007400 continue;
7401 att_end = equal;
7402 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007403
Willy Tarreau24581ba2010-08-31 22:39:35 +02007404 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7405 * is between <att_beg> and <equal>, both may be identical.
7406 */
7407
7408 /* look for end of cookie if there is an equal sign */
7409 if (equal < hdr_end && *equal == '=') {
7410 /* look for the beginning of the value */
7411 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007412 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007413 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007414
Willy Tarreau24581ba2010-08-31 22:39:35 +02007415 /* find the end of the value, respecting quotes */
7416 next = find_cookie_value_end(val_beg, hdr_end);
7417
7418 /* make val_end point to the first white space or delimitor after the value */
7419 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007420 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007421 val_end--;
7422 } else {
7423 /* <equal> points to next comma, semi-colon or EOL */
7424 val_beg = val_end = next = equal;
7425 }
7426
7427 if (next < hdr_end) {
7428 /* Set-Cookie2 supports multiple cookies, and <next> points to
7429 * a colon or semi-colon before the end. So skip all attr-value
7430 * pairs and look for the next comma. For Set-Cookie, since
7431 * commas are permitted in values, skip to the end.
7432 */
7433 if (is_cookie2)
7434 next = find_hdr_value_end(next, hdr_end);
7435 else
7436 next = hdr_end;
7437 }
7438
7439 /* Now everything is as on the diagram above */
7440
7441 /* Ignore cookies with no equal sign */
7442 if (equal == val_end)
7443 continue;
7444
7445 /* If there are spaces around the equal sign, we need to
7446 * strip them otherwise we'll get trouble for cookie captures,
7447 * or even for rewrites. Since this happens extremely rarely,
7448 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007449 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007450 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7451 int stripped_before = 0;
7452 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007453
Willy Tarreau24581ba2010-08-31 22:39:35 +02007454 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007455 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007456 equal += stripped_before;
7457 val_beg += stripped_before;
7458 }
7459
7460 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007461 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007462 val_beg += stripped_after;
7463 stripped_before += stripped_after;
7464 }
7465
7466 val_end += stripped_before;
7467 next += stripped_before;
7468 hdr_end += stripped_before;
7469 hdr_next += stripped_before;
7470 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007471 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007472 }
7473
7474 /* First, let's see if we want to capture this cookie. We check
7475 * that we don't already have a server side cookie, because we
7476 * can only capture one. Also as an optimisation, we ignore
7477 * cookies shorter than the declared name.
7478 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007479 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007480 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007481 (val_end - att_beg >= sess->fe->capture_namelen) &&
7482 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007483 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007484 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007485 Alert("HTTP logging : out of memory.\n");
7486 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007487 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007488 if (log_len > sess->fe->capture_len)
7489 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01007490 memcpy(txn->srv_cookie, att_beg, log_len);
7491 txn->srv_cookie[log_len] = 0;
7492 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007493 }
7494
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007495 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007496 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007497 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007498 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7499 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007500 /* assume passive cookie by default */
7501 txn->flags &= ~TX_SCK_MASK;
7502 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007503
7504 /* If the cookie is in insert mode on a known server, we'll delete
7505 * this occurrence because we'll insert another one later.
7506 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007507 * a direct access.
7508 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007509 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007510 /* The "preserve" flag was set, we don't want to touch the
7511 * server's cookie.
7512 */
7513 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007514 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007515 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007516 /* this cookie must be deleted */
7517 if (*prev == ':' && next == hdr_end) {
7518 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007519 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007520 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7521 txn->hdr_idx.used--;
7522 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007523 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007524 hdr_next += delta;
7525 http_msg_move_end(&txn->rsp, delta);
7526 /* note: while both invalid now, <next> and <hdr_end>
7527 * are still equal, so the for() will stop as expected.
7528 */
7529 } else {
7530 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007531 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007532 next = prev;
7533 hdr_end += delta;
7534 hdr_next += delta;
7535 cur_hdr->len += delta;
7536 http_msg_move_end(&txn->rsp, delta);
7537 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007538 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007539 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007540 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007541 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007542 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007543 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007544 * with this server since we know it.
7545 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007546 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007547 next += delta;
7548 hdr_end += delta;
7549 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007550 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007551 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007552
Willy Tarreauf1348312010-10-07 15:54:11 +02007553 txn->flags &= ~TX_SCK_MASK;
7554 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007555 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007556 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007557 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007558 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007559 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007560 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007561 next += delta;
7562 hdr_end += delta;
7563 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007564 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007565 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007566
Willy Tarreau827aee92011-03-10 16:55:02 +01007567 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007568 txn->flags &= ~TX_SCK_MASK;
7569 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007570 }
7571 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007572 /* that's done for this cookie, check the next one on the same
7573 * line when next != hdr_end (only if is_cookie2).
7574 */
7575 }
7576 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007577 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007578 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007579}
7580
7581
Willy Tarreaua15645d2007-03-18 16:22:39 +01007582/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007583 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007584 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007585void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007586{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007587 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007588 char *p1, *p2;
7589
7590 char *cur_ptr, *cur_end, *cur_next;
7591 int cur_idx;
7592
Willy Tarreau5df51872007-11-25 16:20:08 +01007593 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007594 return;
7595
7596 /* Iterate through the headers.
7597 * we start with the start line.
7598 */
7599 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007600 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007601
7602 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7603 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007604 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007605
7606 cur_hdr = &txn->hdr_idx.v[cur_idx];
7607 cur_ptr = cur_next;
7608 cur_end = cur_ptr + cur_hdr->len;
7609 cur_next = cur_end + cur_hdr->cr + 1;
7610
7611 /* We have one full header between cur_ptr and cur_end, and the
7612 * next header starts at cur_next. We're only interested in
7613 * "Cookie:" headers.
7614 */
7615
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007616 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7617 if (val) {
7618 if ((cur_end - (cur_ptr + val) >= 8) &&
7619 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7620 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7621 return;
7622 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007623 }
7624
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007625 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7626 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007627 continue;
7628
7629 /* OK, right now we know we have a cache-control header at cur_ptr */
7630
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007631 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007632
7633 if (p1 >= cur_end) /* no more info */
7634 continue;
7635
7636 /* p1 is at the beginning of the value */
7637 p2 = p1;
7638
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007639 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007640 p2++;
7641
7642 /* we have a complete value between p1 and p2 */
7643 if (p2 < cur_end && *p2 == '=') {
7644 /* we have something of the form no-cache="set-cookie" */
7645 if ((cur_end - p1 >= 21) &&
7646 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7647 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007648 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007649 continue;
7650 }
7651
7652 /* OK, so we know that either p2 points to the end of string or to a comma */
7653 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007654 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01007655 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7656 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7657 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007658 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007659 return;
7660 }
7661
7662 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007663 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007664 continue;
7665 }
7666 }
7667}
7668
Willy Tarreau58f10d72006-12-04 02:26:12 +01007669
Willy Tarreaub2513902006-12-17 14:52:38 +01007670/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007671 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007672 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007673 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007674 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007675 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007676 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007677 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007678 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007679int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007680{
7681 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007682 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007683 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007684
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007685 if (!uri_auth)
7686 return 0;
7687
Cyril Bonté70be45d2010-10-12 00:14:35 +02007688 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007689 return 0;
7690
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007691 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007692 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007693 return 0;
7694
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007695 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007696 return 0;
7697
Willy Tarreaub2513902006-12-17 14:52:38 +01007698 return 1;
7699}
7700
Willy Tarreau4076a152009-04-02 15:18:36 +02007701/*
7702 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007703 * By default it tries to report the error position as msg->err_pos. However if
7704 * this one is not set, it will then report msg->next, which is the last known
7705 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007706 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007707 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007708void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007709 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007710 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007711{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007712 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007713 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007714 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007715
Willy Tarreauf3764b72016-03-31 13:45:10 +02007716 es->len = MIN(chn->buf->i, global.tune.bufsize);
Willy Tarreau9b28e032012-10-12 23:49:43 +02007717 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007718 len1 = MIN(len1, es->len);
7719 len2 = es->len - len1; /* remaining data if buffer wraps */
7720
Willy Tarreauf3764b72016-03-31 13:45:10 +02007721 if (!es->buf)
7722 es->buf = malloc(global.tune.bufsize);
7723
7724 if (es->buf) {
7725 memcpy(es->buf, chn->buf->p, len1);
7726 if (len2)
7727 memcpy(es->buf + len1, chn->buf->data, len2);
7728 }
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007729
Willy Tarreau4076a152009-04-02 15:18:36 +02007730 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007731 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007732 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007733 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007734
Willy Tarreau4076a152009-04-02 15:18:36 +02007735 es->when = date; // user-visible date
7736 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007737 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007738 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007739 if (objt_conn(sess->origin))
7740 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007741 else
7742 memset(&es->src, 0, sizeof(es->src));
7743
Willy Tarreau078272e2010-12-12 12:46:33 +01007744 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007745 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007746 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007747 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007748 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007749 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007750 es->b_out = chn->buf->o;
7751 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007752 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007753 es->m_clen = msg->chunk_len;
7754 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02007755}
Willy Tarreaub2513902006-12-17 14:52:38 +01007756
Willy Tarreau294c4732011-12-16 21:35:50 +01007757/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7758 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7759 * performed over the whole headers. Otherwise it must contain a valid header
7760 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7761 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7762 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7763 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007764 * -1. The value fetch stops at commas, so this function is suited for use with
7765 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007766 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007767 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007768unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007769 struct hdr_idx *idx, int occ,
7770 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007771{
Willy Tarreau294c4732011-12-16 21:35:50 +01007772 struct hdr_ctx local_ctx;
7773 char *ptr_hist[MAX_HDR_HISTORY];
7774 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007775 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007776 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007777
Willy Tarreau294c4732011-12-16 21:35:50 +01007778 if (!ctx) {
7779 local_ctx.idx = 0;
7780 ctx = &local_ctx;
7781 }
7782
Willy Tarreaubce70882009-09-07 11:51:47 +02007783 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007784 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007785 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007786 occ--;
7787 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007788 *vptr = ctx->line + ctx->val;
7789 *vlen = ctx->vlen;
7790 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007791 }
7792 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007793 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007794 }
7795
7796 /* negative occurrence, we scan all the list then walk back */
7797 if (-occ > MAX_HDR_HISTORY)
7798 return 0;
7799
Willy Tarreau294c4732011-12-16 21:35:50 +01007800 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007801 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007802 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7803 len_hist[hist_ptr] = ctx->vlen;
7804 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007805 hist_ptr = 0;
7806 found++;
7807 }
7808 if (-occ > found)
7809 return 0;
7810 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02007811 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7812 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7813 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02007814 */
Willy Tarreau67dad272013-06-12 22:27:44 +02007815 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02007816 if (hist_ptr >= MAX_HDR_HISTORY)
7817 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007818 *vptr = ptr_hist[hist_ptr];
7819 *vlen = len_hist[hist_ptr];
7820 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007821}
7822
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007823/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7824 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7825 * performed over the whole headers. Otherwise it must contain a valid header
7826 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7827 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7828 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7829 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7830 * -1. This function differs from http_get_hdr() in that it only returns full
7831 * line header values and does not stop at commas.
7832 * The return value is 0 if nothing was found, or non-zero otherwise.
7833 */
7834unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
7835 struct hdr_idx *idx, int occ,
7836 struct hdr_ctx *ctx, char **vptr, int *vlen)
7837{
7838 struct hdr_ctx local_ctx;
7839 char *ptr_hist[MAX_HDR_HISTORY];
7840 int len_hist[MAX_HDR_HISTORY];
7841 unsigned int hist_ptr;
7842 int found;
7843
7844 if (!ctx) {
7845 local_ctx.idx = 0;
7846 ctx = &local_ctx;
7847 }
7848
7849 if (occ >= 0) {
7850 /* search from the beginning */
7851 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
7852 occ--;
7853 if (occ <= 0) {
7854 *vptr = ctx->line + ctx->val;
7855 *vlen = ctx->vlen;
7856 return 1;
7857 }
7858 }
7859 return 0;
7860 }
7861
7862 /* negative occurrence, we scan all the list then walk back */
7863 if (-occ > MAX_HDR_HISTORY)
7864 return 0;
7865
7866 found = hist_ptr = 0;
7867 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
7868 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7869 len_hist[hist_ptr] = ctx->vlen;
7870 if (++hist_ptr >= MAX_HDR_HISTORY)
7871 hist_ptr = 0;
7872 found++;
7873 }
7874 if (-occ > found)
7875 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007876
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007877 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007878 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7879 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7880 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007881 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007882 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007883 if (hist_ptr >= MAX_HDR_HISTORY)
7884 hist_ptr -= MAX_HDR_HISTORY;
7885 *vptr = ptr_hist[hist_ptr];
7886 *vlen = len_hist[hist_ptr];
7887 return 1;
7888}
7889
Willy Tarreaubaaee002006-06-26 02:48:02 +02007890/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007891 * Print a debug line with a header. Always stop at the first CR or LF char,
7892 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7893 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007894 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007895void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007896{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007897 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007898 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007899
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007900 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007901 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02007902 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
7903 objt_conn(s->si[1].end) ? (unsigned short)objt_conn(s->si[1].end)->handle.fd : -1);
Willy Tarreaue92693a2012-09-24 21:13:39 +02007904
7905 for (max = 0; start + max < end; max++)
7906 if (start[max] == '\r' || start[max] == '\n')
7907 break;
7908
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007909 UBOUND(max, trash.size - trash.len - 3);
7910 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
7911 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01007912 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007913}
7914
Willy Tarreaueee5b512015-04-03 23:46:31 +02007915
7916/* Allocate a new HTTP transaction for stream <s> unless there is one already.
7917 * The hdr_idx is allocated as well. In case of allocation failure, everything
7918 * allocated is freed and NULL is returned. Otherwise the new transaction is
7919 * assigned to the stream and returned.
7920 */
7921struct http_txn *http_alloc_txn(struct stream *s)
7922{
7923 struct http_txn *txn = s->txn;
7924
7925 if (txn)
7926 return txn;
7927
7928 txn = pool_alloc2(pool2_http_txn);
7929 if (!txn)
7930 return txn;
7931
7932 txn->hdr_idx.size = global.tune.max_http_hdr;
7933 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
7934 if (!txn->hdr_idx.v) {
7935 pool_free2(pool2_http_txn, txn);
7936 return NULL;
7937 }
7938
7939 s->txn = txn;
7940 return txn;
7941}
7942
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007943void http_txn_reset_req(struct http_txn *txn)
7944{
7945 txn->req.flags = 0;
7946 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
7947 txn->req.next = 0;
7948 txn->req.chunk_len = 0LL;
7949 txn->req.body_len = 0LL;
7950 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7951}
7952
7953void http_txn_reset_res(struct http_txn *txn)
7954{
7955 txn->rsp.flags = 0;
7956 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
7957 txn->rsp.next = 0;
7958 txn->rsp.chunk_len = 0LL;
7959 txn->rsp.body_len = 0LL;
7960 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
7961}
7962
Willy Tarreau0937bc42009-12-22 15:03:09 +01007963/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007964 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01007965 * the required fields are properly allocated and that we only need to (re)init
7966 * them. This should be used before processing any new request.
7967 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007968void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007969{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007970 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007971 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007972
7973 txn->flags = 0;
7974 txn->status = -1;
7975
Willy Tarreauf64d1412010-10-07 20:06:11 +02007976 txn->cookie_first_date = 0;
7977 txn->cookie_last_date = 0;
7978
Willy Tarreaueee5b512015-04-03 23:46:31 +02007979 txn->srv_cookie = NULL;
7980 txn->cli_cookie = NULL;
7981 txn->uri = NULL;
7982
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007983 http_txn_reset_req(txn);
7984 http_txn_reset_res(txn);
7985
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007986 txn->req.chn = &s->req;
7987 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007988
7989 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007990
7991 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7992 if (fe->options2 & PR_O2_REQBUG_OK)
7993 txn->req.err_pos = -1; /* let buggy requests pass */
7994
Willy Tarreau0937bc42009-12-22 15:03:09 +01007995 if (txn->hdr_idx.v)
7996 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02007997
7998 vars_init(&s->vars_txn, SCOPE_TXN);
7999 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008000}
8001
8002/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008003void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008004{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008005 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008006 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008007
8008 /* these ones will have been dynamically allocated */
8009 pool_free2(pool2_requri, txn->uri);
8010 pool_free2(pool2_capture, txn->cli_cookie);
8011 pool_free2(pool2_capture, txn->srv_cookie);
William Lallemanda73203e2012-03-12 12:48:57 +01008012 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008013
William Lallemanda73203e2012-03-12 12:48:57 +01008014 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008015 txn->uri = NULL;
8016 txn->srv_cookie = NULL;
8017 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008018
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008019 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008020 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008021 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008022 pool_free2(h->pool, s->req_cap[h->index]);
8023 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008024 }
8025
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008026 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008027 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008028 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008029 pool_free2(h->pool, s->res_cap[h->index]);
8030 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008031 }
8032
Willy Tarreau6204cd92016-03-10 16:33:04 +01008033 vars_prune(&s->vars_txn, s->sess, s);
8034 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008035}
8036
8037/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008038void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008039{
8040 http_end_txn(s);
8041 http_init_txn(s);
8042
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008043 /* reinitialise the current rule list pointer to NULL. We are sure that
8044 * any rulelist match the NULL pointer.
8045 */
8046 s->current_rule_list = NULL;
8047
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008048 s->be = strm_fe(s);
8049 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008050 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008051 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008052 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008053 /* re-init store persistence */
8054 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008055 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008056
Willy Tarreau0937bc42009-12-22 15:03:09 +01008057 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008058
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008059 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008060
Willy Tarreau739cfba2010-01-25 23:11:14 +01008061 /* We must trim any excess data from the response buffer, because we
8062 * may have blocked an invalid response from a server that we don't
8063 * want to accidentely forward once we disable the analysers, nor do
8064 * we want those data to come along with next response. A typical
8065 * example of such data would be from a buggy server responding to
8066 * a HEAD with some data, or sending more than the advertised
8067 * content-length.
8068 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008069 if (unlikely(s->res.buf->i))
8070 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008071
Christopher Fauletc0c672a2017-03-28 11:51:33 +02008072 /* Now we can realign the response buffer */
8073 buffer_realign(s->res.buf);
8074
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008075 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008076 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008077
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008078 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008079 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008080
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008081 s->req.rex = TICK_ETERNITY;
8082 s->req.wex = TICK_ETERNITY;
8083 s->req.analyse_exp = TICK_ETERNITY;
8084 s->res.rex = TICK_ETERNITY;
8085 s->res.wex = TICK_ETERNITY;
8086 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01008087 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008088}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008089
Sasha Pachev218f0642014-06-16 12:05:59 -06008090void free_http_res_rules(struct list *r)
8091{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008092 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06008093
8094 list_for_each_entry_safe(pr, tr, r, list) {
8095 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008096 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008097 free(pr);
8098 }
8099}
8100
8101void free_http_req_rules(struct list *r)
8102{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008103 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01008104
8105 list_for_each_entry_safe(pr, tr, r, list) {
8106 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008107 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008108 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008109
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008110 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008111 free(pr);
8112 }
8113}
8114
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008115/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008116struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01008117{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008118 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008119 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008120 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008121 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008122
Vincent Bernat02779b62016-04-03 13:48:43 +02008123 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01008124 if (!rule) {
8125 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008126 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008127 }
8128
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008129 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008130 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01008131 cur_arg = 1;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008132 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block") || !strcmp(args[0], "tarpit")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04008133 int code;
8134 int hc;
8135
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008136 if (!strcmp(args[0], "tarpit")) {
8137 rule->action = ACT_HTTP_REQ_TARPIT;
8138 rule->deny_status = HTTP_ERR_500;
8139 }
8140 else {
8141 rule->action = ACT_ACTION_DENY;
8142 rule->deny_status = HTTP_ERR_403;
8143 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008144 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04008145 if (strcmp(args[cur_arg], "deny_status") == 0) {
8146 cur_arg++;
8147 if (!args[cur_arg]) {
8148 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
8149 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
8150 goto out_err;
8151 }
8152
8153 code = atol(args[cur_arg]);
8154 cur_arg++;
8155 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
8156 if (http_err_codes[hc] == code) {
8157 rule->deny_status = hc;
8158 break;
8159 }
8160 }
8161
8162 if (hc >= HTTP_ERR_SIZE) {
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008163 Warning("parsing [%s:%d] : status code %d not handled, using default code %d.\n",
8164 file, linenum, code, http_err_codes[rule->deny_status]);
CJ Ess108b1dd2015-04-07 12:03:37 -04008165 }
8166 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008167 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008168 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008169 cur_arg = 1;
8170
8171 while(*args[cur_arg]) {
8172 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008173 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008174 cur_arg+=2;
8175 continue;
8176 } else
8177 break;
8178 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008179 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008180 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008181 cur_arg = 1;
8182
8183 if (!*args[cur_arg] ||
8184 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8185 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8186 file, linenum, args[0]);
8187 goto out_err;
8188 }
8189 rule->arg.nice = atoi(args[cur_arg]);
8190 if (rule->arg.nice < -1024)
8191 rule->arg.nice = -1024;
8192 else if (rule->arg.nice > 1024)
8193 rule->arg.nice = 1024;
8194 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008195 } else if (!strcmp(args[0], "set-tos")) {
8196#ifdef IP_TOS
8197 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008198 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008199 cur_arg = 1;
8200
8201 if (!*args[cur_arg] ||
8202 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8203 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8204 file, linenum, args[0]);
8205 goto out_err;
8206 }
8207
8208 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8209 if (err && *err != '\0') {
8210 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8211 file, linenum, err, args[0]);
8212 goto out_err;
8213 }
8214 cur_arg++;
8215#else
8216 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8217 goto out_err;
8218#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008219 } else if (!strcmp(args[0], "set-mark")) {
8220#ifdef SO_MARK
8221 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008222 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008223 cur_arg = 1;
8224
8225 if (!*args[cur_arg] ||
8226 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8227 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8228 file, linenum, args[0]);
8229 goto out_err;
8230 }
8231
8232 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8233 if (err && *err != '\0') {
8234 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8235 file, linenum, err, args[0]);
8236 goto out_err;
8237 }
8238 cur_arg++;
8239 global.last_checks |= LSTCHK_NETADM;
8240#else
8241 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8242 goto out_err;
8243#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008244 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008245 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008246 cur_arg = 1;
8247
8248 if (!*args[cur_arg] ||
8249 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8250 bad_log_level:
8251 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8252 file, linenum, args[0]);
8253 goto out_err;
8254 }
8255 if (strcmp(args[cur_arg], "silent") == 0)
8256 rule->arg.loglevel = -1;
8257 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8258 goto bad_log_level;
8259 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008260 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008261 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008262 cur_arg = 1;
8263
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008264 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8265 (*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 +01008266 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8267 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008268 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008269 }
8270
8271 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8272 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8273 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008274
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008275 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008276 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008277 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 +01008278 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8279 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8280 file, linenum, args[0], error);
8281 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008282 goto out_err;
8283 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008284 free(proxy->conf.lfs_file);
8285 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8286 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008287 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02008288 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008289 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008290 cur_arg = 1;
8291
8292 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02008293 (*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 -06008294 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
8295 file, linenum, args[0]);
8296 goto out_err;
8297 }
8298
8299 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8300 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8301 LIST_INIT(&rule->arg.hdr_add.fmt);
8302
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008303 error = NULL;
8304 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
8305 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8306 args[cur_arg + 1], error);
8307 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008308 goto out_err;
8309 }
8310
8311 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008312 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008313 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 +01008314 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8315 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8316 file, linenum, args[0], error);
8317 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008318 goto out_err;
8319 }
Sasha Pachev218f0642014-06-16 12:05:59 -06008320
8321 free(proxy->conf.lfs_file);
8322 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8323 proxy->conf.lfs_line = proxy->conf.args.line;
8324 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008325 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008326 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008327 cur_arg = 1;
8328
8329 if (!*args[cur_arg] ||
8330 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8331 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8332 file, linenum, args[0]);
8333 goto out_err;
8334 }
8335
8336 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8337 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8338
8339 proxy->conf.args.ctx = ARGC_HRQ;
8340 free(proxy->conf.lfs_file);
8341 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8342 proxy->conf.lfs_line = proxy->conf.args.line;
8343 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02008344 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
8345 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02008346 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02008347 struct sample_expr *expr;
8348 unsigned int where;
8349 char *err = NULL;
8350
8351 cur_arg = 1;
8352 proxy->conf.args.ctx = ARGC_TRK;
8353
8354 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
8355 if (!expr) {
8356 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8357 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
8358 free(err);
8359 goto out_err;
8360 }
8361
8362 where = 0;
8363 if (proxy->cap & PR_CAP_FE)
8364 where |= SMP_VAL_FE_HRQ_HDR;
8365 if (proxy->cap & PR_CAP_BE)
8366 where |= SMP_VAL_BE_HRQ_HDR;
8367
8368 if (!(expr->fetch->val & where)) {
8369 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
8370 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
8371 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
8372 args[cur_arg-1], sample_src_names(expr->fetch->use));
8373 free(expr);
8374 goto out_err;
8375 }
8376
8377 if (strcmp(args[cur_arg], "table") == 0) {
8378 cur_arg++;
8379 if (!args[cur_arg]) {
8380 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
8381 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
8382 free(expr);
8383 goto out_err;
8384 }
8385 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008386 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02008387 cur_arg++;
8388 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008389 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008390 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01008391 } else if (strcmp(args[0], "redirect") == 0) {
8392 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008393 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008394
Willy Tarreaube4653b2015-05-28 15:26:58 +02008395 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01008396 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8397 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8398 goto out_err;
8399 }
8400
8401 /* this redirect rule might already contain a parsed condition which
8402 * we'll pass to the http-request rule.
8403 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008404 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008405 rule->arg.redir = redir;
8406 rule->cond = redir->cond;
8407 redir->cond = NULL;
8408 cur_arg = 2;
8409 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008410 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8411 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008412 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008413 /*
8414 * '+ 8' for 'add-acl('
8415 * '- 9' for 'add-acl(' + trailing ')'
8416 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008417 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008418
8419 cur_arg = 1;
8420
8421 if (!*args[cur_arg] ||
8422 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8423 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8424 file, linenum, args[0]);
8425 goto out_err;
8426 }
8427
8428 LIST_INIT(&rule->arg.map.key);
8429 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008430 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008431 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008432 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8433 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8434 file, linenum, args[0], error);
8435 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008436 goto out_err;
8437 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008438 free(proxy->conf.lfs_file);
8439 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8440 proxy->conf.lfs_line = proxy->conf.args.line;
8441 cur_arg += 1;
8442 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8443 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008444 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008445 /*
8446 * '+ 8' for 'del-acl('
8447 * '- 9' for 'del-acl(' + trailing ')'
8448 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008449 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008450
8451 cur_arg = 1;
8452
8453 if (!*args[cur_arg] ||
8454 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8455 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8456 file, linenum, args[0]);
8457 goto out_err;
8458 }
8459
8460 LIST_INIT(&rule->arg.map.key);
8461 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008462 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008463 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008464 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8465 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8466 file, linenum, args[0], error);
8467 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008468 goto out_err;
8469 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008470 free(proxy->conf.lfs_file);
8471 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8472 proxy->conf.lfs_line = proxy->conf.args.line;
8473 cur_arg += 1;
8474 } else if (strncmp(args[0], "del-map", 7) == 0) {
8475 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008476 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008477 /*
8478 * '+ 8' for 'del-map('
8479 * '- 9' for 'del-map(' + trailing ')'
8480 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008481 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008482
8483 cur_arg = 1;
8484
8485 if (!*args[cur_arg] ||
8486 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8487 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8488 file, linenum, args[0]);
8489 goto out_err;
8490 }
8491
8492 LIST_INIT(&rule->arg.map.key);
8493 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008494 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008495 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008496 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8497 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8498 file, linenum, args[0], error);
8499 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008500 goto out_err;
8501 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008502 free(proxy->conf.lfs_file);
8503 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8504 proxy->conf.lfs_line = proxy->conf.args.line;
8505 cur_arg += 1;
8506 } else if (strncmp(args[0], "set-map", 7) == 0) {
8507 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008508 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008509 /*
8510 * '+ 8' for 'set-map('
8511 * '- 9' for 'set-map(' + trailing ')'
8512 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008513 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008514
8515 cur_arg = 1;
8516
8517 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8518 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8519 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8520 file, linenum, args[0]);
8521 goto out_err;
8522 }
8523
8524 LIST_INIT(&rule->arg.map.key);
8525 LIST_INIT(&rule->arg.map.value);
8526 proxy->conf.args.ctx = ARGC_HRQ;
8527
8528 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008529 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008530 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008531 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8532 Alert("parsing [%s:%d]: 'http-request %s' key: %s.\n",
8533 file, linenum, args[0], error);
8534 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008535 goto out_err;
8536 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008537
8538 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008539 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008540 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 +01008541 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8542 Alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n",
8543 file, linenum, args[0], error);
8544 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008545 goto out_err;
8546 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008547 free(proxy->conf.lfs_file);
8548 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8549 proxy->conf.lfs_line = proxy->conf.args.line;
8550
8551 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02008552 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
8553 char *errmsg = NULL;
8554 cur_arg = 1;
8555 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02008556 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02008557 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02008558 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02008559 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8560 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8561 free(errmsg);
8562 goto out_err;
8563 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008564 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02008565 action_build_list(&http_req_keywords.list, &trash);
8566 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
8567 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08008568 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02008569 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02008570 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008571 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008572 }
8573
8574 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8575 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008576 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008577
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008578 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8579 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8580 file, linenum, args[0], errmsg);
8581 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008582 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008583 }
8584 rule->cond = cond;
8585 }
8586 else if (*args[cur_arg]) {
8587 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8588 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8589 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008590 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008591 }
8592
8593 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008594 out_err:
8595 free(rule);
8596 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008597}
8598
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008599/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008600struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008601{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008602 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008603 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008604 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008605 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008606
8607 rule = calloc(1, sizeof(*rule));
8608 if (!rule) {
8609 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
8610 goto out_err;
8611 }
8612
8613 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008614 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008615 cur_arg = 1;
8616 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008617 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008618 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008619 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008620 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008621 cur_arg = 1;
8622
8623 if (!*args[cur_arg] ||
8624 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8625 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8626 file, linenum, args[0]);
8627 goto out_err;
8628 }
8629 rule->arg.nice = atoi(args[cur_arg]);
8630 if (rule->arg.nice < -1024)
8631 rule->arg.nice = -1024;
8632 else if (rule->arg.nice > 1024)
8633 rule->arg.nice = 1024;
8634 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008635 } else if (!strcmp(args[0], "set-tos")) {
8636#ifdef IP_TOS
8637 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008638 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008639 cur_arg = 1;
8640
8641 if (!*args[cur_arg] ||
8642 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8643 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8644 file, linenum, args[0]);
8645 goto out_err;
8646 }
8647
8648 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8649 if (err && *err != '\0') {
8650 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8651 file, linenum, err, args[0]);
8652 goto out_err;
8653 }
8654 cur_arg++;
8655#else
8656 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8657 goto out_err;
8658#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008659 } else if (!strcmp(args[0], "set-mark")) {
8660#ifdef SO_MARK
8661 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008662 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008663 cur_arg = 1;
8664
8665 if (!*args[cur_arg] ||
8666 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8667 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8668 file, linenum, args[0]);
8669 goto out_err;
8670 }
8671
8672 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8673 if (err && *err != '\0') {
8674 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8675 file, linenum, err, args[0]);
8676 goto out_err;
8677 }
8678 cur_arg++;
8679 global.last_checks |= LSTCHK_NETADM;
8680#else
8681 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8682 goto out_err;
8683#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008684 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008685 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008686 cur_arg = 1;
8687
8688 if (!*args[cur_arg] ||
8689 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8690 bad_log_level:
8691 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8692 file, linenum, args[0]);
8693 goto out_err;
8694 }
8695 if (strcmp(args[cur_arg], "silent") == 0)
8696 rule->arg.loglevel = -1;
Ruoshan Huangdd016782016-06-15 22:16:03 +08008697 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008698 goto bad_log_level;
8699 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008700 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008701 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008702 cur_arg = 1;
8703
8704 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8705 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8706 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8707 file, linenum, args[0]);
8708 goto out_err;
8709 }
8710
8711 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8712 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8713 LIST_INIT(&rule->arg.hdr_add.fmt);
8714
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008715 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008716 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008717 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 +01008718 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8719 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8720 file, linenum, args[0], error);
8721 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008722 goto out_err;
8723 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008724 free(proxy->conf.lfs_file);
8725 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8726 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008727 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06008728 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008729 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008730 cur_arg = 1;
8731
8732 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02008733 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
8734 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06008735 file, linenum, args[0]);
8736 goto out_err;
8737 }
8738
8739 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8740 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8741 LIST_INIT(&rule->arg.hdr_add.fmt);
8742
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008743 error = NULL;
8744 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
8745 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8746 args[cur_arg + 1], error);
8747 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008748 goto out_err;
8749 }
8750
8751 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008752 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008753 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 +01008754 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8755 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8756 file, linenum, args[0], error);
8757 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008758 goto out_err;
8759 }
Sasha Pachev218f0642014-06-16 12:05:59 -06008760
8761 free(proxy->conf.lfs_file);
8762 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8763 proxy->conf.lfs_line = proxy->conf.args.line;
8764 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008765 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008766 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008767 cur_arg = 1;
8768
8769 if (!*args[cur_arg] ||
8770 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8771 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8772 file, linenum, args[0]);
8773 goto out_err;
8774 }
8775
8776 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8777 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8778
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008779 proxy->conf.args.ctx = ARGC_HRS;
8780 free(proxy->conf.lfs_file);
8781 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8782 proxy->conf.lfs_line = proxy->conf.args.line;
8783 cur_arg += 1;
8784 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8785 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008786 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008787 /*
8788 * '+ 8' for 'add-acl('
8789 * '- 9' for 'add-acl(' + trailing ')'
8790 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008791 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008792
8793 cur_arg = 1;
8794
8795 if (!*args[cur_arg] ||
8796 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8797 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8798 file, linenum, args[0]);
8799 goto out_err;
8800 }
8801
8802 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008803 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008804 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008805 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008806 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8807 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8808 file, linenum, args[0], error);
8809 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008810 goto out_err;
8811 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008812 free(proxy->conf.lfs_file);
8813 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8814 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008815
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008816 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008817 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8818 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008819 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008820 /*
8821 * '+ 8' for 'del-acl('
8822 * '- 9' for 'del-acl(' + trailing ')'
8823 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008824 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008825
8826 cur_arg = 1;
8827
8828 if (!*args[cur_arg] ||
8829 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8830 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8831 file, linenum, args[0]);
8832 goto out_err;
8833 }
8834
8835 LIST_INIT(&rule->arg.map.key);
8836 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008837 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008838 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008839 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8840 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8841 file, linenum, args[0], error);
8842 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008843 goto out_err;
8844 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008845 free(proxy->conf.lfs_file);
8846 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8847 proxy->conf.lfs_line = proxy->conf.args.line;
8848 cur_arg += 1;
8849 } else if (strncmp(args[0], "del-map", 7) == 0) {
8850 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008851 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008852 /*
8853 * '+ 8' for 'del-map('
8854 * '- 9' for 'del-map(' + trailing ')'
8855 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008856 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008857
8858 cur_arg = 1;
8859
8860 if (!*args[cur_arg] ||
8861 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8862 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8863 file, linenum, args[0]);
8864 goto out_err;
8865 }
8866
8867 LIST_INIT(&rule->arg.map.key);
8868 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008869 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008870 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008871 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8872 Alert("parsing [%s:%d]: 'http-response %s' %s.\n",
8873 file, linenum, args[0], error);
8874 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008875 goto out_err;
8876 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008877 free(proxy->conf.lfs_file);
8878 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8879 proxy->conf.lfs_line = proxy->conf.args.line;
8880 cur_arg += 1;
8881 } else if (strncmp(args[0], "set-map", 7) == 0) {
8882 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008883 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008884 /*
8885 * '+ 8' for 'set-map('
8886 * '- 9' for 'set-map(' + trailing ')'
8887 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008888 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008889
8890 cur_arg = 1;
8891
8892 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8893 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8894 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8895 file, linenum, args[0]);
8896 goto out_err;
8897 }
8898
8899 LIST_INIT(&rule->arg.map.key);
8900 LIST_INIT(&rule->arg.map.value);
8901
8902 proxy->conf.args.ctx = ARGC_HRS;
8903
8904 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008905 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008906 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008907 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8908 Alert("parsing [%s:%d]: 'http-response %s' name: %s.\n",
8909 file, linenum, args[0], error);
8910 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008911 goto out_err;
8912 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008913
8914 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008915 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008916 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 +01008917 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8918 Alert("parsing [%s:%d]: 'http-response %s' value: %s.\n",
8919 file, linenum, args[0], error);
8920 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008921 goto out_err;
8922 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008923
8924 free(proxy->conf.lfs_file);
8925 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8926 proxy->conf.lfs_line = proxy->conf.args.line;
8927
8928 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02008929 } else if (strcmp(args[0], "redirect") == 0) {
8930 struct redirect_rule *redir;
8931 char *errmsg = NULL;
8932
8933 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
8934 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
8935 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8936 goto out_err;
8937 }
8938
8939 /* this redirect rule might already contain a parsed condition which
8940 * we'll pass to the http-request rule.
8941 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008942 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02008943 rule->arg.redir = redir;
8944 rule->cond = redir->cond;
8945 redir->cond = NULL;
8946 cur_arg = 2;
8947 return rule;
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08008948 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
8949 args[0][9] == '\0' && args[0][8] >= '0' &&
8950 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
8951 struct sample_expr *expr;
8952 unsigned int where;
8953 char *err = NULL;
8954
8955 cur_arg = 1;
8956 proxy->conf.args.ctx = ARGC_TRK;
8957
8958 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
8959 if (!expr) {
8960 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
8961 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
8962 free(err);
8963 goto out_err;
8964 }
8965
8966 where = 0;
8967 if (proxy->cap & PR_CAP_FE)
8968 where |= SMP_VAL_FE_HRS_HDR;
8969 if (proxy->cap & PR_CAP_BE)
8970 where |= SMP_VAL_BE_HRS_HDR;
8971
8972 if (!(expr->fetch->val & where)) {
8973 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :"
8974 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
8975 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
8976 args[cur_arg-1], sample_src_names(expr->fetch->use));
8977 free(expr);
8978 goto out_err;
8979 }
8980
8981 if (strcmp(args[cur_arg], "table") == 0) {
8982 cur_arg++;
8983 if (!args[cur_arg]) {
8984 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n",
8985 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
8986 free(expr);
8987 goto out_err;
8988 }
8989 /* we copy the table name for now, it will be resolved later */
8990 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
8991 cur_arg++;
8992 }
8993 rule->arg.trk_ctr.expr = expr;
8994 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
William Lallemand73025dd2014-04-24 14:38:37 +02008995 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
8996 char *errmsg = NULL;
8997 cur_arg = 1;
8998 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02008999 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009000 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009001 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009002 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9003 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9004 free(errmsg);
9005 goto out_err;
9006 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009007 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009008 action_build_list(&http_res_keywords.list, &trash);
9009 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9010 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009011 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02009012 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009013 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009014 goto out_err;
9015 }
9016
9017 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9018 struct acl_cond *cond;
9019 char *errmsg = NULL;
9020
9021 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9022 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9023 file, linenum, args[0], errmsg);
9024 free(errmsg);
9025 goto out_err;
9026 }
9027 rule->cond = cond;
9028 }
9029 else if (*args[cur_arg]) {
9030 Alert("parsing [%s:%d]: 'http-response %s' expects"
9031 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9032 file, linenum, args[0], args[cur_arg]);
9033 goto out_err;
9034 }
9035
9036 return rule;
9037 out_err:
9038 free(rule);
9039 return NULL;
9040}
9041
Willy Tarreau4baae242012-12-27 12:00:31 +01009042/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009043 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009044 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9045 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009046 */
9047struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009048 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009049{
9050 struct redirect_rule *rule;
9051 int cur_arg;
9052 int type = REDIRECT_TYPE_NONE;
9053 int code = 302;
9054 const char *destination = NULL;
9055 const char *cookie = NULL;
9056 int cookie_set = 0;
9057 unsigned int flags = REDIRECT_FLAG_NONE;
9058 struct acl_cond *cond = NULL;
9059
9060 cur_arg = 0;
9061 while (*(args[cur_arg])) {
9062 if (strcmp(args[cur_arg], "location") == 0) {
9063 if (!*args[cur_arg + 1])
9064 goto missing_arg;
9065
9066 type = REDIRECT_TYPE_LOCATION;
9067 cur_arg++;
9068 destination = args[cur_arg];
9069 }
9070 else if (strcmp(args[cur_arg], "prefix") == 0) {
9071 if (!*args[cur_arg + 1])
9072 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009073 type = REDIRECT_TYPE_PREFIX;
9074 cur_arg++;
9075 destination = args[cur_arg];
9076 }
9077 else if (strcmp(args[cur_arg], "scheme") == 0) {
9078 if (!*args[cur_arg + 1])
9079 goto missing_arg;
9080
9081 type = REDIRECT_TYPE_SCHEME;
9082 cur_arg++;
9083 destination = args[cur_arg];
9084 }
9085 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9086 if (!*args[cur_arg + 1])
9087 goto missing_arg;
9088
9089 cur_arg++;
9090 cookie = args[cur_arg];
9091 cookie_set = 1;
9092 }
9093 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9094 if (!*args[cur_arg + 1])
9095 goto missing_arg;
9096
9097 cur_arg++;
9098 cookie = args[cur_arg];
9099 cookie_set = 0;
9100 }
9101 else if (strcmp(args[cur_arg], "code") == 0) {
9102 if (!*args[cur_arg + 1])
9103 goto missing_arg;
9104
9105 cur_arg++;
9106 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009107 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009108 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009109 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009110 args[cur_arg - 1], args[cur_arg]);
9111 return NULL;
9112 }
9113 }
9114 else if (!strcmp(args[cur_arg],"drop-query")) {
9115 flags |= REDIRECT_FLAG_DROP_QS;
9116 }
9117 else if (!strcmp(args[cur_arg],"append-slash")) {
9118 flags |= REDIRECT_FLAG_APPEND_SLASH;
9119 }
9120 else if (strcmp(args[cur_arg], "if") == 0 ||
9121 strcmp(args[cur_arg], "unless") == 0) {
9122 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9123 if (!cond) {
9124 memprintf(errmsg, "error in condition: %s", *errmsg);
9125 return NULL;
9126 }
9127 break;
9128 }
9129 else {
9130 memprintf(errmsg,
9131 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9132 args[cur_arg]);
9133 return NULL;
9134 }
9135 cur_arg++;
9136 }
9137
9138 if (type == REDIRECT_TYPE_NONE) {
9139 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9140 return NULL;
9141 }
9142
Willy Tarreaube4653b2015-05-28 15:26:58 +02009143 if (dir && type != REDIRECT_TYPE_LOCATION) {
9144 memprintf(errmsg, "response only supports redirect type 'location'");
9145 return NULL;
9146 }
9147
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02009148 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +01009149 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009150 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009151
9152 if (!use_fmt) {
9153 /* old-style static redirect rule */
9154 rule->rdr_str = strdup(destination);
9155 rule->rdr_len = strlen(destination);
9156 }
9157 else {
9158 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009159
9160 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9161 * if prefix == "/", we don't want to add anything, otherwise it
9162 * makes it hard for the user to configure a self-redirection.
9163 */
Godbachd9722032014-12-18 15:44:58 +08009164 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009165 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009166 if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
9167 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 +01009168 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9169 errmsg)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009170 return NULL;
9171 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009172 free(curproxy->conf.lfs_file);
9173 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9174 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009175 }
9176 }
9177
Willy Tarreau4baae242012-12-27 12:00:31 +01009178 if (cookie) {
9179 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9180 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9181 */
9182 rule->cookie_len = strlen(cookie);
9183 if (cookie_set) {
9184 rule->cookie_str = malloc(rule->cookie_len + 10);
9185 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9186 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9187 rule->cookie_len += 9;
9188 } else {
9189 rule->cookie_str = malloc(rule->cookie_len + 21);
9190 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9191 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9192 rule->cookie_len += 20;
9193 }
9194 }
9195 rule->type = type;
9196 rule->code = code;
9197 rule->flags = flags;
9198 LIST_INIT(&rule->list);
9199 return rule;
9200
9201 missing_arg:
9202 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9203 return NULL;
9204}
9205
Willy Tarreau8797c062007-05-07 00:55:35 +02009206/************************************************************************/
9207/* The code below is dedicated to ACL parsing and matching */
9208/************************************************************************/
9209
9210
Willy Tarreau14174bc2012-04-16 14:34:04 +02009211/* This function ensures that the prerequisites for an L7 fetch are ready,
9212 * which means that a request or response is ready. If some data is missing,
9213 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009214 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9215 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009216 *
9217 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009218 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9219 * decide whether or not an HTTP message is present ;
9220 * 0 if the requested data cannot be fetched or if it is certain that
9221 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009222 * 1 if an HTTP message is ready
9223 */
James Rosewell91a41cb2015-09-18 17:11:16 +01009224int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009225 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009226{
Willy Tarreau192252e2015-04-04 01:47:55 +02009227 struct http_txn *txn;
9228 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009229
Willy Tarreaube508f12016-03-10 11:47:01 +01009230 /* Note: it is possible that <s> is NULL when called before stream
9231 * initialization (eg: tcp-request connection), so this function is the
9232 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009233 */
Willy Tarreau192252e2015-04-04 01:47:55 +02009234 if (!s)
9235 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +01009236
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +02009237 if (!s->txn) {
9238 if (unlikely(!http_alloc_txn(s)))
9239 return 0; /* not enough memory */
9240 http_init_txn(s);
9241 }
Willy Tarreau192252e2015-04-04 01:47:55 +02009242 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +02009243 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009244
9245 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009246 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009247
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009248 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009249 /* If the buffer does not leave enough free space at the end,
9250 * we must first realign it.
9251 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009252 if (s->req.buf->p > s->req.buf->data &&
9253 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
9254 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +01009255
Willy Tarreau14174bc2012-04-16 14:34:04 +02009256 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009257 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009258 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009259
9260 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009261 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009262 http_msg_analyzer(msg, &txn->hdr_idx);
9263
9264 /* Still no valid request ? */
9265 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009266 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009267 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009268 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009269 }
9270 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009271 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009272 return 0;
9273 }
9274
9275 /* OK we just got a valid HTTP request. We have some minor
9276 * preparation to perform so that further checks can rely
9277 * on HTTP tests.
9278 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009279
9280 /* If the request was parsed but was too large, we must absolutely
9281 * return an error so that it is not processed. At the moment this
9282 * cannot happen, but if the parsers are to change in the future,
9283 * we want this check to be maintained.
9284 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009285 if (unlikely(s->req.buf->i + s->req.buf->p >
9286 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01009287 msg->err_state = msg->msg_state;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009288 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009289 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009290 return 1;
9291 }
9292
Willy Tarreau9b28e032012-10-12 23:49:43 +02009293 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009294 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02009295 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009296
Willy Tarreau506d0502013-07-06 13:29:24 +02009297 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9298 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009299 }
9300
Willy Tarreau506d0502013-07-06 13:29:24 +02009301 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009302 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009303 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009304
9305 /* otherwise everything's ready for the request */
9306 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009307 else {
9308 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009309 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9310 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009311 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009312 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009313 }
9314
9315 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009316 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009317 return 1;
9318}
Willy Tarreau8797c062007-05-07 00:55:35 +02009319
Willy Tarreau8797c062007-05-07 00:55:35 +02009320/* 1. Check on METHOD
9321 * We use the pre-parsed method if it is known, and store its number as an
9322 * integer. If it is unknown, we use the pointer and the length.
9323 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009324static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009325{
9326 int len, meth;
9327
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009328 len = strlen(text);
9329 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009330
9331 pattern->val.i = meth;
9332 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +02009333 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009334 pattern->len = len;
9335 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009336 else {
9337 pattern->ptr.str = NULL;
9338 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009339 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009340 return 1;
9341}
9342
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009343/* This function fetches the method of current HTTP request and stores
9344 * it in the global pattern struct as a chunk. There are two possibilities :
9345 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9346 * in <len> and <ptr> is NULL ;
9347 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9348 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009349 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009350 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009351static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009352smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009353{
9354 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +01009355 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009356
Willy Tarreau24e32d82012-04-23 23:55:44 +02009357 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009358
Willy Tarreaube508f12016-03-10 11:47:01 +01009359 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009360 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009361 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009362 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009363 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009364 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9365 /* ensure the indexes are not affected */
9366 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009367 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009368 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
9369 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009370 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009371 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009372 return 1;
9373}
9374
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009375/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009376static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009377{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009378 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009379 struct pattern_list *lst;
9380 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009381
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009382 list_for_each_entry(lst, &expr->patterns, list) {
9383 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009384
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009385 /* well-known method */
9386 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009387 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009388 return pattern;
9389 else
9390 continue;
9391 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009392
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009393 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009394 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009395 continue;
9396
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009397 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009398 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
9399 (!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 +01009400 return pattern;
9401 }
9402 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009403}
9404
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009405static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009406smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009407{
Willy Tarreaube508f12016-03-10 11:47:01 +01009408 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009409 char *ptr;
9410 int len;
9411
Willy Tarreauc0239e02012-04-16 14:42:55 +02009412 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009413
Willy Tarreaube508f12016-03-10 11:47:01 +01009414 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009415 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009416 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009417
9418 while ((len-- > 0) && (*ptr++ != '/'));
9419 if (len <= 0)
9420 return 0;
9421
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009422 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009423 smp->data.u.str.str = ptr;
9424 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009425
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009426 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009427 return 1;
9428}
9429
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009430static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009431smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009432{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009433 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009434 char *ptr;
9435 int len;
9436
Willy Tarreauc0239e02012-04-16 14:42:55 +02009437 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009438
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009439 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009440 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9441 return 0;
9442
Willy Tarreau8797c062007-05-07 00:55:35 +02009443 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009444 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009445
9446 while ((len-- > 0) && (*ptr++ != '/'));
9447 if (len <= 0)
9448 return 0;
9449
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009450 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009451 smp->data.u.str.str = ptr;
9452 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009453
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009454 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009455 return 1;
9456}
9457
9458/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009459static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009460smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009461{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009462 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009463 char *ptr;
9464 int len;
9465
Willy Tarreauc0239e02012-04-16 14:42:55 +02009466 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009467
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009468 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009469 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9470 return 0;
9471
Willy Tarreau8797c062007-05-07 00:55:35 +02009472 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009473 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009474
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009475 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009476 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009477 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009478 return 1;
9479}
9480
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009481static int
9482smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
9483{
9484 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
9485 return 0;
9486
9487 if (!smp->strm->unique_id) {
9488 if ((smp->strm->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
9489 return 0;
9490 smp->strm->unique_id[0] = '\0';
9491 }
9492 smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id,
9493 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
9494
9495 smp->data.type = SMP_T_STR;
9496 smp->data.u.str.str = smp->strm->unique_id;
9497 smp->flags = SMP_F_CONST;
9498 return 1;
9499}
9500
Thierry FOURNIERd7d88812017-04-19 15:15:14 +02009501/* Returns a string block containing all headers including the
9502 * empty line wich separes headers from the body. This is useful
9503 * form some headers analysis.
9504 */
9505static int
9506smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
9507{
9508 struct http_msg *msg;
9509 struct hdr_idx *idx;
9510 struct http_txn *txn;
9511
9512 CHECK_HTTP_MESSAGE_FIRST();
9513
9514 txn = smp->strm->txn;
9515 idx = &txn->hdr_idx;
9516 msg = &txn->req;
9517
9518 smp->data.type = SMP_T_STR;
9519 smp->data.u.str.str = msg->chn->buf->p + hdr_idx_first_pos(idx);
9520 smp->data.u.str.len = msg->eoh - hdr_idx_first_pos(idx) + 1 +
9521 (msg->chn->buf->p[msg->eoh] == '\r');
9522
9523 return 1;
9524}
9525
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009526/* Returns the header request in a length/value encoded format.
9527 * This is useful for exchanges with the SPOE.
9528 *
9529 * A "length value" is a multibyte code encoding numbers. It uses the
9530 * SPOE format. The encoding is the following:
9531 *
9532 * Each couple "header name" / "header value" is composed
9533 * like this:
9534 * "length value" "header name bytes"
9535 * "length value" "header value bytes"
9536 * When the last header is reached, the header name and the header
9537 * value are empty. Their length are 0
9538 */
9539static int
9540smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
9541{
9542 struct http_msg *msg;
9543 struct chunk *temp;
9544 struct hdr_idx *idx;
9545 const char *cur_ptr, *cur_next, *p;
9546 int old_idx, cur_idx;
9547 struct hdr_idx_elem *cur_hdr;
9548 const char *hn, *hv;
9549 int hnl, hvl;
9550 int ret;
9551 struct http_txn *txn;
9552 char *buf;
9553 char *end;
9554
9555 CHECK_HTTP_MESSAGE_FIRST();
9556
9557 temp = get_trash_chunk();
9558 buf = temp->str;
9559 end = temp->str + temp->size;
9560
9561 txn = smp->strm->txn;
9562 idx = &txn->hdr_idx;
9563 msg = &txn->req;
9564
9565 /* Build array of headers. */
9566 old_idx = 0;
9567 cur_next = msg->chn->buf->p + hdr_idx_first_pos(idx);
9568 while (1) {
9569 cur_idx = idx->v[old_idx].next;
9570 if (!cur_idx)
9571 break;
9572 old_idx = cur_idx;
9573
9574 cur_hdr = &idx->v[cur_idx];
9575 cur_ptr = cur_next;
9576 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
9577
9578 /* Now we have one full header at cur_ptr of len cur_hdr->len,
9579 * and the next header starts at cur_next. We'll check
9580 * this header in the list as well as against the default
9581 * rule.
9582 */
9583
9584 /* look for ': *'. */
9585 hn = cur_ptr;
9586 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
9587 if (p >= cur_ptr+cur_hdr->len)
9588 continue;
9589 hnl = p - hn;
9590 p++;
9591 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
9592 p++;
9593 if (p >= cur_ptr + cur_hdr->len)
9594 continue;
9595 hv = p;
9596 hvl = cur_ptr + cur_hdr->len-p;
9597
9598 /* encode the header name. */
9599 ret = encode_varint(hnl, &buf, end);
9600 if (ret == -1)
9601 return 0;
9602 if (buf + hnl > end)
9603 return 0;
9604 memcpy(buf, hn, hnl);
9605 buf += hnl;
9606
9607 /* encode and copy the value. */
9608 ret = encode_varint(hvl, &buf, end);
9609 if (ret == -1)
9610 return 0;
9611 if (buf + hvl > end)
9612 return 0;
9613 memcpy(buf, hv, hvl);
9614 buf += hvl;
9615 }
9616
9617 /* encode the end of the header list with empty
9618 * header name and header value.
9619 */
9620 ret = encode_varint(0, &buf, end);
9621 if (ret == -1)
9622 return 0;
9623 ret = encode_varint(0, &buf, end);
9624 if (ret == -1)
9625 return 0;
9626
9627 /* Initialise sample data which will be filled. */
9628 smp->data.type = SMP_T_BIN;
9629 smp->data.u.str.str = temp->str;
9630 smp->data.u.str.len = buf - temp->str;
9631 smp->data.u.str.size = temp->size;
9632
9633 return 1;
9634}
9635
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009636/* returns the longest available part of the body. This requires that the body
9637 * has been waited for using http-buffer-request.
9638 */
9639static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009640smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009641{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009642 struct http_msg *msg;
9643 unsigned long len;
9644 unsigned long block1;
9645 char *body;
9646 struct chunk *temp;
9647
9648 CHECK_HTTP_MESSAGE_FIRST();
9649
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009650 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009651 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009652 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009653 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009654
9655 len = http_body_bytes(msg);
9656 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
9657
9658 block1 = len;
9659 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
9660 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
9661
9662 if (block1 == len) {
9663 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009664 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009665 smp->data.u.str.str = body;
9666 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009667 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
9668 }
9669 else {
9670 /* buffer is wrapped, we need to defragment it */
9671 temp = get_trash_chunk();
9672 memcpy(temp->str, body, block1);
9673 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009674 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009675 smp->data.u.str.str = temp->str;
9676 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009677 smp->flags = SMP_F_VOL_TEST;
9678 }
9679 return 1;
9680}
9681
9682
9683/* returns the available length of the body. This requires that the body
9684 * has been waited for using http-buffer-request.
9685 */
9686static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009687smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009688{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009689 struct http_msg *msg;
9690
9691 CHECK_HTTP_MESSAGE_FIRST();
9692
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009693 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009694 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009695 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009696 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009697
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009698 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009699 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009700
9701 smp->flags = SMP_F_VOL_TEST;
9702 return 1;
9703}
9704
9705
9706/* returns the advertised length of the body, or the advertised size of the
9707 * chunks available in the buffer. This requires that the body has been waited
9708 * for using http-buffer-request.
9709 */
9710static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009711smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009712{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009713 struct http_msg *msg;
9714
9715 CHECK_HTTP_MESSAGE_FIRST();
9716
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009717 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009718 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009719 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009720 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009721
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009722 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009723 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009724
9725 smp->flags = SMP_F_VOL_TEST;
9726 return 1;
9727}
9728
9729
Willy Tarreau8797c062007-05-07 00:55:35 +02009730/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009731static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009732smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009733{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009734 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009735
Willy Tarreauc0239e02012-04-16 14:42:55 +02009736 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009737
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009738 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009739 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009740 smp->data.u.str.len = txn->req.sl.rq.u_l;
9741 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009742 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009743 return 1;
9744}
9745
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009746static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009747smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009748{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009749 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009750 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009751
Willy Tarreauc0239e02012-04-16 14:42:55 +02009752 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009753
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009754 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009755 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 +02009756 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009757 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009758
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009759 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009760 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009761 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009762 return 1;
9763}
9764
9765static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009766smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009767{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009768 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009769 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009770
Willy Tarreauc0239e02012-04-16 14:42:55 +02009771 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009772
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009773 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009774 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 +02009775 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
9776 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009777
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009778 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009779 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009780 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009781 return 1;
9782}
9783
Willy Tarreau185b5c42012-04-26 15:11:51 +02009784/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9785 * Accepts an optional argument of type string containing the header field name,
9786 * and an optional argument of type signed or unsigned integer to request an
9787 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009788 * headers are considered from the first one. It does not stop on commas and
9789 * returns full lines instead (useful for User-Agent or Date for example).
9790 */
9791static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009792smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009793{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009794 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009795 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +02009796 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009797 int occ = 0;
9798 const char *name_str = NULL;
9799 int name_len = 0;
9800
9801 if (!ctx) {
9802 /* first call */
9803 ctx = &static_hdr_ctx;
9804 ctx->idx = 0;
9805 smp->ctx.a[0] = ctx;
9806 }
9807
9808 if (args) {
9809 if (args[0].type != ARGT_STR)
9810 return 0;
9811 name_str = args[0].data.str.str;
9812 name_len = args[0].data.str.len;
9813
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02009814 if (args[1].type == ARGT_SINT)
9815 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009816 }
9817
9818 CHECK_HTTP_MESSAGE_FIRST();
9819
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009820 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009821 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 +02009822
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009823 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9824 /* search for header from the beginning */
9825 ctx->idx = 0;
9826
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009827 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009828 /* no explicit occurrence and single fetch => last header by default */
9829 occ = -1;
9830
9831 if (!occ)
9832 /* prepare to report multiple occurrences for ACL fetches */
9833 smp->flags |= SMP_F_NOT_LAST;
9834
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009835 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009836 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009837 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 +02009838 return 1;
9839
9840 smp->flags &= ~SMP_F_NOT_LAST;
9841 return 0;
9842}
9843
9844/* 6. Check on HTTP header count. The number of occurrences is returned.
9845 * Accepts exactly 1 argument of type string. It does not stop on commas and
9846 * returns full lines instead (useful for User-Agent or Date for example).
9847 */
9848static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009849smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009850{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009851 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009852 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +02009853 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009854 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +02009855 const char *name = NULL;
9856 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009857
Willy Tarreau601a4d12015-04-01 19:16:09 +02009858 if (args && args->type == ARGT_STR) {
9859 name = args->data.str.str;
9860 len = args->data.str.len;
9861 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009862
9863 CHECK_HTTP_MESSAGE_FIRST();
9864
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009865 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009866 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 +02009867
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009868 ctx.idx = 0;
9869 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +02009870 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009871 cnt++;
9872
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009873 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009874 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009875 smp->flags = SMP_F_VOL_HDR;
9876 return 1;
9877}
9878
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009879static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009880smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009881{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009882 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009883 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +02009884 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009885 struct chunk *temp;
9886 char del = ',';
9887
9888 if (args && args->type == ARGT_STR)
9889 del = *args[0].data.str.str;
9890
9891 CHECK_HTTP_MESSAGE_FIRST();
9892
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009893 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009894 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 +02009895
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009896 temp = get_trash_chunk();
9897
9898 ctx.idx = 0;
9899 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
9900 if (temp->len)
9901 temp->str[temp->len++] = del;
9902 memcpy(temp->str + temp->len, ctx.line, ctx.del);
9903 temp->len += ctx.del;
9904 }
9905
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009906 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009907 smp->data.u.str.str = temp->str;
9908 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009909 smp->flags = SMP_F_VOL_HDR;
9910 return 1;
9911}
9912
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009913/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9914 * Accepts an optional argument of type string containing the header field name,
9915 * and an optional argument of type signed or unsigned integer to request an
9916 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009917 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009918 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009919static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009920smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009921{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009922 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009923 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +02009924 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009925 int occ = 0;
9926 const char *name_str = NULL;
9927 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009928
Willy Tarreaua890d072013-04-02 12:01:06 +02009929 if (!ctx) {
9930 /* first call */
9931 ctx = &static_hdr_ctx;
9932 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02009933 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009934 }
9935
Willy Tarreau185b5c42012-04-26 15:11:51 +02009936 if (args) {
9937 if (args[0].type != ARGT_STR)
9938 return 0;
9939 name_str = args[0].data.str.str;
9940 name_len = args[0].data.str.len;
9941
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02009942 if (args[1].type == ARGT_SINT)
9943 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009944 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009945
Willy Tarreaue333ec92012-04-16 16:26:40 +02009946 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02009947
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009948 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009949 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 +02009950
Willy Tarreau185b5c42012-04-26 15:11:51 +02009951 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009952 /* search for header from the beginning */
9953 ctx->idx = 0;
9954
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009955 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +02009956 /* no explicit occurrence and single fetch => last header by default */
9957 occ = -1;
9958
9959 if (!occ)
9960 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02009961 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01009962
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009963 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009964 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009965 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 +02009966 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009967
Willy Tarreau37406352012-04-23 16:16:37 +02009968 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009969 return 0;
9970}
9971
Willy Tarreauc11416f2007-06-17 16:58:38 +02009972/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02009973 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009974 */
9975static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009976smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009977{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009978 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009979 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +02009980 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009981 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +02009982 const char *name = NULL;
9983 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02009984
Willy Tarreau601a4d12015-04-01 19:16:09 +02009985 if (args && args->type == ARGT_STR) {
9986 name = args->data.str.str;
9987 len = args->data.str.len;
9988 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009989
Willy Tarreaue333ec92012-04-16 16:26:40 +02009990 CHECK_HTTP_MESSAGE_FIRST();
9991
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009992 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009993 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 +02009994
Willy Tarreau33a7e692007-06-10 19:45:56 +02009995 ctx.idx = 0;
9996 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +02009997 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009998 cnt++;
9999
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010000 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010001 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010002 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010003 return 1;
10004}
10005
Willy Tarreau185b5c42012-04-26 15:11:51 +020010006/* Fetch an HTTP header's integer value. The integer value is returned. It
10007 * takes a mandatory argument of type string and an optional one of type int
10008 * to designate a specific occurrence. It returns an unsigned integer, which
10009 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010010 */
10011static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010012smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010013{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010014 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010015
Willy Tarreauf853c462012-04-23 18:53:56 +020010016 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010017 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010018 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010019 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010020
Willy Tarreaud53e2422012-04-16 17:21:11 +020010021 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010022}
10023
Cyril Bonté69fa9922012-10-25 00:01:06 +020010024/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10025 * and an optional one of type int to designate a specific occurrence.
10026 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010027 */
10028static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010029smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010030{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010031 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010032
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010033 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010034 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010035 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010036 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010037 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010038 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010039 if (smp->data.u.str.len < temp->size - 1) {
10040 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10041 temp->str[smp->data.u.str.len] = '\0';
10042 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010043 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010044 break;
10045 }
10046 }
10047 }
10048
Willy Tarreaud53e2422012-04-16 17:21:11 +020010049 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010050 if (!(smp->flags & SMP_F_NOT_LAST))
10051 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010052 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010053 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010054}
10055
Willy Tarreau737b0c12007-06-10 21:28:46 +020010056/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10057 * the first '/' after the possible hostname, and ends before the possible '?'.
10058 */
10059static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010060smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010061{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010062 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010063 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010064
Willy Tarreauc0239e02012-04-16 14:42:55 +020010065 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010066
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010067 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010068 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010069 ptr = http_get_path(txn);
10070 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010071 return 0;
10072
10073 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010074 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010075 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010076
10077 while (ptr < end && *ptr != '?')
10078 ptr++;
10079
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010080 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010081 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010082 return 1;
10083}
10084
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010085/* This produces a concatenation of the first occurrence of the Host header
10086 * followed by the path component if it begins with a slash ('/'). This means
10087 * that '*' will not be added, resulting in exactly the first Host entry.
10088 * If no Host header is found, then the path is returned as-is. The returned
10089 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010090 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010091 */
10092static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010093smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010094{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010095 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010096 char *ptr, *end, *beg;
10097 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010098 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010099
10100 CHECK_HTTP_MESSAGE_FIRST();
10101
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010102 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010103 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010104 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010105 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010106
10107 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010108 temp = get_trash_chunk();
10109 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010110 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010111 smp->data.u.str.str = temp->str;
10112 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010113
10114 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010115 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010116 beg = http_get_path(txn);
10117 if (!beg)
10118 beg = end;
10119
10120 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10121
10122 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010123 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10124 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010125 }
10126
10127 smp->flags = SMP_F_VOL_1ST;
10128 return 1;
10129}
10130
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010131/* This produces a 32-bit hash of the concatenation of the first occurrence of
10132 * the Host header followed by the path component if it begins with a slash ('/').
10133 * This means that '*' will not be added, resulting in exactly the first Host
10134 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010135 * is hashed using the path hash followed by a full avalanche hash and provides a
10136 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010137 * high-traffic sites without having to store whole paths.
10138 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010139int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010140smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010141{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010142 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010143 struct hdr_ctx ctx;
10144 unsigned int hash = 0;
10145 char *ptr, *beg, *end;
10146 int len;
10147
10148 CHECK_HTTP_MESSAGE_FIRST();
10149
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010150 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010151 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010152 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010153 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10154 ptr = ctx.line + ctx.val;
10155 len = ctx.vlen;
10156 while (len--)
10157 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10158 }
10159
10160 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010161 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010162 beg = http_get_path(txn);
10163 if (!beg)
10164 beg = end;
10165
10166 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10167
10168 if (beg < ptr && *beg == '/') {
10169 while (beg < ptr)
10170 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10171 }
10172 hash = full_hash(hash);
10173
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010174 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010175 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010176 smp->flags = SMP_F_VOL_1ST;
10177 return 1;
10178}
10179
Willy Tarreau4a550602012-12-09 14:53:32 +010010180/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010181 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10182 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10183 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010184 * that in environments where IPv6 is insignificant, truncating the output to
10185 * 8 bytes would still work.
10186 */
10187static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010188smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010189{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010190 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010191 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010192
10193 if (!cli_conn)
10194 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010195
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010196 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010197 return 0;
10198
Willy Tarreau47ca5452012-12-23 20:22:19 +010010199 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010200 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010201 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010202
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010203 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010204 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010205 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010206 temp->len += 4;
10207 break;
10208 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010209 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010210 temp->len += 16;
10211 break;
10212 default:
10213 return 0;
10214 }
10215
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010216 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010217 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010218 return 1;
10219}
10220
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010221/* Extracts the query string, which comes after the question mark '?'. If no
10222 * question mark is found, nothing is returned. Otherwise it returns a sample
10223 * of type string carrying the whole query string.
10224 */
10225static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010226smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010227{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010228 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010229 char *ptr, *end;
10230
10231 CHECK_HTTP_MESSAGE_FIRST();
10232
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010233 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010234 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10235 end = ptr + txn->req.sl.rq.u_l;
10236
10237 /* look up the '?' */
10238 do {
10239 if (ptr == end)
10240 return 0;
10241 } while (*ptr++ != '?');
10242
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010243 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010244 smp->data.u.str.str = ptr;
10245 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010246 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10247 return 1;
10248}
10249
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010250static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010251smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010252{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010253 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10254 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10255 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010256
Willy Tarreau24e32d82012-04-23 23:55:44 +020010257 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010258
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010259 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010260 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010261 return 1;
10262}
10263
Willy Tarreau7f18e522010-10-22 20:04:13 +020010264/* return a valid test if the current request is the first one on the connection */
10265static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010266smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010267{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010268 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010269 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010270 return 1;
10271}
10272
Willy Tarreau34db1082012-04-19 17:16:54 +020010273/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010274static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010275smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010276{
10277
Willy Tarreau24e32d82012-04-23 23:55:44 +020010278 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010279 return 0;
10280
Willy Tarreauc0239e02012-04-16 14:42:55 +020010281 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010282
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010283 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010284 return 0;
10285
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010286 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010287 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010288 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010289 return 1;
10290}
Willy Tarreau8797c062007-05-07 00:55:35 +020010291
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010292/* Accepts exactly 1 argument of type userlist */
10293static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010294smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010295{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010296 if (!args || args->type != ARGT_USR)
10297 return 0;
10298
10299 CHECK_HTTP_MESSAGE_FIRST();
10300
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010301 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010302 return 0;
10303
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010304 /* if the user does not belong to the userlist or has a wrong password,
10305 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010306 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010307 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010308 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10309 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010310 return 0;
10311
10312 /* pat_match_auth() will need the user list */
10313 smp->ctx.a[0] = args->data.usr;
10314
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010315 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010316 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010317 smp->data.u.str.str = smp->strm->txn->auth.user;
10318 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010319
10320 return 1;
10321}
10322
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010323/* Try to find the next occurrence of a cookie name in a cookie header value.
10324 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10325 * the cookie value is returned into *value and *value_l, and the function
10326 * returns a pointer to the next pointer to search from if the value was found.
10327 * Otherwise if the cookie was not found, NULL is returned and neither value
10328 * nor value_l are touched. The input <hdr> string should first point to the
10329 * header's value, and the <hdr_end> pointer must point to the first character
10330 * not part of the value. <list> must be non-zero if value may represent a list
10331 * of values (cookie headers). This makes it faster to abort parsing when no
10332 * list is expected.
10333 */
David Carlier4686f792015-09-25 14:10:50 +010010334char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010335extract_cookie_value(char *hdr, const char *hdr_end,
10336 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010337 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010338{
10339 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10340 char *next;
10341
10342 /* we search at least a cookie name followed by an equal, and more
10343 * generally something like this :
10344 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10345 */
10346 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10347 /* Iterate through all cookies on this line */
10348
Willy Tarreau2235b262016-11-05 15:50:20 +010010349 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010350 att_beg++;
10351
10352 /* find att_end : this is the first character after the last non
10353 * space before the equal. It may be equal to hdr_end.
10354 */
10355 equal = att_end = att_beg;
10356
10357 while (equal < hdr_end) {
10358 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10359 break;
Willy Tarreau2235b262016-11-05 15:50:20 +010010360 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010361 continue;
10362 att_end = equal;
10363 }
10364
10365 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10366 * is between <att_beg> and <equal>, both may be identical.
10367 */
10368
10369 /* look for end of cookie if there is an equal sign */
10370 if (equal < hdr_end && *equal == '=') {
10371 /* look for the beginning of the value */
10372 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +010010373 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010374 val_beg++;
10375
10376 /* find the end of the value, respecting quotes */
10377 next = find_cookie_value_end(val_beg, hdr_end);
10378
10379 /* make val_end point to the first white space or delimitor after the value */
10380 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +010010381 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010382 val_end--;
10383 } else {
10384 val_beg = val_end = next = equal;
10385 }
10386
10387 /* We have nothing to do with attributes beginning with '$'. However,
10388 * they will automatically be removed if a header before them is removed,
10389 * since they're supposed to be linked together.
10390 */
10391 if (*att_beg == '$')
10392 continue;
10393
10394 /* Ignore cookies with no equal sign */
10395 if (equal == next)
10396 continue;
10397
10398 /* Now we have the cookie name between att_beg and att_end, and
10399 * its value between val_beg and val_end.
10400 */
10401
10402 if (att_end - att_beg == cookie_name_l &&
10403 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10404 /* let's return this value and indicate where to go on from */
10405 *value = val_beg;
10406 *value_l = val_end - val_beg;
10407 return next + 1;
10408 }
10409
10410 /* Set-Cookie headers only have the name in the first attr=value part */
10411 if (!list)
10412 break;
10413 }
10414
10415 return NULL;
10416}
10417
William Lallemanda43ba4e2014-01-28 18:14:25 +010010418/* Fetch a captured HTTP request header. The index is the position of
10419 * the "capture" option in the configuration file
10420 */
10421static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010422smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010423{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010424 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010425 int idx;
10426
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010427 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010428 return 0;
10429
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010430 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010431
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010432 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 +010010433 return 0;
10434
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010435 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010436 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010437 smp->data.u.str.str = smp->strm->req_cap[idx];
10438 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010439
10440 return 1;
10441}
10442
10443/* Fetch a captured HTTP response header. The index is the position of
10444 * the "capture" option in the configuration file
10445 */
10446static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010447smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010448{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010449 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010450 int idx;
10451
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010452 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010453 return 0;
10454
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010455 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010456
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010457 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 +010010458 return 0;
10459
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010460 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010461 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010462 smp->data.u.str.str = smp->strm->res_cap[idx];
10463 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010464
10465 return 1;
10466}
10467
William Lallemand65ad6e12014-01-31 15:08:02 +010010468/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10469static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010470smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010471{
10472 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010473 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010010474 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010475
Willy Tarreau15e91e12015-04-04 00:52:09 +020010476 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010477 return 0;
10478
William Lallemand96a77852014-02-05 00:30:02 +010010479 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010480
William Lallemand96a77852014-02-05 00:30:02 +010010481 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10482 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010483
William Lallemand96a77852014-02-05 00:30:02 +010010484 temp = get_trash_chunk();
10485 temp->str = txn->uri;
10486 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010487 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010488 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010489 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010490
10491 return 1;
10492
10493}
10494
10495/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10496static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010497smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010498{
10499 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010500 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010010501 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010502
Willy Tarreau15e91e12015-04-04 00:52:09 +020010503 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010504 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010505
William Lallemand65ad6e12014-01-31 15:08:02 +010010506 ptr = txn->uri;
10507
10508 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10509 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010510
William Lallemand65ad6e12014-01-31 15:08:02 +010010511 if (!*ptr)
10512 return 0;
10513
10514 ptr++; /* skip the space */
10515
10516 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010517 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010518 if (!ptr)
10519 return 0;
10520 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10521 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010522
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010523 smp->data.u.str = *temp;
10524 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010525 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010526 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010527
10528 return 1;
10529}
10530
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010531/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10532 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10533 */
10534static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010535smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010536{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010537 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010538
Willy Tarreau15e91e12015-04-04 00:52:09 +020010539 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010540 return 0;
10541
10542 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010543 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010544 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010545 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010546
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010547 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010548 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010549 smp->flags = SMP_F_CONST;
10550 return 1;
10551
10552}
10553
10554/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10555 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10556 */
10557static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010558smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010559{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010560 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010561
Willy Tarreau15e91e12015-04-04 00:52:09 +020010562 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010563 return 0;
10564
10565 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010566 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010567 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010568 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010569
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010570 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010571 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010572 smp->flags = SMP_F_CONST;
10573 return 1;
10574
10575}
10576
William Lallemand65ad6e12014-01-31 15:08:02 +010010577
Willy Tarreaue333ec92012-04-16 16:26:40 +020010578/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010579 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010580 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010581 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010582 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010583 * Accepts exactly 1 argument of type string. If the input options indicate
10584 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010585 * The returned sample is of type CSTR. Can be used to parse cookies in other
10586 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010587 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010588int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010589{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010590 struct http_txn *txn;
10591 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010592 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010593 const struct http_msg *msg;
10594 const char *hdr_name;
10595 int hdr_name_len;
10596 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010597 int occ = 0;
10598 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010599
Willy Tarreau24e32d82012-04-23 23:55:44 +020010600 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010601 return 0;
10602
Willy Tarreaua890d072013-04-02 12:01:06 +020010603 if (!ctx) {
10604 /* first call */
10605 ctx = &static_hdr_ctx;
10606 ctx->idx = 0;
10607 smp->ctx.a[2] = ctx;
10608 }
10609
Willy Tarreaue333ec92012-04-16 16:26:40 +020010610 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010611
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010612 txn = smp->strm->txn;
10613 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010614
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010615 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010616 msg = &txn->req;
10617 hdr_name = "Cookie";
10618 hdr_name_len = 6;
10619 } else {
10620 msg = &txn->rsp;
10621 hdr_name = "Set-Cookie";
10622 hdr_name_len = 10;
10623 }
10624
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010625 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020010626 /* no explicit occurrence and single fetch => last cookie by default */
10627 occ = -1;
10628
10629 /* OK so basically here, either we want only one value and it's the
10630 * last one, or we want to iterate over all of them and we fetch the
10631 * next one.
10632 */
10633
Willy Tarreau9b28e032012-10-12 23:49:43 +020010634 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010635 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010636 /* search for the header from the beginning, we must first initialize
10637 * the search parameters.
10638 */
Willy Tarreau37406352012-04-23 16:16:37 +020010639 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010640 ctx->idx = 0;
10641 }
10642
Willy Tarreau28376d62012-04-26 21:26:10 +020010643 smp->flags |= SMP_F_VOL_HDR;
10644
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010645 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010646 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10647 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010648 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10649 goto out;
10650
Willy Tarreau24e32d82012-04-23 23:55:44 +020010651 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010652 continue;
10653
Willy Tarreau37406352012-04-23 16:16:37 +020010654 smp->ctx.a[0] = ctx->line + ctx->val;
10655 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010656 }
10657
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010658 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010659 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010660 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010661 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010662 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010663 &smp->data.u.str.str,
10664 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010665 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010666 found = 1;
10667 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010668 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020010669 smp->flags |= SMP_F_NOT_LAST;
10670 return 1;
10671 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010672 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010673 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010674 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010675 /* all cookie headers and values were scanned. If we're looking for the
10676 * last occurrence, we may return it now.
10677 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010678 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010679 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010680 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010681}
10682
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010683/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010684 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010685 * multiple cookies may be parsed on the same line. The returned sample is of
10686 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010687 */
10688static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010689smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010690{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010691 struct http_txn *txn;
10692 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010693 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010694 const struct http_msg *msg;
10695 const char *hdr_name;
10696 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010697 int cnt;
10698 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010699 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010700
Willy Tarreau24e32d82012-04-23 23:55:44 +020010701 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010702 return 0;
10703
Willy Tarreaue333ec92012-04-16 16:26:40 +020010704 CHECK_HTTP_MESSAGE_FIRST();
10705
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010706 txn = smp->strm->txn;
10707 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010708
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010709 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010710 msg = &txn->req;
10711 hdr_name = "Cookie";
10712 hdr_name_len = 6;
10713 } else {
10714 msg = &txn->rsp;
10715 hdr_name = "Set-Cookie";
10716 hdr_name_len = 10;
10717 }
10718
Willy Tarreau9b28e032012-10-12 23:49:43 +020010719 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010720 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010721 ctx.idx = 0;
10722 cnt = 0;
10723
10724 while (1) {
10725 /* Note: val_beg == NULL every time we need to fetch a new header */
10726 if (!val_beg) {
10727 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10728 break;
10729
Willy Tarreau24e32d82012-04-23 23:55:44 +020010730 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010731 continue;
10732
10733 val_beg = ctx.line + ctx.val;
10734 val_end = val_beg + ctx.vlen;
10735 }
10736
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010737 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010738 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010739 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010740 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010741 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010742 &smp->data.u.str.str,
10743 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010744 cnt++;
10745 }
10746 }
10747
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010748 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010749 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010750 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010751 return 1;
10752}
10753
Willy Tarreau51539362012-05-08 12:46:28 +020010754/* Fetch an cookie's integer value. The integer value is returned. It
10755 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10756 */
10757static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010758smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020010759{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010760 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020010761
10762 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010763 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010764 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020010765 }
10766
10767 return ret;
10768}
10769
Willy Tarreau8797c062007-05-07 00:55:35 +020010770/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010771/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010772/************************************************************************/
10773
David Cournapeau16023ee2010-12-23 20:55:41 +090010774/*
10775 * Given a path string and its length, find the position of beginning of the
10776 * query string. Returns NULL if no query string is found in the path.
10777 *
10778 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
10779 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010780 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090010781 */
bedis4c75cca2012-10-05 08:38:24 +020010782static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010783{
10784 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020010785
bedis4c75cca2012-10-05 08:38:24 +020010786 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090010787 return p ? p + 1 : NULL;
10788}
10789
bedis4c75cca2012-10-05 08:38:24 +020010790static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010791{
bedis4c75cca2012-10-05 08:38:24 +020010792 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090010793}
10794
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010795/* after increasing a pointer value, it can exceed the first buffer
10796 * size. This function transform the value of <ptr> according with
10797 * the expected position. <chunks> is an array of the one or two
10798 * avalaible chunks. The first value is the start of the first chunk,
10799 * the second value if the end+1 of the first chunks. The third value
10800 * is NULL or the start of the second chunk and the fourth value is
10801 * the end+1 of the second chunk. The function returns 1 if does a
10802 * wrap, else returns 0.
10803 */
10804static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
10805{
10806 if (*ptr < chunks[1])
10807 return 0;
10808 if (!chunks[2])
10809 return 0;
10810 *ptr = chunks[2] + ( *ptr - chunks[1] );
10811 return 1;
10812}
10813
David Cournapeau16023ee2010-12-23 20:55:41 +090010814/*
10815 * Given a url parameter, find the starting position of the first occurence,
10816 * or NULL if the parameter is not found.
10817 *
10818 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
10819 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010820 *
Willy Tarreauf6625822015-12-27 14:51:01 +010010821 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010822 * or the second chunk. The caller must be check the position before using the
10823 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090010824 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010825static const char *
10826find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010827 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020010828 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010829{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010830 const char *pos, *last, *equal;
10831 const char **bufs = chunks;
10832 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090010833
David Cournapeau16023ee2010-12-23 20:55:41 +090010834
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010835 pos = bufs[0];
10836 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010010837 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010838 /* Check the equal. */
10839 equal = pos + url_param_name_l;
10840 if (fix_pointer_if_wrap(chunks, &equal)) {
10841 if (equal >= chunks[3])
10842 return NULL;
10843 } else {
10844 if (equal >= chunks[1])
10845 return NULL;
10846 }
10847 if (*equal == '=') {
10848 if (pos + url_param_name_l > last) {
10849 /* process wrap case, we detect a wrap. In this case, the
10850 * comparison is performed in two parts.
10851 */
10852
10853 /* This is the end, we dont have any other chunk. */
10854 if (bufs != chunks || !bufs[2])
10855 return NULL;
10856
10857 /* Compute the length of each part of the comparison. */
10858 l1 = last - pos;
10859 l2 = url_param_name_l - l1;
10860
10861 /* The second buffer is too short to contain the compared string. */
10862 if (bufs[2] + l2 > bufs[3])
10863 return NULL;
10864
10865 if (memcmp(pos, url_param_name, l1) == 0 &&
10866 memcmp(bufs[2], url_param_name+l1, l2) == 0)
10867 return pos;
10868
10869 /* Perform wrapping and jump the string who fail the comparison. */
10870 bufs += 2;
10871 pos = bufs[0] + l2;
10872 last = bufs[1];
10873
10874 } else {
10875 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010010876 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
10877 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010878 pos += url_param_name_l + 1;
10879 if (fix_pointer_if_wrap(chunks, &pos))
10880 last = bufs[2];
10881 }
10882 }
10883
10884 while (1) {
10885 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010010886 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010887 pos++;
10888 if (pos < last)
10889 break;
10890 /* process buffer wrapping. */
10891 if (bufs != chunks || !bufs[2])
10892 return NULL;
10893 bufs += 2;
10894 pos = bufs[0];
10895 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090010896 }
David Cournapeau16023ee2010-12-23 20:55:41 +090010897 pos++;
10898 }
10899 return NULL;
10900}
10901
10902/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010010903 * Given a url parameter name and a query string, find the next value.
10904 * An empty url_param_name matches the first available parameter.
10905 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
10906 * respectively provide a pointer to the value and its end.
10907 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090010908 */
10909static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010910find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010911 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010912 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010913{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010914 const char *arg_start, *qs_end;
10915 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090010916
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010917 arg_start = chunks[0];
10918 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010919 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010920 /* Looks for an argument name. */
10921 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010922 url_param_name, url_param_name_l,
10923 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010924 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010010925 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010926 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010927 }
David Cournapeau16023ee2010-12-23 20:55:41 +090010928 if (!arg_start)
10929 return 0;
10930
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010931 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010932 while (1) {
10933 /* looks for the first argument. */
10934 value_start = memchr(arg_start, '=', qs_end - arg_start);
10935 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010936 /* Check for wrapping. */
10937 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010010938 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010939 chunks[2]) {
10940 arg_start = chunks[2];
10941 qs_end = chunks[3];
10942 continue;
10943 }
10944 return 0;
10945 }
10946 break;
10947 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010948 value_start++;
10949 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010950 else {
10951 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010952 value_start = arg_start + url_param_name_l + 1;
10953
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010954 /* Check for pointer wrapping. */
10955 if (fix_pointer_if_wrap(chunks, &value_start)) {
10956 /* Update the end pointer. */
10957 qs_end = chunks[3];
10958
10959 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010010960 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010961 return 0;
10962 }
10963 }
10964
David Cournapeau16023ee2010-12-23 20:55:41 +090010965 value_end = value_start;
10966
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010967 while (1) {
10968 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
10969 value_end++;
10970 if (value_end < qs_end)
10971 break;
10972 /* process buffer wrapping. */
10973 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010010974 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010975 chunks[2]) {
10976 value_end = chunks[2];
10977 qs_end = chunks[3];
10978 continue;
10979 }
10980 break;
10981 }
David Cournapeau16023ee2010-12-23 20:55:41 +090010982
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010983 *vstart = value_start;
10984 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010010985 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090010986}
10987
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010988/* This scans a URL-encoded query string. It takes an optionally wrapping
10989 * string whose first contigous chunk has its beginning in ctx->a[0] and end
10990 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
10991 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010992 */
David Cournapeau16023ee2010-12-23 20:55:41 +090010993static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010994smp_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 +090010995{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010996 const char *vstart, *vend;
10997 struct chunk *temp;
10998 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020010999
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011000 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011001 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011002 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011003 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011004 return 0;
11005
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011006 /* Create sample. If the value is contiguous, return the pointer as CONST,
11007 * if the value is wrapped, copy-it in a buffer.
11008 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011009 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011010 if (chunks[2] &&
11011 vstart >= chunks[0] && vstart <= chunks[1] &&
11012 vend >= chunks[2] && vend <= chunks[3]) {
11013 /* Wrapped case. */
11014 temp = get_trash_chunk();
11015 memcpy(temp->str, vstart, chunks[1] - vstart);
11016 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011017 smp->data.u.str.str = temp->str;
11018 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011019 } else {
11020 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011021 smp->data.u.str.str = (char *)vstart;
11022 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011023 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11024 }
11025
11026 /* Update context, check wrapping. */
11027 chunks[0] = vend;
11028 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11029 chunks[1] = chunks[3];
11030 chunks[2] = NULL;
11031 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011032
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011033 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011034 smp->flags |= SMP_F_NOT_LAST;
11035
David Cournapeau16023ee2010-12-23 20:55:41 +090011036 return 1;
11037}
11038
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011039/* This function iterates over each parameter of the query string. It uses
11040 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011041 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11042 * An optional parameter name is passed in args[0], otherwise any parameter is
11043 * considered. It supports an optional delimiter argument for the beginning of
11044 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011045 */
11046static int
11047smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11048{
11049 struct http_msg *msg;
11050 char delim = '?';
11051 const char *name;
11052 int name_len;
11053
Dragan Dosen26f77e52015-05-25 10:02:11 +020011054 if (!args ||
11055 (args[0].type && args[0].type != ARGT_STR) ||
11056 (args[1].type && args[1].type != ARGT_STR))
11057 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011058
Dragan Dosen26f77e52015-05-25 10:02:11 +020011059 name = "";
11060 name_len = 0;
11061 if (args->type == ARGT_STR) {
11062 name = args->data.str.str;
11063 name_len = args->data.str.len;
11064 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011065
Dragan Dosen26f77e52015-05-25 10:02:11 +020011066 if (args[1].type)
11067 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011068
Dragan Dosen26f77e52015-05-25 10:02:11 +020011069 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011070 CHECK_HTTP_MESSAGE_FIRST();
11071
11072 msg = &smp->strm->txn->req;
11073
11074 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11075 msg->sl.rq.u_l, delim);
11076 if (!smp->ctx.a[0])
11077 return 0;
11078
11079 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011080
11081 /* Assume that the context is filled with NULL pointer
11082 * before the first call.
11083 * smp->ctx.a[2] = NULL;
11084 * smp->ctx.a[3] = NULL;
11085 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011086 }
11087
11088 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11089}
11090
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011091/* This function iterates over each parameter of the body. This requires
11092 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011093 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11094 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11095 * optional second part if the body wraps at the end of the buffer. An optional
11096 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011097 */
11098static int
11099smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11100{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011101 struct http_msg *msg;
11102 unsigned long len;
11103 unsigned long block1;
11104 char *body;
11105 const char *name;
11106 int name_len;
11107
11108 if (!args || (args[0].type && args[0].type != ARGT_STR))
11109 return 0;
11110
11111 name = "";
11112 name_len = 0;
11113 if (args[0].type == ARGT_STR) {
11114 name = args[0].data.str.str;
11115 name_len = args[0].data.str.len;
11116 }
11117
11118 if (!smp->ctx.a[0]) { // first call, find the query string
11119 CHECK_HTTP_MESSAGE_FIRST();
11120
11121 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010011122 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011123 else
Willy Tarreaube508f12016-03-10 11:47:01 +010011124 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011125
11126 len = http_body_bytes(msg);
11127 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11128
11129 block1 = len;
11130 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11131 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11132
11133 if (block1 == len) {
11134 /* buffer is not wrapped (or empty) */
11135 smp->ctx.a[0] = body;
11136 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011137
11138 /* Assume that the context is filled with NULL pointer
11139 * before the first call.
11140 * smp->ctx.a[2] = NULL;
11141 * smp->ctx.a[3] = NULL;
11142 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011143 }
11144 else {
11145 /* buffer is wrapped, we need to defragment it */
11146 smp->ctx.a[0] = body;
11147 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011148 smp->ctx.a[2] = msg->chn->buf->data;
11149 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011150 }
11151 }
11152 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11153}
11154
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011155/* Return the signed integer value for the specified url parameter (see url_param
11156 * above).
11157 */
11158static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011159smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011160{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011161 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011162
11163 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011164 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011165 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011166 }
11167
11168 return ret;
11169}
11170
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011171/* This produces a 32-bit hash of the concatenation of the first occurrence of
11172 * the Host header followed by the path component if it begins with a slash ('/').
11173 * This means that '*' will not be added, resulting in exactly the first Host
11174 * entry. If no Host header is found, then the path is used. The resulting value
11175 * is hashed using the url hash followed by a full avalanche hash and provides a
11176 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11177 * high-traffic sites without having to store whole paths.
11178 * this differs from the base32 functions in that it includes the url parameters
11179 * as well as the path
11180 */
11181static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011182smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011183{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011184 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011185 struct hdr_ctx ctx;
11186 unsigned int hash = 0;
11187 char *ptr, *beg, *end;
11188 int len;
11189
11190 CHECK_HTTP_MESSAGE_FIRST();
11191
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011192 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011193 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011194 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011195 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11196 ptr = ctx.line + ctx.val;
11197 len = ctx.vlen;
11198 while (len--)
11199 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11200 }
11201
11202 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011203 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 +000011204 beg = http_get_path(txn);
11205 if (!beg)
11206 beg = end;
11207
11208 for (ptr = beg; ptr < end ; ptr++);
11209
11210 if (beg < ptr && *beg == '/') {
11211 while (beg < ptr)
11212 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11213 }
11214 hash = full_hash(hash);
11215
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011216 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011217 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011218 smp->flags = SMP_F_VOL_1ST;
11219 return 1;
11220}
11221
11222/* This concatenates the source address with the 32-bit hash of the Host and
11223 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11224 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11225 * on the source address length. The URL hash is stored before the address so
11226 * that in environments where IPv6 is insignificant, truncating the output to
11227 * 8 bytes would still work.
11228 */
11229static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011230smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011231{
11232 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011233 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011234
Dragan Dosendb5af612016-06-16 11:23:01 +020011235 if (!cli_conn)
11236 return 0;
11237
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011238 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011239 return 0;
11240
11241 temp = get_trash_chunk();
Dragan Dosene5f41332016-06-16 11:08:08 +020011242 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
11243 temp->len += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011244
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011245 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011246 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011247 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011248 temp->len += 4;
11249 break;
11250 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011251 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011252 temp->len += 16;
11253 break;
11254 default:
11255 return 0;
11256 }
11257
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011258 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011259 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011260 return 1;
11261}
11262
Willy Tarreau185b5c42012-04-26 15:11:51 +020011263/* This function is used to validate the arguments passed to any "hdr" fetch
11264 * keyword. These keywords support an optional positive or negative occurrence
11265 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11266 * is assumed that the types are already the correct ones. Returns 0 on error,
11267 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11268 * error message in case of error, that the caller is responsible for freeing.
11269 * The initial location must either be freeable or NULL.
11270 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011271int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011272{
11273 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011274 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011275 return 0;
11276 }
11277 return 1;
11278}
11279
Willy Tarreau276fae92013-07-25 14:36:01 +020011280/* takes an UINT value on input supposed to represent the time since EPOCH,
11281 * adds an optional offset found in args[0] and emits a string representing
11282 * the date in RFC-1123/5322 format.
11283 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011284static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011285{
Cyril Bontéf78d8962016-01-22 19:40:28 +010011286 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020011287 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11288 struct chunk *temp;
11289 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011290 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011291 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020011292
11293 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011294 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020011295 curr_date += args[0].data.sint;
11296
11297 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020011298 if (!tm)
11299 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011300
11301 temp = get_trash_chunk();
11302 temp->len = snprintf(temp->str, temp->size - temp->len,
11303 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11304 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11305 tm->tm_hour, tm->tm_min, tm->tm_sec);
11306
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011307 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011308 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020011309 return 1;
11310}
11311
Thierry FOURNIERad903512014-04-11 17:51:01 +020011312/* Match language range with language tag. RFC2616 14.4:
11313 *
11314 * A language-range matches a language-tag if it exactly equals
11315 * the tag, or if it exactly equals a prefix of the tag such
11316 * that the first tag character following the prefix is "-".
11317 *
11318 * Return 1 if the strings match, else return 0.
11319 */
11320static inline int language_range_match(const char *range, int range_len,
11321 const char *tag, int tag_len)
11322{
11323 const char *end = range + range_len;
11324 const char *tend = tag + tag_len;
11325 while (range < end) {
11326 if (*range == '-' && tag == tend)
11327 return 1;
11328 if (*range != *tag || tag == tend)
11329 return 0;
11330 range++;
11331 tag++;
11332 }
11333 /* Return true only if the last char of the tag is matched. */
11334 return tag == tend;
11335}
11336
11337/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011338static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011339{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011340 const char *al = smp->data.u.str.str;
11341 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011342 const char *token;
11343 int toklen;
11344 int qvalue;
11345 const char *str;
11346 const char *w;
11347 int best_q = 0;
11348
11349 /* Set the constant to the sample, because the output of the
11350 * function will be peek in the constant configuration string.
11351 */
11352 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011353 smp->data.u.str.size = 0;
11354 smp->data.u.str.str = "";
11355 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011356
11357 /* Parse the accept language */
11358 while (1) {
11359
11360 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011361 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011362 al++;
11363 if (al >= end)
11364 break;
11365
11366 /* Start of the fisrt word. */
11367 token = al;
11368
11369 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011370 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011371 al++;
11372 if (al == token)
11373 goto expect_comma;
11374
11375 /* Length of the token. */
11376 toklen = al - token;
11377 qvalue = 1000;
11378
11379 /* Check if the token exists in the list. If the token not exists,
11380 * jump to the next token.
11381 */
11382 str = args[0].data.str.str;
11383 w = str;
11384 while (1) {
11385 if (*str == ';' || *str == '\0') {
11386 if (language_range_match(token, toklen, w, str-w))
11387 goto look_for_q;
11388 if (*str == '\0')
11389 goto expect_comma;
11390 w = str + 1;
11391 }
11392 str++;
11393 }
11394 goto expect_comma;
11395
11396look_for_q:
11397
11398 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011399 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011400 al++;
11401 if (al >= end)
11402 goto process_value;
11403
11404 /* If ',' is found, process the result */
11405 if (*al == ',')
11406 goto process_value;
11407
11408 /* If the character is different from ';', look
11409 * for the end of the header part in best effort.
11410 */
11411 if (*al != ';')
11412 goto expect_comma;
11413
11414 /* Assumes that the char is ';', now expect "q=". */
11415 al++;
11416
11417 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011418 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011419 al++;
11420 if (al >= end)
11421 goto process_value;
11422
11423 /* Expect 'q'. If no 'q', continue in best effort */
11424 if (*al != 'q')
11425 goto process_value;
11426 al++;
11427
11428 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011429 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011430 al++;
11431 if (al >= end)
11432 goto process_value;
11433
11434 /* Expect '='. If no '=', continue in best effort */
11435 if (*al != '=')
11436 goto process_value;
11437 al++;
11438
11439 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011440 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011441 al++;
11442 if (al >= end)
11443 goto process_value;
11444
11445 /* Parse the q value. */
11446 qvalue = parse_qvalue(al, &al);
11447
11448process_value:
11449
11450 /* If the new q value is the best q value, then store the associated
11451 * language in the response. If qvalue is the biggest value (1000),
11452 * break the process.
11453 */
11454 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011455 smp->data.u.str.str = (char *)w;
11456 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011457 if (qvalue >= 1000)
11458 break;
11459 best_q = qvalue;
11460 }
11461
11462expect_comma:
11463
11464 /* Expect comma or end. If the end is detected, quit the loop. */
11465 while (al < end && *al != ',')
11466 al++;
11467 if (al >= end)
11468 break;
11469
11470 /* Comma is found, jump it and restart the analyzer. */
11471 al++;
11472 }
11473
11474 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011475 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
11476 smp->data.u.str.str = args[1].data.str.str;
11477 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011478 }
11479
11480 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011481 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011482}
11483
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011484/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011485static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011486{
11487 /* If the constant flag is set or if not size is avalaible at
11488 * the end of the buffer, copy the string in other buffer
11489 * before decoding.
11490 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011491 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011492 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011493 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
11494 smp->data.u.str.str = str->str;
11495 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011496 smp->flags &= ~SMP_F_CONST;
11497 }
11498
11499 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011500 smp->data.u.str.str[smp->data.u.str.len] = '\0';
11501 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Christopher Fauleta2584792017-10-05 10:03:12 +020011502 return (smp->data.u.str.len >= 0);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011503}
11504
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011505static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
11506{
11507 struct proxy *fe = strm_fe(smp->strm);
11508 int idx, i;
11509 struct cap_hdr *hdr;
11510 int len;
11511
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011512 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011513 return 0;
11514
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011515 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011516
11517 /* Check the availibity of the capture id. */
11518 if (idx > fe->nb_req_cap - 1)
11519 return 0;
11520
11521 /* Look for the original configuration. */
11522 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
11523 hdr != NULL && i != idx ;
11524 i--, hdr = hdr->next);
11525 if (!hdr)
11526 return 0;
11527
11528 /* check for the memory allocation */
11529 if (smp->strm->req_cap[hdr->index] == NULL)
11530 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
11531 if (smp->strm->req_cap[hdr->index] == NULL)
11532 return 0;
11533
11534 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011535 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011536 if (len > hdr->len)
11537 len = hdr->len;
11538
11539 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011540 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011541 smp->strm->req_cap[idx][len] = '\0';
11542
11543 return 1;
11544}
11545
11546static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
11547{
11548 struct proxy *fe = strm_fe(smp->strm);
11549 int idx, i;
11550 struct cap_hdr *hdr;
11551 int len;
11552
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011553 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011554 return 0;
11555
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011556 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011557
11558 /* Check the availibity of the capture id. */
11559 if (idx > fe->nb_rsp_cap - 1)
11560 return 0;
11561
11562 /* Look for the original configuration. */
11563 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
11564 hdr != NULL && i != idx ;
11565 i--, hdr = hdr->next);
11566 if (!hdr)
11567 return 0;
11568
11569 /* check for the memory allocation */
11570 if (smp->strm->res_cap[hdr->index] == NULL)
11571 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
11572 if (smp->strm->res_cap[hdr->index] == NULL)
11573 return 0;
11574
11575 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011576 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011577 if (len > hdr->len)
11578 len = hdr->len;
11579
11580 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011581 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011582 smp->strm->res_cap[idx][len] = '\0';
11583
11584 return 1;
11585}
11586
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011587/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011588 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011589 * the relevant part of the request line accordingly. Then it updates various
11590 * pointers to the next elements which were moved, and the total buffer length.
11591 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011592 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
11593 * error, though this can be revisited when this code is finally exploited.
11594 *
11595 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
11596 * query string and 3 to replace uri.
11597 *
11598 * In query string case, the mark question '?' must be set at the start of the
11599 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011600 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011601int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011602 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011603{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011604 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011605 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011606 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011607 int delta;
11608
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011609 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011610 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011611 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011612 cur_end = cur_ptr + txn->req.sl.rq.m_l;
11613
11614 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011615 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011616 txn->req.sl.rq.m_l += delta;
11617 txn->req.sl.rq.u += delta;
11618 txn->req.sl.rq.v += delta;
11619 break;
11620
11621 case 1: // path
11622 cur_ptr = http_get_path(txn);
11623 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011624 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011625
11626 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011627 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 +010011628 cur_end++;
11629
11630 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011631 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011632 txn->req.sl.rq.u_l += delta;
11633 txn->req.sl.rq.v += delta;
11634 break;
11635
11636 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011637 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011638 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011639 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11640 while (cur_ptr < cur_end && *cur_ptr != '?')
11641 cur_ptr++;
11642
11643 /* skip the question mark or indicate that we must insert it
11644 * (but only if the format string is not empty then).
11645 */
11646 if (cur_ptr < cur_end)
11647 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011648 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011649 offset = 0;
11650
11651 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011652 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011653 txn->req.sl.rq.u_l += delta;
11654 txn->req.sl.rq.v += delta;
11655 break;
11656
11657 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011658 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011659 cur_end = cur_ptr + txn->req.sl.rq.u_l;
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 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011668 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011669 }
11670
11671 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011672 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020011673 txn->req.sl.rq.l += delta;
11674 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011675 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011676 return 0;
11677}
11678
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011679/* This function replace the HTTP status code and the associated message. The
11680 * variable <status> contains the new status code. This function never fails.
11681 */
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011682void http_set_status(unsigned int status, const char *reason, struct stream *s)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011683{
11684 struct http_txn *txn = s->txn;
11685 char *cur_ptr, *cur_end;
11686 int delta;
11687 char *res;
11688 int c_l;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011689 const char *msg = reason;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011690 int msg_len;
11691
11692 chunk_reset(&trash);
11693
11694 res = ultoa_o(status, trash.str, trash.size);
11695 c_l = res - trash.str;
11696
11697 trash.str[c_l] = ' ';
11698 trash.len = c_l + 1;
11699
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011700 /* Do we have a custom reason format string? */
11701 if (msg == NULL)
11702 msg = get_reason(status);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011703 msg_len = strlen(msg);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011704 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
11705 trash.len += msg_len;
11706
11707 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
11708 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
11709
11710 /* commit changes and adjust message */
11711 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
11712
11713 /* adjust res line offsets and lengths */
11714 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
11715 txn->rsp.sl.st.c_l = c_l;
11716 txn->rsp.sl.st.r_l = msg_len;
11717
11718 delta = trash.len - (cur_end - cur_ptr);
11719 txn->rsp.sl.st.l += delta;
11720 txn->hdr_idx.v[0].len += delta;
11721 http_msg_move_end(&txn->rsp, delta);
11722}
11723
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011724/* This function executes one of the set-{method,path,query,uri} actions. It
11725 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020011726 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011727 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020011728 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
11729 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011730 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011731enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011732 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011733{
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011734 struct chunk *replace;
11735 enum act_return ret = ACT_RET_ERR;
11736
11737 replace = alloc_trash_chunk();
11738 if (!replace)
11739 goto leave;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011740
11741 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011742 if (rule->arg.http.action == 2)
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011743 replace->str[replace->len++] = '?';
11744 replace->len += build_logline(s, replace->str + replace->len, replace->size - replace->len,
11745 &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011746
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011747 http_replace_req_line(rule->arg.http.action, replace->str, replace->len, px, s);
11748
11749 ret = ACT_RET_CONT;
11750
11751leave:
11752 free_trash_chunk(replace);
11753 return ret;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011754}
11755
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011756/* This function is just a compliant action wrapper for "set-status". */
11757enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011758 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011759{
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011760 http_set_status(rule->arg.status.code, rule->arg.status.reason, s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011761 return ACT_RET_CONT;
11762}
11763
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011764/* parse an http-request action among :
11765 * set-method
11766 * set-path
11767 * set-query
11768 * set-uri
11769 *
11770 * All of them accept a single argument of type string representing a log-format.
11771 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
11772 * 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 +020011773 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011774 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011775enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
11776 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011777{
11778 int cur_arg = *orig_arg;
11779
Thierry FOURNIER42148732015-09-02 17:17:33 +020011780 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011781
11782 switch (args[0][4]) {
11783 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011784 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011785 rule->action_ptr = http_action_set_req_line;
11786 break;
11787 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011788 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011789 rule->action_ptr = http_action_set_req_line;
11790 break;
11791 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011792 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011793 rule->action_ptr = http_action_set_req_line;
11794 break;
11795 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011796 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011797 rule->action_ptr = http_action_set_req_line;
11798 break;
11799 default:
11800 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011801 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011802 }
11803
11804 if (!*args[cur_arg] ||
11805 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
11806 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011807 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011808 }
11809
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011810 LIST_INIT(&rule->arg.http.logfmt);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011811 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010011812 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +010011813 (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 +010011814 return ACT_RET_PRS_ERR;
11815 }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011816
11817 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011818 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011819}
11820
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011821/* parse set-status action:
11822 * This action accepts a single argument of type int representing
11823 * an http status code. It returns ACT_RET_PRS_OK on success,
11824 * ACT_RET_PRS_ERR on error.
11825 */
11826enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
11827 struct act_rule *rule, char **err)
11828{
11829 char *error;
11830
Thierry FOURNIER42148732015-09-02 17:17:33 +020011831 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011832 rule->action_ptr = action_http_set_status;
11833
11834 /* Check if an argument is available */
11835 if (!*args[*orig_arg]) {
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011836 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011837 return ACT_RET_PRS_ERR;
11838 }
11839
11840 /* convert status code as integer */
11841 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
11842 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
11843 memprintf(err, "expects an integer status code between 100 and 999");
11844 return ACT_RET_PRS_ERR;
11845 }
11846
11847 (*orig_arg)++;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011848
11849 /* set custom reason string */
11850 rule->arg.status.reason = NULL; // If null, we use the default reason for the status code.
11851 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
11852 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
11853 (*orig_arg)++;
11854 rule->arg.status.reason = strdup(args[*orig_arg]);
11855 (*orig_arg)++;
11856 }
11857
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011858 return ACT_RET_PRS_OK;
11859}
11860
Willy Tarreaua9083d02015-05-08 15:27:59 +020011861/* This function executes the "capture" action. It executes a fetch expression,
11862 * turns the result into a string and puts it in a capture slot. It always
11863 * returns 1. If an error occurs the action is cancelled, but the rule
11864 * processing continues.
11865 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011866enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011867 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020011868{
Willy Tarreaua9083d02015-05-08 15:27:59 +020011869 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020011870 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011871 char **cap = s->req_cap;
11872 int len;
11873
Thierry FOURNIER32b15002015-07-31 08:56:16 +020011874 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 +020011875 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011876 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011877
11878 if (cap[h->index] == NULL)
11879 cap[h->index] = pool_alloc2(h->pool);
11880
11881 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011882 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011883
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011884 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011885 if (len > h->len)
11886 len = h->len;
11887
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011888 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020011889 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011890 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011891}
11892
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011893/* This function executes the "capture" action and store the result in a
11894 * capture slot if exists. It executes a fetch expression, turns the result
11895 * into a string and puts it in a capture slot. It always returns 1. If an
11896 * error occurs the action is cancelled, but the rule processing continues.
11897 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011898enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011899 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011900{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011901 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011902 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011903 char **cap = s->req_cap;
11904 struct proxy *fe = strm_fe(s);
11905 int len;
11906 int i;
11907
11908 /* Look for the original configuration. */
11909 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020011910 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011911 i--, h = h->next);
11912 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011913 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011914
Thierry FOURNIERe2097972015-07-31 08:56:35 +020011915 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 +020011916 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011917 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011918
11919 if (cap[h->index] == NULL)
11920 cap[h->index] = pool_alloc2(h->pool);
11921
11922 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011923 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011924
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011925 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011926 if (len > h->len)
11927 len = h->len;
11928
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011929 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011930 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011931 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011932}
11933
Willy Tarreaua9083d02015-05-08 15:27:59 +020011934/* parse an "http-request capture" action. It takes a single argument which is
11935 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011936 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011937 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020011938 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011939enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
11940 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020011941{
11942 struct sample_expr *expr;
11943 struct cap_hdr *hdr;
11944 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020011945 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011946
11947 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
11948 if (strcmp(args[cur_arg], "if") == 0 ||
11949 strcmp(args[cur_arg], "unless") == 0)
11950 break;
11951
11952 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011953 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011954 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011955 }
11956
Willy Tarreaua9083d02015-05-08 15:27:59 +020011957 cur_arg = *orig_arg;
11958 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
11959 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011960 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011961
11962 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
11963 memprintf(err,
11964 "fetch method '%s' extracts information from '%s', none of which is available here",
11965 args[cur_arg-1], sample_src_names(expr->fetch->use));
11966 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011967 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011968 }
11969
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011970 if (!args[cur_arg] || !*args[cur_arg]) {
11971 memprintf(err, "expects 'len or 'id'");
11972 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011973 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011974 }
11975
Willy Tarreaua9083d02015-05-08 15:27:59 +020011976 if (strcmp(args[cur_arg], "len") == 0) {
11977 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011978
11979 if (!(px->cap & PR_CAP_FE)) {
11980 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011981 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011982 }
11983
11984 proxy->conf.args.ctx = ARGC_CAP;
11985
Willy Tarreaua9083d02015-05-08 15:27:59 +020011986 if (!args[cur_arg]) {
11987 memprintf(err, "missing length value");
11988 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011989 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011990 }
11991 /* we copy the table name for now, it will be resolved later */
11992 len = atoi(args[cur_arg]);
11993 if (len <= 0) {
11994 memprintf(err, "length must be > 0");
11995 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011996 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011997 }
11998 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011999
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012000 if (!len) {
12001 memprintf(err, "a positive 'len' argument is mandatory");
12002 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012003 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012004 }
12005
Vincent Bernat02779b62016-04-03 13:48:43 +020012006 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012007 hdr->next = px->req_cap;
12008 hdr->name = NULL; /* not a header capture */
12009 hdr->namelen = 0;
12010 hdr->len = len;
12011 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12012 hdr->index = px->nb_req_cap++;
12013
12014 px->req_cap = hdr;
12015 px->to_log |= LW_REQHDR;
12016
Thierry FOURNIER42148732015-09-02 17:17:33 +020012017 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012018 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012019 rule->arg.cap.expr = expr;
12020 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012021 }
12022
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012023 else if (strcmp(args[cur_arg], "id") == 0) {
12024 int id;
12025 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012026
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012027 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012028
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012029 if (!args[cur_arg]) {
12030 memprintf(err, "missing id value");
12031 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012032 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012033 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012034
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012035 id = strtol(args[cur_arg], &error, 10);
12036 if (*error != '\0') {
12037 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12038 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012039 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012040 }
12041 cur_arg++;
12042
12043 proxy->conf.args.ctx = ARGC_CAP;
12044
Thierry FOURNIER42148732015-09-02 17:17:33 +020012045 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012046 rule->action_ptr = http_action_req_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012047 rule->arg.capid.expr = expr;
12048 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012049 }
12050
12051 else {
12052 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12053 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012054 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012055 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012056
12057 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012058 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012059}
12060
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012061/* This function executes the "capture" action and store the result in a
12062 * capture slot if exists. It executes a fetch expression, turns the result
12063 * into a string and puts it in a capture slot. It always returns 1. If an
12064 * error occurs the action is cancelled, but the rule processing continues.
12065 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012066enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012067 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012068{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012069 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012070 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012071 char **cap = s->res_cap;
12072 struct proxy *fe = strm_fe(s);
12073 int len;
12074 int i;
12075
12076 /* Look for the original configuration. */
12077 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012078 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012079 i--, h = h->next);
12080 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012081 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012082
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012083 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 +020012084 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012085 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012086
12087 if (cap[h->index] == NULL)
12088 cap[h->index] = pool_alloc2(h->pool);
12089
12090 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012091 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012092
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012093 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012094 if (len > h->len)
12095 len = h->len;
12096
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012097 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012098 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012099 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012100}
12101
12102/* parse an "http-response capture" action. It takes a single argument which is
12103 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12104 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012105 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012106 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012107enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12108 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012109{
12110 struct sample_expr *expr;
12111 int cur_arg;
12112 int id;
12113 char *error;
12114
12115 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12116 if (strcmp(args[cur_arg], "if") == 0 ||
12117 strcmp(args[cur_arg], "unless") == 0)
12118 break;
12119
12120 if (cur_arg < *orig_arg + 3) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012121 memprintf(err, "expects <expression> id <idx>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012122 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012123 }
12124
12125 cur_arg = *orig_arg;
12126 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12127 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012128 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012129
12130 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12131 memprintf(err,
12132 "fetch method '%s' extracts information from '%s', none of which is available here",
12133 args[cur_arg-1], sample_src_names(expr->fetch->use));
12134 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012135 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012136 }
12137
12138 if (!args[cur_arg] || !*args[cur_arg]) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012139 memprintf(err, "expects 'id'");
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012140 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012141 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012142 }
12143
12144 if (strcmp(args[cur_arg], "id") != 0) {
12145 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12146 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012147 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012148 }
12149
12150 cur_arg++;
12151
12152 if (!args[cur_arg]) {
12153 memprintf(err, "missing id value");
12154 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012155 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012156 }
12157
12158 id = strtol(args[cur_arg], &error, 10);
12159 if (*error != '\0') {
12160 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12161 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012162 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012163 }
12164 cur_arg++;
12165
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012166 proxy->conf.args.ctx = ARGC_CAP;
12167
Thierry FOURNIER42148732015-09-02 17:17:33 +020012168 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012169 rule->action_ptr = http_action_res_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012170 rule->arg.capid.expr = expr;
12171 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012172
12173 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012174 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012175}
12176
William Lallemand73025dd2014-04-24 14:38:37 +020012177/*
12178 * Return the struct http_req_action_kw associated to a keyword.
12179 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012180struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012181{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012182 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012183}
12184
12185/*
12186 * Return the struct http_res_action_kw associated to a keyword.
12187 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012188struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012189{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012190 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012191}
12192
Willy Tarreau12207b32016-11-22 19:48:51 +010012193
12194/* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop
12195 * now.
12196 */
12197static int cli_parse_show_errors(char **args, struct appctx *appctx, void *private)
12198{
12199 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
12200 return 1;
12201
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012202 if (*args[2]) {
12203 struct proxy *px;
12204
12205 px = proxy_find_by_name(args[2], 0, 0);
12206 if (px)
12207 appctx->ctx.errors.iid = px->uuid;
12208 else
12209 appctx->ctx.errors.iid = atoi(args[2]);
12210
12211 if (!appctx->ctx.errors.iid) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +020012212 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012213 appctx->ctx.cli.msg = "No such proxy.\n";
12214 appctx->st0 = CLI_ST_PRINT;
12215 return 1;
12216 }
12217 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012218 else
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012219 appctx->ctx.errors.iid = -1; // dump all proxies
12220
Willy Tarreau35069f82016-11-25 09:16:37 +010012221 appctx->ctx.errors.flag = 0;
12222 if (strcmp(args[3], "request") == 0)
12223 appctx->ctx.errors.flag |= 4; // ignore response
12224 else if (strcmp(args[3], "response") == 0)
12225 appctx->ctx.errors.flag |= 2; // ignore request
Willy Tarreau12207b32016-11-22 19:48:51 +010012226 appctx->ctx.errors.px = NULL;
Willy Tarreau12207b32016-11-22 19:48:51 +010012227 return 0;
12228}
12229
12230/* This function dumps all captured errors onto the stream interface's
12231 * read buffer. It returns 0 if the output buffer is full and it needs
12232 * to be called again, otherwise non-zero.
12233 */
12234static int cli_io_handler_show_errors(struct appctx *appctx)
12235{
12236 struct stream_interface *si = appctx->owner;
12237 extern const char *monthname[12];
12238
12239 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
12240 return 1;
12241
12242 chunk_reset(&trash);
12243
12244 if (!appctx->ctx.errors.px) {
12245 /* the function had not been called yet, let's prepare the
12246 * buffer for a response.
12247 */
12248 struct tm tm;
12249
12250 get_localtime(date.tv_sec, &tm);
12251 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
12252 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12253 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
12254 error_snapshot_id);
12255
Willy Tarreau06d80a92017-10-19 14:32:15 +020012256 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012257 /* Socket buffer full. Let's try again later from the same point */
12258 si_applet_cant_put(si);
12259 return 0;
12260 }
12261
12262 appctx->ctx.errors.px = proxy;
Willy Tarreau12207b32016-11-22 19:48:51 +010012263 appctx->ctx.errors.bol = 0;
12264 appctx->ctx.errors.ptr = -1;
12265 }
12266
12267 /* we have two inner loops here, one for the proxy, the other one for
12268 * the buffer.
12269 */
12270 while (appctx->ctx.errors.px) {
12271 struct error_snapshot *es;
12272
Willy Tarreau35069f82016-11-25 09:16:37 +010012273 if ((appctx->ctx.errors.flag & 1) == 0) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012274 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau35069f82016-11-25 09:16:37 +010012275 if (appctx->ctx.errors.flag & 2) // skip req
12276 goto next;
12277 }
12278 else {
Willy Tarreau12207b32016-11-22 19:48:51 +010012279 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau35069f82016-11-25 09:16:37 +010012280 if (appctx->ctx.errors.flag & 4) // skip resp
12281 goto next;
12282 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012283
12284 if (!es->when.tv_sec)
12285 goto next;
12286
12287 if (appctx->ctx.errors.iid >= 0 &&
12288 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
12289 es->oe->uuid != appctx->ctx.errors.iid)
12290 goto next;
12291
12292 if (appctx->ctx.errors.ptr < 0) {
12293 /* just print headers now */
12294
12295 char pn[INET6_ADDRSTRLEN];
12296 struct tm tm;
12297 int port;
12298
12299 get_localtime(es->when.tv_sec, &tm);
12300 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
12301 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12302 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
12303
12304 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
12305 case AF_INET:
12306 case AF_INET6:
12307 port = get_host_port(&es->src);
12308 break;
12309 default:
12310 port = 0;
12311 }
12312
Willy Tarreau35069f82016-11-25 09:16:37 +010012313 switch (appctx->ctx.errors.flag & 1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012314 case 0:
12315 chunk_appendf(&trash,
12316 " frontend %s (#%d): invalid request\n"
12317 " backend %s (#%d)",
12318 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
12319 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
12320 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
12321 break;
12322 case 1:
12323 chunk_appendf(&trash,
12324 " backend %s (#%d): invalid response\n"
12325 " frontend %s (#%d)",
12326 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
12327 es->oe->id, es->oe->uuid);
12328 break;
12329 }
12330
12331 chunk_appendf(&trash,
12332 ", server %s (#%d), event #%u\n"
12333 " src %s:%d, session #%d, session flags 0x%08x\n"
Willy Tarreau10e61cb2017-01-04 14:51:22 +010012334 " HTTP msg state %s(%d), msg flags 0x%08x, tx flags 0x%08x\n"
Willy Tarreau12207b32016-11-22 19:48:51 +010012335 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
12336 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
12337 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
12338 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
12339 es->ev_id,
12340 pn, port, es->sid, es->s_flags,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020012341 h1_msg_state_str(es->state), es->state, es->m_flags, es->t_flags,
Willy Tarreau12207b32016-11-22 19:48:51 +010012342 es->m_clen, es->m_blen,
12343 es->b_flags, es->b_out, es->b_tot,
12344 es->len, es->b_wrap, es->pos);
12345
Willy Tarreau06d80a92017-10-19 14:32:15 +020012346 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012347 /* Socket buffer full. Let's try again later from the same point */
12348 si_applet_cant_put(si);
12349 return 0;
12350 }
12351 appctx->ctx.errors.ptr = 0;
12352 appctx->ctx.errors.sid = es->sid;
12353 }
12354
12355 if (appctx->ctx.errors.sid != es->sid) {
12356 /* the snapshot changed while we were dumping it */
12357 chunk_appendf(&trash,
12358 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +020012359 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012360 si_applet_cant_put(si);
12361 return 0;
12362 }
12363 goto next;
12364 }
12365
12366 /* OK, ptr >= 0, so we have to dump the current line */
12367 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
12368 int newptr;
12369 int newline;
12370
12371 newline = appctx->ctx.errors.bol;
12372 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
12373 if (newptr == appctx->ctx.errors.ptr)
12374 return 0;
12375
Willy Tarreau06d80a92017-10-19 14:32:15 +020012376 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012377 /* Socket buffer full. Let's try again later from the same point */
12378 si_applet_cant_put(si);
12379 return 0;
12380 }
12381 appctx->ctx.errors.ptr = newptr;
12382 appctx->ctx.errors.bol = newline;
12383 };
12384 next:
12385 appctx->ctx.errors.bol = 0;
12386 appctx->ctx.errors.ptr = -1;
Willy Tarreau35069f82016-11-25 09:16:37 +010012387 appctx->ctx.errors.flag ^= 1;
12388 if (!(appctx->ctx.errors.flag & 1))
Willy Tarreau12207b32016-11-22 19:48:51 +010012389 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau12207b32016-11-22 19:48:51 +010012390 }
12391
12392 /* dump complete */
12393 return 1;
12394}
12395
12396/* register cli keywords */
12397static struct cli_kw_list cli_kws = {{ },{
12398 { { "show", "errors", NULL },
12399 "show errors : report last request and response errors for each proxy",
12400 cli_parse_show_errors, cli_io_handler_show_errors, NULL,
12401 },
12402 {{},}
12403}};
12404
Willy Tarreau4a568972010-05-12 08:08:50 +020012405/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012406/* All supported ACL keywords must be declared here. */
12407/************************************************************************/
12408
12409/* Note: must not be declared <const> as its list will be overwritten.
12410 * Please take care of keeping this list alphabetically sorted.
12411 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012412static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012413 { "base", "base", PAT_MATCH_STR },
12414 { "base_beg", "base", PAT_MATCH_BEG },
12415 { "base_dir", "base", PAT_MATCH_DIR },
12416 { "base_dom", "base", PAT_MATCH_DOM },
12417 { "base_end", "base", PAT_MATCH_END },
12418 { "base_len", "base", PAT_MATCH_LEN },
12419 { "base_reg", "base", PAT_MATCH_REG },
12420 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012421
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012422 { "cook", "req.cook", PAT_MATCH_STR },
12423 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12424 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12425 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12426 { "cook_end", "req.cook", PAT_MATCH_END },
12427 { "cook_len", "req.cook", PAT_MATCH_LEN },
12428 { "cook_reg", "req.cook", PAT_MATCH_REG },
12429 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012430
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012431 { "hdr", "req.hdr", PAT_MATCH_STR },
12432 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12433 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12434 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12435 { "hdr_end", "req.hdr", PAT_MATCH_END },
12436 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12437 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12438 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012439
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012440 /* these two declarations uses strings with list storage (in place
12441 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12442 * and delete functions are relative to the list management. The parse
12443 * and match method are related to the corresponding fetch methods. This
12444 * is very particular ACL declaration mode.
12445 */
12446 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12447 { "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 +020012448
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012449 { "path", "path", PAT_MATCH_STR },
12450 { "path_beg", "path", PAT_MATCH_BEG },
12451 { "path_dir", "path", PAT_MATCH_DIR },
12452 { "path_dom", "path", PAT_MATCH_DOM },
12453 { "path_end", "path", PAT_MATCH_END },
12454 { "path_len", "path", PAT_MATCH_LEN },
12455 { "path_reg", "path", PAT_MATCH_REG },
12456 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012457
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012458 { "req_ver", "req.ver", PAT_MATCH_STR },
12459 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012460
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012461 { "scook", "res.cook", PAT_MATCH_STR },
12462 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12463 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12464 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12465 { "scook_end", "res.cook", PAT_MATCH_END },
12466 { "scook_len", "res.cook", PAT_MATCH_LEN },
12467 { "scook_reg", "res.cook", PAT_MATCH_REG },
12468 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012469
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012470 { "shdr", "res.hdr", PAT_MATCH_STR },
12471 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12472 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12473 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12474 { "shdr_end", "res.hdr", PAT_MATCH_END },
12475 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12476 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12477 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012478
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012479 { "url", "url", PAT_MATCH_STR },
12480 { "url_beg", "url", PAT_MATCH_BEG },
12481 { "url_dir", "url", PAT_MATCH_DIR },
12482 { "url_dom", "url", PAT_MATCH_DOM },
12483 { "url_end", "url", PAT_MATCH_END },
12484 { "url_len", "url", PAT_MATCH_LEN },
12485 { "url_reg", "url", PAT_MATCH_REG },
12486 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012487
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012488 { "urlp", "urlp", PAT_MATCH_STR },
12489 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12490 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12491 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12492 { "urlp_end", "urlp", PAT_MATCH_END },
12493 { "urlp_len", "urlp", PAT_MATCH_LEN },
12494 { "urlp_reg", "urlp", PAT_MATCH_REG },
12495 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012496
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012497 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012498}};
12499
12500/************************************************************************/
12501/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012502/************************************************************************/
12503/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012504static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012505 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012506 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012507 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12508
Willy Tarreau87b09662015-04-03 00:22:06 +020012509 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012510 { "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 +020012511
12512 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012513 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12514 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12515 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012516
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012517 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
12518 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012519
Willy Tarreau409bcde2013-01-08 00:31:00 +010012520 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12521 * are only here to match the ACL's name, are request-only and are used
12522 * for ACL compatibility only.
12523 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012524 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12525 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012526 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12527 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012528
12529 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12530 * only here to match the ACL's name, are request-only and are used for
12531 * ACL compatibility only.
12532 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012533 { "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 +020012534 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012535 { "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 +020012536 { "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 +010012537
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012538 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012539 { "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 +010012540 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012541 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012542 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012543 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012544
12545 /* HTTP protocol on the request path */
12546 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012547 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012548
12549 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012550 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12551 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012552
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012553 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012554 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
12555 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012556 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012557
Thierry FOURNIERd7d88812017-04-19 15:15:14 +020012558 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER5617dce2017-04-09 05:38:19 +020012559 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12560
Willy Tarreau18ed2562013-01-14 15:56:36 +010012561 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012562 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12563 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012564
Willy Tarreau18ed2562013-01-14 15:56:36 +010012565 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012566 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012567 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12568 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012569
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012570 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012571 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012572 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012573 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012574 { "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 +010012575 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012576 { "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 +010012577
12578 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012579 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012580 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12581 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012582
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012583 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012584 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012585 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012586 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012587 { "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 +010012588 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012589 { "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 +010012590
Willy Tarreau409bcde2013-01-08 00:31:00 +010012591 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012592 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012593 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12594 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012595 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010012596
12597 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012598 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012599 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012600 { "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 +020012601 { "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 +010012602
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012603 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020012604 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012605 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012606 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012607 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012608 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012609 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020012610 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12611 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012612 { "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 +010012613 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020012614}};
12615
Willy Tarreau8797c062007-05-07 00:55:35 +020012616
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012617/************************************************************************/
12618/* All supported converter keywords must be declared here. */
12619/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020012620/* Note: must not be declared <const> as its list will be overwritten */
12621static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012622 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020012623 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012624 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
12625 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012626 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020012627 { NULL, NULL, 0, 0, 0 },
12628}};
12629
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012630
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012631/************************************************************************/
12632/* All supported http-request action keywords must be declared here. */
12633/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012634struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012635 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020012636 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012637 { "set-method", parse_set_req_line },
12638 { "set-path", parse_set_req_line },
12639 { "set-query", parse_set_req_line },
12640 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020012641 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012642 }
12643};
12644
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012645struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012646 .kw = {
12647 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012648 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012649 { NULL, NULL }
12650 }
12651};
12652
Willy Tarreau8797c062007-05-07 00:55:35 +020012653__attribute__((constructor))
12654static void __http_protocol_init(void)
12655{
12656 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020012657 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020012658 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012659 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012660 http_res_keywords_register(&http_res_actions);
Willy Tarreau12207b32016-11-22 19:48:51 +010012661 cli_register_kw(&cli_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020012662}
12663
12664
Willy Tarreau58f10d72006-12-04 02:26:12 +010012665/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020012666 * Local variables:
12667 * c-indent-level: 8
12668 * c-basic-offset: 8
12669 * End:
12670 */