blob: efbbc84bb46eef78689490f48c71101296f2d8a9 [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
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002720 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02002721 struct stktable *t;
2722 struct stksess *ts;
2723 struct stktable_key *key;
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))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002730 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02002731
2732 /* let's count a new HTTP request as it's the first time we do it */
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
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002742 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002743 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002744 stkctr_set_flags(&s->stkctr[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
Christopher Faulet4fce0d82017-09-18 11:57:31 +02002993 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002994 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))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +02003003 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003004
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
Christopher Faulet4fce0d82017-09-18 11:57:31 +02003015 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003016 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +02003017 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003018
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;
Olivier Houchardc2aae742017-09-22 18:26:28 +02003344 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaud787e662009-07-07 10:14:51 +02003345
Willy Tarreau655dce92009-11-08 13:10:58 +01003346 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003347 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003348 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02003349 }
3350
Willy Tarreau87b09662015-04-03 00:22:06 +02003351 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 +02003352 now_ms, __FUNCTION__,
3353 s,
3354 req,
3355 req->rex, req->wex,
3356 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003357 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003358 req->analysers);
3359
Willy Tarreau65410832014-04-28 21:25:43 +02003360 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02003361 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02003362
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003363 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02003364 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02003365 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01003366
Willy Tarreau0b748332014-04-29 00:13:29 +02003367 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003368 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
3369 goto return_prx_yield;
3370
Willy Tarreau0b748332014-04-29 00:13:29 +02003371 case HTTP_RULE_RES_CONT:
3372 case HTTP_RULE_RES_STOP: /* nothing to do */
3373 break;
Willy Tarreau52542592014-04-28 18:33:26 +02003374
Willy Tarreau0b748332014-04-29 00:13:29 +02003375 case HTTP_RULE_RES_DENY: /* deny or tarpit */
3376 if (txn->flags & TX_CLTARPIT)
3377 goto tarpit;
3378 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003379
Willy Tarreau0b748332014-04-29 00:13:29 +02003380 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
3381 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02003382
Willy Tarreau0b748332014-04-29 00:13:29 +02003383 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02003384 goto done;
3385
Willy Tarreau0b748332014-04-29 00:13:29 +02003386 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
3387 goto return_bad_req;
3388 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003389 }
3390
Olivier Houchardc2aae742017-09-22 18:26:28 +02003391 if (conn && conn->flags & CO_FL_EARLY_DATA) {
3392 struct hdr_ctx ctx;
3393
3394 ctx.idx = 0;
3395 if (!http_find_header2("Early-Data", strlen("Early-Data"),
3396 s->req.buf->p, &txn->hdr_idx, &ctx)) {
3397 if (unlikely(http_header_add_tail2(&txn->req,
3398 &txn->hdr_idx, "Early-Data: 1",
3399 strlen("Early-Data: 1"))) < 0) {
3400 goto return_bad_req;
3401 }
3402 }
3403
3404 }
3405
Willy Tarreau52542592014-04-28 18:33:26 +02003406 /* OK at this stage, we know that the request was accepted according to
3407 * the http-request rules, we can check for the stats. Note that the
3408 * URI is detected *before* the req* rules in order not to be affected
3409 * by a possible reqrep, while they are processed *after* so that a
3410 * reqdeny can still block them. This clearly needs to change in 1.6!
3411 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003412 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02003413 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01003414 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02003415 txn->status = 500;
3416 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003417 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003418
Willy Tarreaue7dff022015-04-03 01:14:29 +02003419 if (!(s->flags & SF_ERR_MASK))
3420 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02003421 goto return_prx_cond;
3422 }
3423
3424 /* parse the whole stats request and extract the relevant information */
3425 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02003426 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02003427 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003428
Willy Tarreau0b748332014-04-29 00:13:29 +02003429 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
3430 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003431
Willy Tarreau0b748332014-04-29 00:13:29 +02003432 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
3433 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003434 }
3435
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003436 /* evaluate the req* rules except reqadd */
3437 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003438 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003439 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003440
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003441 if (txn->flags & TX_CLDENY)
3442 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003443
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003444 if (txn->flags & TX_CLTARPIT) {
3445 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003446 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003447 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003448 }
Willy Tarreau06619262006-12-17 08:37:22 +01003449
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003450 /* add request headers from the rule sets in the same order */
3451 list_for_each_entry(wl, &px->req_add, list) {
3452 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003453 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003454 ret = acl_pass(ret);
3455 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3456 ret = !ret;
3457 if (!ret)
3458 continue;
3459 }
3460
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003461 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003462 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003463 }
3464
Willy Tarreau52542592014-04-28 18:33:26 +02003465
3466 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003467 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003468 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003469 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
3470 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003471
Willy Tarreaue7dff022015-04-03 01:14:29 +02003472 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
3473 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
3474 if (!(s->flags & SF_FINST_MASK))
3475 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003476
Willy Tarreau70730dd2014-04-24 18:06:27 +02003477 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01003478 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
3479 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003480 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003481 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003482 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003483
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003484 /* check whether we have some ACLs set to redirect this request */
3485 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003486 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003487 int ret;
3488
Willy Tarreau192252e2015-04-04 01:47:55 +02003489 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003490 ret = acl_pass(ret);
3491 if (rule->cond->pol == ACL_COND_UNLESS)
3492 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003493 if (!ret)
3494 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003495 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003496 if (!http_apply_redirect_rule(rule, s, txn))
3497 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003498 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003499 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003500
Willy Tarreau2be39392010-01-03 17:24:51 +01003501 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3502 * If this happens, then the data will not come immediately, so we must
3503 * send all what we have without waiting. Note that due to the small gain
3504 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003505 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003506 * itself once used.
3507 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003508 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003509
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003510 done: /* done with this analyser, continue with next ones that the calling
3511 * points will have set, if any.
3512 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003513 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003514 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
3515 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003516 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003517
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003518 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02003519 /* Allow cookie logging
3520 */
3521 if (s->be->cookie_name || sess->fe->capture_name)
3522 manage_client_side_cookies(s, req);
3523
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003524 /* When a connection is tarpitted, we use the tarpit timeout,
3525 * which may be the same as the connect timeout if unspecified.
3526 * If unset, then set it to zero because we really want it to
3527 * eventually expire. We build the tarpit as an analyser.
3528 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003529 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003530
3531 /* wipe the request out so that we can drop the connection early
3532 * if the client closes first.
3533 */
3534 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003535
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003536 txn->status = http_err_codes[deny_status];
3537
Christopher Faulet0184ea72017-01-05 14:06:34 +01003538 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003539 req->analysers |= AN_REQ_HTTP_TARPIT;
3540 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3541 if (!req->analyse_exp)
3542 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02003543 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003544 sess->fe->fe_counters.denied_req++;
3545 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003546 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003547 if (sess->listener->counters)
3548 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003549 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003550
3551 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003552
3553 /* Allow cookie logging
3554 */
3555 if (s->be->cookie_name || sess->fe->capture_name)
3556 manage_client_side_cookies(s, req);
3557
Willy Tarreau0b748332014-04-29 00:13:29 +02003558 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003559 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003560 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003561 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02003562 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003563 sess->fe->fe_counters.denied_req++;
3564 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003565 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003566 if (sess->listener->counters)
3567 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003568 goto return_prx_cond;
3569
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003570 return_bad_req:
3571 /* We centralize bad requests processing here */
3572 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3573 /* we detected a parsing error. We want to archive this request
3574 * in the dedicated proxy area for later troubleshooting.
3575 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003576 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003577 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003578
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003579 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003580 txn->req.msg_state = HTTP_MSG_ERROR;
3581 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003582 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003583
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003584 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003585 if (sess->listener->counters)
3586 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003587
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003588 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003589 if (!(s->flags & SF_ERR_MASK))
3590 s->flags |= SF_ERR_PRXCOND;
3591 if (!(s->flags & SF_FINST_MASK))
3592 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003593
Christopher Faulet0184ea72017-01-05 14:06:34 +01003594 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003595 req->analyse_exp = TICK_ETERNITY;
3596 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003597
3598 return_prx_yield:
3599 channel_dont_connect(req);
3600 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003601}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003602
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003603/* This function performs all the processing enabled for the current request.
3604 * It returns 1 if the processing can continue on next analysers, or zero if it
3605 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003606 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003607 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003608int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003609{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003610 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003611 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003612 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02003613 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003614
Willy Tarreau655dce92009-11-08 13:10:58 +01003615 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003616 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003617 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003618 return 0;
3619 }
3620
Willy Tarreau87b09662015-04-03 00:22:06 +02003621 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 +02003622 now_ms, __FUNCTION__,
3623 s,
3624 req,
3625 req->rex, req->wex,
3626 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003627 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003628 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003629
Willy Tarreau59234e92008-11-30 23:51:27 +01003630 /*
3631 * Right now, we know that we have processed the entire headers
3632 * and that unwanted requests have been filtered out. We can do
3633 * whatever we want with the remaining request. Also, now we
3634 * may have separate values for ->fe, ->be.
3635 */
Willy Tarreau06619262006-12-17 08:37:22 +01003636
Willy Tarreau59234e92008-11-30 23:51:27 +01003637 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003638 * If HTTP PROXY is set we simply get remote server address parsing
3639 * incoming request. Note that this requires that a connection is
3640 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003641 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003642 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003643 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003644 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003645
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003646 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau973a5422015-08-05 21:47:23 +02003647 if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003648 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003649 txn->req.msg_state = HTTP_MSG_ERROR;
3650 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003651 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003652 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003653
Willy Tarreaue7dff022015-04-03 01:14:29 +02003654 if (!(s->flags & SF_ERR_MASK))
3655 s->flags |= SF_ERR_RESOURCE;
3656 if (!(s->flags & SF_FINST_MASK))
3657 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003658
3659 return 0;
3660 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003661
3662 path = http_get_path(txn);
3663 url2sa(req->buf->p + msg->sl.rq.u,
3664 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01003665 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003666 /* if the path was found, we have to remove everything between
3667 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
3668 * found, we need to replace from req->buf->p + msg->sl.rq.u for
3669 * u_l characters by a single "/".
3670 */
3671 if (path) {
3672 char *cur_ptr = req->buf->p;
3673 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3674 int delta;
3675
3676 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
3677 http_msg_move_end(&txn->req, delta);
3678 cur_end += delta;
3679 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3680 goto return_bad_req;
3681 }
3682 else {
3683 char *cur_ptr = req->buf->p;
3684 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3685 int delta;
3686
3687 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
3688 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
3689 http_msg_move_end(&txn->req, delta);
3690 cur_end += delta;
3691 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3692 goto return_bad_req;
3693 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003694 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003695
Willy Tarreau59234e92008-11-30 23:51:27 +01003696 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003697 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003698 * Note that doing so might move headers in the request, but
3699 * the fields will stay coherent and the URI will not move.
3700 * This should only be performed in the backend.
3701 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003702 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003703 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003704
William Lallemanda73203e2012-03-12 12:48:57 +01003705 /* add unique-id if "header-unique-id" is specified */
3706
Thierry Fournierf4011dd2016-03-29 17:23:51 +02003707 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003708 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
3709 goto return_bad_req;
3710 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003711 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003712 }
William Lallemanda73203e2012-03-12 12:48:57 +01003713
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003714 if (sess->fe->header_unique_id && s->unique_id) {
3715 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003716 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003717 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003718 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003719 goto return_bad_req;
3720 }
3721
Cyril Bontéb21570a2009-11-29 20:04:48 +01003722 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003723 * 9: add X-Forwarded-For if either the frontend or the backend
3724 * asks for it.
3725 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003726 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003727 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003728 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
3729 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
3730 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003731 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003732 /* The header is set to be added only if none is present
3733 * and we found it, so don't do anything.
3734 */
3735 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003736 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003737 /* Add an X-Forwarded-For header unless the source IP is
3738 * in the 'except' network range.
3739 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003740 if ((!sess->fe->except_mask.s_addr ||
3741 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
3742 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01003743 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003744 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003745 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003746 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003747 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003748 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003749
3750 /* Note: we rely on the backend to get the header name to be used for
3751 * x-forwarded-for, because the header is really meant for the backends.
3752 * However, if the backend did not specify any option, we have to rely
3753 * on the frontend's header name.
3754 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003755 if (s->be->fwdfor_hdr_len) {
3756 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003757 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003758 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003759 len = sess->fe->fwdfor_hdr_len;
3760 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003761 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003762 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 +01003763
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003764 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003765 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003766 }
3767 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003768 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003769 /* FIXME: for the sake of completeness, we should also support
3770 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003771 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003772 int len;
3773 char pn[INET6_ADDRSTRLEN];
3774 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003775 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003776 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003777
Willy Tarreau59234e92008-11-30 23:51:27 +01003778 /* Note: we rely on the backend to get the header name to be used for
3779 * x-forwarded-for, because the header is really meant for the backends.
3780 * However, if the backend did not specify any option, we have to rely
3781 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003782 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003783 if (s->be->fwdfor_hdr_len) {
3784 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003785 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003786 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003787 len = sess->fe->fwdfor_hdr_len;
3788 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003789 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003790 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003791
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003792 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003793 goto return_bad_req;
3794 }
3795 }
3796
3797 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003798 * 10: add X-Original-To if either the frontend or the backend
3799 * asks for it.
3800 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003801 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003802
3803 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003804 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003805 /* Add an X-Original-To header unless the destination IP is
3806 * in the 'except' network range.
3807 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003808 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003809
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003810 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003811 ((!sess->fe->except_mask_to.s_addr ||
3812 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
3813 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003814 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003815 (((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 +02003816 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003817 int len;
3818 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003819 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003820
3821 /* Note: we rely on the backend to get the header name to be used for
3822 * x-original-to, because the header is really meant for the backends.
3823 * However, if the backend did not specify any option, we have to rely
3824 * on the frontend's header name.
3825 */
3826 if (s->be->orgto_hdr_len) {
3827 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003828 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003829 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003830 len = sess->fe->orgto_hdr_len;
3831 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003832 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003833 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 +02003834
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003835 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003836 goto return_bad_req;
3837 }
3838 }
3839 }
3840
Willy Tarreau50fc7772012-11-11 22:19:57 +01003841 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3842 * If an "Upgrade" token is found, the header is left untouched in order not to have
3843 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3844 * "Upgrade" is present in the Connection header.
3845 */
3846 if (!(txn->flags & TX_HDR_CONN_UPG) &&
3847 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003848 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003849 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003850 unsigned int want_flags = 0;
3851
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003852 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003853 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003854 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003855 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003856 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003857 want_flags |= TX_CON_CLO_SET;
3858 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003859 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003860 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003861 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003862 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003863 want_flags |= TX_CON_KAL_SET;
3864 }
3865
3866 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003867 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003868 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003869
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003870
Willy Tarreau522d6c02009-12-06 18:49:18 +01003871 /* If we have no server assigned yet and we're balancing on url_param
3872 * with a POST request, we may be interested in checking the body for
3873 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003874 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003875 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02003876 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003877 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003878 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003879 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003880 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003881
Christopher Fauletbe821b92017-03-30 11:21:53 +02003882 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3883 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003884#ifdef TCP_QUICKACK
Christopher Fauletbe821b92017-03-30 11:21:53 +02003885 /* We expect some data from the client. Unless we know for sure
3886 * we already have a full request, we have to re-enable quick-ack
3887 * in case we previously disabled it, otherwise we might cause
3888 * the client to delay further data.
3889 */
3890 if ((sess->listener->options & LI_O_NOQUICKACK) &&
3891 cli_conn && conn_ctrl_ready(cli_conn) &&
3892 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
3893 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreau585744b2017-08-24 14:31:19 +02003894 setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003895#endif
Willy Tarreau03945942009-12-22 16:50:27 +01003896
Willy Tarreau59234e92008-11-30 23:51:27 +01003897 /*************************************************************
3898 * OK, that's finished for the headers. We have done what we *
3899 * could. Let's switch to the DATA state. *
3900 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003901 req->analyse_exp = TICK_ETERNITY;
3902 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003903
Willy Tarreau59234e92008-11-30 23:51:27 +01003904 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003905 /* OK let's go on with the BODY now */
3906 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003907
Willy Tarreau59234e92008-11-30 23:51:27 +01003908 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003909 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003910 /* we detected a parsing error. We want to archive this request
3911 * in the dedicated proxy area for later troubleshooting.
3912 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003913 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003914 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003915
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003916 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01003917 txn->req.msg_state = HTTP_MSG_ERROR;
3918 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003919 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003920 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003921
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003922 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003923 if (sess->listener->counters)
3924 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003925
Willy Tarreaue7dff022015-04-03 01:14:29 +02003926 if (!(s->flags & SF_ERR_MASK))
3927 s->flags |= SF_ERR_PRXCOND;
3928 if (!(s->flags & SF_FINST_MASK))
3929 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003930 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003931}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003932
Willy Tarreau60b85b02008-11-30 23:28:40 +01003933/* This function is an analyser which processes the HTTP tarpit. It always
3934 * returns zero, at the beginning because it prevents any other processing
3935 * from occurring, and at the end because it terminates the request.
3936 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003937int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003938{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003939 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003940
3941 /* This connection is being tarpitted. The CLIENT side has
3942 * already set the connect expiration date to the right
3943 * timeout. We just have to check that the client is still
3944 * there and that the timeout has not expired.
3945 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003946 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003947 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003948 !tick_is_expired(req->analyse_exp, now_ms))
3949 return 0;
3950
3951 /* We will set the queue timer to the time spent, just for
3952 * logging purposes. We fake a 500 server error, so that the
3953 * attacker will not suspect his connection has been tarpitted.
3954 * It will not cause trouble to the logs because we can exclude
3955 * the tarpitted connections by filtering on the 'PT' status flags.
3956 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003957 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3958
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003959 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003960 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003961
Christopher Faulet0184ea72017-01-05 14:06:34 +01003962 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003963 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003964
Willy Tarreaue7dff022015-04-03 01:14:29 +02003965 if (!(s->flags & SF_ERR_MASK))
3966 s->flags |= SF_ERR_PRXCOND;
3967 if (!(s->flags & SF_FINST_MASK))
3968 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003969 return 0;
3970}
3971
Willy Tarreau5a8f9472014-04-10 11:16:06 +02003972/* This function is an analyser which waits for the HTTP request body. It waits
3973 * for either the buffer to be full, or the full advertised contents to have
3974 * reached the buffer. It must only be called after the standard HTTP request
3975 * processing has occurred, because it expects the request to be parsed and will
3976 * look for the Expect header. It may send a 100-Continue interim response. It
3977 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
3978 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
3979 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01003980 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003981int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003982{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003983 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003984 struct http_txn *txn = s->txn;
3985 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003986
3987 /* We have to parse the HTTP request body to find any required data.
3988 * "balance url_param check_post" should have been the only way to get
3989 * into this. We were brought here after HTTP header analysis, so all
3990 * related structures are ready.
3991 */
3992
Willy Tarreau890988f2014-04-10 11:59:33 +02003993 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3994 /* This is the first call */
3995 if (msg->msg_state < HTTP_MSG_BODY)
3996 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003997
Willy Tarreau890988f2014-04-10 11:59:33 +02003998 if (msg->msg_state < HTTP_MSG_100_SENT) {
3999 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4000 * send an HTTP/1.1 100 Continue intermediate response.
4001 */
4002 if (msg->flags & HTTP_MSGF_VER_11) {
4003 struct hdr_ctx ctx;
4004 ctx.idx = 0;
4005 /* Expect is allowed in 1.1, look for it */
4006 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4007 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02004008 co_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01004009 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02004010 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004011 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004012 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004013 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004014
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004015 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004016 * req->buf->p still points to the beginning of the message. We
4017 * must save the body in msg->next because it survives buffer
4018 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004019 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004020 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004021
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004022 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004023 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4024 else
4025 msg->msg_state = HTTP_MSG_DATA;
4026 }
4027
Willy Tarreau890988f2014-04-10 11:59:33 +02004028 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4029 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004030 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004031 goto missing_data;
4032
4033 /* OK we have everything we need now */
4034 goto http_end;
4035 }
4036
4037 /* OK here we're parsing a chunked-encoded message */
4038
Willy Tarreau522d6c02009-12-06 18:49:18 +01004039 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004040 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004041 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004042 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004043 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004044 unsigned int chunk;
4045 int ret = h1_parse_chunk_size(req->buf, msg->next, req->buf->i, &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01004046
Willy Tarreau115acb92009-12-26 13:56:06 +01004047 if (!ret)
4048 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004049 else if (ret < 0) {
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004050 msg->err_pos = req->buf->i + ret;
4051 if (msg->err_pos < 0)
4052 msg->err_pos += req->buf->size;
Willy Tarreau87b09662015-04-03 00:22:06 +02004053 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004054 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004055 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02004056
4057 msg->chunk_len = chunk;
4058 msg->body_len += chunk;
4059
4060 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01004061 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004062 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01004063 }
4064
Willy Tarreaud98cf932009-12-27 22:54:55 +01004065 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02004066 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4067 * for at least a whole chunk or the whole content length bytes after
4068 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004069 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004070 if (msg->msg_state == HTTP_MSG_TRAILERS)
4071 goto http_end;
4072
Willy Tarreaue115b492015-05-01 23:05:14 +02004073 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004074 goto http_end;
4075
4076 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004077 /* we get here if we need to wait for more data. If the buffer is full,
4078 * we have the maximum we can expect.
4079 */
4080 if (buffer_full(req->buf, global.tune.maxrewrite))
4081 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004082
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004083 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004084 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004085 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004086
Willy Tarreaue7dff022015-04-03 01:14:29 +02004087 if (!(s->flags & SF_ERR_MASK))
4088 s->flags |= SF_ERR_CLITO;
4089 if (!(s->flags & SF_FINST_MASK))
4090 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004091 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004092 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004093
4094 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004095 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004096 /* Not enough data. We'll re-use the http-request
4097 * timeout here. Ideally, we should set the timeout
4098 * relative to the accept() date. We just set the
4099 * request timeout once at the beginning of the
4100 * request.
4101 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004102 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004103 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004104 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004105 return 0;
4106 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004107
4108 http_end:
4109 /* The situation will not evolve, so let's give up on the analysis. */
4110 s->logs.tv_request = now; /* update the request timer to reflect full request */
4111 req->analysers &= ~an_bit;
4112 req->analyse_exp = TICK_ETERNITY;
4113 return 1;
4114
4115 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004116 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004117 txn->req.msg_state = HTTP_MSG_ERROR;
4118 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004119 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004120
Willy Tarreaue7dff022015-04-03 01:14:29 +02004121 if (!(s->flags & SF_ERR_MASK))
4122 s->flags |= SF_ERR_PRXCOND;
4123 if (!(s->flags & SF_FINST_MASK))
4124 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004125
Willy Tarreau522d6c02009-12-06 18:49:18 +01004126 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01004127 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004128 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004129 if (sess->listener->counters)
4130 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004131 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004132}
4133
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004134/* send a server's name with an outgoing request over an established connection.
4135 * Note: this function is designed to be called once the request has been scheduled
4136 * for being forwarded. This is the reason why it rewinds the buffer before
4137 * proceeding.
4138 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004139int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004140
4141 struct hdr_ctx ctx;
4142
Mark Lamourinec2247f02012-01-04 13:02:01 -05004143 char *hdr_name = be->server_id_hdr_name;
4144 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004145 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004146 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004147 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004148
William Lallemandd9e90662012-01-30 17:27:17 +01004149 ctx.idx = 0;
4150
Willy Tarreau211cdec2014-04-17 20:18:08 +02004151 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004152 if (old_o) {
4153 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004154 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004155 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004156 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004157 }
4158
Willy Tarreau9b28e032012-10-12 23:49:43 +02004159 old_i = chn->buf->i;
4160 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 -05004161 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004162 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004163 }
4164
4165 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004166 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004167 memcpy(hdr_val, hdr_name, hdr_name_len);
4168 hdr_val += hdr_name_len;
4169 *hdr_val++ = ':';
4170 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004171 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4172 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004173
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004174 if (old_o) {
4175 /* If this was a forwarded request, we must readjust the amount of
4176 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004177 * variations. Note that the current state is >= HTTP_MSG_BODY,
4178 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004179 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004180 old_o += chn->buf->i - old_i;
4181 b_adv(chn->buf, old_o);
4182 txn->req.next -= old_o;
4183 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004184 }
4185
Mark Lamourinec2247f02012-01-04 13:02:01 -05004186 return 0;
4187}
4188
Willy Tarreau610ecce2010-01-04 21:15:02 +01004189/* Terminate current transaction and prepare a new one. This is very tricky
4190 * right now but it works.
4191 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004192void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004193{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004194 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02004195 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01004196 struct proxy *be = s->be;
Willy Tarreau323a2d92015-08-04 19:00:17 +02004197 struct connection *srv_conn;
4198 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02004199 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01004200
Willy Tarreau610ecce2010-01-04 21:15:02 +01004201 /* FIXME: We need a more portable way of releasing a backend's and a
4202 * server's connections. We need a safer way to reinitialize buffer
4203 * flags. We also need a more accurate method for computing per-request
4204 * data.
4205 */
Willy Tarreau323a2d92015-08-04 19:00:17 +02004206 srv_conn = objt_conn(s->si[1].end);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004207
Willy Tarreau4213a112013-12-15 10:25:42 +01004208 /* unless we're doing keep-alive, we want to quickly close the connection
4209 * to the server.
4210 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004211 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004212 !si_conn_ready(&s->si[1])) {
4213 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4214 si_shutr(&s->si[1]);
4215 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01004216 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004217
Willy Tarreaue7dff022015-04-03 01:14:29 +02004218 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau858b1032015-12-07 17:04:59 +01004219 be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004220 if (unlikely(s->srv_conn))
4221 sess_change_server(s, NULL);
4222 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004223
4224 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02004225 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004226
Willy Tarreaueee5b512015-04-03 23:46:31 +02004227 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004228 int n;
4229
Willy Tarreaueee5b512015-04-03 23:46:31 +02004230 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004231 if (n < 1 || n > 5)
4232 n = 0;
4233
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004234 if (fe->mode == PR_MODE_HTTP) {
4235 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004236 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02004237 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01004238 (be->mode == PR_MODE_HTTP)) {
4239 be->be_counters.p.http.rsp[n]++;
4240 be->be_counters.p.http.cum_req++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004241 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004242 }
4243
4244 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004245 s->logs.bytes_in -= s->req.buf->i;
4246 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004247
4248 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004249 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02004250 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004251 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004252 s->do_log(s);
4253 }
4254
Willy Tarreaud713bcc2014-06-25 15:36:04 +02004255 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02004256 stream_stop_content_counters(s);
4257 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004258
Willy Tarreau610ecce2010-01-04 21:15:02 +01004259 s->logs.accept_date = date; /* user-visible date for logging */
4260 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02004261 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
4262 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004263 tv_zero(&s->logs.tv_request);
4264 s->logs.t_queue = -1;
4265 s->logs.t_connect = -1;
4266 s->logs.t_data = -1;
4267 s->logs.t_close = 0;
4268 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4269 s->logs.srv_queue_size = 0; /* we will get this number soon */
4270
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004271 s->logs.bytes_in = s->req.total = s->req.buf->i;
4272 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004273
4274 if (s->pend_pos)
4275 pendconn_free(s->pend_pos);
4276
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004277 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004278 if (s->flags & SF_CURR_SESS) {
4279 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004280 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004281 }
Willy Tarreau858b1032015-12-07 17:04:59 +01004282 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004283 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004284 }
4285
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004286 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004287
Willy Tarreau4213a112013-12-15 10:25:42 +01004288 /* only release our endpoint if we don't intend to reuse the
4289 * connection.
4290 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004291 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004292 !si_conn_ready(&s->si[1])) {
4293 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02004294 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004295 }
4296
Willy Tarreau350f4872014-11-28 14:42:25 +01004297 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
4298 s->si[1].err_type = SI_ET_NONE;
4299 s->si[1].conn_retries = 0; /* used for logging too */
4300 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02004301 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 +01004302 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);
4303 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 +02004304 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
4305 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
4306 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01004307
Willy Tarreaueee5b512015-04-03 23:46:31 +02004308 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004309 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02004310 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004311
4312 if (prev_status == 401 || prev_status == 407) {
4313 /* In HTTP keep-alive mode, if we receive a 401, we still have
4314 * a chance of being able to send the visitor again to the same
4315 * server over the same connection. This is required by some
4316 * broken protocols such as NTLM, and anyway whenever there is
4317 * an opportunity for sending the challenge to the proper place,
4318 * it's better to do it (at least it helps with debugging).
4319 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004320 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02004321 if (srv_conn)
4322 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01004323 }
4324
Willy Tarreau53f96852016-02-02 18:50:47 +01004325 /* Never ever allow to reuse a connection from a non-reuse backend */
4326 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
4327 srv_conn->flags |= CO_FL_PRIVATE;
4328
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004329 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01004330 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004331
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004332 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004333 s->req.flags |= CF_NEVER_WAIT;
4334 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004335 }
4336
Willy Tarreau714ea782015-11-25 20:11:11 +01004337 /* we're removing the analysers, we MUST re-enable events detection.
4338 * We don't enable close on the response channel since it's either
4339 * already closed, or in keep-alive with an idle connection handler.
4340 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004341 channel_auto_read(&s->req);
4342 channel_auto_close(&s->req);
4343 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004344
Willy Tarreau1c59bd52015-11-02 20:20:11 +01004345 /* we're in keep-alive with an idle connection, monitor it if not already done */
4346 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02004347 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02004348 if (!srv)
4349 si_idle_conn(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01004350 else if (srv_conn->flags & CO_FL_PRIVATE)
Willy Tarreau8dff9982015-08-04 20:45:52 +02004351 si_idle_conn(&s->si[1], &srv->priv_conns);
Willy Tarreau449d74a2015-08-05 17:16:33 +02004352 else if (prev_flags & TX_NOT_FIRST)
4353 /* note: we check the request, not the connection, but
4354 * this is valid for strategies SAFE and AGGR, and in
4355 * case of ALWS, we don't care anyway.
4356 */
4357 si_idle_conn(&s->si[1], &srv->safe_conns);
Willy Tarreau8dff9982015-08-04 20:45:52 +02004358 else
4359 si_idle_conn(&s->si[1], &srv->idle_conns);
Willy Tarreau4320eaa2015-08-05 11:08:30 +02004360 }
Christopher Faulete6006242017-03-10 11:52:44 +01004361 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
4362 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004363}
4364
4365
4366/* This function updates the request state machine according to the response
4367 * state machine and buffer flags. It returns 1 if it changes anything (flag
4368 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4369 * it is only used to find when a request/response couple is complete. Both
4370 * this function and its equivalent should loop until both return zero. It
4371 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4372 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004373int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004374{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004375 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004376 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004377 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004378 unsigned int old_state = txn->req.msg_state;
4379
Christopher Faulet4be98032017-07-18 10:48:24 +02004380 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004381 return 0;
4382
4383 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004384 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004385 * We can shut the read side unless we want to abort_on_close,
4386 * or we have a POST request. The issue with POST requests is
4387 * that some browsers still send a CRLF after the request, and
4388 * this CRLF must be read so that it does not remain in the kernel
4389 * buffers, otherwise a close could cause an RST on some systems
4390 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004391 * Note that if we're using keep-alive on the client side, we'd
4392 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02004393 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01004394 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004395 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004396 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4397 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4398 !(s->be->options & PR_O_ABRT_CLOSE) &&
4399 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004400 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004401
Willy Tarreau40f151a2012-12-20 12:10:09 +01004402 /* if the server closes the connection, we want to immediately react
4403 * and close the socket to save packets and syscalls.
4404 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004405 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01004406
Willy Tarreau7f876a12015-11-18 11:59:55 +01004407 /* In any case we've finished parsing the request so we must
4408 * disable Nagle when sending data because 1) we're not going
4409 * to shut this side, and 2) the server is waiting for us to
4410 * send pending data.
4411 */
4412 chn->flags |= CF_NEVER_WAIT;
4413
Willy Tarreau610ecce2010-01-04 21:15:02 +01004414 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4415 goto wait_other_side;
4416
4417 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4418 /* The server has not finished to respond, so we
4419 * don't want to move in order not to upset it.
4420 */
4421 goto wait_other_side;
4422 }
4423
Willy Tarreau610ecce2010-01-04 21:15:02 +01004424 /* When we get here, it means that both the request and the
4425 * response have finished receiving. Depending on the connection
4426 * mode, we'll have to wait for the last bytes to leave in either
4427 * direction, and sometimes for a close to be effective.
4428 */
4429
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004430 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4431 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004432 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4433 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004434 }
4435 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4436 /* Option forceclose is set, or either side wants to close,
4437 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004438 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004439 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004440 *
4441 * However, there is an exception if the response
4442 * length is undefined. In this case, we need to wait
4443 * the close from the server. The response will be
4444 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004445 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004446 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4447 txn->rsp.msg_state != HTTP_MSG_CLOSED)
4448 goto check_channel_flags;
4449
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004450 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4451 channel_shutr_now(chn);
4452 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004453 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004454 }
4455 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004456 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4457 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004458 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004459 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4460 channel_auto_read(chn);
4461 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004462 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004463 }
4464
Christopher Faulet4be98032017-07-18 10:48:24 +02004465 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004466 }
4467
4468 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4469 http_msg_closing:
4470 /* nothing else to forward, just waiting for the output buffer
4471 * to be empty and for the shutw_now to take effect.
4472 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004473 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004474 txn->req.msg_state = HTTP_MSG_CLOSED;
4475 goto http_msg_closed;
4476 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004477 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004478 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004479 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004480 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004481 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004482 }
4483
4484 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4485 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01004486 /* see above in MSG_DONE why we only do this in these states */
4487 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4488 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4489 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004490 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004491 goto wait_other_side;
4492 }
4493
Christopher Faulet4be98032017-07-18 10:48:24 +02004494 check_channel_flags:
4495 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4496 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4497 /* if we've just closed an output, let's switch */
4498 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4499 txn->req.msg_state = HTTP_MSG_CLOSING;
4500 goto http_msg_closing;
4501 }
4502
4503
Willy Tarreau610ecce2010-01-04 21:15:02 +01004504 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004505 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004506}
4507
4508
4509/* This function updates the response state machine according to the request
4510 * state machine and buffer flags. It returns 1 if it changes anything (flag
4511 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4512 * it is only used to find when a request/response couple is complete. Both
4513 * this function and its equivalent should loop until both return zero. It
4514 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4515 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004516int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004517{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004518 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004519 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004520 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004521 unsigned int old_state = txn->rsp.msg_state;
4522
Christopher Faulet4be98032017-07-18 10:48:24 +02004523 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004524 return 0;
4525
4526 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4527 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004528 * still monitor the server connection for a possible close
4529 * while the request is being uploaded, so we don't disable
4530 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004531 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004532 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004533
4534 if (txn->req.msg_state == HTTP_MSG_ERROR)
4535 goto wait_other_side;
4536
4537 if (txn->req.msg_state < HTTP_MSG_DONE) {
4538 /* The client seems to still be sending data, probably
4539 * because we got an error response during an upload.
4540 * We have the choice of either breaking the connection
4541 * or letting it pass through. Let's do the later.
4542 */
4543 goto wait_other_side;
4544 }
4545
Willy Tarreau610ecce2010-01-04 21:15:02 +01004546 /* When we get here, it means that both the request and the
4547 * response have finished receiving. Depending on the connection
4548 * mode, we'll have to wait for the last bytes to leave in either
4549 * direction, and sometimes for a close to be effective.
4550 */
4551
4552 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4553 /* Server-close mode : shut read and wait for the request
4554 * side to close its output buffer. The caller will detect
4555 * when we're in DONE and the other is in CLOSED and will
4556 * catch that for the final cleanup.
4557 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004558 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4559 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004560 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004561 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4562 /* Option forceclose is set, or either side wants to close,
4563 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004564 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004565 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004566 *
4567 * However, there is an exception if the response length
4568 * is undefined. In this case, we switch in TUNNEL mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004569 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004570 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN)) {
4571 channel_auto_read(chn);
4572 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4573 chn->flags |= CF_NEVER_WAIT;
4574 }
4575 else if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004576 channel_shutr_now(chn);
4577 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004578 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004579 }
4580 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004581 /* The last possible modes are keep-alive and tunnel. Tunnel will
4582 * need to forward remaining data. Keep-alive will need to monitor
4583 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004584 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004585 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004586 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004587 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4588 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004589 }
4590
Christopher Faulet4be98032017-07-18 10:48:24 +02004591 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004592 }
4593
4594 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4595 http_msg_closing:
4596 /* nothing else to forward, just waiting for the output buffer
4597 * to be empty and for the shutw_now to take effect.
4598 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004599 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004600 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4601 goto http_msg_closed;
4602 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004603 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02004604 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004605 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004606 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004607 if (objt_server(s->target))
4608 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004609 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004610 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004611 }
4612
4613 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4614 http_msg_closed:
4615 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01004616 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004617 channel_auto_close(chn);
4618 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004619 goto wait_other_side;
4620 }
4621
Christopher Faulet4be98032017-07-18 10:48:24 +02004622 check_channel_flags:
4623 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4624 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4625 /* if we've just closed an output, let's switch */
4626 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4627 goto http_msg_closing;
4628 }
4629
Willy Tarreau610ecce2010-01-04 21:15:02 +01004630 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004631 /* We force the response to leave immediately if we're waiting for the
4632 * other side, since there is no pending shutdown to push it out.
4633 */
4634 if (!channel_is_empty(chn))
4635 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004636 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004637}
4638
4639
Christopher Faulet894da4c2017-07-18 11:29:07 +02004640/* Resync the request and response state machines. */
4641void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004642{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004643 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004644#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01004645 int old_req_state = txn->req.msg_state;
4646 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004647#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01004648
Willy Tarreau610ecce2010-01-04 21:15:02 +01004649 http_sync_req_state(s);
4650 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004651 if (!http_sync_res_state(s))
4652 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004653 if (!http_sync_req_state(s))
4654 break;
4655 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004656
Christopher Faulet894da4c2017-07-18 11:29:07 +02004657 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
4658 "req->analysers=0x%08x res->analysers=0x%08x\n",
4659 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02004660 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
4661 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02004662 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02004663
4664
Willy Tarreau610ecce2010-01-04 21:15:02 +01004665 /* OK, both state machines agree on a compatible state.
4666 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01004667 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4668 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02004669 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
4670 * means we must abort the request.
4671 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
4672 * corresponding channel.
4673 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
4674 * on the response with server-close mode means we've completed one
4675 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004676 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02004677 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4678 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004679 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004680 channel_auto_close(&s->req);
4681 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004682 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004683 channel_auto_close(&s->res);
4684 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004685 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004686 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4687 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004688 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004689 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004690 channel_auto_close(&s->res);
4691 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004692 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004693 channel_abort(&s->req);
4694 channel_auto_close(&s->req);
4695 channel_auto_read(&s->req);
4696 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004697 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004698 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4699 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4700 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4701 s->req.analysers &= AN_REQ_FLT_END;
4702 if (HAS_REQ_DATA_FILTERS(s))
4703 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
4704 }
4705 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4706 s->res.analysers &= AN_RES_FLT_END;
4707 if (HAS_RSP_DATA_FILTERS(s))
4708 s->res.analysers |= AN_RES_FLT_XFER_DATA;
4709 }
4710 channel_auto_close(&s->req);
4711 channel_auto_read(&s->req);
4712 channel_auto_close(&s->res);
4713 channel_auto_read(&s->res);
4714 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004715 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4716 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004717 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004718 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4719 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4720 /* server-close/keep-alive: terminate this transaction,
4721 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004722 * a fresh-new transaction, but only once we're sure there's
4723 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02004724 * another request. They must not hold any pending output data
4725 * and the response buffer must realigned
4726 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01004727 */
Christopher Faulet894da4c2017-07-18 11:29:07 +02004728 if (s->req.buf->o)
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004729 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004730 else if (s->res.buf->o)
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004731 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02004732 else {
4733 s->req.analysers = AN_REQ_FLT_END;
4734 s->res.analysers = AN_RES_FLT_END;
4735 txn->flags |= TX_WAIT_CLEANUP;
4736 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004737 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004738}
4739
Willy Tarreaud98cf932009-12-27 22:54:55 +01004740/* This function is an analyser which forwards request body (including chunk
4741 * sizes if any). It is called as soon as we must forward, even if we forward
4742 * zero byte. The only situation where it must not be called is when we're in
4743 * tunnel mode and we want to forward till the close. It's used both to forward
4744 * remaining data and to resync after end of body. It expects the msg_state to
4745 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02004746 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01004747 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004748 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004749 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004750int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004751{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004752 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004753 struct http_txn *txn = s->txn;
4754 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01004755 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004756
Christopher Faulet814d2702017-03-30 11:33:44 +02004757 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
4758 now_ms, __FUNCTION__,
4759 s,
4760 req,
4761 req->rex, req->wex,
4762 req->flags,
4763 req->buf->i,
4764 req->analysers);
4765
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004766 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4767 return 0;
4768
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004769 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004770 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004771 /* Output closed while we were sending data. We must abort and
4772 * wake the other side up.
4773 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004774 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02004775 msg->msg_state = HTTP_MSG_ERROR;
4776 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004777 return 1;
4778 }
4779
Willy Tarreaud98cf932009-12-27 22:54:55 +01004780 /* Note that we don't have to send 100-continue back because we don't
4781 * need the data to complete our job, and it's up to the server to
4782 * decide whether to return 100, 417 or anything else in return of
4783 * an "Expect: 100-continue" header.
4784 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004785 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004786 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4787 ? HTTP_MSG_CHUNK_SIZE
4788 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004789
4790 /* TODO/filters: when http-buffer-request option is set or if a
4791 * rule on url_param exists, the first chunk size could be
4792 * already parsed. In that case, msg->next is after the chunk
4793 * size (including the CRLF after the size). So this case should
4794 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004795 }
4796
Willy Tarreau7ba23542014-04-17 21:50:00 +02004797 /* Some post-connect processing might want us to refrain from starting to
4798 * forward data. Currently, the only reason for this is "balance url_param"
4799 * whichs need to parse/process the request after we've enabled forwarding.
4800 */
4801 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004802 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004803 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004804 req->flags |= CF_WAKE_CONNECT;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004805 goto missing_data_or_waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004806 }
4807 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4808 }
4809
Willy Tarreau80a92c02014-03-12 10:41:13 +01004810 /* in most states, we should abort in case of early close */
4811 channel_auto_close(req);
4812
Willy Tarreauefdf0942014-04-24 20:08:57 +02004813 if (req->to_forward) {
4814 /* We can't process the buffer's contents yet */
4815 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004816 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004817 }
4818
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004819 if (msg->msg_state < HTTP_MSG_DONE) {
4820 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4821 ? http_msg_forward_chunked_body(s, msg)
4822 : http_msg_forward_body(s, msg));
4823 if (!ret)
4824 goto missing_data_or_waiting;
4825 if (ret < 0)
4826 goto return_bad_req;
4827 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004828
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004829 /* other states, DONE...TUNNEL */
4830 /* we don't want to forward closes on DONE except in tunnel mode. */
4831 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4832 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004833
Christopher Faulet894da4c2017-07-18 11:29:07 +02004834 http_resync_states(s);
4835 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004836 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4837 if (req->flags & CF_SHUTW) {
4838 /* request errors are most likely due to the
4839 * server aborting the transfer. */
4840 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004841 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004842 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004843 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004844 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004845 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004846 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004847 }
4848
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004849 /* If "option abortonclose" is set on the backend, we want to monitor
4850 * the client's connection and forward any shutdown notification to the
4851 * server, which will decide whether to close or to go on processing the
4852 * request. We only do that in tunnel mode, and not in other modes since
4853 * it can be abused to exhaust source ports. */
4854 if (s->be->options & PR_O_ABRT_CLOSE) {
4855 channel_auto_read(req);
4856 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4857 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4858 s->si[1].flags |= SI_FL_NOLINGER;
4859 channel_auto_close(req);
4860 }
4861 else if (s->txn->meth == HTTP_METH_POST) {
4862 /* POST requests may require to read extra CRLF sent by broken
4863 * browsers and which could cause an RST to be sent upon close
4864 * on some systems (eg: Linux). */
4865 channel_auto_read(req);
4866 }
4867 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004868
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004869 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004870 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02004871 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004872 if (!(s->flags & SF_ERR_MASK))
4873 s->flags |= SF_ERR_CLICL;
4874 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004875 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004876 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004877 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004878 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004879 }
4880
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004881 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004882 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004883 if (objt_server(s->target))
4884 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004885
4886 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004887 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004888
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004889 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004890 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004891 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004892
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004893 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004894 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004895 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004896 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004897 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004898
Willy Tarreau5c620922011-05-11 19:56:11 +02004899 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004900 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004901 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004902 * modes are already handled by the stream sock layer. We must not do
4903 * this in content-length mode because it could present the MSG_MORE
4904 * flag with the last block of forwarded data, which would cause an
4905 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004906 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004907 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004908 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004909
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004910 return 0;
4911
Willy Tarreaud98cf932009-12-27 22:54:55 +01004912 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004913 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004914 if (sess->listener->counters)
4915 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004916
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004917 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004918 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004919 txn->req.msg_state = HTTP_MSG_ERROR;
4920 if (txn->status) {
4921 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004922 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004923 } else {
4924 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004925 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004926 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004927 req->analysers &= AN_REQ_FLT_END;
4928 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 +01004929
Willy Tarreaue7dff022015-04-03 01:14:29 +02004930 if (!(s->flags & SF_ERR_MASK))
4931 s->flags |= SF_ERR_PRXCOND;
4932 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004933 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004934 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004935 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004936 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004937 }
4938 return 0;
4939
4940 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004941 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004942 txn->req.msg_state = HTTP_MSG_ERROR;
4943 if (txn->status) {
4944 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004945 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004946 } else {
4947 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004948 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004949 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004950 req->analysers &= AN_REQ_FLT_END;
4951 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 +01004952
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004953 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004954 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004955 if (objt_server(s->target))
4956 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004957
Willy Tarreaue7dff022015-04-03 01:14:29 +02004958 if (!(s->flags & SF_ERR_MASK))
4959 s->flags |= SF_ERR_SRVCL;
4960 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004961 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004962 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004963 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004964 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004965 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004966 return 0;
4967}
4968
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004969/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4970 * processing can continue on next analysers, or zero if it either needs more
4971 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004972 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004973 * when it has nothing left to do, and may remove any analyser when it wants to
4974 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004975 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004976int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004977{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004978 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004979 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004980 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004981 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004982 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004983 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004984 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004985
Willy Tarreau87b09662015-04-03 00:22:06 +02004986 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 +02004987 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004988 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004989 rep,
4990 rep->rex, rep->wex,
4991 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004992 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004993 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004994
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004995 /*
4996 * Now parse the partial (or complete) lines.
4997 * We will check the response syntax, and also join multi-line
4998 * headers. An index of all the lines will be elaborated while
4999 * parsing.
5000 *
5001 * For the parsing, we use a 28 states FSM.
5002 *
5003 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005004 * rep->buf->p = beginning of response
5005 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5006 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005007 * msg->eol = end of current header or line (LF or CRLF)
5008 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005009 */
5010
Willy Tarreau628c40c2014-04-24 19:11:26 +02005011 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005012 /* There's a protected area at the end of the buffer for rewriting
5013 * purposes. We don't want to start to parse the request if the
5014 * protected area is affected, because we may have to move processed
5015 * data later, which is much more complicated.
5016 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005017 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005018 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005019 /* some data has still not left the buffer, wake us once that's done */
5020 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5021 goto abort_response;
5022 channel_dont_close(rep);
5023 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005024 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005025 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005026 }
5027
Willy Tarreau379357a2013-06-08 12:55:46 +02005028 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5029 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5030 buffer_slow_realign(rep->buf);
5031
Willy Tarreau9b28e032012-10-12 23:49:43 +02005032 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005033 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005034 }
5035
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005036 /* 1: we might have to print this header in debug mode */
5037 if (unlikely((global.mode & MODE_DEBUG) &&
5038 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005039 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005040 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005041
Willy Tarreau9b28e032012-10-12 23:49:43 +02005042 sol = rep->buf->p;
5043 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005044 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005045
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005046 sol += hdr_idx_first_pos(&txn->hdr_idx);
5047 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005048
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005049 while (cur_idx) {
5050 eol = sol + txn->hdr_idx.v[cur_idx].len;
5051 debug_hdr("srvhdr", s, sol, eol);
5052 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5053 cur_idx = txn->hdr_idx.v[cur_idx].next;
5054 }
5055 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005056
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005057 /*
5058 * Now we quickly check if we have found a full valid response.
5059 * If not so, we check the FD and buffer states before leaving.
5060 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005061 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005062 * responses are checked first.
5063 *
5064 * Depending on whether the client is still there or not, we
5065 * may send an error response back or not. Note that normally
5066 * we should only check for HTTP status there, and check I/O
5067 * errors somewhere else.
5068 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005069
Willy Tarreau655dce92009-11-08 13:10:58 +01005070 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005071 /* Invalid response */
5072 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5073 /* we detected a parsing error. We want to archive this response
5074 * in the dedicated proxy area for later troubleshooting.
5075 */
5076 hdr_response_bad:
5077 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005078 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005079
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005080 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005081 if (objt_server(s->target)) {
5082 objt_server(s->target)->counters.failed_resp++;
5083 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005084 }
Willy Tarreau64648412010-03-05 10:41:54 +01005085 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005086 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005087 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005088 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005089 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005090 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005091 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005092
Willy Tarreaue7dff022015-04-03 01:14:29 +02005093 if (!(s->flags & SF_ERR_MASK))
5094 s->flags |= SF_ERR_PRXCOND;
5095 if (!(s->flags & SF_FINST_MASK))
5096 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005097
5098 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005099 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005100
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005101 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005102 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005103 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005104 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005105 goto hdr_response_bad;
5106 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005107
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005108 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005109 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005110 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005111 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005112 else if (txn->flags & TX_NOT_FIRST)
5113 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005114
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005115 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005116 if (objt_server(s->target)) {
5117 objt_server(s->target)->counters.failed_resp++;
5118 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005119 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005120
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005121 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005122 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005123 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005124 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005125 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005126 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02005127
Willy Tarreaue7dff022015-04-03 01:14:29 +02005128 if (!(s->flags & SF_ERR_MASK))
5129 s->flags |= SF_ERR_SRVCL;
5130 if (!(s->flags & SF_FINST_MASK))
5131 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005132 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005133 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005134
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005135 /* read timeout : return a 504 to the client. */
5136 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005137 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005138 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005139
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005140 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005141 if (objt_server(s->target)) {
5142 objt_server(s->target)->counters.failed_resp++;
5143 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005144 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005145
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005146 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005147 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005148 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01005149 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005150 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005151 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02005152
Willy Tarreaue7dff022015-04-03 01:14:29 +02005153 if (!(s->flags & SF_ERR_MASK))
5154 s->flags |= SF_ERR_SRVTO;
5155 if (!(s->flags & SF_FINST_MASK))
5156 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005157 return 0;
5158 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005159
Willy Tarreauf003d372012-11-26 13:35:37 +01005160 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005161 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005162 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01005163 s->be->be_counters.cli_aborts++;
5164 if (objt_server(s->target))
5165 objt_server(s->target)->counters.cli_aborts++;
5166
Christopher Faulet0184ea72017-01-05 14:06:34 +01005167 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01005168 channel_auto_close(rep);
5169
5170 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01005171 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005172 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01005173
Willy Tarreaue7dff022015-04-03 01:14:29 +02005174 if (!(s->flags & SF_ERR_MASK))
5175 s->flags |= SF_ERR_CLICL;
5176 if (!(s->flags & SF_FINST_MASK))
5177 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01005178
Willy Tarreau87b09662015-04-03 00:22:06 +02005179 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01005180 return 0;
5181 }
5182
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005183 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005184 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005185 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005186 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005187 else if (txn->flags & TX_NOT_FIRST)
5188 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005189
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005190 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005191 if (objt_server(s->target)) {
5192 objt_server(s->target)->counters.failed_resp++;
5193 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005194 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005195
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005196 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005197 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005198 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005199 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005200 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005201 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005202
Willy Tarreaue7dff022015-04-03 01:14:29 +02005203 if (!(s->flags & SF_ERR_MASK))
5204 s->flags |= SF_ERR_SRVCL;
5205 if (!(s->flags & SF_FINST_MASK))
5206 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005207 return 0;
5208 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005209
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005210 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005211 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005212 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005213 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005214 else if (txn->flags & TX_NOT_FIRST)
5215 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005216
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005217 s->be->be_counters.failed_resp++;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005218 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005219 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005220
Willy Tarreaue7dff022015-04-03 01:14:29 +02005221 if (!(s->flags & SF_ERR_MASK))
5222 s->flags |= SF_ERR_CLICL;
5223 if (!(s->flags & SF_FINST_MASK))
5224 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005225
Willy Tarreau87b09662015-04-03 00:22:06 +02005226 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005227 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005228 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005229
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005230 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005231 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005232 return 0;
5233 }
5234
5235 /* More interesting part now : we know that we have a complete
5236 * response which at least looks like HTTP. We have an indicator
5237 * of each header's length, so we can parse them quickly.
5238 */
5239
5240 if (unlikely(msg->err_pos >= 0))
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005241 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005242
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005243 /*
5244 * 1: get the status code
5245 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005246 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005247 if (n < 1 || n > 5)
5248 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005249 /* when the client triggers a 4xx from the server, it's most often due
5250 * to a missing object or permission. These events should be tracked
5251 * because if they happen often, it may indicate a brute force or a
5252 * vulnerability scan.
5253 */
5254 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02005255 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02005256
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005257 if (objt_server(s->target))
5258 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005259
Willy Tarreau91852eb2015-05-01 13:26:00 +02005260 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
5261 * exactly one digit "." one digit. This check may be disabled using
5262 * option accept-invalid-http-response.
5263 */
5264 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
5265 if (msg->sl.st.v_l != 8) {
5266 msg->err_pos = 0;
5267 goto hdr_response_bad;
5268 }
5269
5270 if (rep->buf->p[4] != '/' ||
5271 !isdigit((unsigned char)rep->buf->p[5]) ||
5272 rep->buf->p[6] != '.' ||
5273 !isdigit((unsigned char)rep->buf->p[7])) {
5274 msg->err_pos = 4;
5275 goto hdr_response_bad;
5276 }
5277 }
5278
Willy Tarreau5b154472009-12-21 20:11:07 +01005279 /* check if the response is HTTP/1.1 or above */
5280 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005281 ((rep->buf->p[5] > '1') ||
5282 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005283 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005284
5285 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005286 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 +01005287
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005288 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005289 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005290
Willy Tarreau9b28e032012-10-12 23:49:43 +02005291 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005292
Willy Tarreau39650402010-03-15 19:44:39 +01005293 /* Adjust server's health based on status code. Note: status codes 501
5294 * and 505 are triggered on demand by client request, so we must not
5295 * count them as server failures.
5296 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005297 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005298 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005299 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005300 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005301 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005302 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005303
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005304 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02005305 * We may be facing a 100-continue response, or any other informational
5306 * 1xx response which is non-final, in which case this is not the right
5307 * response, and we're waiting for the next one. Let's allow this response
5308 * to go to the client and wait for the next one. There's an exception for
5309 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005310 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02005311 if (txn->status < 200 &&
5312 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02005313 hdr_idx_init(&txn->hdr_idx);
5314 msg->next -= channel_forward(rep, msg->next);
5315 msg->msg_state = HTTP_MSG_RPBEFORE;
5316 txn->status = 0;
5317 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01005318 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02005319 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02005320 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02005321
Willy Tarreaua14ad722017-07-07 11:36:32 +02005322 /*
5323 * 2: check for cacheability.
5324 */
5325
5326 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005327 case 200:
5328 case 203:
5329 case 206:
5330 case 300:
5331 case 301:
5332 case 410:
5333 /* RFC2616 @13.4:
5334 * "A response received with a status code of
5335 * 200, 203, 206, 300, 301 or 410 MAY be stored
5336 * by a cache (...) unless a cache-control
5337 * directive prohibits caching."
5338 *
5339 * RFC2616 @9.5: POST method :
5340 * "Responses to this method are not cacheable,
5341 * unless the response includes appropriate
5342 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005343 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005344 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005345 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005346 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5347 break;
5348 default:
5349 break;
5350 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005351
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005352 /*
5353 * 3: we may need to capture headers
5354 */
5355 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005356 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005357 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005358 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005359
Willy Tarreau557f1992015-05-01 10:05:17 +02005360 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
5361 * by RFC7230#3.3.3 :
5362 *
5363 * The length of a message body is determined by one of the following
5364 * (in order of precedence):
5365 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005366 * 1. Any 2xx (Successful) response to a CONNECT request implies that
5367 * the connection will become a tunnel immediately after the empty
5368 * line that concludes the header fields. A client MUST ignore
5369 * any Content-Length or Transfer-Encoding header fields received
5370 * in such a message. Any 101 response (Switching Protocols) is
5371 * managed in the same manner.
5372 *
5373 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02005374 * (Informational), 204 (No Content), or 304 (Not Modified) status
5375 * code is always terminated by the first empty line after the
5376 * header fields, regardless of the header fields present in the
5377 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005378 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005379 * 3. If a Transfer-Encoding header field is present and the chunked
5380 * transfer coding (Section 4.1) is the final encoding, the message
5381 * body length is determined by reading and decoding the chunked
5382 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005383 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005384 * If a Transfer-Encoding header field is present in a response and
5385 * the chunked transfer coding is not the final encoding, the
5386 * message body length is determined by reading the connection until
5387 * it is closed by the server. If a Transfer-Encoding header field
5388 * is present in a request and the chunked transfer coding is not
5389 * the final encoding, the message body length cannot be determined
5390 * reliably; the server MUST respond with the 400 (Bad Request)
5391 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005392 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005393 * If a message is received with both a Transfer-Encoding and a
5394 * Content-Length header field, the Transfer-Encoding overrides the
5395 * Content-Length. Such a message might indicate an attempt to
5396 * perform request smuggling (Section 9.5) or response splitting
5397 * (Section 9.4) and ought to be handled as an error. A sender MUST
5398 * remove the received Content-Length field prior to forwarding such
5399 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005400 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005401 * 4. If a message is received without Transfer-Encoding and with
5402 * either multiple Content-Length header fields having differing
5403 * field-values or a single Content-Length header field having an
5404 * invalid value, then the message framing is invalid and the
5405 * recipient MUST treat it as an unrecoverable error. If this is a
5406 * request message, the server MUST respond with a 400 (Bad Request)
5407 * status code and then close the connection. If this is a response
5408 * message received by a proxy, the proxy MUST close the connection
5409 * to the server, discard the received response, and send a 502 (Bad
5410 * Gateway) response to the client. If this is a response message
5411 * received by a user agent, the user agent MUST close the
5412 * connection to the server and discard the received response.
5413 *
5414 * 5. If a valid Content-Length header field is present without
5415 * Transfer-Encoding, its decimal value defines the expected message
5416 * body length in octets. If the sender closes the connection or
5417 * the recipient times out before the indicated number of octets are
5418 * received, the recipient MUST consider the message to be
5419 * incomplete and close the connection.
5420 *
5421 * 6. If this is a request message and none of the above are true, then
5422 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005423 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005424 * 7. Otherwise, this is a response message without a declared message
5425 * body length, so the message body length is determined by the
5426 * number of octets received prior to the server closing the
5427 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005428 */
5429
5430 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005431 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005432 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005433 * FIXME: should we parse anyway and return an error on chunked encoding ?
5434 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005435 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5436 txn->status == 101)) {
5437 /* Either we've established an explicit tunnel, or we're
5438 * switching the protocol. In both cases, we're very unlikely
5439 * to understand the next protocols. We have to switch to tunnel
5440 * mode, so that we transfer the request and responses then let
5441 * this protocol pass unmodified. When we later implement specific
5442 * parsers for such protocols, we'll want to check the Upgrade
5443 * header which contains information about that protocol for
5444 * responses with status 101 (eg: see RFC2817 about TLS).
5445 */
5446 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5447 msg->flags |= HTTP_MSGF_XFER_LEN;
5448 goto end;
5449 }
5450
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005451 if (txn->meth == HTTP_METH_HEAD ||
5452 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005453 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005454 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005455 goto skip_content_length;
5456 }
5457
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005458 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005459 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02005460 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005461 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005462 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5463 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005464 /* bad transfer-encoding (chunked followed by something else) */
5465 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005466 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005467 break;
5468 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005469 }
5470
Willy Tarreau1c913912015-04-30 10:57:51 +02005471 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005472 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005473 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02005474 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
5475 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5476 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005477 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005478 signed long long cl;
5479
Willy Tarreauad14f752011-09-02 20:33:27 +02005480 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005481 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005482 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005483 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005484
Willy Tarreauad14f752011-09-02 20:33:27 +02005485 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005486 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005487 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005488 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005489
Willy Tarreauad14f752011-09-02 20:33:27 +02005490 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005491 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005492 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005493 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005494
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005495 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005496 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005497 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005498 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005499
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005500 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005501 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005502 }
5503
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005504 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01005505 /* Now we have to check if we need to modify the Connection header.
5506 * This is more difficult on the response than it is on the request,
5507 * because we can have two different HTTP versions and we don't know
5508 * how the client will interprete a response. For instance, let's say
5509 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5510 * HTTP/1.1 response without any header. Maybe it will bound itself to
5511 * HTTP/1.0 because it only knows about it, and will consider the lack
5512 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5513 * the lack of header as a keep-alive. Thus we will use two flags
5514 * indicating how a request MAY be understood by the client. In case
5515 * of multiple possibilities, we'll fix the header to be explicit. If
5516 * ambiguous cases such as both close and keepalive are seen, then we
5517 * will fall back to explicit close. Note that we won't take risks with
5518 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005519 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005520 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005521 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5522 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5523 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5524 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005525 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005526
Willy Tarreau70dffda2014-01-30 03:07:23 +01005527 /* this situation happens when combining pretend-keepalive with httpclose. */
5528 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005529 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005530 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01005531 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
5532
Willy Tarreau60466522010-01-18 19:08:45 +01005533 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005534 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005535 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5536 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005537
Willy Tarreau60466522010-01-18 19:08:45 +01005538 /* now adjust header transformations depending on current state */
5539 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5540 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5541 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005542 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005543 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005544 }
Willy Tarreau60466522010-01-18 19:08:45 +01005545 else { /* SCL / KAL */
5546 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005547 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005548 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005549 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005550
Willy Tarreau60466522010-01-18 19:08:45 +01005551 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005552 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005553
Willy Tarreau60466522010-01-18 19:08:45 +01005554 /* Some keep-alive responses are converted to Server-close if
5555 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005556 */
Willy Tarreau60466522010-01-18 19:08:45 +01005557 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5558 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005559 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005560 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005561 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005562 }
5563
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005564 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02005565 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005566 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005567
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005568 /* end of job, return OK */
5569 rep->analysers &= ~an_bit;
5570 rep->analyse_exp = TICK_ETERNITY;
5571 channel_auto_close(rep);
5572 return 1;
5573
5574 abort_keep_alive:
5575 /* A keep-alive request to the server failed on a network error.
5576 * The client is required to retry. We need to close without returning
5577 * any other information so that the client retries.
5578 */
5579 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005580 rep->analysers &= AN_RES_FLT_END;
5581 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005582 channel_auto_close(rep);
5583 s->logs.logwait = 0;
5584 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005585 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01005586 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005587 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005588 return 0;
5589}
5590
5591/* This function performs all the processing enabled for the current response.
5592 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005593 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005594 * other functions. It works like process_request (see indications above).
5595 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005596int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005597{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005598 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005599 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005600 struct http_msg *msg = &txn->rsp;
5601 struct proxy *cur_proxy;
5602 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01005603 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005604
Willy Tarreau87b09662015-04-03 00:22:06 +02005605 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 +02005606 now_ms, __FUNCTION__,
5607 s,
5608 rep,
5609 rep->rex, rep->wex,
5610 rep->flags,
5611 rep->buf->i,
5612 rep->analysers);
5613
5614 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
5615 return 0;
5616
Willy Tarreau70730dd2014-04-24 18:06:27 +02005617 /* The stats applet needs to adjust the Connection header but we don't
5618 * apply any filter there.
5619 */
Willy Tarreau612adb82015-03-10 15:25:54 +01005620 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
5621 rep->analysers &= ~an_bit;
5622 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02005623 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01005624 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02005625
Willy Tarreau58975672014-04-24 21:13:57 +02005626 /*
5627 * We will have to evaluate the filters.
5628 * As opposed to version 1.2, now they will be evaluated in the
5629 * filters order and not in the header order. This means that
5630 * each filter has to be validated among all headers.
5631 *
5632 * Filters are tried with ->be first, then with ->fe if it is
5633 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005634 *
5635 * Maybe we are in resume condiion. In this case I choose the
5636 * "struct proxy" which contains the rule list matching the resume
5637 * pointer. If none of theses "struct proxy" match, I initialise
5638 * the process with the first one.
5639 *
5640 * In fact, I check only correspondance betwwen the current list
5641 * pointer and the ->fe rule list. If it doesn't match, I initialize
5642 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02005643 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005644 if (s->current_rule_list == &sess->fe->http_res_rules)
5645 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005646 else
5647 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02005648 while (1) {
5649 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005650
Willy Tarreau58975672014-04-24 21:13:57 +02005651 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02005652 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005653 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005654
Willy Tarreau51d861a2015-05-22 17:30:48 +02005655 if (ret == HTTP_RULE_RES_BADREQ)
5656 goto return_srv_prx_502;
5657
5658 if (ret == HTTP_RULE_RES_DONE) {
5659 rep->analysers &= ~an_bit;
5660 rep->analyse_exp = TICK_ETERNITY;
5661 return 1;
5662 }
5663 }
5664
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005665 /* we need to be called again. */
5666 if (ret == HTTP_RULE_RES_YIELD) {
5667 channel_dont_close(rep);
5668 return 0;
5669 }
5670
Willy Tarreau58975672014-04-24 21:13:57 +02005671 /* try headers filters */
5672 if (rule_set->rsp_exp != NULL) {
5673 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5674 return_bad_resp:
5675 if (objt_server(s->target)) {
5676 objt_server(s->target)->counters.failed_resp++;
5677 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005678 }
Willy Tarreau58975672014-04-24 21:13:57 +02005679 s->be->be_counters.failed_resp++;
5680 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005681 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02005682 txn->status = 502;
5683 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01005684 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005685 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005686 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02005687 if (!(s->flags & SF_ERR_MASK))
5688 s->flags |= SF_ERR_PRXCOND;
5689 if (!(s->flags & SF_FINST_MASK))
5690 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02005691 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005692 }
Willy Tarreau58975672014-04-24 21:13:57 +02005693 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005694
Willy Tarreau58975672014-04-24 21:13:57 +02005695 /* has the response been denied ? */
5696 if (txn->flags & TX_SVDENY) {
5697 if (objt_server(s->target))
5698 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005699
Willy Tarreau58975672014-04-24 21:13:57 +02005700 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005701 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005702 if (sess->listener->counters)
5703 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005704
Willy Tarreau58975672014-04-24 21:13:57 +02005705 goto return_srv_prx_502;
5706 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005707
Willy Tarreau58975672014-04-24 21:13:57 +02005708 /* add response headers from the rule sets in the same order */
5709 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02005710 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005711 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005712 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005713 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02005714 ret = acl_pass(ret);
5715 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5716 ret = !ret;
5717 if (!ret)
5718 continue;
5719 }
5720 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
5721 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005722 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005723
Willy Tarreau58975672014-04-24 21:13:57 +02005724 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005725 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02005726 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005727 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02005728 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005729
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005730 /* After this point, this anayzer can't return yield, so we can
5731 * remove the bit corresponding to this analyzer from the list.
5732 *
5733 * Note that the intermediate returns and goto found previously
5734 * reset the analyzers.
5735 */
5736 rep->analysers &= ~an_bit;
5737 rep->analyse_exp = TICK_ETERNITY;
5738
Willy Tarreau58975672014-04-24 21:13:57 +02005739 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005740 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005741 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005742
Willy Tarreau58975672014-04-24 21:13:57 +02005743 /*
5744 * Now check for a server cookie.
5745 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005746 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005747 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005748
Willy Tarreau58975672014-04-24 21:13:57 +02005749 /*
5750 * Check for cache-control or pragma headers if required.
5751 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005752 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 +02005753 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005754
Willy Tarreau58975672014-04-24 21:13:57 +02005755 /*
5756 * Add server cookie in the response if needed
5757 */
5758 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5759 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005760 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005761 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5762 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5763 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5764 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5765 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005766 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005767 /* the server is known, it's not the one the client requested, or the
5768 * cookie's last seen date needs to be refreshed. We have to
5769 * insert a set-cookie here, except if we want to insert only on POST
5770 * requests and this one isn't. Note that servers which don't have cookies
5771 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005772 */
Willy Tarreau58975672014-04-24 21:13:57 +02005773 if (!objt_server(s->target)->cookie) {
5774 chunk_printf(&trash,
5775 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5776 s->be->cookie_name);
5777 }
5778 else {
5779 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005780
Willy Tarreau58975672014-04-24 21:13:57 +02005781 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5782 /* emit last_date, which is mandatory */
5783 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5784 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5785 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005786
Willy Tarreau58975672014-04-24 21:13:57 +02005787 if (s->be->cookie_maxlife) {
5788 /* emit first_date, which is either the original one or
5789 * the current date.
5790 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005791 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005792 s30tob64(txn->cookie_first_date ?
5793 txn->cookie_first_date >> 2 :
5794 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005795 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005796 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005797 }
Willy Tarreau58975672014-04-24 21:13:57 +02005798 chunk_appendf(&trash, "; path=/");
5799 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005800
Willy Tarreau58975672014-04-24 21:13:57 +02005801 if (s->be->cookie_domain)
5802 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005803
Willy Tarreau58975672014-04-24 21:13:57 +02005804 if (s->be->ck_opts & PR_CK_HTTPONLY)
5805 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005806
Willy Tarreau58975672014-04-24 21:13:57 +02005807 if (s->be->ck_opts & PR_CK_SECURE)
5808 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005809
Willy Tarreau58975672014-04-24 21:13:57 +02005810 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
5811 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005812
Willy Tarreau58975672014-04-24 21:13:57 +02005813 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005814 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005815 /* the server did not change, only the date was updated */
5816 txn->flags |= TX_SCK_UPDATED;
5817 else
5818 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005819
Willy Tarreau58975672014-04-24 21:13:57 +02005820 /* Here, we will tell an eventual cache on the client side that we don't
5821 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5822 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5823 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005824 */
Willy Tarreau58975672014-04-24 21:13:57 +02005825 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005826
Willy Tarreau58975672014-04-24 21:13:57 +02005827 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005828
Willy Tarreau58975672014-04-24 21:13:57 +02005829 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5830 "Cache-control: private", 22) < 0))
5831 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005832 }
Willy Tarreau58975672014-04-24 21:13:57 +02005833 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005834
Willy Tarreau58975672014-04-24 21:13:57 +02005835 /*
5836 * Check if result will be cacheable with a cookie.
5837 * We'll block the response if security checks have caught
5838 * nasty things such as a cacheable cookie.
5839 */
5840 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5841 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5842 (s->be->options & PR_O_CHK_CACHE)) {
5843 /* we're in presence of a cacheable response containing
5844 * a set-cookie header. We'll block it as requested by
5845 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005846 */
Willy Tarreau58975672014-04-24 21:13:57 +02005847 if (objt_server(s->target))
5848 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01005849
Willy Tarreau58975672014-04-24 21:13:57 +02005850 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005851 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005852 if (sess->listener->counters)
5853 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005854
Willy Tarreau58975672014-04-24 21:13:57 +02005855 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5856 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5857 send_log(s->be, LOG_ALERT,
5858 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5859 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5860 goto return_srv_prx_502;
5861 }
Willy Tarreau03945942009-12-22 16:50:27 +01005862
Willy Tarreau70730dd2014-04-24 18:06:27 +02005863 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005864 /*
5865 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5866 * If an "Upgrade" token is found, the header is left untouched in order
5867 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005868 * if anything but "Upgrade" is present in the Connection header. We don't
5869 * want to touch any 101 response either since it's switching to another
5870 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005871 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005872 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02005873 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005874 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02005875 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
5876 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005877
Willy Tarreau58975672014-04-24 21:13:57 +02005878 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5879 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5880 /* we want a keep-alive response here. Keep-alive header
5881 * required if either side is not 1.1.
5882 */
5883 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5884 want_flags |= TX_CON_KAL_SET;
5885 }
5886 else {
5887 /* we want a close response here. Close header required if
5888 * the server is 1.1, regardless of the client.
5889 */
5890 if (msg->flags & HTTP_MSGF_VER_11)
5891 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005892 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005893
Willy Tarreau58975672014-04-24 21:13:57 +02005894 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5895 http_change_connection_header(txn, msg, want_flags);
5896 }
5897
5898 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02005899 /* Always enter in the body analyzer */
5900 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
5901 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005902
Willy Tarreau58975672014-04-24 21:13:57 +02005903 /* if the user wants to log as soon as possible, without counting
5904 * bytes from the server, then this is the right moment. We have
5905 * to temporarily assign bytes_out to log what we currently have.
5906 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005907 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005908 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
5909 s->logs.bytes_out = txn->rsp.eoh;
5910 s->do_log(s);
5911 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005912 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005913 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005914}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005915
Willy Tarreaud98cf932009-12-27 22:54:55 +01005916/* This function is an analyser which forwards response body (including chunk
5917 * sizes if any). It is called as soon as we must forward, even if we forward
5918 * zero byte. The only situation where it must not be called is when we're in
5919 * tunnel mode and we want to forward till the close. It's used both to forward
5920 * remaining data and to resync after end of body. It expects the msg_state to
5921 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005922 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02005923 *
5924 * It is capable of compressing response data both in content-length mode and
5925 * in chunked mode. The state machines follows different flows depending on
5926 * whether content-length and chunked modes are used, since there are no
5927 * trailers in content-length :
5928 *
5929 * chk-mode cl-mode
5930 * ,----- BODY -----.
5931 * / \
5932 * V size > 0 V chk-mode
5933 * .--> SIZE -------------> DATA -------------> CRLF
5934 * | | size == 0 | last byte |
5935 * | v final crlf v inspected |
5936 * | TRAILERS -----------> DONE |
5937 * | |
5938 * `----------------------------------------------'
5939 *
5940 * Compression only happens in the DATA state, and must be flushed in final
5941 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
5942 * is performed at once on final states for all bytes parsed, or when leaving
5943 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005944 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005945int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005946{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005947 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005948 struct http_txn *txn = s->txn;
5949 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01005950 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005951
Christopher Faulet814d2702017-03-30 11:33:44 +02005952 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
5953 now_ms, __FUNCTION__,
5954 s,
5955 res,
5956 res->rex, res->wex,
5957 res->flags,
5958 res->buf->i,
5959 res->analysers);
5960
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005961 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5962 return 0;
5963
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005964 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005965 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02005966 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005967 /* Output closed while we were sending data. We must abort and
5968 * wake the other side up.
5969 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005970 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005971 msg->msg_state = HTTP_MSG_ERROR;
5972 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005973 return 1;
5974 }
5975
Willy Tarreau4fe41902010-06-07 22:27:41 +02005976 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005977 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005978
Christopher Fauletd7c91962015-04-30 11:48:27 +02005979 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005980 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5981 ? HTTP_MSG_CHUNK_SIZE
5982 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005983 }
5984
Willy Tarreauefdf0942014-04-24 20:08:57 +02005985 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005986 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02005987 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005988 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005989 }
5990
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005991 if (msg->msg_state < HTTP_MSG_DONE) {
5992 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5993 ? http_msg_forward_chunked_body(s, msg)
5994 : http_msg_forward_body(s, msg));
5995 if (!ret)
5996 goto missing_data_or_waiting;
5997 if (ret < 0)
5998 goto return_bad_res;
5999 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02006000
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006001 /* other states, DONE...TUNNEL */
6002 /* for keep-alive we don't want to forward closes on DONE */
6003 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6004 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
6005 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006006
Christopher Faulet894da4c2017-07-18 11:29:07 +02006007 http_resync_states(s);
6008 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006009 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6010 if (res->flags & CF_SHUTW) {
6011 /* response errors are most likely due to the
6012 * client aborting the transfer. */
6013 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01006014 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006015 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006016 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006017 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006018 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006019 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006020 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02006021 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006022
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006023 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01006024 if (res->flags & CF_SHUTW)
6025 goto aborted_xfer;
6026
6027 /* stop waiting for data if the input is closed before the end. If the
6028 * client side was already closed, it means that the client has aborted,
6029 * so we don't want to count this as a server abort. Otherwise it's a
6030 * server abort.
6031 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02006032 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006033 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006034 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006035 /* If we have some pending data, we continue the processing */
6036 if (!buffer_pending(res->buf)) {
6037 if (!(s->flags & SF_ERR_MASK))
6038 s->flags |= SF_ERR_SRVCL;
6039 s->be->be_counters.srv_aborts++;
6040 if (objt_server(s->target))
6041 objt_server(s->target)->counters.srv_aborts++;
6042 goto return_bad_res_stats_ok;
6043 }
Willy Tarreau40dba092010-03-04 18:14:51 +01006044 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006045
Willy Tarreau40dba092010-03-04 18:14:51 +01006046 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006047 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006048 goto return_bad_res;
6049
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006050 /* When TE: chunked is used, we need to get there again to parse
6051 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02006052 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
6053 * or if there are filters registered on the stream, we don't want to
6054 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006055 */
Christopher Faulet69744d92017-03-30 10:54:35 +02006056 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006057 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006058 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6059 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006060 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006061
Willy Tarreau5c620922011-05-11 19:56:11 +02006062 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006063 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006064 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006065 * modes are already handled by the stream sock layer. We must not do
6066 * this in content-length mode because it could present the MSG_MORE
6067 * flag with the last block of forwarded data, which would cause an
6068 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006069 */
Christopher Faulet92d36382015-11-05 13:35:03 +01006070 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006071 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006072
Willy Tarreau87b09662015-04-03 00:22:06 +02006073 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006074 return 0;
6075
Willy Tarreau40dba092010-03-04 18:14:51 +01006076 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006077 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006078 if (objt_server(s->target))
6079 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006080
6081 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006082 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006083 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006084 /* 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 Tarreau3fdb3662012-11-12 00:42:33 +01006088 if (objt_server(s->target))
6089 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006090
Willy Tarreaue7dff022015-04-03 01:14:29 +02006091 if (!(s->flags & SF_ERR_MASK))
6092 s->flags |= SF_ERR_PRXCOND;
6093 if (!(s->flags & SF_FINST_MASK))
6094 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006095 return 0;
6096
6097 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006098 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006099 txn->rsp.msg_state = HTTP_MSG_ERROR;
6100 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006101 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006102 res->analysers &= AN_RES_FLT_END;
6103 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 +01006104
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006105 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006106 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006107 if (objt_server(s->target))
6108 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006109
Willy Tarreaue7dff022015-04-03 01:14:29 +02006110 if (!(s->flags & SF_ERR_MASK))
6111 s->flags |= SF_ERR_CLICL;
6112 if (!(s->flags & SF_FINST_MASK))
6113 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006114 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006115}
6116
6117
6118static inline int
6119http_msg_forward_body(struct stream *s, struct http_msg *msg)
6120{
6121 struct channel *chn = msg->chn;
6122 int ret;
6123
6124 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
6125
6126 if (msg->msg_state == HTTP_MSG_ENDING)
6127 goto ending;
6128
6129 /* Neither content-length, nor transfer-encoding was found, so we must
6130 * read the body until the server connection is closed. In that case, we
6131 * eat data as they come. Of course, this happens for response only. */
6132 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6133 unsigned long long len = (chn->buf->i - msg->next);
6134 msg->chunk_len += len;
6135 msg->body_len += len;
6136 }
Christopher Fauletda02e172015-12-04 09:25:05 +01006137 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
6138 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6139 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006140 msg->next += ret;
6141 msg->chunk_len -= ret;
6142 if (msg->chunk_len) {
6143 /* input empty or output full */
6144 if (chn->buf->i > msg->next)
6145 chn->flags |= CF_WAKE_WRITE;
6146 goto missing_data_or_waiting;
6147 }
6148
Christopher Faulet1486b0a2017-07-18 11:42:08 +02006149 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
6150 * always set for a request. */
6151 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6152 /* The server still sending data that should be filtered */
6153 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
6154 goto missing_data_or_waiting;
6155 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006156
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006157 msg->msg_state = HTTP_MSG_ENDING;
6158
6159 ending:
6160 /* we may have some pending data starting at res->buf->p such as a last
6161 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006162 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6163 /* default_ret */ msg->next,
6164 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006165 b_adv(chn->buf, ret);
6166 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006167 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6168 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006169 if (msg->next)
6170 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006171
Christopher Fauletda02e172015-12-04 09:25:05 +01006172 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
6173 /* default_ret */ 1,
6174 /* on_error */ goto error,
6175 /* on_wait */ goto waiting);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006176 msg->msg_state = HTTP_MSG_DONE;
6177 return 1;
6178
6179 missing_data_or_waiting:
6180 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006181 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6182 /* default_ret */ msg->next,
6183 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006184 b_adv(chn->buf, ret);
6185 msg->next -= ret;
6186 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6187 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006188 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006189 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006190 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006191 return 0;
6192 error:
6193 return -1;
6194}
6195
6196static inline int
6197http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
6198{
6199 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006200 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006201 int ret;
6202
6203 /* Here we have the guarantee to be in one of the following state:
6204 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
6205 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
6206
6207 switch_states:
6208 switch (msg->msg_state) {
6209 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01006210 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
6211 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6212 /* on_error */ goto error);
6213 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006214 msg->chunk_len -= ret;
6215 if (msg->chunk_len) {
6216 /* input empty or output full */
6217 if (chn->buf->i > msg->next)
6218 chn->flags |= CF_WAKE_WRITE;
6219 goto missing_data_or_waiting;
6220 }
6221
6222 /* nothing left to forward for this chunk*/
6223 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
6224 /* fall through for HTTP_MSG_CHUNK_CRLF */
6225
6226 case HTTP_MSG_CHUNK_CRLF:
6227 /* we want the CRLF after the data */
Willy Tarreaub2892562017-09-21 11:33:54 +02006228 ret = h1_skip_chunk_crlf(chn->buf, msg->next, chn->buf->i);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006229 if (ret == 0)
6230 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02006231 if (ret < 0) {
6232 msg->err_pos = chn->buf->i + ret;
6233 if (msg->err_pos < 0)
6234 msg->err_pos += chn->buf->size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006235 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02006236 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01006237 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006238 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6239 /* fall through for HTTP_MSG_CHUNK_SIZE */
6240
6241 case HTTP_MSG_CHUNK_SIZE:
6242 /* read the chunk size and assign it to ->chunk_len,
6243 * then set ->next to point to the body and switch to
6244 * DATA or TRAILERS state.
6245 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006246 ret = h1_parse_chunk_size(chn->buf, msg->next, chn->buf->i, &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006247 if (ret == 0)
6248 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006249 if (ret < 0) {
6250 msg->err_pos = chn->buf->i + ret;
6251 if (msg->err_pos < 0)
6252 msg->err_pos += chn->buf->size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006253 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006254 }
6255
6256 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01006257 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006258 msg->chunk_len = chunk;
6259 msg->body_len += chunk;
6260
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006261 if (msg->chunk_len) {
6262 msg->msg_state = HTTP_MSG_DATA;
6263 goto switch_states;
6264 }
6265 msg->msg_state = HTTP_MSG_TRAILERS;
6266 /* fall through for HTTP_MSG_TRAILERS */
6267
6268 case HTTP_MSG_TRAILERS:
6269 ret = http_forward_trailers(msg);
6270 if (ret < 0)
6271 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01006272 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
6273 /* default_ret */ 1,
6274 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006275 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006276 if (!ret)
6277 goto missing_data_or_waiting;
6278 break;
6279
6280 case HTTP_MSG_ENDING:
6281 goto ending;
6282
6283 default:
6284 /* This should no happen in this function */
6285 goto error;
6286 }
6287
6288 msg->msg_state = HTTP_MSG_ENDING;
6289 ending:
6290 /* we may have some pending data starting at res->buf->p such as a last
6291 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006292 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006293 /* default_ret */ msg->next,
6294 /* on_error */ goto error);
6295 b_adv(chn->buf, ret);
6296 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006297 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6298 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006299 if (msg->next)
6300 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006301
Christopher Fauletda02e172015-12-04 09:25:05 +01006302 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006303 /* default_ret */ 1,
6304 /* on_error */ goto error,
6305 /* on_wait */ goto waiting);
6306 msg->msg_state = HTTP_MSG_DONE;
6307 return 1;
6308
6309 missing_data_or_waiting:
6310 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006311 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006312 /* default_ret */ msg->next,
6313 /* on_error */ goto error);
6314 b_adv(chn->buf, ret);
6315 msg->next -= ret;
6316 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6317 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006318 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006319 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006320 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006321 return 0;
6322
6323 chunk_parsing_error:
6324 if (msg->err_pos >= 0) {
6325 if (chn->flags & CF_ISRESP)
6326 http_capture_bad_message(&s->be->invalid_rep, s, msg,
6327 msg->msg_state, strm_fe(s));
6328 else
6329 http_capture_bad_message(&strm_fe(s)->invalid_req, s,
6330 msg, msg->msg_state, s->be);
6331 }
6332 error:
6333 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006334}
6335
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006336
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006337/* Iterate the same filter through all request headers.
6338 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006339 * Since it can manage the switch to another backend, it updates the per-proxy
6340 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006341 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006342int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006343{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006344 char *cur_ptr, *cur_end, *cur_next;
6345 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006346 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006347 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006348 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006349
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006350 last_hdr = 0;
6351
Willy Tarreau9b28e032012-10-12 23:49:43 +02006352 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006353 old_idx = 0;
6354
6355 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006356 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006357 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006358 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006359 (exp->action == ACT_ALLOW ||
6360 exp->action == ACT_DENY ||
6361 exp->action == ACT_TARPIT))
6362 return 0;
6363
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006364 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006365 if (!cur_idx)
6366 break;
6367
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006368 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006369 cur_ptr = cur_next;
6370 cur_end = cur_ptr + cur_hdr->len;
6371 cur_next = cur_end + cur_hdr->cr + 1;
6372
6373 /* Now we have one header between cur_ptr and cur_end,
6374 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006375 */
6376
Willy Tarreau15a53a42015-01-21 13:39:42 +01006377 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006378 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006379 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006380 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006381 last_hdr = 1;
6382 break;
6383
6384 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006385 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006386 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006387 break;
6388
6389 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006390 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006391 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006392 break;
6393
6394 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006395 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6396 if (trash.len < 0)
6397 return -1;
6398
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006399 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006400 /* FIXME: if the user adds a newline in the replacement, the
6401 * index will not be recalculated for now, and the new line
6402 * will not be counted as a new header.
6403 */
6404
6405 cur_end += delta;
6406 cur_next += delta;
6407 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006408 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006409 break;
6410
6411 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006412 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006413 cur_next += delta;
6414
Willy Tarreaufa355d42009-11-29 18:12:29 +01006415 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006416 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6417 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006418 cur_hdr->len = 0;
6419 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006420 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006421 break;
6422
6423 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006424 }
6425
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006426 /* keep the link from this header to next one in case of later
6427 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006428 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006429 old_idx = cur_idx;
6430 }
6431 return 0;
6432}
6433
6434
6435/* Apply the filter to the request line.
6436 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6437 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006438 * Since it can manage the switch to another backend, it updates the per-proxy
6439 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006440 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006441int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006442{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006443 char *cur_ptr, *cur_end;
6444 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006445 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006446 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006447
Willy Tarreau3d300592007-03-18 18:34:41 +01006448 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006449 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006450 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006451 (exp->action == ACT_ALLOW ||
6452 exp->action == ACT_DENY ||
6453 exp->action == ACT_TARPIT))
6454 return 0;
6455 else if (exp->action == ACT_REMOVE)
6456 return 0;
6457
6458 done = 0;
6459
Willy Tarreau9b28e032012-10-12 23:49:43 +02006460 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006461 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006462
6463 /* Now we have the request line between cur_ptr and cur_end */
6464
Willy Tarreau15a53a42015-01-21 13:39:42 +01006465 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006466 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006467 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006468 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006469 done = 1;
6470 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006471
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006472 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006473 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006474 done = 1;
6475 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006476
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006477 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006478 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006479 done = 1;
6480 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006481
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006482 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006483 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6484 if (trash.len < 0)
6485 return -1;
6486
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006487 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006488 /* FIXME: if the user adds a newline in the replacement, the
6489 * index will not be recalculated for now, and the new line
6490 * will not be counted as a new header.
6491 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006492
Willy Tarreaufa355d42009-11-29 18:12:29 +01006493 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006494 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006495 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006496 HTTP_MSG_RQMETH,
6497 cur_ptr, cur_end + 1,
6498 NULL, NULL);
6499 if (unlikely(!cur_end))
6500 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006501
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006502 /* we have a full request and we know that we have either a CR
6503 * or an LF at <ptr>.
6504 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006505 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6506 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006507 /* there is no point trying this regex on headers */
6508 return 1;
6509 }
6510 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006511 return done;
6512}
Willy Tarreau97de6242006-12-27 17:18:38 +01006513
Willy Tarreau58f10d72006-12-04 02:26:12 +01006514
Willy Tarreau58f10d72006-12-04 02:26:12 +01006515
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006516/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006517 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006518 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006519 * unparsable request. Since it can manage the switch to another backend, it
6520 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006521 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006522int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006523{
Willy Tarreau192252e2015-04-04 01:47:55 +02006524 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006525 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006526 struct hdr_exp *exp;
6527
6528 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006529 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006530
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006531 /*
6532 * The interleaving of transformations and verdicts
6533 * makes it difficult to decide to continue or stop
6534 * the evaluation.
6535 */
6536
Willy Tarreau6c123b12010-01-28 20:22:06 +01006537 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6538 break;
6539
Willy Tarreau3d300592007-03-18 18:34:41 +01006540 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006541 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006542 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006543 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006544
6545 /* if this filter had a condition, evaluate it now and skip to
6546 * next filter if the condition does not match.
6547 */
6548 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006549 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006550 ret = acl_pass(ret);
6551 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6552 ret = !ret;
6553
6554 if (!ret)
6555 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006556 }
6557
6558 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006559 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006560 if (unlikely(ret < 0))
6561 return -1;
6562
6563 if (likely(ret == 0)) {
6564 /* The filter did not match the request, it can be
6565 * iterated through all headers.
6566 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01006567 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
6568 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006569 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006570 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006571 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006572}
6573
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006574
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006575/* Find the end of a cookie value contained between <s> and <e>. It works the
6576 * same way as with headers above except that the semi-colon also ends a token.
6577 * See RFC2965 for more information. Note that it requires a valid header to
6578 * return a valid result.
6579 */
6580char *find_cookie_value_end(char *s, const char *e)
6581{
6582 int quoted, qdpair;
6583
6584 quoted = qdpair = 0;
6585 for (; s < e; s++) {
6586 if (qdpair) qdpair = 0;
6587 else if (quoted) {
6588 if (*s == '\\') qdpair = 1;
6589 else if (*s == '"') quoted = 0;
6590 }
6591 else if (*s == '"') quoted = 1;
6592 else if (*s == ',' || *s == ';') return s;
6593 }
6594 return s;
6595}
6596
6597/* Delete a value in a header between delimiters <from> and <next> in buffer
6598 * <buf>. The number of characters displaced is returned, and the pointer to
6599 * the first delimiter is updated if required. The function tries as much as
6600 * possible to respect the following principles :
6601 * - replace <from> delimiter by the <next> one unless <from> points to a
6602 * colon, in which case <next> is simply removed
6603 * - set exactly one space character after the new first delimiter, unless
6604 * there are not enough characters in the block being moved to do so.
6605 * - remove unneeded spaces before the previous delimiter and after the new
6606 * one.
6607 *
6608 * It is the caller's responsibility to ensure that :
6609 * - <from> points to a valid delimiter or the colon ;
6610 * - <next> points to a valid delimiter or the final CR/LF ;
6611 * - there are non-space chars before <from> ;
6612 * - there is a CR/LF at or after <next>.
6613 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006614int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006615{
6616 char *prev = *from;
6617
6618 if (*prev == ':') {
6619 /* We're removing the first value, preserve the colon and add a
6620 * space if possible.
6621 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006622 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006623 next++;
6624 prev++;
6625 if (prev < next)
6626 *prev++ = ' ';
6627
Willy Tarreau2235b262016-11-05 15:50:20 +01006628 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006629 next++;
6630 } else {
6631 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01006632 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006633 prev--;
6634 *from = prev;
6635
6636 /* copy the delimiter and if possible a space if we're
6637 * not at the end of the line.
6638 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006639 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006640 *prev++ = *next++;
6641 if (prev + 1 < next)
6642 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01006643 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006644 next++;
6645 }
6646 }
6647 return buffer_replace2(buf, prev, next, NULL, 0);
6648}
6649
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006650/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006651 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006652 * desirable to call it only when needed. This code is quite complex because
6653 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6654 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006655 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006656void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006657{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006658 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006659 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006660 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006661 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006662 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6663 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006664
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006665 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006666 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006667 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006668
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006669 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006670 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006671 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006672
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006673 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006674 hdr_beg = hdr_next;
6675 hdr_end = hdr_beg + cur_hdr->len;
6676 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006677
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006678 /* We have one full header between hdr_beg and hdr_end, and the
6679 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006680 * "Cookie:" headers.
6681 */
6682
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006683 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006684 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006685 old_idx = cur_idx;
6686 continue;
6687 }
6688
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006689 del_from = NULL; /* nothing to be deleted */
6690 preserve_hdr = 0; /* assume we may kill the whole header */
6691
Willy Tarreau58f10d72006-12-04 02:26:12 +01006692 /* Now look for cookies. Conforming to RFC2109, we have to support
6693 * attributes whose name begin with a '$', and associate them with
6694 * the right cookie, if we want to delete this cookie.
6695 * So there are 3 cases for each cookie read :
6696 * 1) it's a special attribute, beginning with a '$' : ignore it.
6697 * 2) it's a server id cookie that we *MAY* want to delete : save
6698 * some pointers on it (last semi-colon, beginning of cookie...)
6699 * 3) it's an application cookie : we *MAY* have to delete a previous
6700 * "special" cookie.
6701 * At the end of loop, if a "special" cookie remains, we may have to
6702 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006703 * *MUST* delete it.
6704 *
6705 * Note: RFC2965 is unclear about the processing of spaces around
6706 * the equal sign in the ATTR=VALUE form. A careful inspection of
6707 * the RFC explicitly allows spaces before it, and not within the
6708 * tokens (attrs or values). An inspection of RFC2109 allows that
6709 * too but section 10.1.3 lets one think that spaces may be allowed
6710 * after the equal sign too, resulting in some (rare) buggy
6711 * implementations trying to do that. So let's do what servers do.
6712 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6713 * allowed quoted strings in values, with any possible character
6714 * after a backslash, including control chars and delimitors, which
6715 * causes parsing to become ambiguous. Browsers also allow spaces
6716 * within values even without quotes.
6717 *
6718 * We have to keep multiple pointers in order to support cookie
6719 * removal at the beginning, middle or end of header without
6720 * corrupting the header. All of these headers are valid :
6721 *
6722 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6723 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6724 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6725 * | | | | | | | | |
6726 * | | | | | | | | hdr_end <--+
6727 * | | | | | | | +--> next
6728 * | | | | | | +----> val_end
6729 * | | | | | +-----------> val_beg
6730 * | | | | +--------------> equal
6731 * | | | +----------------> att_end
6732 * | | +---------------------> att_beg
6733 * | +--------------------------> prev
6734 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006735 */
6736
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006737 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6738 /* Iterate through all cookies on this line */
6739
6740 /* find att_beg */
6741 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006742 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006743 att_beg++;
6744
6745 /* find att_end : this is the first character after the last non
6746 * space before the equal. It may be equal to hdr_end.
6747 */
6748 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006749
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006750 while (equal < hdr_end) {
6751 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006752 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006753 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006754 continue;
6755 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006756 }
6757
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006758 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6759 * is between <att_beg> and <equal>, both may be identical.
6760 */
6761
6762 /* look for end of cookie if there is an equal sign */
6763 if (equal < hdr_end && *equal == '=') {
6764 /* look for the beginning of the value */
6765 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006766 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006767 val_beg++;
6768
6769 /* find the end of the value, respecting quotes */
6770 next = find_cookie_value_end(val_beg, hdr_end);
6771
6772 /* make val_end point to the first white space or delimitor after the value */
6773 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006774 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006775 val_end--;
6776 } else {
6777 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006778 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006779
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006780 /* We have nothing to do with attributes beginning with '$'. However,
6781 * they will automatically be removed if a header before them is removed,
6782 * since they're supposed to be linked together.
6783 */
6784 if (*att_beg == '$')
6785 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006786
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006787 /* Ignore cookies with no equal sign */
6788 if (equal == next) {
6789 /* This is not our cookie, so we must preserve it. But if we already
6790 * scheduled another cookie for removal, we cannot remove the
6791 * complete header, but we can remove the previous block itself.
6792 */
6793 preserve_hdr = 1;
6794 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006795 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006796 val_end += delta;
6797 next += delta;
6798 hdr_end += delta;
6799 hdr_next += delta;
6800 cur_hdr->len += delta;
6801 http_msg_move_end(&txn->req, delta);
6802 prev = del_from;
6803 del_from = NULL;
6804 }
6805 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006806 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006807
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006808 /* if there are spaces around the equal sign, we need to
6809 * strip them otherwise we'll get trouble for cookie captures,
6810 * or even for rewrites. Since this happens extremely rarely,
6811 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006812 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006813 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6814 int stripped_before = 0;
6815 int stripped_after = 0;
6816
6817 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006818 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006819 equal += stripped_before;
6820 val_beg += stripped_before;
6821 }
6822
6823 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006824 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006825 val_beg += stripped_after;
6826 stripped_before += stripped_after;
6827 }
6828
6829 val_end += stripped_before;
6830 next += stripped_before;
6831 hdr_end += stripped_before;
6832 hdr_next += stripped_before;
6833 cur_hdr->len += stripped_before;
6834 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006835 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006836 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006837
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006838 /* First, let's see if we want to capture this cookie. We check
6839 * that we don't already have a client side cookie, because we
6840 * can only capture one. Also as an optimisation, we ignore
6841 * cookies shorter than the declared name.
6842 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006843 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6844 (val_end - att_beg >= sess->fe->capture_namelen) &&
6845 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006846 int log_len = val_end - att_beg;
6847
6848 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6849 Alert("HTTP logging : out of memory.\n");
6850 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006851 if (log_len > sess->fe->capture_len)
6852 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006853 memcpy(txn->cli_cookie, att_beg, log_len);
6854 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006855 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006856 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006857
Willy Tarreaubca99692010-10-06 19:25:55 +02006858 /* Persistence cookies in passive, rewrite or insert mode have the
6859 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006860 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006861 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006862 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006863 * For cookies in prefix mode, the form is :
6864 *
6865 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006866 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006867 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6868 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6869 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006870 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006871
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006872 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6873 * have the server ID between val_beg and delim, and the original cookie between
6874 * delim+1 and val_end. Otherwise, delim==val_end :
6875 *
6876 * Cookie: NAME=SRV; # in all but prefix modes
6877 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6878 * | || || | |+-> next
6879 * | || || | +--> val_end
6880 * | || || +---------> delim
6881 * | || |+------------> val_beg
6882 * | || +-------------> att_end = equal
6883 * | |+-----------------> att_beg
6884 * | +------------------> prev
6885 * +-------------------------> hdr_beg
6886 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006887
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006888 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006889 for (delim = val_beg; delim < val_end; delim++)
6890 if (*delim == COOKIE_DELIM)
6891 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006892 } else {
6893 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006894 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006895 /* Now check if the cookie contains a date field, which would
6896 * appear after a vertical bar ('|') just after the server name
6897 * and before the delimiter.
6898 */
6899 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6900 if (vbar1) {
6901 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006902 * right is the last seen date. It is a base64 encoded
6903 * 30-bit value representing the UNIX date since the
6904 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006905 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006906 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006907 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006908 if (val_end - vbar1 >= 5) {
6909 val = b64tos30(vbar1);
6910 if (val > 0)
6911 txn->cookie_last_date = val << 2;
6912 }
6913 /* look for a second vertical bar */
6914 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6915 if (vbar1 && (val_end - vbar1 > 5)) {
6916 val = b64tos30(vbar1 + 1);
6917 if (val > 0)
6918 txn->cookie_first_date = val << 2;
6919 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006920 }
6921 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006922
Willy Tarreauf64d1412010-10-07 20:06:11 +02006923 /* if the cookie has an expiration date and the proxy wants to check
6924 * it, then we do that now. We first check if the cookie is too old,
6925 * then only if it has expired. We detect strict overflow because the
6926 * time resolution here is not great (4 seconds). Cookies with dates
6927 * in the future are ignored if their offset is beyond one day. This
6928 * allows an admin to fix timezone issues without expiring everyone
6929 * and at the same time avoids keeping unwanted side effects for too
6930 * long.
6931 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006932 if (txn->cookie_first_date && s->be->cookie_maxlife &&
6933 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006934 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006935 txn->flags &= ~TX_CK_MASK;
6936 txn->flags |= TX_CK_OLD;
6937 delim = val_beg; // let's pretend we have not found the cookie
6938 txn->cookie_first_date = 0;
6939 txn->cookie_last_date = 0;
6940 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006941 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
6942 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006943 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006944 txn->flags &= ~TX_CK_MASK;
6945 txn->flags |= TX_CK_EXPIRED;
6946 delim = val_beg; // let's pretend we have not found the cookie
6947 txn->cookie_first_date = 0;
6948 txn->cookie_last_date = 0;
6949 }
6950
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006951 /* Here, we'll look for the first running server which supports the cookie.
6952 * This allows to share a same cookie between several servers, for example
6953 * to dedicate backup servers to specific servers only.
6954 * However, to prevent clients from sticking to cookie-less backup server
6955 * when they have incidentely learned an empty cookie, we simply ignore
6956 * empty cookies and mark them as invalid.
6957 * The same behaviour is applied when persistence must be ignored.
6958 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006959 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006960 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006961
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006962 while (srv) {
6963 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6964 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02006965 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006966 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006967 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006968 /* we found the server and we can use it */
6969 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02006970 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006971 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006972 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006973 break;
6974 } else {
6975 /* we found a server, but it's down,
6976 * mark it as such and go on in case
6977 * another one is available.
6978 */
6979 txn->flags &= ~TX_CK_MASK;
6980 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006981 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006982 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006983 srv = srv->next;
6984 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006985
Willy Tarreauf64d1412010-10-07 20:06:11 +02006986 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006987 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006988 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006989 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006990 txn->flags |= TX_CK_UNUSED;
6991 else
6992 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006993 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006994
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006995 /* depending on the cookie mode, we may have to either :
6996 * - delete the complete cookie if we're in insert+indirect mode, so that
6997 * the server never sees it ;
6998 * - remove the server id from the cookie value, and tag the cookie as an
6999 * application cookie so that it does not get accidentely removed later,
7000 * if we're in cookie prefix mode
7001 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007002 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007003 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007004
Willy Tarreau9b28e032012-10-12 23:49:43 +02007005 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007006 val_end += delta;
7007 next += delta;
7008 hdr_end += delta;
7009 hdr_next += delta;
7010 cur_hdr->len += delta;
7011 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007012
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007013 del_from = NULL;
7014 preserve_hdr = 1; /* we want to keep this cookie */
7015 }
7016 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007017 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007018 del_from = prev;
7019 }
7020 } else {
7021 /* This is not our cookie, so we must preserve it. But if we already
7022 * scheduled another cookie for removal, we cannot remove the
7023 * complete header, but we can remove the previous block itself.
7024 */
7025 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007026
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007027 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007028 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007029 if (att_beg >= del_from)
7030 att_beg += delta;
7031 if (att_end >= del_from)
7032 att_end += delta;
7033 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007034 val_end += delta;
7035 next += delta;
7036 hdr_end += delta;
7037 hdr_next += delta;
7038 cur_hdr->len += delta;
7039 http_msg_move_end(&txn->req, delta);
7040 prev = del_from;
7041 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007042 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007043 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007044
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007045 /* continue with next cookie on this header line */
7046 att_beg = next;
7047 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007048
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007049 /* There are no more cookies on this line.
7050 * We may still have one (or several) marked for deletion at the
7051 * end of the line. We must do this now in two ways :
7052 * - if some cookies must be preserved, we only delete from the
7053 * mark to the end of line ;
7054 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007055 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007056 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007057 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007058 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007059 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007060 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007061 cur_hdr->len += delta;
7062 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007063 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007064
7065 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007066 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7067 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007068 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007069 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007070 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007071 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007072 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007073 }
7074
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007075 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007076 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007077 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007078}
7079
7080
Willy Tarreaua15645d2007-03-18 16:22:39 +01007081/* Iterate the same filter through all response headers contained in <rtr>.
7082 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7083 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007084int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007085{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007086 char *cur_ptr, *cur_end, *cur_next;
7087 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007088 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007089 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007090 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007091
7092 last_hdr = 0;
7093
Willy Tarreau9b28e032012-10-12 23:49:43 +02007094 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007095 old_idx = 0;
7096
7097 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007098 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007099 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007100 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007101 (exp->action == ACT_ALLOW ||
7102 exp->action == ACT_DENY))
7103 return 0;
7104
7105 cur_idx = txn->hdr_idx.v[old_idx].next;
7106 if (!cur_idx)
7107 break;
7108
7109 cur_hdr = &txn->hdr_idx.v[cur_idx];
7110 cur_ptr = cur_next;
7111 cur_end = cur_ptr + cur_hdr->len;
7112 cur_next = cur_end + cur_hdr->cr + 1;
7113
7114 /* Now we have one header between cur_ptr and cur_end,
7115 * and the next header starts at cur_next.
7116 */
7117
Willy Tarreau15a53a42015-01-21 13:39:42 +01007118 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007119 switch (exp->action) {
7120 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007121 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007122 last_hdr = 1;
7123 break;
7124
7125 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007126 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007127 last_hdr = 1;
7128 break;
7129
7130 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007131 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7132 if (trash.len < 0)
7133 return -1;
7134
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007135 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007136 /* FIXME: if the user adds a newline in the replacement, the
7137 * index will not be recalculated for now, and the new line
7138 * will not be counted as a new header.
7139 */
7140
7141 cur_end += delta;
7142 cur_next += delta;
7143 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007144 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007145 break;
7146
7147 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007148 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007149 cur_next += delta;
7150
Willy Tarreaufa355d42009-11-29 18:12:29 +01007151 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007152 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7153 txn->hdr_idx.used--;
7154 cur_hdr->len = 0;
7155 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007156 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007157 break;
7158
7159 }
7160 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007161
7162 /* keep the link from this header to next one in case of later
7163 * removal of next header.
7164 */
7165 old_idx = cur_idx;
7166 }
7167 return 0;
7168}
7169
7170
7171/* Apply the filter to the status line in the response buffer <rtr>.
7172 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7173 * or -1 if a replacement resulted in an invalid status line.
7174 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007175int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007176{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007177 char *cur_ptr, *cur_end;
7178 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007179 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007180 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007181
7182
Willy Tarreau3d300592007-03-18 18:34:41 +01007183 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007184 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007185 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007186 (exp->action == ACT_ALLOW ||
7187 exp->action == ACT_DENY))
7188 return 0;
7189 else if (exp->action == ACT_REMOVE)
7190 return 0;
7191
7192 done = 0;
7193
Willy Tarreau9b28e032012-10-12 23:49:43 +02007194 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007195 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007196
7197 /* Now we have the status line between cur_ptr and cur_end */
7198
Willy Tarreau15a53a42015-01-21 13:39:42 +01007199 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007200 switch (exp->action) {
7201 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007202 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007203 done = 1;
7204 break;
7205
7206 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007207 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007208 done = 1;
7209 break;
7210
7211 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007212 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7213 if (trash.len < 0)
7214 return -1;
7215
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007216 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007217 /* FIXME: if the user adds a newline in the replacement, the
7218 * index will not be recalculated for now, and the new line
7219 * will not be counted as a new header.
7220 */
7221
Willy Tarreaufa355d42009-11-29 18:12:29 +01007222 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007223 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007224 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007225 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007226 cur_ptr, cur_end + 1,
7227 NULL, NULL);
7228 if (unlikely(!cur_end))
7229 return -1;
7230
7231 /* we have a full respnse and we know that we have either a CR
7232 * or an LF at <ptr>.
7233 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007234 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007235 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007236 /* there is no point trying this regex on headers */
7237 return 1;
7238 }
7239 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007240 return done;
7241}
7242
7243
7244
7245/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007246 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007247 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7248 * unparsable response.
7249 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007250int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007251{
Willy Tarreau192252e2015-04-04 01:47:55 +02007252 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007253 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007254 struct hdr_exp *exp;
7255
7256 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007257 int ret;
7258
7259 /*
7260 * The interleaving of transformations and verdicts
7261 * makes it difficult to decide to continue or stop
7262 * the evaluation.
7263 */
7264
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007265 if (txn->flags & TX_SVDENY)
7266 break;
7267
Willy Tarreau3d300592007-03-18 18:34:41 +01007268 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007269 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7270 exp->action == ACT_PASS)) {
7271 exp = exp->next;
7272 continue;
7273 }
7274
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007275 /* if this filter had a condition, evaluate it now and skip to
7276 * next filter if the condition does not match.
7277 */
7278 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007279 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007280 ret = acl_pass(ret);
7281 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7282 ret = !ret;
7283 if (!ret)
7284 continue;
7285 }
7286
Willy Tarreaua15645d2007-03-18 16:22:39 +01007287 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007288 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007289 if (unlikely(ret < 0))
7290 return -1;
7291
7292 if (likely(ret == 0)) {
7293 /* The filter did not match the response, it can be
7294 * iterated through all headers.
7295 */
Sasha Pachevc6002042014-05-26 12:33:48 -06007296 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
7297 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007298 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007299 }
7300 return 0;
7301}
7302
7303
Willy Tarreaua15645d2007-03-18 16:22:39 +01007304/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007305 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007306 * desirable to call it only when needed. This function is also used when we
7307 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007308 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007309void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007310{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007311 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007312 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01007313 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007314 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007315 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007316 char *hdr_beg, *hdr_end, *hdr_next;
7317 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007318
Willy Tarreaua15645d2007-03-18 16:22:39 +01007319 /* Iterate through the headers.
7320 * we start with the start line.
7321 */
7322 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007323 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007324
7325 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7326 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007327 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007328
7329 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007330 hdr_beg = hdr_next;
7331 hdr_end = hdr_beg + cur_hdr->len;
7332 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007333
Willy Tarreau24581ba2010-08-31 22:39:35 +02007334 /* We have one full header between hdr_beg and hdr_end, and the
7335 * next header starts at hdr_next. We're only interested in
7336 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007337 */
7338
Willy Tarreau24581ba2010-08-31 22:39:35 +02007339 is_cookie2 = 0;
7340 prev = hdr_beg + 10;
7341 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007342 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007343 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7344 if (!val) {
7345 old_idx = cur_idx;
7346 continue;
7347 }
7348 is_cookie2 = 1;
7349 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007350 }
7351
Willy Tarreau24581ba2010-08-31 22:39:35 +02007352 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7353 * <prev> points to the colon.
7354 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007355 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007356
Willy Tarreau24581ba2010-08-31 22:39:35 +02007357 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7358 * check-cache is enabled) and we are not interested in checking
7359 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007360 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02007361 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007362 return;
7363
Willy Tarreau24581ba2010-08-31 22:39:35 +02007364 /* OK so now we know we have to process this response cookie.
7365 * The format of the Set-Cookie header is slightly different
7366 * from the format of the Cookie header in that it does not
7367 * support the comma as a cookie delimiter (thus the header
7368 * cannot be folded) because the Expires attribute described in
7369 * the original Netscape's spec may contain an unquoted date
7370 * with a comma inside. We have to live with this because
7371 * many browsers don't support Max-Age and some browsers don't
7372 * support quoted strings. However the Set-Cookie2 header is
7373 * clean.
7374 *
7375 * We have to keep multiple pointers in order to support cookie
7376 * removal at the beginning, middle or end of header without
7377 * corrupting the header (in case of set-cookie2). A special
7378 * pointer, <scav> points to the beginning of the set-cookie-av
7379 * fields after the first semi-colon. The <next> pointer points
7380 * either to the end of line (set-cookie) or next unquoted comma
7381 * (set-cookie2). All of these headers are valid :
7382 *
7383 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7384 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7385 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7386 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7387 * | | | | | | | | | |
7388 * | | | | | | | | +-> next hdr_end <--+
7389 * | | | | | | | +------------> scav
7390 * | | | | | | +--------------> val_end
7391 * | | | | | +--------------------> val_beg
7392 * | | | | +----------------------> equal
7393 * | | | +------------------------> att_end
7394 * | | +----------------------------> att_beg
7395 * | +------------------------------> prev
7396 * +-----------------------------------------> hdr_beg
7397 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007398
Willy Tarreau24581ba2010-08-31 22:39:35 +02007399 for (; prev < hdr_end; prev = next) {
7400 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007401
Willy Tarreau24581ba2010-08-31 22:39:35 +02007402 /* find att_beg */
7403 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007404 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007405 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007406
Willy Tarreau24581ba2010-08-31 22:39:35 +02007407 /* find att_end : this is the first character after the last non
7408 * space before the equal. It may be equal to hdr_end.
7409 */
7410 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007411
Willy Tarreau24581ba2010-08-31 22:39:35 +02007412 while (equal < hdr_end) {
7413 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7414 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007415 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007416 continue;
7417 att_end = equal;
7418 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007419
Willy Tarreau24581ba2010-08-31 22:39:35 +02007420 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7421 * is between <att_beg> and <equal>, both may be identical.
7422 */
7423
7424 /* look for end of cookie if there is an equal sign */
7425 if (equal < hdr_end && *equal == '=') {
7426 /* look for the beginning of the value */
7427 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007428 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007429 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007430
Willy Tarreau24581ba2010-08-31 22:39:35 +02007431 /* find the end of the value, respecting quotes */
7432 next = find_cookie_value_end(val_beg, hdr_end);
7433
7434 /* make val_end point to the first white space or delimitor after the value */
7435 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007436 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007437 val_end--;
7438 } else {
7439 /* <equal> points to next comma, semi-colon or EOL */
7440 val_beg = val_end = next = equal;
7441 }
7442
7443 if (next < hdr_end) {
7444 /* Set-Cookie2 supports multiple cookies, and <next> points to
7445 * a colon or semi-colon before the end. So skip all attr-value
7446 * pairs and look for the next comma. For Set-Cookie, since
7447 * commas are permitted in values, skip to the end.
7448 */
7449 if (is_cookie2)
7450 next = find_hdr_value_end(next, hdr_end);
7451 else
7452 next = hdr_end;
7453 }
7454
7455 /* Now everything is as on the diagram above */
7456
7457 /* Ignore cookies with no equal sign */
7458 if (equal == val_end)
7459 continue;
7460
7461 /* If there are spaces around the equal sign, we need to
7462 * strip them otherwise we'll get trouble for cookie captures,
7463 * or even for rewrites. Since this happens extremely rarely,
7464 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007465 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007466 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7467 int stripped_before = 0;
7468 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007469
Willy Tarreau24581ba2010-08-31 22:39:35 +02007470 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007471 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007472 equal += stripped_before;
7473 val_beg += stripped_before;
7474 }
7475
7476 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007477 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007478 val_beg += stripped_after;
7479 stripped_before += stripped_after;
7480 }
7481
7482 val_end += stripped_before;
7483 next += stripped_before;
7484 hdr_end += stripped_before;
7485 hdr_next += stripped_before;
7486 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007487 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007488 }
7489
7490 /* First, let's see if we want to capture this cookie. We check
7491 * that we don't already have a server side cookie, because we
7492 * can only capture one. Also as an optimisation, we ignore
7493 * cookies shorter than the declared name.
7494 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007495 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007496 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007497 (val_end - att_beg >= sess->fe->capture_namelen) &&
7498 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007499 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007500 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007501 Alert("HTTP logging : out of memory.\n");
7502 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007503 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007504 if (log_len > sess->fe->capture_len)
7505 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01007506 memcpy(txn->srv_cookie, att_beg, log_len);
7507 txn->srv_cookie[log_len] = 0;
7508 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007509 }
7510
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007511 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007512 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007513 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007514 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7515 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007516 /* assume passive cookie by default */
7517 txn->flags &= ~TX_SCK_MASK;
7518 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007519
7520 /* If the cookie is in insert mode on a known server, we'll delete
7521 * this occurrence because we'll insert another one later.
7522 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007523 * a direct access.
7524 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007525 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007526 /* The "preserve" flag was set, we don't want to touch the
7527 * server's cookie.
7528 */
7529 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007530 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007531 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007532 /* this cookie must be deleted */
7533 if (*prev == ':' && next == hdr_end) {
7534 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007535 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007536 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7537 txn->hdr_idx.used--;
7538 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007539 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007540 hdr_next += delta;
7541 http_msg_move_end(&txn->rsp, delta);
7542 /* note: while both invalid now, <next> and <hdr_end>
7543 * are still equal, so the for() will stop as expected.
7544 */
7545 } else {
7546 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007547 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007548 next = prev;
7549 hdr_end += delta;
7550 hdr_next += delta;
7551 cur_hdr->len += delta;
7552 http_msg_move_end(&txn->rsp, delta);
7553 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007554 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007555 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007556 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007557 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007558 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007559 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007560 * with this server since we know it.
7561 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007562 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007563 next += delta;
7564 hdr_end += delta;
7565 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007566 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007567 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007568
Willy Tarreauf1348312010-10-07 15:54:11 +02007569 txn->flags &= ~TX_SCK_MASK;
7570 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007571 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007572 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007573 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007574 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007575 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007576 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007577 next += delta;
7578 hdr_end += delta;
7579 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007580 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007581 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007582
Willy Tarreau827aee92011-03-10 16:55:02 +01007583 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007584 txn->flags &= ~TX_SCK_MASK;
7585 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007586 }
7587 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007588 /* that's done for this cookie, check the next one on the same
7589 * line when next != hdr_end (only if is_cookie2).
7590 */
7591 }
7592 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007593 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007594 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007595}
7596
7597
Willy Tarreaua15645d2007-03-18 16:22:39 +01007598/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007599 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007600 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007601void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007602{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007603 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007604 char *p1, *p2;
7605
7606 char *cur_ptr, *cur_end, *cur_next;
7607 int cur_idx;
7608
Willy Tarreau5df51872007-11-25 16:20:08 +01007609 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007610 return;
7611
7612 /* Iterate through the headers.
7613 * we start with the start line.
7614 */
7615 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007616 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007617
7618 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7619 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007620 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007621
7622 cur_hdr = &txn->hdr_idx.v[cur_idx];
7623 cur_ptr = cur_next;
7624 cur_end = cur_ptr + cur_hdr->len;
7625 cur_next = cur_end + cur_hdr->cr + 1;
7626
7627 /* We have one full header between cur_ptr and cur_end, and the
7628 * next header starts at cur_next. We're only interested in
7629 * "Cookie:" headers.
7630 */
7631
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007632 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7633 if (val) {
7634 if ((cur_end - (cur_ptr + val) >= 8) &&
7635 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7636 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7637 return;
7638 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007639 }
7640
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007641 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7642 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007643 continue;
7644
7645 /* OK, right now we know we have a cache-control header at cur_ptr */
7646
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007647 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007648
7649 if (p1 >= cur_end) /* no more info */
7650 continue;
7651
7652 /* p1 is at the beginning of the value */
7653 p2 = p1;
7654
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007655 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007656 p2++;
7657
7658 /* we have a complete value between p1 and p2 */
7659 if (p2 < cur_end && *p2 == '=') {
7660 /* we have something of the form no-cache="set-cookie" */
7661 if ((cur_end - p1 >= 21) &&
7662 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7663 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007664 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007665 continue;
7666 }
7667
7668 /* OK, so we know that either p2 points to the end of string or to a comma */
7669 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007670 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01007671 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7672 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7673 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007674 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007675 return;
7676 }
7677
7678 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007679 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007680 continue;
7681 }
7682 }
7683}
7684
Willy Tarreau58f10d72006-12-04 02:26:12 +01007685
Willy Tarreaub2513902006-12-17 14:52:38 +01007686/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007687 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007688 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007689 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007690 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007691 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007692 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007693 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007694 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007695int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007696{
7697 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007698 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007699 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007700
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007701 if (!uri_auth)
7702 return 0;
7703
Cyril Bonté70be45d2010-10-12 00:14:35 +02007704 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007705 return 0;
7706
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007707 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007708 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007709 return 0;
7710
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007711 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007712 return 0;
7713
Willy Tarreaub2513902006-12-17 14:52:38 +01007714 return 1;
7715}
7716
Willy Tarreau4076a152009-04-02 15:18:36 +02007717/*
7718 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007719 * By default it tries to report the error position as msg->err_pos. However if
7720 * this one is not set, it will then report msg->next, which is the last known
7721 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007722 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007723 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007724void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007725 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007726 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007727{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007728 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007729 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007730 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007731
Willy Tarreauf3764b72016-03-31 13:45:10 +02007732 es->len = MIN(chn->buf->i, global.tune.bufsize);
Willy Tarreau9b28e032012-10-12 23:49:43 +02007733 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007734 len1 = MIN(len1, es->len);
7735 len2 = es->len - len1; /* remaining data if buffer wraps */
7736
Willy Tarreauf3764b72016-03-31 13:45:10 +02007737 if (!es->buf)
7738 es->buf = malloc(global.tune.bufsize);
7739
7740 if (es->buf) {
7741 memcpy(es->buf, chn->buf->p, len1);
7742 if (len2)
7743 memcpy(es->buf + len1, chn->buf->data, len2);
7744 }
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007745
Willy Tarreau4076a152009-04-02 15:18:36 +02007746 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007747 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007748 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007749 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007750
Willy Tarreau4076a152009-04-02 15:18:36 +02007751 es->when = date; // user-visible date
7752 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007753 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007754 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007755 if (objt_conn(sess->origin))
7756 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007757 else
7758 memset(&es->src, 0, sizeof(es->src));
7759
Willy Tarreau078272e2010-12-12 12:46:33 +01007760 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007761 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007762 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007763 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007764 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007765 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007766 es->b_out = chn->buf->o;
7767 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007768 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007769 es->m_clen = msg->chunk_len;
7770 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02007771}
Willy Tarreaub2513902006-12-17 14:52:38 +01007772
Willy Tarreau294c4732011-12-16 21:35:50 +01007773/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7774 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7775 * performed over the whole headers. Otherwise it must contain a valid header
7776 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7777 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7778 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7779 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007780 * -1. The value fetch stops at commas, so this function is suited for use with
7781 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007782 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007783 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007784unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007785 struct hdr_idx *idx, int occ,
7786 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007787{
Willy Tarreau294c4732011-12-16 21:35:50 +01007788 struct hdr_ctx local_ctx;
7789 char *ptr_hist[MAX_HDR_HISTORY];
7790 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007791 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007792 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007793
Willy Tarreau294c4732011-12-16 21:35:50 +01007794 if (!ctx) {
7795 local_ctx.idx = 0;
7796 ctx = &local_ctx;
7797 }
7798
Willy Tarreaubce70882009-09-07 11:51:47 +02007799 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007800 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007801 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007802 occ--;
7803 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007804 *vptr = ctx->line + ctx->val;
7805 *vlen = ctx->vlen;
7806 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007807 }
7808 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007809 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007810 }
7811
7812 /* negative occurrence, we scan all the list then walk back */
7813 if (-occ > MAX_HDR_HISTORY)
7814 return 0;
7815
Willy Tarreau294c4732011-12-16 21:35:50 +01007816 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007817 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007818 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7819 len_hist[hist_ptr] = ctx->vlen;
7820 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007821 hist_ptr = 0;
7822 found++;
7823 }
7824 if (-occ > found)
7825 return 0;
7826 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02007827 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7828 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7829 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02007830 */
Willy Tarreau67dad272013-06-12 22:27:44 +02007831 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02007832 if (hist_ptr >= MAX_HDR_HISTORY)
7833 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007834 *vptr = ptr_hist[hist_ptr];
7835 *vlen = len_hist[hist_ptr];
7836 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007837}
7838
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007839/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7840 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7841 * performed over the whole headers. Otherwise it must contain a valid header
7842 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7843 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7844 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7845 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7846 * -1. This function differs from http_get_hdr() in that it only returns full
7847 * line header values and does not stop at commas.
7848 * The return value is 0 if nothing was found, or non-zero otherwise.
7849 */
7850unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
7851 struct hdr_idx *idx, int occ,
7852 struct hdr_ctx *ctx, char **vptr, int *vlen)
7853{
7854 struct hdr_ctx local_ctx;
7855 char *ptr_hist[MAX_HDR_HISTORY];
7856 int len_hist[MAX_HDR_HISTORY];
7857 unsigned int hist_ptr;
7858 int found;
7859
7860 if (!ctx) {
7861 local_ctx.idx = 0;
7862 ctx = &local_ctx;
7863 }
7864
7865 if (occ >= 0) {
7866 /* search from the beginning */
7867 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
7868 occ--;
7869 if (occ <= 0) {
7870 *vptr = ctx->line + ctx->val;
7871 *vlen = ctx->vlen;
7872 return 1;
7873 }
7874 }
7875 return 0;
7876 }
7877
7878 /* negative occurrence, we scan all the list then walk back */
7879 if (-occ > MAX_HDR_HISTORY)
7880 return 0;
7881
7882 found = hist_ptr = 0;
7883 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
7884 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7885 len_hist[hist_ptr] = ctx->vlen;
7886 if (++hist_ptr >= MAX_HDR_HISTORY)
7887 hist_ptr = 0;
7888 found++;
7889 }
7890 if (-occ > found)
7891 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007892
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007893 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007894 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7895 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7896 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007897 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007898 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007899 if (hist_ptr >= MAX_HDR_HISTORY)
7900 hist_ptr -= MAX_HDR_HISTORY;
7901 *vptr = ptr_hist[hist_ptr];
7902 *vlen = len_hist[hist_ptr];
7903 return 1;
7904}
7905
Willy Tarreaubaaee002006-06-26 02:48:02 +02007906/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007907 * Print a debug line with a header. Always stop at the first CR or LF char,
7908 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7909 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007910 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007911void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007912{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007913 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007914 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007915
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007916 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007917 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02007918 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
7919 objt_conn(s->si[1].end) ? (unsigned short)objt_conn(s->si[1].end)->handle.fd : -1);
Willy Tarreaue92693a2012-09-24 21:13:39 +02007920
7921 for (max = 0; start + max < end; max++)
7922 if (start[max] == '\r' || start[max] == '\n')
7923 break;
7924
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007925 UBOUND(max, trash.size - trash.len - 3);
7926 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
7927 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01007928 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007929}
7930
Willy Tarreaueee5b512015-04-03 23:46:31 +02007931
7932/* Allocate a new HTTP transaction for stream <s> unless there is one already.
7933 * The hdr_idx is allocated as well. In case of allocation failure, everything
7934 * allocated is freed and NULL is returned. Otherwise the new transaction is
7935 * assigned to the stream and returned.
7936 */
7937struct http_txn *http_alloc_txn(struct stream *s)
7938{
7939 struct http_txn *txn = s->txn;
7940
7941 if (txn)
7942 return txn;
7943
7944 txn = pool_alloc2(pool2_http_txn);
7945 if (!txn)
7946 return txn;
7947
7948 txn->hdr_idx.size = global.tune.max_http_hdr;
7949 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
7950 if (!txn->hdr_idx.v) {
7951 pool_free2(pool2_http_txn, txn);
7952 return NULL;
7953 }
7954
7955 s->txn = txn;
7956 return txn;
7957}
7958
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007959void http_txn_reset_req(struct http_txn *txn)
7960{
7961 txn->req.flags = 0;
7962 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
7963 txn->req.next = 0;
7964 txn->req.chunk_len = 0LL;
7965 txn->req.body_len = 0LL;
7966 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7967}
7968
7969void http_txn_reset_res(struct http_txn *txn)
7970{
7971 txn->rsp.flags = 0;
7972 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
7973 txn->rsp.next = 0;
7974 txn->rsp.chunk_len = 0LL;
7975 txn->rsp.body_len = 0LL;
7976 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
7977}
7978
Willy Tarreau0937bc42009-12-22 15:03:09 +01007979/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007980 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01007981 * the required fields are properly allocated and that we only need to (re)init
7982 * them. This should be used before processing any new request.
7983 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007984void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007985{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007986 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007987 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007988
7989 txn->flags = 0;
7990 txn->status = -1;
7991
Willy Tarreauf64d1412010-10-07 20:06:11 +02007992 txn->cookie_first_date = 0;
7993 txn->cookie_last_date = 0;
7994
Willy Tarreaueee5b512015-04-03 23:46:31 +02007995 txn->srv_cookie = NULL;
7996 txn->cli_cookie = NULL;
7997 txn->uri = NULL;
7998
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007999 http_txn_reset_req(txn);
8000 http_txn_reset_res(txn);
8001
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008002 txn->req.chn = &s->req;
8003 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008004
8005 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008006
8007 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8008 if (fe->options2 & PR_O2_REQBUG_OK)
8009 txn->req.err_pos = -1; /* let buggy requests pass */
8010
Willy Tarreau0937bc42009-12-22 15:03:09 +01008011 if (txn->hdr_idx.v)
8012 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008013
8014 vars_init(&s->vars_txn, SCOPE_TXN);
8015 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008016}
8017
8018/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008019void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008020{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008021 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008022 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008023
8024 /* these ones will have been dynamically allocated */
8025 pool_free2(pool2_requri, txn->uri);
8026 pool_free2(pool2_capture, txn->cli_cookie);
8027 pool_free2(pool2_capture, txn->srv_cookie);
William Lallemanda73203e2012-03-12 12:48:57 +01008028 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008029
William Lallemanda73203e2012-03-12 12:48:57 +01008030 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008031 txn->uri = NULL;
8032 txn->srv_cookie = NULL;
8033 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008034
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008035 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008036 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008037 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008038 pool_free2(h->pool, s->req_cap[h->index]);
8039 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008040 }
8041
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008042 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008043 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008044 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008045 pool_free2(h->pool, s->res_cap[h->index]);
8046 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008047 }
8048
Willy Tarreau6204cd92016-03-10 16:33:04 +01008049 vars_prune(&s->vars_txn, s->sess, s);
8050 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008051}
8052
8053/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008054void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008055{
8056 http_end_txn(s);
8057 http_init_txn(s);
8058
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008059 /* reinitialise the current rule list pointer to NULL. We are sure that
8060 * any rulelist match the NULL pointer.
8061 */
8062 s->current_rule_list = NULL;
8063
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008064 s->be = strm_fe(s);
8065 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008066 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008067 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008068 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008069 /* re-init store persistence */
8070 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008071 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008072
Willy Tarreau0937bc42009-12-22 15:03:09 +01008073 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008074
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008075 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008076
Willy Tarreau739cfba2010-01-25 23:11:14 +01008077 /* We must trim any excess data from the response buffer, because we
8078 * may have blocked an invalid response from a server that we don't
8079 * want to accidentely forward once we disable the analysers, nor do
8080 * we want those data to come along with next response. A typical
8081 * example of such data would be from a buggy server responding to
8082 * a HEAD with some data, or sending more than the advertised
8083 * content-length.
8084 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008085 if (unlikely(s->res.buf->i))
8086 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008087
Christopher Fauletc0c672a2017-03-28 11:51:33 +02008088 /* Now we can realign the response buffer */
8089 buffer_realign(s->res.buf);
8090
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008091 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008092 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008093
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008094 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008095 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008096
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008097 s->req.rex = TICK_ETERNITY;
8098 s->req.wex = TICK_ETERNITY;
8099 s->req.analyse_exp = TICK_ETERNITY;
8100 s->res.rex = TICK_ETERNITY;
8101 s->res.wex = TICK_ETERNITY;
8102 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01008103 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008104}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008105
Sasha Pachev218f0642014-06-16 12:05:59 -06008106void free_http_res_rules(struct list *r)
8107{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008108 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06008109
8110 list_for_each_entry_safe(pr, tr, r, list) {
8111 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008112 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008113 free(pr);
8114 }
8115}
8116
8117void free_http_req_rules(struct list *r)
8118{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008119 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01008120
8121 list_for_each_entry_safe(pr, tr, r, list) {
8122 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008123 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008124 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008125
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008126 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008127 free(pr);
8128 }
8129}
8130
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008131/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008132struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01008133{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008134 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008135 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008136 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008137 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008138
Vincent Bernat02779b62016-04-03 13:48:43 +02008139 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01008140 if (!rule) {
8141 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008142 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008143 }
8144
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008145 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008146 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01008147 cur_arg = 1;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008148 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block") || !strcmp(args[0], "tarpit")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04008149 int code;
8150 int hc;
8151
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008152 if (!strcmp(args[0], "tarpit")) {
8153 rule->action = ACT_HTTP_REQ_TARPIT;
8154 rule->deny_status = HTTP_ERR_500;
8155 }
8156 else {
8157 rule->action = ACT_ACTION_DENY;
8158 rule->deny_status = HTTP_ERR_403;
8159 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008160 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04008161 if (strcmp(args[cur_arg], "deny_status") == 0) {
8162 cur_arg++;
8163 if (!args[cur_arg]) {
8164 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
8165 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
8166 goto out_err;
8167 }
8168
8169 code = atol(args[cur_arg]);
8170 cur_arg++;
8171 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
8172 if (http_err_codes[hc] == code) {
8173 rule->deny_status = hc;
8174 break;
8175 }
8176 }
8177
8178 if (hc >= HTTP_ERR_SIZE) {
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008179 Warning("parsing [%s:%d] : status code %d not handled, using default code %d.\n",
8180 file, linenum, code, http_err_codes[rule->deny_status]);
CJ Ess108b1dd2015-04-07 12:03:37 -04008181 }
8182 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008183 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008184 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008185 cur_arg = 1;
8186
8187 while(*args[cur_arg]) {
8188 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008189 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008190 cur_arg+=2;
8191 continue;
8192 } else
8193 break;
8194 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008195 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008196 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008197 cur_arg = 1;
8198
8199 if (!*args[cur_arg] ||
8200 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8201 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8202 file, linenum, args[0]);
8203 goto out_err;
8204 }
8205 rule->arg.nice = atoi(args[cur_arg]);
8206 if (rule->arg.nice < -1024)
8207 rule->arg.nice = -1024;
8208 else if (rule->arg.nice > 1024)
8209 rule->arg.nice = 1024;
8210 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008211 } else if (!strcmp(args[0], "set-tos")) {
8212#ifdef IP_TOS
8213 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008214 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008215 cur_arg = 1;
8216
8217 if (!*args[cur_arg] ||
8218 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8219 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8220 file, linenum, args[0]);
8221 goto out_err;
8222 }
8223
8224 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8225 if (err && *err != '\0') {
8226 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8227 file, linenum, err, args[0]);
8228 goto out_err;
8229 }
8230 cur_arg++;
8231#else
8232 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8233 goto out_err;
8234#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008235 } else if (!strcmp(args[0], "set-mark")) {
8236#ifdef SO_MARK
8237 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008238 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008239 cur_arg = 1;
8240
8241 if (!*args[cur_arg] ||
8242 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8243 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8244 file, linenum, args[0]);
8245 goto out_err;
8246 }
8247
8248 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8249 if (err && *err != '\0') {
8250 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8251 file, linenum, err, args[0]);
8252 goto out_err;
8253 }
8254 cur_arg++;
8255 global.last_checks |= LSTCHK_NETADM;
8256#else
8257 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8258 goto out_err;
8259#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008260 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008261 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008262 cur_arg = 1;
8263
8264 if (!*args[cur_arg] ||
8265 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8266 bad_log_level:
8267 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8268 file, linenum, args[0]);
8269 goto out_err;
8270 }
8271 if (strcmp(args[cur_arg], "silent") == 0)
8272 rule->arg.loglevel = -1;
8273 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8274 goto bad_log_level;
8275 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008276 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008277 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008278 cur_arg = 1;
8279
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008280 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8281 (*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 +01008282 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8283 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008284 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008285 }
8286
8287 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8288 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8289 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008290
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008291 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008292 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008293 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 +01008294 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8295 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8296 file, linenum, args[0], error);
8297 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008298 goto out_err;
8299 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008300 free(proxy->conf.lfs_file);
8301 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8302 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008303 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02008304 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008305 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008306 cur_arg = 1;
8307
8308 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02008309 (*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 -06008310 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
8311 file, linenum, args[0]);
8312 goto out_err;
8313 }
8314
8315 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8316 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8317 LIST_INIT(&rule->arg.hdr_add.fmt);
8318
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008319 error = NULL;
8320 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
8321 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8322 args[cur_arg + 1], error);
8323 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008324 goto out_err;
8325 }
8326
8327 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008328 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008329 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 +01008330 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8331 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8332 file, linenum, args[0], error);
8333 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008334 goto out_err;
8335 }
Sasha Pachev218f0642014-06-16 12:05:59 -06008336
8337 free(proxy->conf.lfs_file);
8338 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8339 proxy->conf.lfs_line = proxy->conf.args.line;
8340 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008341 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008342 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008343 cur_arg = 1;
8344
8345 if (!*args[cur_arg] ||
8346 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8347 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8348 file, linenum, args[0]);
8349 goto out_err;
8350 }
8351
8352 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8353 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8354
8355 proxy->conf.args.ctx = ARGC_HRQ;
8356 free(proxy->conf.lfs_file);
8357 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8358 proxy->conf.lfs_line = proxy->conf.args.line;
8359 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02008360 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
8361 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02008362 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02008363 struct sample_expr *expr;
8364 unsigned int where;
8365 char *err = NULL;
8366
8367 cur_arg = 1;
8368 proxy->conf.args.ctx = ARGC_TRK;
8369
8370 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
8371 if (!expr) {
8372 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8373 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
8374 free(err);
8375 goto out_err;
8376 }
8377
8378 where = 0;
8379 if (proxy->cap & PR_CAP_FE)
8380 where |= SMP_VAL_FE_HRQ_HDR;
8381 if (proxy->cap & PR_CAP_BE)
8382 where |= SMP_VAL_BE_HRQ_HDR;
8383
8384 if (!(expr->fetch->val & where)) {
8385 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
8386 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
8387 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
8388 args[cur_arg-1], sample_src_names(expr->fetch->use));
8389 free(expr);
8390 goto out_err;
8391 }
8392
8393 if (strcmp(args[cur_arg], "table") == 0) {
8394 cur_arg++;
8395 if (!args[cur_arg]) {
8396 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
8397 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
8398 free(expr);
8399 goto out_err;
8400 }
8401 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008402 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02008403 cur_arg++;
8404 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008405 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008406 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Christopher Faulet78880fb2017-09-18 14:43:55 +02008407 rule->check_ptr = check_trk_action;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008408 } else if (strcmp(args[0], "redirect") == 0) {
8409 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008410 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008411
Willy Tarreaube4653b2015-05-28 15:26:58 +02008412 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01008413 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8414 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8415 goto out_err;
8416 }
8417
8418 /* this redirect rule might already contain a parsed condition which
8419 * we'll pass to the http-request rule.
8420 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008421 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008422 rule->arg.redir = redir;
8423 rule->cond = redir->cond;
8424 redir->cond = NULL;
8425 cur_arg = 2;
8426 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008427 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8428 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008429 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008430 /*
8431 * '+ 8' for 'add-acl('
8432 * '- 9' for 'add-acl(' + trailing ')'
8433 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008434 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008435
8436 cur_arg = 1;
8437
8438 if (!*args[cur_arg] ||
8439 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8440 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8441 file, linenum, args[0]);
8442 goto out_err;
8443 }
8444
8445 LIST_INIT(&rule->arg.map.key);
8446 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008447 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008448 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008449 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8450 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8451 file, linenum, args[0], error);
8452 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008453 goto out_err;
8454 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008455 free(proxy->conf.lfs_file);
8456 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8457 proxy->conf.lfs_line = proxy->conf.args.line;
8458 cur_arg += 1;
8459 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8460 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008461 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008462 /*
8463 * '+ 8' for 'del-acl('
8464 * '- 9' for 'del-acl(' + trailing ')'
8465 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008466 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008467
8468 cur_arg = 1;
8469
8470 if (!*args[cur_arg] ||
8471 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8472 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8473 file, linenum, args[0]);
8474 goto out_err;
8475 }
8476
8477 LIST_INIT(&rule->arg.map.key);
8478 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008479 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008480 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008481 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8482 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8483 file, linenum, args[0], error);
8484 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008485 goto out_err;
8486 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008487 free(proxy->conf.lfs_file);
8488 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8489 proxy->conf.lfs_line = proxy->conf.args.line;
8490 cur_arg += 1;
8491 } else if (strncmp(args[0], "del-map", 7) == 0) {
8492 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008493 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008494 /*
8495 * '+ 8' for 'del-map('
8496 * '- 9' for 'del-map(' + trailing ')'
8497 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008498 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008499
8500 cur_arg = 1;
8501
8502 if (!*args[cur_arg] ||
8503 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8504 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8505 file, linenum, args[0]);
8506 goto out_err;
8507 }
8508
8509 LIST_INIT(&rule->arg.map.key);
8510 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008511 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008512 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008513 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8514 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8515 file, linenum, args[0], error);
8516 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008517 goto out_err;
8518 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008519 free(proxy->conf.lfs_file);
8520 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8521 proxy->conf.lfs_line = proxy->conf.args.line;
8522 cur_arg += 1;
8523 } else if (strncmp(args[0], "set-map", 7) == 0) {
8524 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008525 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008526 /*
8527 * '+ 8' for 'set-map('
8528 * '- 9' for 'set-map(' + trailing ')'
8529 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008530 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008531
8532 cur_arg = 1;
8533
8534 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8535 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8536 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8537 file, linenum, args[0]);
8538 goto out_err;
8539 }
8540
8541 LIST_INIT(&rule->arg.map.key);
8542 LIST_INIT(&rule->arg.map.value);
8543 proxy->conf.args.ctx = ARGC_HRQ;
8544
8545 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008546 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008547 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008548 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8549 Alert("parsing [%s:%d]: 'http-request %s' key: %s.\n",
8550 file, linenum, args[0], error);
8551 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008552 goto out_err;
8553 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008554
8555 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008556 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008557 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 +01008558 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8559 Alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n",
8560 file, linenum, args[0], error);
8561 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008562 goto out_err;
8563 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008564 free(proxy->conf.lfs_file);
8565 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8566 proxy->conf.lfs_line = proxy->conf.args.line;
8567
8568 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02008569 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
8570 char *errmsg = NULL;
8571 cur_arg = 1;
8572 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02008573 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02008574 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02008575 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02008576 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8577 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8578 free(errmsg);
8579 goto out_err;
8580 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008581 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02008582 action_build_list(&http_req_keywords.list, &trash);
8583 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
8584 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08008585 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02008586 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02008587 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008588 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008589 }
8590
8591 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8592 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008593 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008594
Christopher Faulet1b421ea2017-09-22 14:38:56 +02008595 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008596 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8597 file, linenum, args[0], errmsg);
8598 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008599 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008600 }
8601 rule->cond = cond;
8602 }
8603 else if (*args[cur_arg]) {
8604 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8605 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8606 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008607 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008608 }
8609
8610 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008611 out_err:
8612 free(rule);
8613 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008614}
8615
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008616/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008617struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008618{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008619 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008620 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008621 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008622 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008623
8624 rule = calloc(1, sizeof(*rule));
8625 if (!rule) {
8626 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
8627 goto out_err;
8628 }
8629
8630 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008631 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008632 cur_arg = 1;
8633 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008634 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008635 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008636 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008637 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008638 cur_arg = 1;
8639
8640 if (!*args[cur_arg] ||
8641 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8642 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8643 file, linenum, args[0]);
8644 goto out_err;
8645 }
8646 rule->arg.nice = atoi(args[cur_arg]);
8647 if (rule->arg.nice < -1024)
8648 rule->arg.nice = -1024;
8649 else if (rule->arg.nice > 1024)
8650 rule->arg.nice = 1024;
8651 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008652 } else if (!strcmp(args[0], "set-tos")) {
8653#ifdef IP_TOS
8654 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008655 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008656 cur_arg = 1;
8657
8658 if (!*args[cur_arg] ||
8659 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8660 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8661 file, linenum, args[0]);
8662 goto out_err;
8663 }
8664
8665 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8666 if (err && *err != '\0') {
8667 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8668 file, linenum, err, args[0]);
8669 goto out_err;
8670 }
8671 cur_arg++;
8672#else
8673 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8674 goto out_err;
8675#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008676 } else if (!strcmp(args[0], "set-mark")) {
8677#ifdef SO_MARK
8678 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008679 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008680 cur_arg = 1;
8681
8682 if (!*args[cur_arg] ||
8683 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8684 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8685 file, linenum, args[0]);
8686 goto out_err;
8687 }
8688
8689 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8690 if (err && *err != '\0') {
8691 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8692 file, linenum, err, args[0]);
8693 goto out_err;
8694 }
8695 cur_arg++;
8696 global.last_checks |= LSTCHK_NETADM;
8697#else
8698 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8699 goto out_err;
8700#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008701 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008702 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008703 cur_arg = 1;
8704
8705 if (!*args[cur_arg] ||
8706 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8707 bad_log_level:
8708 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8709 file, linenum, args[0]);
8710 goto out_err;
8711 }
8712 if (strcmp(args[cur_arg], "silent") == 0)
8713 rule->arg.loglevel = -1;
Ruoshan Huangdd016782016-06-15 22:16:03 +08008714 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008715 goto bad_log_level;
8716 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008717 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008718 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008719 cur_arg = 1;
8720
8721 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8722 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8723 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8724 file, linenum, args[0]);
8725 goto out_err;
8726 }
8727
8728 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8729 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8730 LIST_INIT(&rule->arg.hdr_add.fmt);
8731
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008732 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008733 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008734 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 +01008735 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8736 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8737 file, linenum, args[0], error);
8738 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008739 goto out_err;
8740 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008741 free(proxy->conf.lfs_file);
8742 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8743 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008744 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06008745 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008746 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008747 cur_arg = 1;
8748
8749 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02008750 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
8751 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06008752 file, linenum, args[0]);
8753 goto out_err;
8754 }
8755
8756 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8757 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8758 LIST_INIT(&rule->arg.hdr_add.fmt);
8759
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008760 error = NULL;
8761 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
8762 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8763 args[cur_arg + 1], error);
8764 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008765 goto out_err;
8766 }
8767
8768 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008769 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008770 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 +01008771 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8772 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8773 file, linenum, args[0], error);
8774 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008775 goto out_err;
8776 }
Sasha Pachev218f0642014-06-16 12:05:59 -06008777
8778 free(proxy->conf.lfs_file);
8779 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8780 proxy->conf.lfs_line = proxy->conf.args.line;
8781 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008782 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008783 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008784 cur_arg = 1;
8785
8786 if (!*args[cur_arg] ||
8787 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8788 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8789 file, linenum, args[0]);
8790 goto out_err;
8791 }
8792
8793 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8794 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8795
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008796 proxy->conf.args.ctx = ARGC_HRS;
8797 free(proxy->conf.lfs_file);
8798 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8799 proxy->conf.lfs_line = proxy->conf.args.line;
8800 cur_arg += 1;
8801 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8802 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008803 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008804 /*
8805 * '+ 8' for 'add-acl('
8806 * '- 9' for 'add-acl(' + trailing ')'
8807 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008808 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008809
8810 cur_arg = 1;
8811
8812 if (!*args[cur_arg] ||
8813 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8814 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8815 file, linenum, args[0]);
8816 goto out_err;
8817 }
8818
8819 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008820 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008821 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008822 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008823 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8824 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8825 file, linenum, args[0], error);
8826 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008827 goto out_err;
8828 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008829 free(proxy->conf.lfs_file);
8830 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8831 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008832
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008833 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008834 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8835 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008836 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008837 /*
8838 * '+ 8' for 'del-acl('
8839 * '- 9' for 'del-acl(' + trailing ')'
8840 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008841 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008842
8843 cur_arg = 1;
8844
8845 if (!*args[cur_arg] ||
8846 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8847 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8848 file, linenum, args[0]);
8849 goto out_err;
8850 }
8851
8852 LIST_INIT(&rule->arg.map.key);
8853 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008854 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008855 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008856 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8857 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8858 file, linenum, args[0], error);
8859 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008860 goto out_err;
8861 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008862 free(proxy->conf.lfs_file);
8863 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8864 proxy->conf.lfs_line = proxy->conf.args.line;
8865 cur_arg += 1;
8866 } else if (strncmp(args[0], "del-map", 7) == 0) {
8867 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008868 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008869 /*
8870 * '+ 8' for 'del-map('
8871 * '- 9' for 'del-map(' + trailing ')'
8872 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008873 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008874
8875 cur_arg = 1;
8876
8877 if (!*args[cur_arg] ||
8878 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8879 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8880 file, linenum, args[0]);
8881 goto out_err;
8882 }
8883
8884 LIST_INIT(&rule->arg.map.key);
8885 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008886 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008887 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008888 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8889 Alert("parsing [%s:%d]: 'http-response %s' %s.\n",
8890 file, linenum, args[0], error);
8891 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008892 goto out_err;
8893 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008894 free(proxy->conf.lfs_file);
8895 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8896 proxy->conf.lfs_line = proxy->conf.args.line;
8897 cur_arg += 1;
8898 } else if (strncmp(args[0], "set-map", 7) == 0) {
8899 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008900 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008901 /*
8902 * '+ 8' for 'set-map('
8903 * '- 9' for 'set-map(' + trailing ')'
8904 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008905 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008906
8907 cur_arg = 1;
8908
8909 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8910 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8911 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8912 file, linenum, args[0]);
8913 goto out_err;
8914 }
8915
8916 LIST_INIT(&rule->arg.map.key);
8917 LIST_INIT(&rule->arg.map.value);
8918
8919 proxy->conf.args.ctx = ARGC_HRS;
8920
8921 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008922 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008923 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008924 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8925 Alert("parsing [%s:%d]: 'http-response %s' name: %s.\n",
8926 file, linenum, args[0], error);
8927 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008928 goto out_err;
8929 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008930
8931 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008932 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008933 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 +01008934 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8935 Alert("parsing [%s:%d]: 'http-response %s' value: %s.\n",
8936 file, linenum, args[0], error);
8937 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008938 goto out_err;
8939 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008940
8941 free(proxy->conf.lfs_file);
8942 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8943 proxy->conf.lfs_line = proxy->conf.args.line;
8944
8945 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02008946 } else if (strcmp(args[0], "redirect") == 0) {
8947 struct redirect_rule *redir;
8948 char *errmsg = NULL;
8949
8950 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
8951 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
8952 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8953 goto out_err;
8954 }
8955
8956 /* this redirect rule might already contain a parsed condition which
8957 * we'll pass to the http-request rule.
8958 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008959 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02008960 rule->arg.redir = redir;
8961 rule->cond = redir->cond;
8962 redir->cond = NULL;
8963 cur_arg = 2;
8964 return rule;
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08008965 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
8966 args[0][9] == '\0' && args[0][8] >= '0' &&
8967 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
8968 struct sample_expr *expr;
8969 unsigned int where;
8970 char *err = NULL;
8971
8972 cur_arg = 1;
8973 proxy->conf.args.ctx = ARGC_TRK;
8974
8975 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
8976 if (!expr) {
8977 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
8978 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
8979 free(err);
8980 goto out_err;
8981 }
8982
8983 where = 0;
8984 if (proxy->cap & PR_CAP_FE)
8985 where |= SMP_VAL_FE_HRS_HDR;
8986 if (proxy->cap & PR_CAP_BE)
8987 where |= SMP_VAL_BE_HRS_HDR;
8988
8989 if (!(expr->fetch->val & where)) {
8990 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :"
8991 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
8992 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
8993 args[cur_arg-1], sample_src_names(expr->fetch->use));
8994 free(expr);
8995 goto out_err;
8996 }
8997
8998 if (strcmp(args[cur_arg], "table") == 0) {
8999 cur_arg++;
9000 if (!args[cur_arg]) {
9001 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n",
9002 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9003 free(expr);
9004 goto out_err;
9005 }
9006 /* we copy the table name for now, it will be resolved later */
9007 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
9008 cur_arg++;
9009 }
9010 rule->arg.trk_ctr.expr = expr;
9011 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Christopher Faulet78880fb2017-09-18 14:43:55 +02009012 rule->check_ptr = check_trk_action;
William Lallemand73025dd2014-04-24 14:38:37 +02009013 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9014 char *errmsg = NULL;
9015 cur_arg = 1;
9016 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009017 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009018 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009019 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009020 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9021 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9022 free(errmsg);
9023 goto out_err;
9024 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009025 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009026 action_build_list(&http_res_keywords.list, &trash);
9027 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9028 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009029 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02009030 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009031 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009032 goto out_err;
9033 }
9034
9035 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9036 struct acl_cond *cond;
9037 char *errmsg = NULL;
9038
Christopher Faulet1b421ea2017-09-22 14:38:56 +02009039 if ((cond = build_acl_cond(file, linenum, &proxy->acl, proxy, args+cur_arg, &errmsg)) == NULL) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009040 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9041 file, linenum, args[0], errmsg);
9042 free(errmsg);
9043 goto out_err;
9044 }
9045 rule->cond = cond;
9046 }
9047 else if (*args[cur_arg]) {
9048 Alert("parsing [%s:%d]: 'http-response %s' expects"
9049 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9050 file, linenum, args[0], args[cur_arg]);
9051 goto out_err;
9052 }
9053
9054 return rule;
9055 out_err:
9056 free(rule);
9057 return NULL;
9058}
9059
Willy Tarreau4baae242012-12-27 12:00:31 +01009060/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009061 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009062 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9063 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009064 */
9065struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009066 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009067{
9068 struct redirect_rule *rule;
9069 int cur_arg;
9070 int type = REDIRECT_TYPE_NONE;
9071 int code = 302;
9072 const char *destination = NULL;
9073 const char *cookie = NULL;
9074 int cookie_set = 0;
9075 unsigned int flags = REDIRECT_FLAG_NONE;
9076 struct acl_cond *cond = NULL;
9077
9078 cur_arg = 0;
9079 while (*(args[cur_arg])) {
9080 if (strcmp(args[cur_arg], "location") == 0) {
9081 if (!*args[cur_arg + 1])
9082 goto missing_arg;
9083
9084 type = REDIRECT_TYPE_LOCATION;
9085 cur_arg++;
9086 destination = args[cur_arg];
9087 }
9088 else if (strcmp(args[cur_arg], "prefix") == 0) {
9089 if (!*args[cur_arg + 1])
9090 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009091 type = REDIRECT_TYPE_PREFIX;
9092 cur_arg++;
9093 destination = args[cur_arg];
9094 }
9095 else if (strcmp(args[cur_arg], "scheme") == 0) {
9096 if (!*args[cur_arg + 1])
9097 goto missing_arg;
9098
9099 type = REDIRECT_TYPE_SCHEME;
9100 cur_arg++;
9101 destination = args[cur_arg];
9102 }
9103 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9104 if (!*args[cur_arg + 1])
9105 goto missing_arg;
9106
9107 cur_arg++;
9108 cookie = args[cur_arg];
9109 cookie_set = 1;
9110 }
9111 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9112 if (!*args[cur_arg + 1])
9113 goto missing_arg;
9114
9115 cur_arg++;
9116 cookie = args[cur_arg];
9117 cookie_set = 0;
9118 }
9119 else if (strcmp(args[cur_arg], "code") == 0) {
9120 if (!*args[cur_arg + 1])
9121 goto missing_arg;
9122
9123 cur_arg++;
9124 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009125 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009126 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009127 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009128 args[cur_arg - 1], args[cur_arg]);
9129 return NULL;
9130 }
9131 }
9132 else if (!strcmp(args[cur_arg],"drop-query")) {
9133 flags |= REDIRECT_FLAG_DROP_QS;
9134 }
9135 else if (!strcmp(args[cur_arg],"append-slash")) {
9136 flags |= REDIRECT_FLAG_APPEND_SLASH;
9137 }
9138 else if (strcmp(args[cur_arg], "if") == 0 ||
9139 strcmp(args[cur_arg], "unless") == 0) {
Christopher Faulet1b421ea2017-09-22 14:38:56 +02009140 cond = build_acl_cond(file, linenum, &proxy->acl, curproxy, (const char **)args + cur_arg, errmsg);
Willy Tarreau4baae242012-12-27 12:00:31 +01009141 if (!cond) {
9142 memprintf(errmsg, "error in condition: %s", *errmsg);
9143 return NULL;
9144 }
9145 break;
9146 }
9147 else {
9148 memprintf(errmsg,
9149 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9150 args[cur_arg]);
9151 return NULL;
9152 }
9153 cur_arg++;
9154 }
9155
9156 if (type == REDIRECT_TYPE_NONE) {
9157 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9158 return NULL;
9159 }
9160
Willy Tarreaube4653b2015-05-28 15:26:58 +02009161 if (dir && type != REDIRECT_TYPE_LOCATION) {
9162 memprintf(errmsg, "response only supports redirect type 'location'");
9163 return NULL;
9164 }
9165
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02009166 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +01009167 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009168 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009169
9170 if (!use_fmt) {
9171 /* old-style static redirect rule */
9172 rule->rdr_str = strdup(destination);
9173 rule->rdr_len = strlen(destination);
9174 }
9175 else {
9176 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009177
9178 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9179 * if prefix == "/", we don't want to add anything, otherwise it
9180 * makes it hard for the user to configure a self-redirection.
9181 */
Godbachd9722032014-12-18 15:44:58 +08009182 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009183 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009184 if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
9185 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 +01009186 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9187 errmsg)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009188 return NULL;
9189 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009190 free(curproxy->conf.lfs_file);
9191 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9192 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009193 }
9194 }
9195
Willy Tarreau4baae242012-12-27 12:00:31 +01009196 if (cookie) {
9197 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9198 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9199 */
9200 rule->cookie_len = strlen(cookie);
9201 if (cookie_set) {
9202 rule->cookie_str = malloc(rule->cookie_len + 10);
9203 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9204 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9205 rule->cookie_len += 9;
9206 } else {
9207 rule->cookie_str = malloc(rule->cookie_len + 21);
9208 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9209 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9210 rule->cookie_len += 20;
9211 }
9212 }
9213 rule->type = type;
9214 rule->code = code;
9215 rule->flags = flags;
9216 LIST_INIT(&rule->list);
9217 return rule;
9218
9219 missing_arg:
9220 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9221 return NULL;
9222}
9223
Willy Tarreau8797c062007-05-07 00:55:35 +02009224/************************************************************************/
9225/* The code below is dedicated to ACL parsing and matching */
9226/************************************************************************/
9227
9228
Willy Tarreau14174bc2012-04-16 14:34:04 +02009229/* This function ensures that the prerequisites for an L7 fetch are ready,
9230 * which means that a request or response is ready. If some data is missing,
9231 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009232 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9233 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009234 *
9235 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009236 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9237 * decide whether or not an HTTP message is present ;
9238 * 0 if the requested data cannot be fetched or if it is certain that
9239 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009240 * 1 if an HTTP message is ready
9241 */
James Rosewell91a41cb2015-09-18 17:11:16 +01009242int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009243 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009244{
Willy Tarreau192252e2015-04-04 01:47:55 +02009245 struct http_txn *txn;
9246 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009247
Willy Tarreaube508f12016-03-10 11:47:01 +01009248 /* Note: it is possible that <s> is NULL when called before stream
9249 * initialization (eg: tcp-request connection), so this function is the
9250 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009251 */
Willy Tarreau192252e2015-04-04 01:47:55 +02009252 if (!s)
9253 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +01009254
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +02009255 if (!s->txn) {
9256 if (unlikely(!http_alloc_txn(s)))
9257 return 0; /* not enough memory */
9258 http_init_txn(s);
9259 }
Willy Tarreau192252e2015-04-04 01:47:55 +02009260 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +02009261 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009262
9263 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009264 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009265
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009266 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009267 /* If the buffer does not leave enough free space at the end,
9268 * we must first realign it.
9269 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009270 if (s->req.buf->p > s->req.buf->data &&
9271 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
9272 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +01009273
Willy Tarreau14174bc2012-04-16 14:34:04 +02009274 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009275 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009276 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009277
9278 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009279 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009280 http_msg_analyzer(msg, &txn->hdr_idx);
9281
9282 /* Still no valid request ? */
9283 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009284 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009285 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009286 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009287 }
9288 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009289 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009290 return 0;
9291 }
9292
9293 /* OK we just got a valid HTTP request. We have some minor
9294 * preparation to perform so that further checks can rely
9295 * on HTTP tests.
9296 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009297
9298 /* If the request was parsed but was too large, we must absolutely
9299 * return an error so that it is not processed. At the moment this
9300 * cannot happen, but if the parsers are to change in the future,
9301 * we want this check to be maintained.
9302 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009303 if (unlikely(s->req.buf->i + s->req.buf->p >
9304 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01009305 msg->err_state = msg->msg_state;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009306 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009307 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009308 return 1;
9309 }
9310
Willy Tarreau9b28e032012-10-12 23:49:43 +02009311 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009312 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02009313 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009314
Willy Tarreau506d0502013-07-06 13:29:24 +02009315 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9316 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009317 }
9318
Willy Tarreau506d0502013-07-06 13:29:24 +02009319 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009320 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009321 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009322
9323 /* otherwise everything's ready for the request */
9324 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009325 else {
9326 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009327 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9328 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009329 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009330 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009331 }
9332
9333 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009334 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009335 return 1;
9336}
Willy Tarreau8797c062007-05-07 00:55:35 +02009337
Willy Tarreau8797c062007-05-07 00:55:35 +02009338/* 1. Check on METHOD
9339 * We use the pre-parsed method if it is known, and store its number as an
9340 * integer. If it is unknown, we use the pointer and the length.
9341 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009342static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009343{
9344 int len, meth;
9345
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009346 len = strlen(text);
9347 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009348
9349 pattern->val.i = meth;
9350 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +02009351 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009352 pattern->len = len;
9353 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009354 else {
9355 pattern->ptr.str = NULL;
9356 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009357 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009358 return 1;
9359}
9360
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009361/* This function fetches the method of current HTTP request and stores
9362 * it in the global pattern struct as a chunk. There are two possibilities :
9363 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9364 * in <len> and <ptr> is NULL ;
9365 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9366 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009367 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009368 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009369static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009370smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009371{
9372 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +01009373 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009374
Willy Tarreau24e32d82012-04-23 23:55:44 +02009375 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009376
Willy Tarreaube508f12016-03-10 11:47:01 +01009377 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009378 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009379 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009380 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009381 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009382 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9383 /* ensure the indexes are not affected */
9384 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009385 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009386 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
9387 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009388 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009389 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009390 return 1;
9391}
9392
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009393/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009394static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009395{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009396 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009397 struct pattern_list *lst;
9398 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009399
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009400 list_for_each_entry(lst, &expr->patterns, list) {
9401 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009402
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009403 /* well-known method */
9404 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009405 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009406 return pattern;
9407 else
9408 continue;
9409 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009410
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009411 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009412 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009413 continue;
9414
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009415 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009416 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
9417 (!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 +01009418 return pattern;
9419 }
9420 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009421}
9422
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009423static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009424smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009425{
Willy Tarreaube508f12016-03-10 11:47:01 +01009426 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009427 char *ptr;
9428 int len;
9429
Willy Tarreauc0239e02012-04-16 14:42:55 +02009430 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009431
Willy Tarreaube508f12016-03-10 11:47:01 +01009432 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009433 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009434 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009435
9436 while ((len-- > 0) && (*ptr++ != '/'));
9437 if (len <= 0)
9438 return 0;
9439
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009440 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009441 smp->data.u.str.str = ptr;
9442 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009443
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009444 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009445 return 1;
9446}
9447
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009448static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009449smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009450{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009451 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009452 char *ptr;
9453 int len;
9454
Willy Tarreauc0239e02012-04-16 14:42:55 +02009455 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009456
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009457 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009458 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9459 return 0;
9460
Willy Tarreau8797c062007-05-07 00:55:35 +02009461 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009462 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009463
9464 while ((len-- > 0) && (*ptr++ != '/'));
9465 if (len <= 0)
9466 return 0;
9467
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009468 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009469 smp->data.u.str.str = ptr;
9470 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009471
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009472 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009473 return 1;
9474}
9475
9476/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009477static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009478smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009479{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009480 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009481 char *ptr;
9482 int len;
9483
Willy Tarreauc0239e02012-04-16 14:42:55 +02009484 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009485
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009486 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009487 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9488 return 0;
9489
Willy Tarreau8797c062007-05-07 00:55:35 +02009490 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009491 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009492
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009493 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009494 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009495 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009496 return 1;
9497}
9498
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009499static int
9500smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
9501{
9502 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
9503 return 0;
9504
9505 if (!smp->strm->unique_id) {
9506 if ((smp->strm->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
9507 return 0;
9508 smp->strm->unique_id[0] = '\0';
9509 }
9510 smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id,
9511 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
9512
9513 smp->data.type = SMP_T_STR;
9514 smp->data.u.str.str = smp->strm->unique_id;
9515 smp->flags = SMP_F_CONST;
9516 return 1;
9517}
9518
Thierry FOURNIERd7d88812017-04-19 15:15:14 +02009519/* Returns a string block containing all headers including the
9520 * empty line wich separes headers from the body. This is useful
9521 * form some headers analysis.
9522 */
9523static int
9524smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
9525{
9526 struct http_msg *msg;
9527 struct hdr_idx *idx;
9528 struct http_txn *txn;
9529
9530 CHECK_HTTP_MESSAGE_FIRST();
9531
9532 txn = smp->strm->txn;
9533 idx = &txn->hdr_idx;
9534 msg = &txn->req;
9535
9536 smp->data.type = SMP_T_STR;
9537 smp->data.u.str.str = msg->chn->buf->p + hdr_idx_first_pos(idx);
9538 smp->data.u.str.len = msg->eoh - hdr_idx_first_pos(idx) + 1 +
9539 (msg->chn->buf->p[msg->eoh] == '\r');
9540
9541 return 1;
9542}
9543
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009544/* Returns the header request in a length/value encoded format.
9545 * This is useful for exchanges with the SPOE.
9546 *
9547 * A "length value" is a multibyte code encoding numbers. It uses the
9548 * SPOE format. The encoding is the following:
9549 *
9550 * Each couple "header name" / "header value" is composed
9551 * like this:
9552 * "length value" "header name bytes"
9553 * "length value" "header value bytes"
9554 * When the last header is reached, the header name and the header
9555 * value are empty. Their length are 0
9556 */
9557static int
9558smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
9559{
9560 struct http_msg *msg;
9561 struct chunk *temp;
9562 struct hdr_idx *idx;
9563 const char *cur_ptr, *cur_next, *p;
9564 int old_idx, cur_idx;
9565 struct hdr_idx_elem *cur_hdr;
9566 const char *hn, *hv;
9567 int hnl, hvl;
9568 int ret;
9569 struct http_txn *txn;
9570 char *buf;
9571 char *end;
9572
9573 CHECK_HTTP_MESSAGE_FIRST();
9574
9575 temp = get_trash_chunk();
9576 buf = temp->str;
9577 end = temp->str + temp->size;
9578
9579 txn = smp->strm->txn;
9580 idx = &txn->hdr_idx;
9581 msg = &txn->req;
9582
9583 /* Build array of headers. */
9584 old_idx = 0;
9585 cur_next = msg->chn->buf->p + hdr_idx_first_pos(idx);
9586 while (1) {
9587 cur_idx = idx->v[old_idx].next;
9588 if (!cur_idx)
9589 break;
9590 old_idx = cur_idx;
9591
9592 cur_hdr = &idx->v[cur_idx];
9593 cur_ptr = cur_next;
9594 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
9595
9596 /* Now we have one full header at cur_ptr of len cur_hdr->len,
9597 * and the next header starts at cur_next. We'll check
9598 * this header in the list as well as against the default
9599 * rule.
9600 */
9601
9602 /* look for ': *'. */
9603 hn = cur_ptr;
9604 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
9605 if (p >= cur_ptr+cur_hdr->len)
9606 continue;
9607 hnl = p - hn;
9608 p++;
9609 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
9610 p++;
9611 if (p >= cur_ptr + cur_hdr->len)
9612 continue;
9613 hv = p;
9614 hvl = cur_ptr + cur_hdr->len-p;
9615
9616 /* encode the header name. */
9617 ret = encode_varint(hnl, &buf, end);
9618 if (ret == -1)
9619 return 0;
9620 if (buf + hnl > end)
9621 return 0;
9622 memcpy(buf, hn, hnl);
9623 buf += hnl;
9624
9625 /* encode and copy the value. */
9626 ret = encode_varint(hvl, &buf, end);
9627 if (ret == -1)
9628 return 0;
9629 if (buf + hvl > end)
9630 return 0;
9631 memcpy(buf, hv, hvl);
9632 buf += hvl;
9633 }
9634
9635 /* encode the end of the header list with empty
9636 * header name and header value.
9637 */
9638 ret = encode_varint(0, &buf, end);
9639 if (ret == -1)
9640 return 0;
9641 ret = encode_varint(0, &buf, end);
9642 if (ret == -1)
9643 return 0;
9644
9645 /* Initialise sample data which will be filled. */
9646 smp->data.type = SMP_T_BIN;
9647 smp->data.u.str.str = temp->str;
9648 smp->data.u.str.len = buf - temp->str;
9649 smp->data.u.str.size = temp->size;
9650
9651 return 1;
9652}
9653
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009654/* returns the longest available part of the body. This requires that the body
9655 * has been waited for using http-buffer-request.
9656 */
9657static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009658smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009659{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009660 struct http_msg *msg;
9661 unsigned long len;
9662 unsigned long block1;
9663 char *body;
9664 struct chunk *temp;
9665
9666 CHECK_HTTP_MESSAGE_FIRST();
9667
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009668 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009669 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009670 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009671 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009672
9673 len = http_body_bytes(msg);
9674 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
9675
9676 block1 = len;
9677 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
9678 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
9679
9680 if (block1 == len) {
9681 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009682 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009683 smp->data.u.str.str = body;
9684 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009685 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
9686 }
9687 else {
9688 /* buffer is wrapped, we need to defragment it */
9689 temp = get_trash_chunk();
9690 memcpy(temp->str, body, block1);
9691 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009692 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009693 smp->data.u.str.str = temp->str;
9694 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009695 smp->flags = SMP_F_VOL_TEST;
9696 }
9697 return 1;
9698}
9699
9700
9701/* returns the available length of the body. This requires that the body
9702 * has been waited for using http-buffer-request.
9703 */
9704static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009705smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009706{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009707 struct http_msg *msg;
9708
9709 CHECK_HTTP_MESSAGE_FIRST();
9710
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009711 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009712 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009713 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009714 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009715
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009716 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009717 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009718
9719 smp->flags = SMP_F_VOL_TEST;
9720 return 1;
9721}
9722
9723
9724/* returns the advertised length of the body, or the advertised size of the
9725 * chunks available in the buffer. This requires that the body has been waited
9726 * for using http-buffer-request.
9727 */
9728static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009729smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009730{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009731 struct http_msg *msg;
9732
9733 CHECK_HTTP_MESSAGE_FIRST();
9734
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009735 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009736 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009737 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009738 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009739
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009740 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009741 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009742
9743 smp->flags = SMP_F_VOL_TEST;
9744 return 1;
9745}
9746
9747
Willy Tarreau8797c062007-05-07 00:55:35 +02009748/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009749static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009750smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009751{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009752 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009753
Willy Tarreauc0239e02012-04-16 14:42:55 +02009754 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009755
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009756 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009757 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009758 smp->data.u.str.len = txn->req.sl.rq.u_l;
9759 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009760 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009761 return 1;
9762}
9763
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009764static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009765smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009766{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009767 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009768 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009769
Willy Tarreauc0239e02012-04-16 14:42:55 +02009770 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009771
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009772 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009773 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 +02009774 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009775 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009776
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009777 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009778 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009779 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009780 return 1;
9781}
9782
9783static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009784smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009785{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009786 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009787 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009788
Willy Tarreauc0239e02012-04-16 14:42:55 +02009789 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009790
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009791 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009792 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 +02009793 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
9794 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009795
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009796 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009797 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009798 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009799 return 1;
9800}
9801
Willy Tarreau185b5c42012-04-26 15:11:51 +02009802/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9803 * Accepts an optional argument of type string containing the header field name,
9804 * and an optional argument of type signed or unsigned integer to request an
9805 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009806 * headers are considered from the first one. It does not stop on commas and
9807 * returns full lines instead (useful for User-Agent or Date for example).
9808 */
9809static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009810smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009811{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009812 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009813 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +02009814 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009815 int occ = 0;
9816 const char *name_str = NULL;
9817 int name_len = 0;
9818
9819 if (!ctx) {
9820 /* first call */
9821 ctx = &static_hdr_ctx;
9822 ctx->idx = 0;
9823 smp->ctx.a[0] = ctx;
9824 }
9825
9826 if (args) {
9827 if (args[0].type != ARGT_STR)
9828 return 0;
9829 name_str = args[0].data.str.str;
9830 name_len = args[0].data.str.len;
9831
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02009832 if (args[1].type == ARGT_SINT)
9833 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009834 }
9835
9836 CHECK_HTTP_MESSAGE_FIRST();
9837
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009838 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009839 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 +02009840
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009841 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9842 /* search for header from the beginning */
9843 ctx->idx = 0;
9844
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009845 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009846 /* no explicit occurrence and single fetch => last header by default */
9847 occ = -1;
9848
9849 if (!occ)
9850 /* prepare to report multiple occurrences for ACL fetches */
9851 smp->flags |= SMP_F_NOT_LAST;
9852
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009853 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009854 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009855 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 +02009856 return 1;
9857
9858 smp->flags &= ~SMP_F_NOT_LAST;
9859 return 0;
9860}
9861
9862/* 6. Check on HTTP header count. The number of occurrences is returned.
9863 * Accepts exactly 1 argument of type string. It does not stop on commas and
9864 * returns full lines instead (useful for User-Agent or Date for example).
9865 */
9866static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009867smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009868{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009869 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009870 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +02009871 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009872 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +02009873 const char *name = NULL;
9874 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009875
Willy Tarreau601a4d12015-04-01 19:16:09 +02009876 if (args && args->type == ARGT_STR) {
9877 name = args->data.str.str;
9878 len = args->data.str.len;
9879 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009880
9881 CHECK_HTTP_MESSAGE_FIRST();
9882
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009883 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009884 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 +02009885
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009886 ctx.idx = 0;
9887 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +02009888 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009889 cnt++;
9890
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009891 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009892 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009893 smp->flags = SMP_F_VOL_HDR;
9894 return 1;
9895}
9896
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009897static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009898smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009899{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009900 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009901 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +02009902 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009903 struct chunk *temp;
9904 char del = ',';
9905
9906 if (args && args->type == ARGT_STR)
9907 del = *args[0].data.str.str;
9908
9909 CHECK_HTTP_MESSAGE_FIRST();
9910
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009911 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009912 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 +02009913
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009914 temp = get_trash_chunk();
9915
9916 ctx.idx = 0;
9917 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
9918 if (temp->len)
9919 temp->str[temp->len++] = del;
9920 memcpy(temp->str + temp->len, ctx.line, ctx.del);
9921 temp->len += ctx.del;
9922 }
9923
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009924 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009925 smp->data.u.str.str = temp->str;
9926 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009927 smp->flags = SMP_F_VOL_HDR;
9928 return 1;
9929}
9930
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009931/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9932 * Accepts an optional argument of type string containing the header field name,
9933 * and an optional argument of type signed or unsigned integer to request an
9934 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009935 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009936 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009937static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009938smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009939{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009940 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009941 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +02009942 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009943 int occ = 0;
9944 const char *name_str = NULL;
9945 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009946
Willy Tarreaua890d072013-04-02 12:01:06 +02009947 if (!ctx) {
9948 /* first call */
9949 ctx = &static_hdr_ctx;
9950 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02009951 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009952 }
9953
Willy Tarreau185b5c42012-04-26 15:11:51 +02009954 if (args) {
9955 if (args[0].type != ARGT_STR)
9956 return 0;
9957 name_str = args[0].data.str.str;
9958 name_len = args[0].data.str.len;
9959
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02009960 if (args[1].type == ARGT_SINT)
9961 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009962 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009963
Willy Tarreaue333ec92012-04-16 16:26:40 +02009964 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02009965
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009966 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009967 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 +02009968
Willy Tarreau185b5c42012-04-26 15:11:51 +02009969 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009970 /* search for header from the beginning */
9971 ctx->idx = 0;
9972
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009973 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +02009974 /* no explicit occurrence and single fetch => last header by default */
9975 occ = -1;
9976
9977 if (!occ)
9978 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02009979 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01009980
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009981 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009982 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009983 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 +02009984 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009985
Willy Tarreau37406352012-04-23 16:16:37 +02009986 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009987 return 0;
9988}
9989
Willy Tarreauc11416f2007-06-17 16:58:38 +02009990/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02009991 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009992 */
9993static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009994smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009995{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009996 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009997 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +02009998 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009999 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010000 const char *name = NULL;
10001 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010002
Willy Tarreau601a4d12015-04-01 19:16:09 +020010003 if (args && args->type == ARGT_STR) {
10004 name = args->data.str.str;
10005 len = args->data.str.len;
10006 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010007
Willy Tarreaue333ec92012-04-16 16:26:40 +020010008 CHECK_HTTP_MESSAGE_FIRST();
10009
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010010 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010011 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 +020010012
Willy Tarreau33a7e692007-06-10 19:45:56 +020010013 ctx.idx = 0;
10014 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010015 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010016 cnt++;
10017
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010018 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010019 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010020 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010021 return 1;
10022}
10023
Willy Tarreau185b5c42012-04-26 15:11:51 +020010024/* Fetch an HTTP header's integer value. The integer value is returned. It
10025 * takes a mandatory argument of type string and an optional one of type int
10026 * to designate a specific occurrence. It returns an unsigned integer, which
10027 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010028 */
10029static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010030smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010031{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010032 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010033
Willy Tarreauf853c462012-04-23 18:53:56 +020010034 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010035 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010036 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010037 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010038
Willy Tarreaud53e2422012-04-16 17:21:11 +020010039 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010040}
10041
Cyril Bonté69fa9922012-10-25 00:01:06 +020010042/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10043 * and an optional one of type int to designate a specific occurrence.
10044 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010045 */
10046static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010047smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010048{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010049 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010050
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010051 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010052 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010053 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010054 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010055 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010056 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010057 if (smp->data.u.str.len < temp->size - 1) {
10058 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10059 temp->str[smp->data.u.str.len] = '\0';
10060 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010061 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010062 break;
10063 }
10064 }
10065 }
10066
Willy Tarreaud53e2422012-04-16 17:21:11 +020010067 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010068 if (!(smp->flags & SMP_F_NOT_LAST))
10069 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010070 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010071 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010072}
10073
Willy Tarreau737b0c12007-06-10 21:28:46 +020010074/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10075 * the first '/' after the possible hostname, and ends before the possible '?'.
10076 */
10077static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010078smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010079{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010080 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010081 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010082
Willy Tarreauc0239e02012-04-16 14:42:55 +020010083 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010084
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010085 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010086 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010087 ptr = http_get_path(txn);
10088 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010089 return 0;
10090
10091 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010092 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010093 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010094
10095 while (ptr < end && *ptr != '?')
10096 ptr++;
10097
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010098 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010099 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010100 return 1;
10101}
10102
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010103/* This produces a concatenation of the first occurrence of the Host header
10104 * followed by the path component if it begins with a slash ('/'). This means
10105 * that '*' will not be added, resulting in exactly the first Host entry.
10106 * If no Host header is found, then the path is returned as-is. The returned
10107 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010108 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010109 */
10110static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010111smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010112{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010113 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010114 char *ptr, *end, *beg;
10115 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010116 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010117
10118 CHECK_HTTP_MESSAGE_FIRST();
10119
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010120 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010121 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010122 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010123 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010124
10125 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010126 temp = get_trash_chunk();
10127 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010128 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010129 smp->data.u.str.str = temp->str;
10130 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010131
10132 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010133 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010134 beg = http_get_path(txn);
10135 if (!beg)
10136 beg = end;
10137
10138 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10139
10140 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010141 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10142 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010143 }
10144
10145 smp->flags = SMP_F_VOL_1ST;
10146 return 1;
10147}
10148
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010149/* This produces a 32-bit hash of the concatenation of the first occurrence of
10150 * the Host header followed by the path component if it begins with a slash ('/').
10151 * This means that '*' will not be added, resulting in exactly the first Host
10152 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010153 * is hashed using the path hash followed by a full avalanche hash and provides a
10154 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010155 * high-traffic sites without having to store whole paths.
10156 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010157int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010158smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010159{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010160 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010161 struct hdr_ctx ctx;
10162 unsigned int hash = 0;
10163 char *ptr, *beg, *end;
10164 int len;
10165
10166 CHECK_HTTP_MESSAGE_FIRST();
10167
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010168 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010169 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010170 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010171 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10172 ptr = ctx.line + ctx.val;
10173 len = ctx.vlen;
10174 while (len--)
10175 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10176 }
10177
10178 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010179 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010180 beg = http_get_path(txn);
10181 if (!beg)
10182 beg = end;
10183
10184 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10185
10186 if (beg < ptr && *beg == '/') {
10187 while (beg < ptr)
10188 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10189 }
10190 hash = full_hash(hash);
10191
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010192 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010193 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010194 smp->flags = SMP_F_VOL_1ST;
10195 return 1;
10196}
10197
Willy Tarreau4a550602012-12-09 14:53:32 +010010198/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010199 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10200 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10201 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010202 * that in environments where IPv6 is insignificant, truncating the output to
10203 * 8 bytes would still work.
10204 */
10205static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010206smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010207{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010208 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010209 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010210
10211 if (!cli_conn)
10212 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010213
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010214 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010215 return 0;
10216
Willy Tarreau47ca5452012-12-23 20:22:19 +010010217 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010218 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010219 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010220
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010221 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010222 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010223 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010224 temp->len += 4;
10225 break;
10226 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010227 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010228 temp->len += 16;
10229 break;
10230 default:
10231 return 0;
10232 }
10233
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010234 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010235 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010236 return 1;
10237}
10238
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010239/* Extracts the query string, which comes after the question mark '?'. If no
10240 * question mark is found, nothing is returned. Otherwise it returns a sample
10241 * of type string carrying the whole query string.
10242 */
10243static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010244smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010245{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010246 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010247 char *ptr, *end;
10248
10249 CHECK_HTTP_MESSAGE_FIRST();
10250
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010251 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010252 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10253 end = ptr + txn->req.sl.rq.u_l;
10254
10255 /* look up the '?' */
10256 do {
10257 if (ptr == end)
10258 return 0;
10259 } while (*ptr++ != '?');
10260
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010261 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010262 smp->data.u.str.str = ptr;
10263 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010264 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10265 return 1;
10266}
10267
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010268static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010269smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010270{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010271 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10272 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10273 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010274
Willy Tarreau24e32d82012-04-23 23:55:44 +020010275 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010276
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010277 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010278 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010279 return 1;
10280}
10281
Willy Tarreau7f18e522010-10-22 20:04:13 +020010282/* return a valid test if the current request is the first one on the connection */
10283static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010284smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010285{
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 = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010288 return 1;
10289}
10290
Willy Tarreau34db1082012-04-19 17:16:54 +020010291/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010292static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010293smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010294{
10295
Willy Tarreau24e32d82012-04-23 23:55:44 +020010296 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010297 return 0;
10298
Willy Tarreauc0239e02012-04-16 14:42:55 +020010299 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010300
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010301 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010302 return 0;
10303
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010304 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010305 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010306 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010307 return 1;
10308}
Willy Tarreau8797c062007-05-07 00:55:35 +020010309
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010310/* Accepts exactly 1 argument of type userlist */
10311static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010312smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010313{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010314 if (!args || args->type != ARGT_USR)
10315 return 0;
10316
10317 CHECK_HTTP_MESSAGE_FIRST();
10318
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010319 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010320 return 0;
10321
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010322 /* if the user does not belong to the userlist or has a wrong password,
10323 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010324 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010325 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010326 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10327 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010328 return 0;
10329
10330 /* pat_match_auth() will need the user list */
10331 smp->ctx.a[0] = args->data.usr;
10332
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010333 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010334 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010335 smp->data.u.str.str = smp->strm->txn->auth.user;
10336 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010337
10338 return 1;
10339}
10340
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010341/* Try to find the next occurrence of a cookie name in a cookie header value.
10342 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10343 * the cookie value is returned into *value and *value_l, and the function
10344 * returns a pointer to the next pointer to search from if the value was found.
10345 * Otherwise if the cookie was not found, NULL is returned and neither value
10346 * nor value_l are touched. The input <hdr> string should first point to the
10347 * header's value, and the <hdr_end> pointer must point to the first character
10348 * not part of the value. <list> must be non-zero if value may represent a list
10349 * of values (cookie headers). This makes it faster to abort parsing when no
10350 * list is expected.
10351 */
David Carlier4686f792015-09-25 14:10:50 +010010352char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010353extract_cookie_value(char *hdr, const char *hdr_end,
10354 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010355 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010356{
10357 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10358 char *next;
10359
10360 /* we search at least a cookie name followed by an equal, and more
10361 * generally something like this :
10362 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10363 */
10364 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10365 /* Iterate through all cookies on this line */
10366
Willy Tarreau2235b262016-11-05 15:50:20 +010010367 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010368 att_beg++;
10369
10370 /* find att_end : this is the first character after the last non
10371 * space before the equal. It may be equal to hdr_end.
10372 */
10373 equal = att_end = att_beg;
10374
10375 while (equal < hdr_end) {
10376 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10377 break;
Willy Tarreau2235b262016-11-05 15:50:20 +010010378 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010379 continue;
10380 att_end = equal;
10381 }
10382
10383 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10384 * is between <att_beg> and <equal>, both may be identical.
10385 */
10386
10387 /* look for end of cookie if there is an equal sign */
10388 if (equal < hdr_end && *equal == '=') {
10389 /* look for the beginning of the value */
10390 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +010010391 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010392 val_beg++;
10393
10394 /* find the end of the value, respecting quotes */
10395 next = find_cookie_value_end(val_beg, hdr_end);
10396
10397 /* make val_end point to the first white space or delimitor after the value */
10398 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +010010399 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010400 val_end--;
10401 } else {
10402 val_beg = val_end = next = equal;
10403 }
10404
10405 /* We have nothing to do with attributes beginning with '$'. However,
10406 * they will automatically be removed if a header before them is removed,
10407 * since they're supposed to be linked together.
10408 */
10409 if (*att_beg == '$')
10410 continue;
10411
10412 /* Ignore cookies with no equal sign */
10413 if (equal == next)
10414 continue;
10415
10416 /* Now we have the cookie name between att_beg and att_end, and
10417 * its value between val_beg and val_end.
10418 */
10419
10420 if (att_end - att_beg == cookie_name_l &&
10421 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10422 /* let's return this value and indicate where to go on from */
10423 *value = val_beg;
10424 *value_l = val_end - val_beg;
10425 return next + 1;
10426 }
10427
10428 /* Set-Cookie headers only have the name in the first attr=value part */
10429 if (!list)
10430 break;
10431 }
10432
10433 return NULL;
10434}
10435
William Lallemanda43ba4e2014-01-28 18:14:25 +010010436/* Fetch a captured HTTP request header. The index is the position of
10437 * the "capture" option in the configuration file
10438 */
10439static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010440smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010441{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010442 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010443 int idx;
10444
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010445 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010446 return 0;
10447
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010448 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010449
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010450 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 +010010451 return 0;
10452
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010453 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010454 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010455 smp->data.u.str.str = smp->strm->req_cap[idx];
10456 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010457
10458 return 1;
10459}
10460
10461/* Fetch a captured HTTP response header. The index is the position of
10462 * the "capture" option in the configuration file
10463 */
10464static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010465smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010466{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010467 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010468 int idx;
10469
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010470 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010471 return 0;
10472
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010473 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010474
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010475 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 +010010476 return 0;
10477
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010478 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010479 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010480 smp->data.u.str.str = smp->strm->res_cap[idx];
10481 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010482
10483 return 1;
10484}
10485
William Lallemand65ad6e12014-01-31 15:08:02 +010010486/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10487static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010488smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010489{
10490 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010491 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010010492 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010493
Willy Tarreau15e91e12015-04-04 00:52:09 +020010494 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010495 return 0;
10496
William Lallemand96a77852014-02-05 00:30:02 +010010497 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010498
William Lallemand96a77852014-02-05 00:30:02 +010010499 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10500 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010501
William Lallemand96a77852014-02-05 00:30:02 +010010502 temp = get_trash_chunk();
10503 temp->str = txn->uri;
10504 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010505 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010506 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010507 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010508
10509 return 1;
10510
10511}
10512
10513/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10514static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010515smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010516{
10517 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010518 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010010519 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010520
Willy Tarreau15e91e12015-04-04 00:52:09 +020010521 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010522 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010523
William Lallemand65ad6e12014-01-31 15:08:02 +010010524 ptr = txn->uri;
10525
10526 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10527 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010528
William Lallemand65ad6e12014-01-31 15:08:02 +010010529 if (!*ptr)
10530 return 0;
10531
10532 ptr++; /* skip the space */
10533
10534 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010535 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010536 if (!ptr)
10537 return 0;
10538 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10539 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010540
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010541 smp->data.u.str = *temp;
10542 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010543 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010544 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010545
10546 return 1;
10547}
10548
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010549/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10550 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10551 */
10552static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010553smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010554{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010555 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010556
Willy Tarreau15e91e12015-04-04 00:52:09 +020010557 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010558 return 0;
10559
10560 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010561 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010562 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010563 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010564
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010565 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010566 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010567 smp->flags = SMP_F_CONST;
10568 return 1;
10569
10570}
10571
10572/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10573 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10574 */
10575static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010576smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010577{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010578 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010579
Willy Tarreau15e91e12015-04-04 00:52:09 +020010580 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010581 return 0;
10582
10583 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010584 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010585 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010586 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010587
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010588 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010589 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010590 smp->flags = SMP_F_CONST;
10591 return 1;
10592
10593}
10594
William Lallemand65ad6e12014-01-31 15:08:02 +010010595
Willy Tarreaue333ec92012-04-16 16:26:40 +020010596/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010597 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010598 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010599 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010600 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010601 * Accepts exactly 1 argument of type string. If the input options indicate
10602 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010603 * The returned sample is of type CSTR. Can be used to parse cookies in other
10604 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010605 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010606int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010607{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010608 struct http_txn *txn;
10609 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010610 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010611 const struct http_msg *msg;
10612 const char *hdr_name;
10613 int hdr_name_len;
10614 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010615 int occ = 0;
10616 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010617
Willy Tarreau24e32d82012-04-23 23:55:44 +020010618 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010619 return 0;
10620
Willy Tarreaua890d072013-04-02 12:01:06 +020010621 if (!ctx) {
10622 /* first call */
10623 ctx = &static_hdr_ctx;
10624 ctx->idx = 0;
10625 smp->ctx.a[2] = ctx;
10626 }
10627
Willy Tarreaue333ec92012-04-16 16:26:40 +020010628 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010629
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010630 txn = smp->strm->txn;
10631 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010632
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010633 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010634 msg = &txn->req;
10635 hdr_name = "Cookie";
10636 hdr_name_len = 6;
10637 } else {
10638 msg = &txn->rsp;
10639 hdr_name = "Set-Cookie";
10640 hdr_name_len = 10;
10641 }
10642
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010643 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020010644 /* no explicit occurrence and single fetch => last cookie by default */
10645 occ = -1;
10646
10647 /* OK so basically here, either we want only one value and it's the
10648 * last one, or we want to iterate over all of them and we fetch the
10649 * next one.
10650 */
10651
Willy Tarreau9b28e032012-10-12 23:49:43 +020010652 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010653 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010654 /* search for the header from the beginning, we must first initialize
10655 * the search parameters.
10656 */
Willy Tarreau37406352012-04-23 16:16:37 +020010657 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010658 ctx->idx = 0;
10659 }
10660
Willy Tarreau28376d62012-04-26 21:26:10 +020010661 smp->flags |= SMP_F_VOL_HDR;
10662
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010663 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010664 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10665 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010666 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10667 goto out;
10668
Willy Tarreau24e32d82012-04-23 23:55:44 +020010669 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010670 continue;
10671
Willy Tarreau37406352012-04-23 16:16:37 +020010672 smp->ctx.a[0] = ctx->line + ctx->val;
10673 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010674 }
10675
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010676 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010677 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010678 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010679 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010680 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010681 &smp->data.u.str.str,
10682 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010683 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010684 found = 1;
10685 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010686 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020010687 smp->flags |= SMP_F_NOT_LAST;
10688 return 1;
10689 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010690 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010691 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010692 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010693 /* all cookie headers and values were scanned. If we're looking for the
10694 * last occurrence, we may return it now.
10695 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010696 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010697 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010698 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010699}
10700
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010701/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010702 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010703 * multiple cookies may be parsed on the same line. The returned sample is of
10704 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010705 */
10706static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010707smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010708{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010709 struct http_txn *txn;
10710 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010711 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010712 const struct http_msg *msg;
10713 const char *hdr_name;
10714 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010715 int cnt;
10716 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010717 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010718
Willy Tarreau24e32d82012-04-23 23:55:44 +020010719 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010720 return 0;
10721
Willy Tarreaue333ec92012-04-16 16:26:40 +020010722 CHECK_HTTP_MESSAGE_FIRST();
10723
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010724 txn = smp->strm->txn;
10725 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010726
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010727 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010728 msg = &txn->req;
10729 hdr_name = "Cookie";
10730 hdr_name_len = 6;
10731 } else {
10732 msg = &txn->rsp;
10733 hdr_name = "Set-Cookie";
10734 hdr_name_len = 10;
10735 }
10736
Willy Tarreau9b28e032012-10-12 23:49:43 +020010737 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010738 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010739 ctx.idx = 0;
10740 cnt = 0;
10741
10742 while (1) {
10743 /* Note: val_beg == NULL every time we need to fetch a new header */
10744 if (!val_beg) {
10745 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10746 break;
10747
Willy Tarreau24e32d82012-04-23 23:55:44 +020010748 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010749 continue;
10750
10751 val_beg = ctx.line + ctx.val;
10752 val_end = val_beg + ctx.vlen;
10753 }
10754
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010755 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010756 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010757 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010758 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010759 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010760 &smp->data.u.str.str,
10761 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010762 cnt++;
10763 }
10764 }
10765
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010766 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010767 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010768 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010769 return 1;
10770}
10771
Willy Tarreau51539362012-05-08 12:46:28 +020010772/* Fetch an cookie's integer value. The integer value is returned. It
10773 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10774 */
10775static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010776smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020010777{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010778 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020010779
10780 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010781 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010782 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020010783 }
10784
10785 return ret;
10786}
10787
Willy Tarreau8797c062007-05-07 00:55:35 +020010788/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010789/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010790/************************************************************************/
10791
David Cournapeau16023ee2010-12-23 20:55:41 +090010792/*
10793 * Given a path string and its length, find the position of beginning of the
10794 * query string. Returns NULL if no query string is found in the path.
10795 *
10796 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
10797 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010798 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090010799 */
bedis4c75cca2012-10-05 08:38:24 +020010800static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010801{
10802 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020010803
bedis4c75cca2012-10-05 08:38:24 +020010804 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090010805 return p ? p + 1 : NULL;
10806}
10807
bedis4c75cca2012-10-05 08:38:24 +020010808static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010809{
bedis4c75cca2012-10-05 08:38:24 +020010810 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090010811}
10812
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010813/* after increasing a pointer value, it can exceed the first buffer
10814 * size. This function transform the value of <ptr> according with
10815 * the expected position. <chunks> is an array of the one or two
10816 * avalaible chunks. The first value is the start of the first chunk,
10817 * the second value if the end+1 of the first chunks. The third value
10818 * is NULL or the start of the second chunk and the fourth value is
10819 * the end+1 of the second chunk. The function returns 1 if does a
10820 * wrap, else returns 0.
10821 */
10822static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
10823{
10824 if (*ptr < chunks[1])
10825 return 0;
10826 if (!chunks[2])
10827 return 0;
10828 *ptr = chunks[2] + ( *ptr - chunks[1] );
10829 return 1;
10830}
10831
David Cournapeau16023ee2010-12-23 20:55:41 +090010832/*
10833 * Given a url parameter, find the starting position of the first occurence,
10834 * or NULL if the parameter is not found.
10835 *
10836 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
10837 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010838 *
Willy Tarreauf6625822015-12-27 14:51:01 +010010839 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010840 * or the second chunk. The caller must be check the position before using the
10841 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090010842 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010843static const char *
10844find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010845 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020010846 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010847{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010848 const char *pos, *last, *equal;
10849 const char **bufs = chunks;
10850 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090010851
David Cournapeau16023ee2010-12-23 20:55:41 +090010852
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010853 pos = bufs[0];
10854 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010010855 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010856 /* Check the equal. */
10857 equal = pos + url_param_name_l;
10858 if (fix_pointer_if_wrap(chunks, &equal)) {
10859 if (equal >= chunks[3])
10860 return NULL;
10861 } else {
10862 if (equal >= chunks[1])
10863 return NULL;
10864 }
10865 if (*equal == '=') {
10866 if (pos + url_param_name_l > last) {
10867 /* process wrap case, we detect a wrap. In this case, the
10868 * comparison is performed in two parts.
10869 */
10870
10871 /* This is the end, we dont have any other chunk. */
10872 if (bufs != chunks || !bufs[2])
10873 return NULL;
10874
10875 /* Compute the length of each part of the comparison. */
10876 l1 = last - pos;
10877 l2 = url_param_name_l - l1;
10878
10879 /* The second buffer is too short to contain the compared string. */
10880 if (bufs[2] + l2 > bufs[3])
10881 return NULL;
10882
10883 if (memcmp(pos, url_param_name, l1) == 0 &&
10884 memcmp(bufs[2], url_param_name+l1, l2) == 0)
10885 return pos;
10886
10887 /* Perform wrapping and jump the string who fail the comparison. */
10888 bufs += 2;
10889 pos = bufs[0] + l2;
10890 last = bufs[1];
10891
10892 } else {
10893 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010010894 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
10895 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010896 pos += url_param_name_l + 1;
10897 if (fix_pointer_if_wrap(chunks, &pos))
10898 last = bufs[2];
10899 }
10900 }
10901
10902 while (1) {
10903 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010010904 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010905 pos++;
10906 if (pos < last)
10907 break;
10908 /* process buffer wrapping. */
10909 if (bufs != chunks || !bufs[2])
10910 return NULL;
10911 bufs += 2;
10912 pos = bufs[0];
10913 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090010914 }
David Cournapeau16023ee2010-12-23 20:55:41 +090010915 pos++;
10916 }
10917 return NULL;
10918}
10919
10920/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010010921 * Given a url parameter name and a query string, find the next value.
10922 * An empty url_param_name matches the first available parameter.
10923 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
10924 * respectively provide a pointer to the value and its end.
10925 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090010926 */
10927static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010928find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010929 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010930 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010931{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010932 const char *arg_start, *qs_end;
10933 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090010934
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010935 arg_start = chunks[0];
10936 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010937 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010938 /* Looks for an argument name. */
10939 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010940 url_param_name, url_param_name_l,
10941 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010942 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010010943 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010944 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010945 }
David Cournapeau16023ee2010-12-23 20:55:41 +090010946 if (!arg_start)
10947 return 0;
10948
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010949 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010950 while (1) {
10951 /* looks for the first argument. */
10952 value_start = memchr(arg_start, '=', qs_end - arg_start);
10953 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010954 /* Check for wrapping. */
10955 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010010956 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010957 chunks[2]) {
10958 arg_start = chunks[2];
10959 qs_end = chunks[3];
10960 continue;
10961 }
10962 return 0;
10963 }
10964 break;
10965 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010966 value_start++;
10967 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010968 else {
10969 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010970 value_start = arg_start + url_param_name_l + 1;
10971
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010972 /* Check for pointer wrapping. */
10973 if (fix_pointer_if_wrap(chunks, &value_start)) {
10974 /* Update the end pointer. */
10975 qs_end = chunks[3];
10976
10977 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010010978 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010979 return 0;
10980 }
10981 }
10982
David Cournapeau16023ee2010-12-23 20:55:41 +090010983 value_end = value_start;
10984
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010985 while (1) {
10986 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
10987 value_end++;
10988 if (value_end < qs_end)
10989 break;
10990 /* process buffer wrapping. */
10991 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010010992 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010993 chunks[2]) {
10994 value_end = chunks[2];
10995 qs_end = chunks[3];
10996 continue;
10997 }
10998 break;
10999 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011000
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011001 *vstart = value_start;
11002 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011003 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011004}
11005
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011006/* This scans a URL-encoded query string. It takes an optionally wrapping
11007 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11008 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11009 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011010 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011011static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011012smp_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 +090011013{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011014 const char *vstart, *vend;
11015 struct chunk *temp;
11016 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011017
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011018 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011019 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011020 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011021 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011022 return 0;
11023
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011024 /* Create sample. If the value is contiguous, return the pointer as CONST,
11025 * if the value is wrapped, copy-it in a buffer.
11026 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011027 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011028 if (chunks[2] &&
11029 vstart >= chunks[0] && vstart <= chunks[1] &&
11030 vend >= chunks[2] && vend <= chunks[3]) {
11031 /* Wrapped case. */
11032 temp = get_trash_chunk();
11033 memcpy(temp->str, vstart, chunks[1] - vstart);
11034 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011035 smp->data.u.str.str = temp->str;
11036 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011037 } else {
11038 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011039 smp->data.u.str.str = (char *)vstart;
11040 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011041 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11042 }
11043
11044 /* Update context, check wrapping. */
11045 chunks[0] = vend;
11046 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11047 chunks[1] = chunks[3];
11048 chunks[2] = NULL;
11049 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011050
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011051 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011052 smp->flags |= SMP_F_NOT_LAST;
11053
David Cournapeau16023ee2010-12-23 20:55:41 +090011054 return 1;
11055}
11056
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011057/* This function iterates over each parameter of the query string. It uses
11058 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011059 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11060 * An optional parameter name is passed in args[0], otherwise any parameter is
11061 * considered. It supports an optional delimiter argument for the beginning of
11062 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011063 */
11064static int
11065smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11066{
11067 struct http_msg *msg;
11068 char delim = '?';
11069 const char *name;
11070 int name_len;
11071
Dragan Dosen26f77e52015-05-25 10:02:11 +020011072 if (!args ||
11073 (args[0].type && args[0].type != ARGT_STR) ||
11074 (args[1].type && args[1].type != ARGT_STR))
11075 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011076
Dragan Dosen26f77e52015-05-25 10:02:11 +020011077 name = "";
11078 name_len = 0;
11079 if (args->type == ARGT_STR) {
11080 name = args->data.str.str;
11081 name_len = args->data.str.len;
11082 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011083
Dragan Dosen26f77e52015-05-25 10:02:11 +020011084 if (args[1].type)
11085 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011086
Dragan Dosen26f77e52015-05-25 10:02:11 +020011087 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011088 CHECK_HTTP_MESSAGE_FIRST();
11089
11090 msg = &smp->strm->txn->req;
11091
11092 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11093 msg->sl.rq.u_l, delim);
11094 if (!smp->ctx.a[0])
11095 return 0;
11096
11097 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011098
11099 /* Assume that the context is filled with NULL pointer
11100 * before the first call.
11101 * smp->ctx.a[2] = NULL;
11102 * smp->ctx.a[3] = NULL;
11103 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011104 }
11105
11106 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11107}
11108
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011109/* This function iterates over each parameter of the body. This requires
11110 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011111 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11112 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11113 * optional second part if the body wraps at the end of the buffer. An optional
11114 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011115 */
11116static int
11117smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11118{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011119 struct http_msg *msg;
11120 unsigned long len;
11121 unsigned long block1;
11122 char *body;
11123 const char *name;
11124 int name_len;
11125
11126 if (!args || (args[0].type && args[0].type != ARGT_STR))
11127 return 0;
11128
11129 name = "";
11130 name_len = 0;
11131 if (args[0].type == ARGT_STR) {
11132 name = args[0].data.str.str;
11133 name_len = args[0].data.str.len;
11134 }
11135
11136 if (!smp->ctx.a[0]) { // first call, find the query string
11137 CHECK_HTTP_MESSAGE_FIRST();
11138
11139 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010011140 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011141 else
Willy Tarreaube508f12016-03-10 11:47:01 +010011142 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011143
11144 len = http_body_bytes(msg);
11145 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11146
11147 block1 = len;
11148 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11149 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11150
11151 if (block1 == len) {
11152 /* buffer is not wrapped (or empty) */
11153 smp->ctx.a[0] = body;
11154 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011155
11156 /* Assume that the context is filled with NULL pointer
11157 * before the first call.
11158 * smp->ctx.a[2] = NULL;
11159 * smp->ctx.a[3] = NULL;
11160 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011161 }
11162 else {
11163 /* buffer is wrapped, we need to defragment it */
11164 smp->ctx.a[0] = body;
11165 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011166 smp->ctx.a[2] = msg->chn->buf->data;
11167 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011168 }
11169 }
11170 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11171}
11172
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011173/* Return the signed integer value for the specified url parameter (see url_param
11174 * above).
11175 */
11176static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011177smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011178{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011179 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011180
11181 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011182 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011183 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011184 }
11185
11186 return ret;
11187}
11188
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011189/* This produces a 32-bit hash of the concatenation of the first occurrence of
11190 * the Host header followed by the path component if it begins with a slash ('/').
11191 * This means that '*' will not be added, resulting in exactly the first Host
11192 * entry. If no Host header is found, then the path is used. The resulting value
11193 * is hashed using the url hash followed by a full avalanche hash and provides a
11194 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11195 * high-traffic sites without having to store whole paths.
11196 * this differs from the base32 functions in that it includes the url parameters
11197 * as well as the path
11198 */
11199static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011200smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011201{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011202 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011203 struct hdr_ctx ctx;
11204 unsigned int hash = 0;
11205 char *ptr, *beg, *end;
11206 int len;
11207
11208 CHECK_HTTP_MESSAGE_FIRST();
11209
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011210 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011211 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011212 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011213 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11214 ptr = ctx.line + ctx.val;
11215 len = ctx.vlen;
11216 while (len--)
11217 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11218 }
11219
11220 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011221 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 +000011222 beg = http_get_path(txn);
11223 if (!beg)
11224 beg = end;
11225
11226 for (ptr = beg; ptr < end ; ptr++);
11227
11228 if (beg < ptr && *beg == '/') {
11229 while (beg < ptr)
11230 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11231 }
11232 hash = full_hash(hash);
11233
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011234 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011235 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011236 smp->flags = SMP_F_VOL_1ST;
11237 return 1;
11238}
11239
11240/* This concatenates the source address with the 32-bit hash of the Host and
11241 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11242 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11243 * on the source address length. The URL hash is stored before the address so
11244 * that in environments where IPv6 is insignificant, truncating the output to
11245 * 8 bytes would still work.
11246 */
11247static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011248smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011249{
11250 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011251 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011252
Dragan Dosendb5af612016-06-16 11:23:01 +020011253 if (!cli_conn)
11254 return 0;
11255
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011256 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011257 return 0;
11258
11259 temp = get_trash_chunk();
Dragan Dosene5f41332016-06-16 11:08:08 +020011260 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
11261 temp->len += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011262
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011263 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011264 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011265 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011266 temp->len += 4;
11267 break;
11268 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011269 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011270 temp->len += 16;
11271 break;
11272 default:
11273 return 0;
11274 }
11275
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011276 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011277 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011278 return 1;
11279}
11280
Willy Tarreau185b5c42012-04-26 15:11:51 +020011281/* This function is used to validate the arguments passed to any "hdr" fetch
11282 * keyword. These keywords support an optional positive or negative occurrence
11283 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11284 * is assumed that the types are already the correct ones. Returns 0 on error,
11285 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11286 * error message in case of error, that the caller is responsible for freeing.
11287 * The initial location must either be freeable or NULL.
11288 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011289int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011290{
11291 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011292 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011293 return 0;
11294 }
11295 return 1;
11296}
11297
Willy Tarreau276fae92013-07-25 14:36:01 +020011298/* takes an UINT value on input supposed to represent the time since EPOCH,
11299 * adds an optional offset found in args[0] and emits a string representing
11300 * the date in RFC-1123/5322 format.
11301 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011302static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011303{
Cyril Bontéf78d8962016-01-22 19:40:28 +010011304 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020011305 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11306 struct chunk *temp;
11307 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011308 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011309 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020011310
11311 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011312 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020011313 curr_date += args[0].data.sint;
11314
11315 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020011316 if (!tm)
11317 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011318
11319 temp = get_trash_chunk();
11320 temp->len = snprintf(temp->str, temp->size - temp->len,
11321 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11322 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11323 tm->tm_hour, tm->tm_min, tm->tm_sec);
11324
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011325 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011326 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020011327 return 1;
11328}
11329
Thierry FOURNIERad903512014-04-11 17:51:01 +020011330/* Match language range with language tag. RFC2616 14.4:
11331 *
11332 * A language-range matches a language-tag if it exactly equals
11333 * the tag, or if it exactly equals a prefix of the tag such
11334 * that the first tag character following the prefix is "-".
11335 *
11336 * Return 1 if the strings match, else return 0.
11337 */
11338static inline int language_range_match(const char *range, int range_len,
11339 const char *tag, int tag_len)
11340{
11341 const char *end = range + range_len;
11342 const char *tend = tag + tag_len;
11343 while (range < end) {
11344 if (*range == '-' && tag == tend)
11345 return 1;
11346 if (*range != *tag || tag == tend)
11347 return 0;
11348 range++;
11349 tag++;
11350 }
11351 /* Return true only if the last char of the tag is matched. */
11352 return tag == tend;
11353}
11354
11355/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011356static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011357{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011358 const char *al = smp->data.u.str.str;
11359 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011360 const char *token;
11361 int toklen;
11362 int qvalue;
11363 const char *str;
11364 const char *w;
11365 int best_q = 0;
11366
11367 /* Set the constant to the sample, because the output of the
11368 * function will be peek in the constant configuration string.
11369 */
11370 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011371 smp->data.u.str.size = 0;
11372 smp->data.u.str.str = "";
11373 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011374
11375 /* Parse the accept language */
11376 while (1) {
11377
11378 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011379 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011380 al++;
11381 if (al >= end)
11382 break;
11383
11384 /* Start of the fisrt word. */
11385 token = al;
11386
11387 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011388 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011389 al++;
11390 if (al == token)
11391 goto expect_comma;
11392
11393 /* Length of the token. */
11394 toklen = al - token;
11395 qvalue = 1000;
11396
11397 /* Check if the token exists in the list. If the token not exists,
11398 * jump to the next token.
11399 */
11400 str = args[0].data.str.str;
11401 w = str;
11402 while (1) {
11403 if (*str == ';' || *str == '\0') {
11404 if (language_range_match(token, toklen, w, str-w))
11405 goto look_for_q;
11406 if (*str == '\0')
11407 goto expect_comma;
11408 w = str + 1;
11409 }
11410 str++;
11411 }
11412 goto expect_comma;
11413
11414look_for_q:
11415
11416 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011417 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011418 al++;
11419 if (al >= end)
11420 goto process_value;
11421
11422 /* If ',' is found, process the result */
11423 if (*al == ',')
11424 goto process_value;
11425
11426 /* If the character is different from ';', look
11427 * for the end of the header part in best effort.
11428 */
11429 if (*al != ';')
11430 goto expect_comma;
11431
11432 /* Assumes that the char is ';', now expect "q=". */
11433 al++;
11434
11435 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011436 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011437 al++;
11438 if (al >= end)
11439 goto process_value;
11440
11441 /* Expect 'q'. If no 'q', continue in best effort */
11442 if (*al != 'q')
11443 goto process_value;
11444 al++;
11445
11446 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011447 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011448 al++;
11449 if (al >= end)
11450 goto process_value;
11451
11452 /* Expect '='. If no '=', continue in best effort */
11453 if (*al != '=')
11454 goto process_value;
11455 al++;
11456
11457 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011458 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011459 al++;
11460 if (al >= end)
11461 goto process_value;
11462
11463 /* Parse the q value. */
11464 qvalue = parse_qvalue(al, &al);
11465
11466process_value:
11467
11468 /* If the new q value is the best q value, then store the associated
11469 * language in the response. If qvalue is the biggest value (1000),
11470 * break the process.
11471 */
11472 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011473 smp->data.u.str.str = (char *)w;
11474 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011475 if (qvalue >= 1000)
11476 break;
11477 best_q = qvalue;
11478 }
11479
11480expect_comma:
11481
11482 /* Expect comma or end. If the end is detected, quit the loop. */
11483 while (al < end && *al != ',')
11484 al++;
11485 if (al >= end)
11486 break;
11487
11488 /* Comma is found, jump it and restart the analyzer. */
11489 al++;
11490 }
11491
11492 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011493 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
11494 smp->data.u.str.str = args[1].data.str.str;
11495 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011496 }
11497
11498 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011499 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011500}
11501
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011502/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011503static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011504{
11505 /* If the constant flag is set or if not size is avalaible at
11506 * the end of the buffer, copy the string in other buffer
11507 * before decoding.
11508 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011509 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011510 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011511 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
11512 smp->data.u.str.str = str->str;
11513 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011514 smp->flags &= ~SMP_F_CONST;
11515 }
11516
11517 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011518 smp->data.u.str.str[smp->data.u.str.len] = '\0';
11519 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Christopher Fauleta2584792017-10-05 10:03:12 +020011520 return (smp->data.u.str.len >= 0);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011521}
11522
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011523static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
11524{
11525 struct proxy *fe = strm_fe(smp->strm);
11526 int idx, i;
11527 struct cap_hdr *hdr;
11528 int len;
11529
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011530 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011531 return 0;
11532
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011533 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011534
11535 /* Check the availibity of the capture id. */
11536 if (idx > fe->nb_req_cap - 1)
11537 return 0;
11538
11539 /* Look for the original configuration. */
11540 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
11541 hdr != NULL && i != idx ;
11542 i--, hdr = hdr->next);
11543 if (!hdr)
11544 return 0;
11545
11546 /* check for the memory allocation */
11547 if (smp->strm->req_cap[hdr->index] == NULL)
11548 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
11549 if (smp->strm->req_cap[hdr->index] == NULL)
11550 return 0;
11551
11552 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011553 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011554 if (len > hdr->len)
11555 len = hdr->len;
11556
11557 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011558 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011559 smp->strm->req_cap[idx][len] = '\0';
11560
11561 return 1;
11562}
11563
11564static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
11565{
11566 struct proxy *fe = strm_fe(smp->strm);
11567 int idx, i;
11568 struct cap_hdr *hdr;
11569 int len;
11570
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011571 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011572 return 0;
11573
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011574 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011575
11576 /* Check the availibity of the capture id. */
11577 if (idx > fe->nb_rsp_cap - 1)
11578 return 0;
11579
11580 /* Look for the original configuration. */
11581 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
11582 hdr != NULL && i != idx ;
11583 i--, hdr = hdr->next);
11584 if (!hdr)
11585 return 0;
11586
11587 /* check for the memory allocation */
11588 if (smp->strm->res_cap[hdr->index] == NULL)
11589 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
11590 if (smp->strm->res_cap[hdr->index] == NULL)
11591 return 0;
11592
11593 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011594 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011595 if (len > hdr->len)
11596 len = hdr->len;
11597
11598 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011599 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011600 smp->strm->res_cap[idx][len] = '\0';
11601
11602 return 1;
11603}
11604
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011605/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011606 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011607 * the relevant part of the request line accordingly. Then it updates various
11608 * pointers to the next elements which were moved, and the total buffer length.
11609 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011610 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
11611 * error, though this can be revisited when this code is finally exploited.
11612 *
11613 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
11614 * query string and 3 to replace uri.
11615 *
11616 * In query string case, the mark question '?' must be set at the start of the
11617 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011618 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011619int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011620 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011621{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011622 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011623 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011624 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011625 int delta;
11626
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011627 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011628 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011629 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011630 cur_end = cur_ptr + txn->req.sl.rq.m_l;
11631
11632 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011633 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011634 txn->req.sl.rq.m_l += delta;
11635 txn->req.sl.rq.u += delta;
11636 txn->req.sl.rq.v += delta;
11637 break;
11638
11639 case 1: // path
11640 cur_ptr = http_get_path(txn);
11641 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011642 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011643
11644 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011645 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 +010011646 cur_end++;
11647
11648 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011649 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011650 txn->req.sl.rq.u_l += delta;
11651 txn->req.sl.rq.v += delta;
11652 break;
11653
11654 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011655 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011656 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011657 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11658 while (cur_ptr < cur_end && *cur_ptr != '?')
11659 cur_ptr++;
11660
11661 /* skip the question mark or indicate that we must insert it
11662 * (but only if the format string is not empty then).
11663 */
11664 if (cur_ptr < cur_end)
11665 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011666 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011667 offset = 0;
11668
11669 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011670 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011671 txn->req.sl.rq.u_l += delta;
11672 txn->req.sl.rq.v += delta;
11673 break;
11674
11675 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011676 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011677 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11678
11679 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011680 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011681 txn->req.sl.rq.u_l += delta;
11682 txn->req.sl.rq.v += delta;
11683 break;
11684
11685 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011686 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011687 }
11688
11689 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011690 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020011691 txn->req.sl.rq.l += delta;
11692 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011693 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011694 return 0;
11695}
11696
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011697/* This function replace the HTTP status code and the associated message. The
11698 * variable <status> contains the new status code. This function never fails.
11699 */
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011700void http_set_status(unsigned int status, const char *reason, struct stream *s)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011701{
11702 struct http_txn *txn = s->txn;
11703 char *cur_ptr, *cur_end;
11704 int delta;
11705 char *res;
11706 int c_l;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011707 const char *msg = reason;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011708 int msg_len;
11709
11710 chunk_reset(&trash);
11711
11712 res = ultoa_o(status, trash.str, trash.size);
11713 c_l = res - trash.str;
11714
11715 trash.str[c_l] = ' ';
11716 trash.len = c_l + 1;
11717
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011718 /* Do we have a custom reason format string? */
11719 if (msg == NULL)
11720 msg = get_reason(status);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011721 msg_len = strlen(msg);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011722 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
11723 trash.len += msg_len;
11724
11725 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
11726 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
11727
11728 /* commit changes and adjust message */
11729 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
11730
11731 /* adjust res line offsets and lengths */
11732 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
11733 txn->rsp.sl.st.c_l = c_l;
11734 txn->rsp.sl.st.r_l = msg_len;
11735
11736 delta = trash.len - (cur_end - cur_ptr);
11737 txn->rsp.sl.st.l += delta;
11738 txn->hdr_idx.v[0].len += delta;
11739 http_msg_move_end(&txn->rsp, delta);
11740}
11741
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011742/* This function executes one of the set-{method,path,query,uri} actions. It
11743 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020011744 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011745 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020011746 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
11747 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011748 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011749enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011750 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011751{
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011752 struct chunk *replace;
11753 enum act_return ret = ACT_RET_ERR;
11754
11755 replace = alloc_trash_chunk();
11756 if (!replace)
11757 goto leave;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011758
11759 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011760 if (rule->arg.http.action == 2)
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011761 replace->str[replace->len++] = '?';
11762 replace->len += build_logline(s, replace->str + replace->len, replace->size - replace->len,
11763 &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011764
Dragan Dosen2ae327e2017-10-26 11:25:10 +020011765 http_replace_req_line(rule->arg.http.action, replace->str, replace->len, px, s);
11766
11767 ret = ACT_RET_CONT;
11768
11769leave:
11770 free_trash_chunk(replace);
11771 return ret;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011772}
11773
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011774/* This function is just a compliant action wrapper for "set-status". */
11775enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011776 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011777{
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011778 http_set_status(rule->arg.status.code, rule->arg.status.reason, s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011779 return ACT_RET_CONT;
11780}
11781
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011782/* parse an http-request action among :
11783 * set-method
11784 * set-path
11785 * set-query
11786 * set-uri
11787 *
11788 * All of them accept a single argument of type string representing a log-format.
11789 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
11790 * 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 +020011791 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011792 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011793enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
11794 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011795{
11796 int cur_arg = *orig_arg;
11797
Thierry FOURNIER42148732015-09-02 17:17:33 +020011798 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011799
11800 switch (args[0][4]) {
11801 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011802 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011803 rule->action_ptr = http_action_set_req_line;
11804 break;
11805 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011806 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011807 rule->action_ptr = http_action_set_req_line;
11808 break;
11809 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011810 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011811 rule->action_ptr = http_action_set_req_line;
11812 break;
11813 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011814 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011815 rule->action_ptr = http_action_set_req_line;
11816 break;
11817 default:
11818 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011819 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011820 }
11821
11822 if (!*args[cur_arg] ||
11823 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
11824 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011825 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011826 }
11827
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011828 LIST_INIT(&rule->arg.http.logfmt);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011829 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010011830 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +010011831 (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 +010011832 return ACT_RET_PRS_ERR;
11833 }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011834
11835 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011836 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011837}
11838
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011839/* parse set-status action:
11840 * This action accepts a single argument of type int representing
11841 * an http status code. It returns ACT_RET_PRS_OK on success,
11842 * ACT_RET_PRS_ERR on error.
11843 */
11844enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
11845 struct act_rule *rule, char **err)
11846{
11847 char *error;
11848
Thierry FOURNIER42148732015-09-02 17:17:33 +020011849 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011850 rule->action_ptr = action_http_set_status;
11851
11852 /* Check if an argument is available */
11853 if (!*args[*orig_arg]) {
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011854 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011855 return ACT_RET_PRS_ERR;
11856 }
11857
11858 /* convert status code as integer */
11859 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
11860 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
11861 memprintf(err, "expects an integer status code between 100 and 999");
11862 return ACT_RET_PRS_ERR;
11863 }
11864
11865 (*orig_arg)++;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011866
11867 /* set custom reason string */
11868 rule->arg.status.reason = NULL; // If null, we use the default reason for the status code.
11869 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
11870 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
11871 (*orig_arg)++;
11872 rule->arg.status.reason = strdup(args[*orig_arg]);
11873 (*orig_arg)++;
11874 }
11875
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011876 return ACT_RET_PRS_OK;
11877}
11878
Willy Tarreaua9083d02015-05-08 15:27:59 +020011879/* This function executes the "capture" action. It executes a fetch expression,
11880 * turns the result into a string and puts it in a capture slot. It always
11881 * returns 1. If an error occurs the action is cancelled, but the rule
11882 * processing continues.
11883 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011884enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011885 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020011886{
Willy Tarreaua9083d02015-05-08 15:27:59 +020011887 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020011888 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011889 char **cap = s->req_cap;
11890 int len;
11891
Thierry FOURNIER32b15002015-07-31 08:56:16 +020011892 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 +020011893 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011894 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011895
11896 if (cap[h->index] == NULL)
11897 cap[h->index] = pool_alloc2(h->pool);
11898
11899 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011900 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011901
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011902 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011903 if (len > h->len)
11904 len = h->len;
11905
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011906 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020011907 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011908 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011909}
11910
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011911/* This function executes the "capture" action and store the result in a
11912 * capture slot if exists. It executes a fetch expression, turns the result
11913 * into a string and puts it in a capture slot. It always returns 1. If an
11914 * error occurs the action is cancelled, but the rule processing continues.
11915 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011916enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011917 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011918{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011919 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011920 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011921 char **cap = s->req_cap;
11922 struct proxy *fe = strm_fe(s);
11923 int len;
11924 int i;
11925
11926 /* Look for the original configuration. */
11927 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020011928 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011929 i--, h = h->next);
11930 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011931 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011932
Thierry FOURNIERe2097972015-07-31 08:56:35 +020011933 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 +020011934 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011935 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011936
11937 if (cap[h->index] == NULL)
11938 cap[h->index] = pool_alloc2(h->pool);
11939
11940 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011941 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011942
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011943 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011944 if (len > h->len)
11945 len = h->len;
11946
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011947 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011948 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011949 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011950}
11951
Christopher Faulet29730ba2017-09-18 15:26:32 +020011952/* Check an "http-request capture" action.
11953 *
11954 * The function returns 1 in success case, otherwise, it returns 0 and err is
11955 * filled.
11956 */
11957int check_http_req_capture(struct act_rule *rule, struct proxy *px, char **err)
11958{
11959 if (rule->arg.capid.idx >= px->nb_req_cap) {
11960 memprintf(err, "unable to find capture id '%d' referenced by http-request capture rule",
11961 rule->arg.capid.idx);
11962 return 0;
11963 }
11964
11965 return 1;
11966}
11967
Willy Tarreaua9083d02015-05-08 15:27:59 +020011968/* parse an "http-request capture" action. It takes a single argument which is
11969 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011970 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011971 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020011972 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011973enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
11974 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020011975{
11976 struct sample_expr *expr;
11977 struct cap_hdr *hdr;
11978 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020011979 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011980
11981 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
11982 if (strcmp(args[cur_arg], "if") == 0 ||
11983 strcmp(args[cur_arg], "unless") == 0)
11984 break;
11985
11986 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011987 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011988 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011989 }
11990
Willy Tarreaua9083d02015-05-08 15:27:59 +020011991 cur_arg = *orig_arg;
11992 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
11993 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011994 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011995
11996 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
11997 memprintf(err,
11998 "fetch method '%s' extracts information from '%s', none of which is available here",
11999 args[cur_arg-1], sample_src_names(expr->fetch->use));
12000 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012001 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012002 }
12003
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012004 if (!args[cur_arg] || !*args[cur_arg]) {
12005 memprintf(err, "expects 'len or 'id'");
12006 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012007 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012008 }
12009
Willy Tarreaua9083d02015-05-08 15:27:59 +020012010 if (strcmp(args[cur_arg], "len") == 0) {
12011 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012012
12013 if (!(px->cap & PR_CAP_FE)) {
12014 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012015 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012016 }
12017
12018 proxy->conf.args.ctx = ARGC_CAP;
12019
Willy Tarreaua9083d02015-05-08 15:27:59 +020012020 if (!args[cur_arg]) {
12021 memprintf(err, "missing length value");
12022 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012023 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012024 }
12025 /* we copy the table name for now, it will be resolved later */
12026 len = atoi(args[cur_arg]);
12027 if (len <= 0) {
12028 memprintf(err, "length must be > 0");
12029 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012030 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012031 }
12032 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012033
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012034 if (!len) {
12035 memprintf(err, "a positive 'len' argument is mandatory");
12036 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012037 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012038 }
12039
Vincent Bernat02779b62016-04-03 13:48:43 +020012040 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012041 hdr->next = px->req_cap;
12042 hdr->name = NULL; /* not a header capture */
12043 hdr->namelen = 0;
12044 hdr->len = len;
12045 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12046 hdr->index = px->nb_req_cap++;
12047
12048 px->req_cap = hdr;
12049 px->to_log |= LW_REQHDR;
12050
Thierry FOURNIER42148732015-09-02 17:17:33 +020012051 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012052 rule->action_ptr = http_action_req_capture;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012053 rule->check_ptr = check_http_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012054 rule->arg.cap.expr = expr;
12055 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012056 }
12057
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012058 else if (strcmp(args[cur_arg], "id") == 0) {
12059 int id;
12060 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012061
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012062 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012063
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012064 if (!args[cur_arg]) {
12065 memprintf(err, "missing id value");
12066 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012067 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012068 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012069
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012070 id = strtol(args[cur_arg], &error, 10);
12071 if (*error != '\0') {
12072 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12073 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012074 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012075 }
12076 cur_arg++;
12077
12078 proxy->conf.args.ctx = ARGC_CAP;
12079
Thierry FOURNIER42148732015-09-02 17:17:33 +020012080 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012081 rule->action_ptr = http_action_req_capture_by_id;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012082 rule->check_ptr = check_http_req_capture;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012083 rule->arg.capid.expr = expr;
12084 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012085 }
12086
12087 else {
12088 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12089 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012090 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012091 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012092
12093 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012094 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012095}
12096
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012097/* This function executes the "capture" action and store the result in a
12098 * capture slot if exists. It executes a fetch expression, turns the result
12099 * into a string and puts it in a capture slot. It always returns 1. If an
12100 * error occurs the action is cancelled, but the rule processing continues.
12101 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012102enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012103 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012104{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012105 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012106 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012107 char **cap = s->res_cap;
12108 struct proxy *fe = strm_fe(s);
12109 int len;
12110 int i;
12111
12112 /* Look for the original configuration. */
12113 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012114 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012115 i--, h = h->next);
12116 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012117 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012118
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012119 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 +020012120 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012121 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012122
12123 if (cap[h->index] == NULL)
12124 cap[h->index] = pool_alloc2(h->pool);
12125
12126 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012127 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012128
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012129 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012130 if (len > h->len)
12131 len = h->len;
12132
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012133 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012134 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012135 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012136}
12137
Christopher Faulet29730ba2017-09-18 15:26:32 +020012138/* Check an "http-response capture" action.
12139 *
12140 * The function returns 1 in success case, otherwise, it returns 0 and err is
12141 * filled.
12142 */
12143int check_http_res_capture(struct act_rule *rule, struct proxy *px, char **err)
12144{
12145 if (rule->arg.capid.idx >= px->nb_rsp_cap) {
12146 memprintf(err, "unable to find capture id '%d' referenced by http-response capture rule",
12147 rule->arg.capid.idx);
12148 return 0;
12149 }
12150
12151 return 1;
12152}
12153
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012154/* parse an "http-response capture" action. It takes a single argument which is
12155 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12156 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012157 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012158 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012159enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12160 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012161{
12162 struct sample_expr *expr;
12163 int cur_arg;
12164 int id;
12165 char *error;
12166
12167 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12168 if (strcmp(args[cur_arg], "if") == 0 ||
12169 strcmp(args[cur_arg], "unless") == 0)
12170 break;
12171
12172 if (cur_arg < *orig_arg + 3) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012173 memprintf(err, "expects <expression> id <idx>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012174 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012175 }
12176
12177 cur_arg = *orig_arg;
12178 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12179 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012180 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012181
12182 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12183 memprintf(err,
12184 "fetch method '%s' extracts information from '%s', none of which is available here",
12185 args[cur_arg-1], sample_src_names(expr->fetch->use));
12186 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012187 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012188 }
12189
12190 if (!args[cur_arg] || !*args[cur_arg]) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012191 memprintf(err, "expects 'id'");
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012192 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012193 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012194 }
12195
12196 if (strcmp(args[cur_arg], "id") != 0) {
12197 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12198 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012199 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012200 }
12201
12202 cur_arg++;
12203
12204 if (!args[cur_arg]) {
12205 memprintf(err, "missing id value");
12206 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012207 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012208 }
12209
12210 id = strtol(args[cur_arg], &error, 10);
12211 if (*error != '\0') {
12212 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12213 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012214 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012215 }
12216 cur_arg++;
12217
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012218 proxy->conf.args.ctx = ARGC_CAP;
12219
Thierry FOURNIER42148732015-09-02 17:17:33 +020012220 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012221 rule->action_ptr = http_action_res_capture_by_id;
Christopher Faulet29730ba2017-09-18 15:26:32 +020012222 rule->check_ptr = check_http_res_capture;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012223 rule->arg.capid.expr = expr;
12224 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012225
12226 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012227 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012228}
12229
William Lallemand73025dd2014-04-24 14:38:37 +020012230/*
12231 * Return the struct http_req_action_kw associated to a keyword.
12232 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012233struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012234{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012235 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012236}
12237
12238/*
12239 * Return the struct http_res_action_kw associated to a keyword.
12240 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012241struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012242{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012243 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012244}
12245
Willy Tarreau12207b32016-11-22 19:48:51 +010012246
12247/* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop
12248 * now.
12249 */
12250static int cli_parse_show_errors(char **args, struct appctx *appctx, void *private)
12251{
12252 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
12253 return 1;
12254
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012255 if (*args[2]) {
12256 struct proxy *px;
12257
12258 px = proxy_find_by_name(args[2], 0, 0);
12259 if (px)
12260 appctx->ctx.errors.iid = px->uuid;
12261 else
12262 appctx->ctx.errors.iid = atoi(args[2]);
12263
12264 if (!appctx->ctx.errors.iid) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +020012265 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012266 appctx->ctx.cli.msg = "No such proxy.\n";
12267 appctx->st0 = CLI_ST_PRINT;
12268 return 1;
12269 }
12270 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012271 else
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012272 appctx->ctx.errors.iid = -1; // dump all proxies
12273
Willy Tarreau35069f82016-11-25 09:16:37 +010012274 appctx->ctx.errors.flag = 0;
12275 if (strcmp(args[3], "request") == 0)
12276 appctx->ctx.errors.flag |= 4; // ignore response
12277 else if (strcmp(args[3], "response") == 0)
12278 appctx->ctx.errors.flag |= 2; // ignore request
Willy Tarreau12207b32016-11-22 19:48:51 +010012279 appctx->ctx.errors.px = NULL;
Willy Tarreau12207b32016-11-22 19:48:51 +010012280 return 0;
12281}
12282
12283/* This function dumps all captured errors onto the stream interface's
12284 * read buffer. It returns 0 if the output buffer is full and it needs
12285 * to be called again, otherwise non-zero.
12286 */
12287static int cli_io_handler_show_errors(struct appctx *appctx)
12288{
12289 struct stream_interface *si = appctx->owner;
12290 extern const char *monthname[12];
12291
12292 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
12293 return 1;
12294
12295 chunk_reset(&trash);
12296
12297 if (!appctx->ctx.errors.px) {
12298 /* the function had not been called yet, let's prepare the
12299 * buffer for a response.
12300 */
12301 struct tm tm;
12302
12303 get_localtime(date.tv_sec, &tm);
12304 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
12305 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12306 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
12307 error_snapshot_id);
12308
Willy Tarreau06d80a92017-10-19 14:32:15 +020012309 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012310 /* Socket buffer full. Let's try again later from the same point */
12311 si_applet_cant_put(si);
12312 return 0;
12313 }
12314
12315 appctx->ctx.errors.px = proxy;
Willy Tarreau12207b32016-11-22 19:48:51 +010012316 appctx->ctx.errors.bol = 0;
12317 appctx->ctx.errors.ptr = -1;
12318 }
12319
12320 /* we have two inner loops here, one for the proxy, the other one for
12321 * the buffer.
12322 */
12323 while (appctx->ctx.errors.px) {
12324 struct error_snapshot *es;
12325
Willy Tarreau35069f82016-11-25 09:16:37 +010012326 if ((appctx->ctx.errors.flag & 1) == 0) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012327 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau35069f82016-11-25 09:16:37 +010012328 if (appctx->ctx.errors.flag & 2) // skip req
12329 goto next;
12330 }
12331 else {
Willy Tarreau12207b32016-11-22 19:48:51 +010012332 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau35069f82016-11-25 09:16:37 +010012333 if (appctx->ctx.errors.flag & 4) // skip resp
12334 goto next;
12335 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012336
12337 if (!es->when.tv_sec)
12338 goto next;
12339
12340 if (appctx->ctx.errors.iid >= 0 &&
12341 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
12342 es->oe->uuid != appctx->ctx.errors.iid)
12343 goto next;
12344
12345 if (appctx->ctx.errors.ptr < 0) {
12346 /* just print headers now */
12347
12348 char pn[INET6_ADDRSTRLEN];
12349 struct tm tm;
12350 int port;
12351
12352 get_localtime(es->when.tv_sec, &tm);
12353 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
12354 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12355 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
12356
12357 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
12358 case AF_INET:
12359 case AF_INET6:
12360 port = get_host_port(&es->src);
12361 break;
12362 default:
12363 port = 0;
12364 }
12365
Willy Tarreau35069f82016-11-25 09:16:37 +010012366 switch (appctx->ctx.errors.flag & 1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012367 case 0:
12368 chunk_appendf(&trash,
12369 " frontend %s (#%d): invalid request\n"
12370 " backend %s (#%d)",
12371 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
12372 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
12373 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
12374 break;
12375 case 1:
12376 chunk_appendf(&trash,
12377 " backend %s (#%d): invalid response\n"
12378 " frontend %s (#%d)",
12379 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
12380 es->oe->id, es->oe->uuid);
12381 break;
12382 }
12383
12384 chunk_appendf(&trash,
12385 ", server %s (#%d), event #%u\n"
12386 " src %s:%d, session #%d, session flags 0x%08x\n"
Willy Tarreau10e61cb2017-01-04 14:51:22 +010012387 " HTTP msg state %s(%d), msg flags 0x%08x, tx flags 0x%08x\n"
Willy Tarreau12207b32016-11-22 19:48:51 +010012388 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
12389 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
12390 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
12391 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
12392 es->ev_id,
12393 pn, port, es->sid, es->s_flags,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020012394 h1_msg_state_str(es->state), es->state, es->m_flags, es->t_flags,
Willy Tarreau12207b32016-11-22 19:48:51 +010012395 es->m_clen, es->m_blen,
12396 es->b_flags, es->b_out, es->b_tot,
12397 es->len, es->b_wrap, es->pos);
12398
Willy Tarreau06d80a92017-10-19 14:32:15 +020012399 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012400 /* Socket buffer full. Let's try again later from the same point */
12401 si_applet_cant_put(si);
12402 return 0;
12403 }
12404 appctx->ctx.errors.ptr = 0;
12405 appctx->ctx.errors.sid = es->sid;
12406 }
12407
12408 if (appctx->ctx.errors.sid != es->sid) {
12409 /* the snapshot changed while we were dumping it */
12410 chunk_appendf(&trash,
12411 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +020012412 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012413 si_applet_cant_put(si);
12414 return 0;
12415 }
12416 goto next;
12417 }
12418
12419 /* OK, ptr >= 0, so we have to dump the current line */
12420 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
12421 int newptr;
12422 int newline;
12423
12424 newline = appctx->ctx.errors.bol;
12425 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
12426 if (newptr == appctx->ctx.errors.ptr)
12427 return 0;
12428
Willy Tarreau06d80a92017-10-19 14:32:15 +020012429 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012430 /* Socket buffer full. Let's try again later from the same point */
12431 si_applet_cant_put(si);
12432 return 0;
12433 }
12434 appctx->ctx.errors.ptr = newptr;
12435 appctx->ctx.errors.bol = newline;
12436 };
12437 next:
12438 appctx->ctx.errors.bol = 0;
12439 appctx->ctx.errors.ptr = -1;
Willy Tarreau35069f82016-11-25 09:16:37 +010012440 appctx->ctx.errors.flag ^= 1;
12441 if (!(appctx->ctx.errors.flag & 1))
Willy Tarreau12207b32016-11-22 19:48:51 +010012442 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau12207b32016-11-22 19:48:51 +010012443 }
12444
12445 /* dump complete */
12446 return 1;
12447}
12448
12449/* register cli keywords */
12450static struct cli_kw_list cli_kws = {{ },{
12451 { { "show", "errors", NULL },
12452 "show errors : report last request and response errors for each proxy",
12453 cli_parse_show_errors, cli_io_handler_show_errors, NULL,
12454 },
12455 {{},}
12456}};
12457
Willy Tarreau4a568972010-05-12 08:08:50 +020012458/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012459/* All supported ACL keywords must be declared here. */
12460/************************************************************************/
12461
12462/* Note: must not be declared <const> as its list will be overwritten.
12463 * Please take care of keeping this list alphabetically sorted.
12464 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012465static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012466 { "base", "base", PAT_MATCH_STR },
12467 { "base_beg", "base", PAT_MATCH_BEG },
12468 { "base_dir", "base", PAT_MATCH_DIR },
12469 { "base_dom", "base", PAT_MATCH_DOM },
12470 { "base_end", "base", PAT_MATCH_END },
12471 { "base_len", "base", PAT_MATCH_LEN },
12472 { "base_reg", "base", PAT_MATCH_REG },
12473 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012474
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012475 { "cook", "req.cook", PAT_MATCH_STR },
12476 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12477 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12478 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12479 { "cook_end", "req.cook", PAT_MATCH_END },
12480 { "cook_len", "req.cook", PAT_MATCH_LEN },
12481 { "cook_reg", "req.cook", PAT_MATCH_REG },
12482 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012483
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012484 { "hdr", "req.hdr", PAT_MATCH_STR },
12485 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12486 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12487 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12488 { "hdr_end", "req.hdr", PAT_MATCH_END },
12489 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12490 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12491 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012492
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012493 /* these two declarations uses strings with list storage (in place
12494 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12495 * and delete functions are relative to the list management. The parse
12496 * and match method are related to the corresponding fetch methods. This
12497 * is very particular ACL declaration mode.
12498 */
12499 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12500 { "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 +020012501
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012502 { "path", "path", PAT_MATCH_STR },
12503 { "path_beg", "path", PAT_MATCH_BEG },
12504 { "path_dir", "path", PAT_MATCH_DIR },
12505 { "path_dom", "path", PAT_MATCH_DOM },
12506 { "path_end", "path", PAT_MATCH_END },
12507 { "path_len", "path", PAT_MATCH_LEN },
12508 { "path_reg", "path", PAT_MATCH_REG },
12509 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012510
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012511 { "req_ver", "req.ver", PAT_MATCH_STR },
12512 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012513
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012514 { "scook", "res.cook", PAT_MATCH_STR },
12515 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12516 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12517 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12518 { "scook_end", "res.cook", PAT_MATCH_END },
12519 { "scook_len", "res.cook", PAT_MATCH_LEN },
12520 { "scook_reg", "res.cook", PAT_MATCH_REG },
12521 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012522
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012523 { "shdr", "res.hdr", PAT_MATCH_STR },
12524 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12525 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12526 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12527 { "shdr_end", "res.hdr", PAT_MATCH_END },
12528 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12529 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12530 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012531
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012532 { "url", "url", PAT_MATCH_STR },
12533 { "url_beg", "url", PAT_MATCH_BEG },
12534 { "url_dir", "url", PAT_MATCH_DIR },
12535 { "url_dom", "url", PAT_MATCH_DOM },
12536 { "url_end", "url", PAT_MATCH_END },
12537 { "url_len", "url", PAT_MATCH_LEN },
12538 { "url_reg", "url", PAT_MATCH_REG },
12539 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012540
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012541 { "urlp", "urlp", PAT_MATCH_STR },
12542 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12543 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12544 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12545 { "urlp_end", "urlp", PAT_MATCH_END },
12546 { "urlp_len", "urlp", PAT_MATCH_LEN },
12547 { "urlp_reg", "urlp", PAT_MATCH_REG },
12548 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012549
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012550 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012551}};
12552
12553/************************************************************************/
12554/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012555/************************************************************************/
12556/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012557static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012558 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012559 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012560 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12561
Willy Tarreau87b09662015-04-03 00:22:06 +020012562 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012563 { "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 +020012564
12565 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012566 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12567 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12568 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012569
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012570 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
12571 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012572
Willy Tarreau409bcde2013-01-08 00:31:00 +010012573 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12574 * are only here to match the ACL's name, are request-only and are used
12575 * for ACL compatibility only.
12576 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012577 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12578 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012579 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12580 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012581
12582 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12583 * only here to match the ACL's name, are request-only and are used for
12584 * ACL compatibility only.
12585 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012586 { "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 +020012587 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012588 { "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 +020012589 { "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 +010012590
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012591 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012592 { "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 +010012593 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012594 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012595 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012596 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012597
12598 /* HTTP protocol on the request path */
12599 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012600 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012601
12602 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012603 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12604 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012605
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012606 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012607 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
12608 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012609 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012610
Thierry FOURNIERd7d88812017-04-19 15:15:14 +020012611 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER5617dce2017-04-09 05:38:19 +020012612 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12613
Willy Tarreau18ed2562013-01-14 15:56:36 +010012614 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012615 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12616 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012617
Willy Tarreau18ed2562013-01-14 15:56:36 +010012618 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012619 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012620 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12621 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012622
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012623 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012624 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012625 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012626 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012627 { "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 +010012628 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012629 { "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 +010012630
12631 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012632 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012633 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12634 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012635
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012636 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012637 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012638 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012639 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012640 { "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 +010012641 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012642 { "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 +010012643
Willy Tarreau409bcde2013-01-08 00:31:00 +010012644 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012645 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012646 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12647 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012648 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010012649
12650 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012651 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012652 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012653 { "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 +020012654 { "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 +010012655
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012656 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020012657 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012658 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012659 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012660 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012661 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012662 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020012663 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12664 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012665 { "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 +010012666 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020012667}};
12668
Willy Tarreau8797c062007-05-07 00:55:35 +020012669
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012670/************************************************************************/
12671/* All supported converter keywords must be declared here. */
12672/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020012673/* Note: must not be declared <const> as its list will be overwritten */
12674static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012675 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020012676 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012677 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
12678 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012679 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020012680 { NULL, NULL, 0, 0, 0 },
12681}};
12682
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012683
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012684/************************************************************************/
12685/* All supported http-request action keywords must be declared here. */
12686/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012687struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012688 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020012689 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012690 { "set-method", parse_set_req_line },
12691 { "set-path", parse_set_req_line },
12692 { "set-query", parse_set_req_line },
12693 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020012694 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012695 }
12696};
12697
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012698struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012699 .kw = {
12700 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012701 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012702 { NULL, NULL }
12703 }
12704};
12705
Willy Tarreau8797c062007-05-07 00:55:35 +020012706__attribute__((constructor))
12707static void __http_protocol_init(void)
12708{
12709 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020012710 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020012711 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012712 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012713 http_res_keywords_register(&http_res_actions);
Willy Tarreau12207b32016-11-22 19:48:51 +010012714 cli_register_kw(&cli_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020012715}
12716
12717
Willy Tarreau58f10d72006-12-04 02:26:12 +010012718/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020012719 * Local variables:
12720 * c-indent-level: 8
12721 * c-basic-offset: 8
12722 * End:
12723 */