blob: 066204166a6feaee5df4a7e5970915a942141bff [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,
CJ Ess108b1dd2015-04-07 12:03:37 -0400143 [HTTP_ERR_429] = 429,
Willy Tarreau0f772532006-12-23 20:51:41 +0100144 [HTTP_ERR_500] = 500,
145 [HTTP_ERR_502] = 502,
146 [HTTP_ERR_503] = 503,
147 [HTTP_ERR_504] = 504,
148};
149
Willy Tarreau80587432006-12-24 17:47:20 +0100150static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200151 [HTTP_ERR_200] =
152 "HTTP/1.0 200 OK\r\n"
153 "Cache-Control: no-cache\r\n"
154 "Connection: close\r\n"
155 "Content-Type: text/html\r\n"
156 "\r\n"
157 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
158
Willy Tarreau0f772532006-12-23 20:51:41 +0100159 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100160 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100161 "Cache-Control: no-cache\r\n"
162 "Connection: close\r\n"
163 "Content-Type: text/html\r\n"
164 "\r\n"
165 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
166
167 [HTTP_ERR_403] =
168 "HTTP/1.0 403 Forbidden\r\n"
169 "Cache-Control: no-cache\r\n"
170 "Connection: close\r\n"
171 "Content-Type: text/html\r\n"
172 "\r\n"
173 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
174
CJ Ess108b1dd2015-04-07 12:03:37 -0400175 [HTTP_ERR_405] =
176 "HTTP/1.0 405 Method Not Allowed\r\n"
177 "Cache-Control: no-cache\r\n"
178 "Connection: close\r\n"
179 "Content-Type: text/html\r\n"
180 "\r\n"
181 "<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",
182
Willy Tarreau0f772532006-12-23 20:51:41 +0100183 [HTTP_ERR_408] =
184 "HTTP/1.0 408 Request Time-out\r\n"
185 "Cache-Control: no-cache\r\n"
186 "Connection: close\r\n"
187 "Content-Type: text/html\r\n"
188 "\r\n"
189 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
190
CJ Ess108b1dd2015-04-07 12:03:37 -0400191 [HTTP_ERR_429] =
192 "HTTP/1.0 429 Too Many Requests\r\n"
193 "Cache-Control: no-cache\r\n"
194 "Connection: close\r\n"
195 "Content-Type: text/html\r\n"
196 "\r\n"
197 "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n",
198
Willy Tarreau0f772532006-12-23 20:51:41 +0100199 [HTTP_ERR_500] =
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200200 "HTTP/1.0 500 Internal Server Error\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100201 "Cache-Control: no-cache\r\n"
202 "Connection: close\r\n"
203 "Content-Type: text/html\r\n"
204 "\r\n"
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200205 "<html><body><h1>500 Internal Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
Willy Tarreau0f772532006-12-23 20:51:41 +0100206
207 [HTTP_ERR_502] =
208 "HTTP/1.0 502 Bad Gateway\r\n"
209 "Cache-Control: no-cache\r\n"
210 "Connection: close\r\n"
211 "Content-Type: text/html\r\n"
212 "\r\n"
213 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
214
215 [HTTP_ERR_503] =
216 "HTTP/1.0 503 Service Unavailable\r\n"
217 "Cache-Control: no-cache\r\n"
218 "Connection: close\r\n"
219 "Content-Type: text/html\r\n"
220 "\r\n"
221 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
222
223 [HTTP_ERR_504] =
224 "HTTP/1.0 504 Gateway Time-out\r\n"
225 "Cache-Control: no-cache\r\n"
226 "Connection: close\r\n"
227 "Content-Type: text/html\r\n"
228 "\r\n"
229 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
230
231};
232
Cyril Bonté19979e12012-04-04 12:57:21 +0200233/* status codes available for the stats admin page (strictly 4 chars length) */
234const char *stat_status_codes[STAT_STATUS_SIZE] = {
235 [STAT_STATUS_DENY] = "DENY",
236 [STAT_STATUS_DONE] = "DONE",
237 [STAT_STATUS_ERRP] = "ERRP",
238 [STAT_STATUS_EXCD] = "EXCD",
239 [STAT_STATUS_NONE] = "NONE",
240 [STAT_STATUS_PART] = "PART",
241 [STAT_STATUS_UNKN] = "UNKN",
242};
243
244
William Lallemand73025dd2014-04-24 14:38:37 +0200245/* List head of all known action keywords for "http-request" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200246struct action_kw_list http_req_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200247 .list = LIST_HEAD_INIT(http_req_keywords.list)
248};
249
250/* List head of all known action keywords for "http-response" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200251struct action_kw_list http_res_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200252 .list = LIST_HEAD_INIT(http_res_keywords.list)
253};
254
Willy Tarreau80587432006-12-24 17:47:20 +0100255/* We must put the messages here since GCC cannot initialize consts depending
256 * on strlen().
257 */
258struct chunk http_err_chunks[HTTP_ERR_SIZE];
259
Willy Tarreaua890d072013-04-02 12:01:06 +0200260/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
261static struct hdr_ctx static_hdr_ctx;
262
Willy Tarreau42250582007-04-01 01:30:43 +0200263#define FD_SETS_ARE_BITFIELDS
264#ifdef FD_SETS_ARE_BITFIELDS
265/*
266 * This map is used with all the FD_* macros to check whether a particular bit
267 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
268 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
269 * byte should be encoded. Be careful to always pass bytes from 0 to 255
270 * exclusively to the macros.
271 */
272fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
273fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100274fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200275
276#else
277#error "Check if your OS uses bitfields for fd_sets"
278#endif
279
Willy Tarreau87b09662015-04-03 00:22:06 +0200280static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn);
Willy Tarreau0b748332014-04-29 00:13:29 +0200281
David Carlier7365f7d2016-04-04 11:54:42 +0100282static inline int http_msg_forward_body(struct stream *s, struct http_msg *msg);
283static inline int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +0100284
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200285/* This function returns a reason associated with the HTTP status.
286 * This function never fails, a message is always returned.
287 */
288const char *get_reason(unsigned int status)
289{
290 switch (status) {
291 case 100: return "Continue";
292 case 101: return "Switching Protocols";
293 case 102: return "Processing";
294 case 200: return "OK";
295 case 201: return "Created";
296 case 202: return "Accepted";
297 case 203: return "Non-Authoritative Information";
298 case 204: return "No Content";
299 case 205: return "Reset Content";
300 case 206: return "Partial Content";
301 case 207: return "Multi-Status";
302 case 210: return "Content Different";
303 case 226: return "IM Used";
304 case 300: return "Multiple Choices";
305 case 301: return "Moved Permanently";
306 case 302: return "Moved Temporarily";
307 case 303: return "See Other";
308 case 304: return "Not Modified";
309 case 305: return "Use Proxy";
310 case 307: return "Temporary Redirect";
311 case 308: return "Permanent Redirect";
312 case 310: return "Too many Redirects";
313 case 400: return "Bad Request";
314 case 401: return "Unauthorized";
315 case 402: return "Payment Required";
316 case 403: return "Forbidden";
317 case 404: return "Not Found";
318 case 405: return "Method Not Allowed";
319 case 406: return "Not Acceptable";
320 case 407: return "Proxy Authentication Required";
321 case 408: return "Request Time-out";
322 case 409: return "Conflict";
323 case 410: return "Gone";
324 case 411: return "Length Required";
325 case 412: return "Precondition Failed";
326 case 413: return "Request Entity Too Large";
327 case 414: return "Request-URI Too Long";
328 case 415: return "Unsupported Media Type";
329 case 416: return "Requested range unsatisfiable";
330 case 417: return "Expectation failed";
331 case 418: return "I'm a teapot";
332 case 422: return "Unprocessable entity";
333 case 423: return "Locked";
334 case 424: return "Method failure";
335 case 425: return "Unordered Collection";
336 case 426: return "Upgrade Required";
337 case 428: return "Precondition Required";
338 case 429: return "Too Many Requests";
339 case 431: return "Request Header Fields Too Large";
340 case 449: return "Retry With";
341 case 450: return "Blocked by Windows Parental Controls";
342 case 451: return "Unavailable For Legal Reasons";
343 case 456: return "Unrecoverable Error";
344 case 499: return "client has closed connection";
345 case 500: return "Internal Server Error";
346 case 501: return "Not Implemented";
Jarno Huuskonen59af2df2016-12-28 10:49:01 +0200347 case 502: return "Bad Gateway or Proxy Error";
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200348 case 503: return "Service Unavailable";
349 case 504: return "Gateway Time-out";
350 case 505: return "HTTP Version not supported";
351 case 506: return "Variant also negociate";
352 case 507: return "Insufficient storage";
353 case 508: return "Loop detected";
354 case 509: return "Bandwidth Limit Exceeded";
355 case 510: return "Not extended";
356 case 511: return "Network authentication required";
357 case 520: return "Web server is returning an unknown error";
358 default:
359 switch (status) {
360 case 100 ... 199: return "Informational";
361 case 200 ... 299: return "Success";
362 case 300 ... 399: return "Redirection";
363 case 400 ... 499: return "Client Error";
364 case 500 ... 599: return "Server Error";
365 default: return "Other";
366 }
367 }
368}
369
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200370/* This function returns HTTP_ERR_<num> (enum) matching http status code.
371 * Returned value should match codes from http_err_codes.
372 */
373static const int http_get_status_idx(unsigned int status)
374{
375 switch (status) {
376 case 200: return HTTP_ERR_200;
377 case 400: return HTTP_ERR_400;
378 case 403: return HTTP_ERR_403;
379 case 405: return HTTP_ERR_405;
380 case 408: return HTTP_ERR_408;
381 case 429: return HTTP_ERR_429;
382 case 500: return HTTP_ERR_500;
383 case 502: return HTTP_ERR_502;
384 case 503: return HTTP_ERR_503;
385 case 504: return HTTP_ERR_504;
386 default: return HTTP_ERR_500;
387 }
388}
389
Willy Tarreau80587432006-12-24 17:47:20 +0100390void init_proto_http()
391{
Willy Tarreau42250582007-04-01 01:30:43 +0200392 int i;
393 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100394 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200395
Willy Tarreau80587432006-12-24 17:47:20 +0100396 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
397 if (!http_err_msgs[msg]) {
398 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
399 abort();
400 }
401
402 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
403 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
404 }
Willy Tarreau42250582007-04-01 01:30:43 +0200405
406 /* initialize the log header encoding map : '{|}"#' should be encoded with
407 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
408 * URL encoding only requires '"', '#' to be encoded as well as non-
409 * printable characters above.
410 */
411 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
412 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100413 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200414 for (i = 0; i < 32; i++) {
415 FD_SET(i, hdr_encode_map);
416 FD_SET(i, url_encode_map);
417 }
418 for (i = 127; i < 256; i++) {
419 FD_SET(i, hdr_encode_map);
420 FD_SET(i, url_encode_map);
421 }
422
423 tmp = "\"#{|}";
424 while (*tmp) {
425 FD_SET(*tmp, hdr_encode_map);
426 tmp++;
427 }
428
429 tmp = "\"#";
430 while (*tmp) {
431 FD_SET(*tmp, url_encode_map);
432 tmp++;
433 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200434
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100435 /* initialize the http header encoding map. The draft httpbis define the
436 * header content as:
437 *
438 * HTTP-message = start-line
439 * *( header-field CRLF )
440 * CRLF
441 * [ message-body ]
442 * header-field = field-name ":" OWS field-value OWS
443 * field-value = *( field-content / obs-fold )
444 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
445 * obs-fold = CRLF 1*( SP / HTAB )
446 * field-vchar = VCHAR / obs-text
447 * VCHAR = %x21-7E
448 * obs-text = %x80-FF
449 *
450 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
451 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
452 * "obs-fold" is volontary forgotten because haproxy remove this.
453 */
454 memset(http_encode_map, 0, sizeof(http_encode_map));
455 for (i = 0x00; i <= 0x08; i++)
456 FD_SET(i, http_encode_map);
457 for (i = 0x0a; i <= 0x1f; i++)
458 FD_SET(i, http_encode_map);
459 FD_SET(0x7f, http_encode_map);
460
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200461 /* memory allocations */
Willy Tarreau63986c72015-04-03 22:55:33 +0200462 pool2_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100463 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100464}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200465
Willy Tarreau53b6c742006-12-17 13:37:46 +0100466/*
467 * We have 26 list of methods (1 per first letter), each of which can have
468 * up to 3 entries (2 valid, 1 null).
469 */
470struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100471 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100472 int len;
473 const char text[8];
474};
475
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100476const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100477 ['C' - 'A'] = {
478 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
479 },
480 ['D' - 'A'] = {
481 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
482 },
483 ['G' - 'A'] = {
484 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
485 },
486 ['H' - 'A'] = {
487 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
488 },
Christopher Fauletd57ad642015-07-31 14:26:57 +0200489 ['O' - 'A'] = {
490 [0] = { .meth = HTTP_METH_OPTIONS , .len=7, .text="OPTIONS" },
491 },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100492 ['P' - 'A'] = {
493 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
494 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
495 },
496 ['T' - 'A'] = {
497 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
498 },
499 /* rest is empty like this :
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200500 * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100501 */
502};
503
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100504const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100505 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
506 [HTTP_METH_GET] = { "GET", 3 },
507 [HTTP_METH_HEAD] = { "HEAD", 4 },
508 [HTTP_METH_POST] = { "POST", 4 },
509 [HTTP_METH_PUT] = { "PUT", 3 },
510 [HTTP_METH_DELETE] = { "DELETE", 6 },
511 [HTTP_METH_TRACE] = { "TRACE", 5 },
512 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
513};
514
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100515/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100516 * Adds a header and its CRLF at the tail of the message's buffer, just before
517 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100518 * The header is also automatically added to the index <hdr_idx>, and the end
519 * of headers is automatically adjusted. The number of bytes added is returned
520 * on success, otherwise <0 is returned indicating an error.
521 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100522int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100523{
524 int bytes, len;
525
526 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200527 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100528 if (!bytes)
529 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100530 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100531 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
532}
533
534/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100535 * Adds a header and its CRLF at the tail of the message's buffer, just before
536 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100537 * the buffer is only opened and the space reserved, but nothing is copied.
538 * The header is also automatically added to the index <hdr_idx>, and the end
539 * of headers is automatically adjusted. The number of bytes added is returned
540 * on success, otherwise <0 is returned indicating an error.
541 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100542int http_header_add_tail2(struct http_msg *msg,
543 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100544{
545 int bytes;
546
Willy Tarreau9b28e032012-10-12 23:49:43 +0200547 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100548 if (!bytes)
549 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100550 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100551 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
552}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200553
554/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100555 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
556 * If so, returns the position of the first non-space character relative to
557 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
558 * to return a pointer to the place after the first space. Returns 0 if the
559 * header name does not match. Checks are case-insensitive.
560 */
561int http_header_match2(const char *hdr, const char *end,
562 const char *name, int len)
563{
564 const char *val;
565
566 if (hdr + len >= end)
567 return 0;
568 if (hdr[len] != ':')
569 return 0;
570 if (strncasecmp(hdr, name, len) != 0)
571 return 0;
572 val = hdr + len + 1;
573 while (val < end && HTTP_IS_SPHT(*val))
574 val++;
575 if ((val >= end) && (len + 2 <= end - hdr))
576 return len + 2; /* we may replace starting from second space */
577 return val - hdr;
578}
579
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200580/* Find the first or next occurrence of header <name> in message buffer <sol>
581 * using headers index <idx>, and return it in the <ctx> structure. This
582 * structure holds everything necessary to use the header and find next
583 * occurrence. If its <idx> member is 0, the header is searched from the
584 * beginning. Otherwise, the next occurrence is returned. The function returns
585 * 1 when it finds a value, and 0 when there is no more. It is very similar to
586 * http_find_header2() except that it is designed to work with full-line headers
587 * whose comma is not a delimiter but is part of the syntax. As a special case,
588 * if ctx->val is NULL when searching for a new values of a header, the current
589 * header is rescanned. This allows rescanning after a header deletion.
590 */
591int http_find_full_header2(const char *name, int len,
592 char *sol, struct hdr_idx *idx,
593 struct hdr_ctx *ctx)
594{
595 char *eol, *sov;
596 int cur_idx, old_idx;
597
598 cur_idx = ctx->idx;
599 if (cur_idx) {
600 /* We have previously returned a header, let's search another one */
601 sol = ctx->line;
602 eol = sol + idx->v[cur_idx].len;
603 goto next_hdr;
604 }
605
606 /* first request for this header */
607 sol += hdr_idx_first_pos(idx);
608 old_idx = 0;
609 cur_idx = hdr_idx_first_idx(idx);
610 while (cur_idx) {
611 eol = sol + idx->v[cur_idx].len;
612
613 if (len == 0) {
614 /* No argument was passed, we want any header.
615 * To achieve this, we simply build a fake request. */
616 while (sol + len < eol && sol[len] != ':')
617 len++;
618 name = sol;
619 }
620
621 if ((len < eol - sol) &&
622 (sol[len] == ':') &&
623 (strncasecmp(sol, name, len) == 0)) {
624 ctx->del = len;
625 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100626 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200627 sov++;
628
629 ctx->line = sol;
630 ctx->prev = old_idx;
631 ctx->idx = cur_idx;
632 ctx->val = sov - sol;
633 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100634 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200635 eol--;
636 ctx->tws++;
637 }
638 ctx->vlen = eol - sov;
639 return 1;
640 }
641 next_hdr:
642 sol = eol + idx->v[cur_idx].cr + 1;
643 old_idx = cur_idx;
644 cur_idx = idx->v[cur_idx].next;
645 }
646 return 0;
647}
648
Willy Tarreauc90dc232015-02-20 13:51:36 +0100649/* Find the first or next header field in message buffer <sol> using headers
650 * index <idx>, and return it in the <ctx> structure. This structure holds
651 * everything necessary to use the header and find next occurrence. If its
652 * <idx> member is 0, the first header is retrieved. Otherwise, the next
653 * occurrence is returned. The function returns 1 when it finds a value, and
654 * 0 when there is no more. It is equivalent to http_find_full_header2() with
655 * no header name.
656 */
657int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
658{
659 char *eol, *sov;
660 int cur_idx, old_idx;
661 int len;
662
663 cur_idx = ctx->idx;
664 if (cur_idx) {
665 /* We have previously returned a header, let's search another one */
666 sol = ctx->line;
667 eol = sol + idx->v[cur_idx].len;
668 goto next_hdr;
669 }
670
671 /* first request for this header */
672 sol += hdr_idx_first_pos(idx);
673 old_idx = 0;
674 cur_idx = hdr_idx_first_idx(idx);
675 while (cur_idx) {
676 eol = sol + idx->v[cur_idx].len;
677
678 len = 0;
679 while (1) {
680 if (len >= eol - sol)
681 goto next_hdr;
682 if (sol[len] == ':')
683 break;
684 len++;
685 }
686
687 ctx->del = len;
688 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100689 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreauc90dc232015-02-20 13:51:36 +0100690 sov++;
691
692 ctx->line = sol;
693 ctx->prev = old_idx;
694 ctx->idx = cur_idx;
695 ctx->val = sov - sol;
696 ctx->tws = 0;
697
Willy Tarreau2235b262016-11-05 15:50:20 +0100698 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreauc90dc232015-02-20 13:51:36 +0100699 eol--;
700 ctx->tws++;
701 }
702 ctx->vlen = eol - sov;
703 return 1;
704
705 next_hdr:
706 sol = eol + idx->v[cur_idx].cr + 1;
707 old_idx = cur_idx;
708 cur_idx = idx->v[cur_idx].next;
709 }
710 return 0;
711}
712
Lukas Tribus23953682017-04-28 13:24:30 +0000713/* Find the end of the header value contained between <s> and <e>. See RFC7230,
714 * par 3.2 for more information. Note that it requires a valid header to return
Willy Tarreau68085d82010-01-18 14:54:04 +0100715 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200716 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100717char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200718{
719 int quoted, qdpair;
720
721 quoted = qdpair = 0;
Willy Tarreaue6d9c212016-11-05 18:23:38 +0100722
723#if defined(__x86_64__) || \
724 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
725 defined(__ARM_ARCH_7A__)
726 /* speedup: skip everything not a comma nor a double quote */
727 for (; s <= e - sizeof(int); s += sizeof(int)) {
728 unsigned int c = *(int *)s; // comma
729 unsigned int q = c; // quote
730
731 c ^= 0x2c2c2c2c; // contains one zero on a comma
732 q ^= 0x22222222; // contains one zero on a quote
733
734 c = (c - 0x01010101) & ~c; // contains 0x80 below a comma
735 q = (q - 0x01010101) & ~q; // contains 0x80 below a quote
736
737 if ((c | q) & 0x80808080)
738 break; // found a comma or a quote
739 }
740#endif
Willy Tarreau33a7e692007-06-10 19:45:56 +0200741 for (; s < e; s++) {
742 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200743 else if (quoted) {
744 if (*s == '\\') qdpair = 1;
745 else if (*s == '"') quoted = 0;
746 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200747 else if (*s == '"') quoted = 1;
748 else if (*s == ',') return s;
749 }
750 return s;
751}
752
753/* Find the first or next occurrence of header <name> in message buffer <sol>
754 * using headers index <idx>, and return it in the <ctx> structure. This
755 * structure holds everything necessary to use the header and find next
756 * occurrence. If its <idx> member is 0, the header is searched from the
757 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100758 * 1 when it finds a value, and 0 when there is no more. It is designed to work
759 * with headers defined as comma-separated lists. As a special case, if ctx->val
760 * is NULL when searching for a new values of a header, the current header is
761 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200762 */
763int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100764 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200765 struct hdr_ctx *ctx)
766{
Willy Tarreau68085d82010-01-18 14:54:04 +0100767 char *eol, *sov;
768 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200769
Willy Tarreau68085d82010-01-18 14:54:04 +0100770 cur_idx = ctx->idx;
771 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200772 /* We have previously returned a value, let's search
773 * another one on the same line.
774 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200775 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200776 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100777 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200778 eol = sol + idx->v[cur_idx].len;
779
780 if (sov >= eol)
781 /* no more values in this header */
782 goto next_hdr;
783
Willy Tarreau68085d82010-01-18 14:54:04 +0100784 /* values remaining for this header, skip the comma but save it
785 * for later use (eg: for header deletion).
786 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200787 sov++;
Willy Tarreau2235b262016-11-05 15:50:20 +0100788 while (sov < eol && HTTP_IS_LWS((*sov)))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200789 sov++;
790
791 goto return_hdr;
792 }
793
794 /* first request for this header */
795 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100796 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200797 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200798 while (cur_idx) {
799 eol = sol + idx->v[cur_idx].len;
800
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200801 if (len == 0) {
802 /* No argument was passed, we want any header.
803 * To achieve this, we simply build a fake request. */
804 while (sol + len < eol && sol[len] != ':')
805 len++;
806 name = sol;
807 }
808
Willy Tarreau33a7e692007-06-10 19:45:56 +0200809 if ((len < eol - sol) &&
810 (sol[len] == ':') &&
811 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100812 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200813 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100814 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200815 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100816
Willy Tarreau33a7e692007-06-10 19:45:56 +0200817 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100818 ctx->prev = old_idx;
819 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200820 ctx->idx = cur_idx;
821 ctx->val = sov - sol;
822
823 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200824 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100825 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200826 eol--;
827 ctx->tws++;
828 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200829 ctx->vlen = eol - sov;
830 return 1;
831 }
832 next_hdr:
833 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100834 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200835 cur_idx = idx->v[cur_idx].next;
836 }
837 return 0;
838}
839
840int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100841 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200842 struct hdr_ctx *ctx)
843{
844 return http_find_header2(name, strlen(name), sol, idx, ctx);
845}
846
Willy Tarreau68085d82010-01-18 14:54:04 +0100847/* Remove one value of a header. This only works on a <ctx> returned by one of
848 * the http_find_header functions. The value is removed, as well as surrounding
849 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100850 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100851 * message <msg>. The new index is returned. If it is zero, it means there is
852 * no more header, so any processing may stop. The ctx is always left in a form
853 * that can be handled by http_find_header2() to find next occurrence.
854 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100855int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100856{
857 int cur_idx = ctx->idx;
858 char *sol = ctx->line;
859 struct hdr_idx_elem *hdr;
860 int delta, skip_comma;
861
862 if (!cur_idx)
863 return 0;
864
865 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200866 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100867 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200868 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100869 http_msg_move_end(msg, delta);
870 idx->used--;
871 hdr->len = 0; /* unused entry */
872 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100873 if (idx->tail == ctx->idx)
874 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100875 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100876 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100877 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200878 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100879 return ctx->idx;
880 }
881
882 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200883 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
884 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100885 */
886
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200887 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200888 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200889 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100890 NULL, 0);
891 hdr->len += delta;
892 http_msg_move_end(msg, delta);
893 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200894 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100895 return ctx->idx;
896}
897
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100898/* This function handles a server error at the stream interface level. The
899 * stream interface is assumed to be already in a closed state. An optional
Willy Tarreau2019f952017-03-14 11:07:31 +0100900 * message is copied into the input buffer.
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100901 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100902 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200903 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200904static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau2019f952017-03-14 11:07:31 +0100905 int err, int finst, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200906{
Willy Tarreau2019f952017-03-14 11:07:31 +0100907 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Willy Tarreau2bb4a962014-11-28 11:11:05 +0100908 channel_auto_read(si_oc(si));
909 channel_abort(si_oc(si));
910 channel_auto_close(si_oc(si));
911 channel_erase(si_oc(si));
912 channel_auto_close(si_ic(si));
913 channel_auto_read(si_ic(si));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200914 if (msg)
Willy Tarreau06d80a92017-10-19 14:32:15 +0200915 co_inject(si_ic(si), msg->str, msg->len);
Willy Tarreaue7dff022015-04-03 01:14:29 +0200916 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200917 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200918 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200919 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200920}
921
Willy Tarreau87b09662015-04-03 00:22:06 +0200922/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +0100923 * and message.
924 */
925
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200926struct chunk *http_error_message(struct stream *s)
Willy Tarreau80587432006-12-24 17:47:20 +0100927{
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +0200928 const int msgnum = http_get_status_idx(s->txn->status);
929
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200930 if (s->be->errmsg[msgnum].str)
931 return &s->be->errmsg[msgnum];
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200932 else if (strm_fe(s)->errmsg[msgnum].str)
933 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100934 else
935 return &http_err_chunks[msgnum];
936}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200937
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100938void
939http_reply_and_close(struct stream *s, short status, struct chunk *msg)
940{
Christopher Fauletd7c91962015-04-30 11:48:27 +0200941 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Faulet3e344292015-11-24 16:24:13 +0100942 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Christopher Fauleta94e5a52015-12-09 15:55:06 +0100943 stream_int_retnclose(&s->si[0], msg);
944}
945
Willy Tarreau53b6c742006-12-17 13:37:46 +0100946/*
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200947 * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown
948 * ones.
Willy Tarreau53b6c742006-12-17 13:37:46 +0100949 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100950enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100951{
952 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100953 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100954
955 m = ((unsigned)*str - 'A');
956
957 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100958 for (h = http_methods[m]; h->len > 0; h++) {
959 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100960 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100961 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100962 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100963 };
Willy Tarreau53b6c742006-12-17 13:37:46 +0100964 }
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200965 return HTTP_METH_OTHER;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100966}
967
Willy Tarreau21d2af32008-02-14 20:25:24 +0100968/* Parse the URI from the given transaction (which is assumed to be in request
969 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
970 * It is returned otherwise.
971 */
Thierry FOURNIER3c331782015-09-17 19:33:35 +0200972char *http_get_path(struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +0100973{
974 char *ptr, *end;
975
Willy Tarreau9b28e032012-10-12 23:49:43 +0200976 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100977 end = ptr + txn->req.sl.rq.u_l;
978
979 if (ptr >= end)
980 return NULL;
981
Lukas Tribus23953682017-04-28 13:24:30 +0000982 /* RFC7230, par. 2.7 :
Willy Tarreau21d2af32008-02-14 20:25:24 +0100983 * Request-URI = "*" | absuri | abspath | authority
984 */
985
986 if (*ptr == '*')
987 return NULL;
988
989 if (isalpha((unsigned char)*ptr)) {
990 /* this is a scheme as described by RFC3986, par. 3.1 */
991 ptr++;
992 while (ptr < end &&
993 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
994 ptr++;
995 /* skip '://' */
996 if (ptr == end || *ptr++ != ':')
997 return NULL;
998 if (ptr == end || *ptr++ != '/')
999 return NULL;
1000 if (ptr == end || *ptr++ != '/')
1001 return NULL;
1002 }
1003 /* skip [user[:passwd]@]host[:[port]] */
1004
1005 while (ptr < end && *ptr != '/')
1006 ptr++;
1007
1008 if (ptr == end)
1009 return NULL;
1010
1011 /* OK, we got the '/' ! */
1012 return ptr;
1013}
1014
William Lallemand65ad6e12014-01-31 15:08:02 +01001015/* Parse the URI from the given string and look for the "/" beginning the PATH.
1016 * If not found, return NULL. It is returned otherwise.
1017 */
1018static char *
1019http_get_path_from_string(char *str)
1020{
1021 char *ptr = str;
1022
1023 /* RFC2616, par. 5.1.2 :
1024 * Request-URI = "*" | absuri | abspath | authority
1025 */
1026
1027 if (*ptr == '*')
1028 return NULL;
1029
1030 if (isalpha((unsigned char)*ptr)) {
1031 /* this is a scheme as described by RFC3986, par. 3.1 */
1032 ptr++;
1033 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
1034 ptr++;
1035 /* skip '://' */
1036 if (*ptr == '\0' || *ptr++ != ':')
1037 return NULL;
1038 if (*ptr == '\0' || *ptr++ != '/')
1039 return NULL;
1040 if (*ptr == '\0' || *ptr++ != '/')
1041 return NULL;
1042 }
1043 /* skip [user[:passwd]@]host[:[port]] */
1044
1045 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
1046 ptr++;
1047
1048 if (*ptr == '\0' || *ptr == ' ')
1049 return NULL;
1050
1051 /* OK, we got the '/' ! */
1052 return ptr;
1053}
1054
Willy Tarreau71241ab2012-12-27 11:30:54 +01001055/* Returns a 302 for a redirectable request that reaches a server working in
1056 * in redirect mode. This may only be called just after the stream interface
1057 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
1058 * follow normal proxy processing. NOTE: this function is designed to support
1059 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001060 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001061void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001062{
1063 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001064 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001065 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001066 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001067
1068 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001069 trash.len = strlen(HTTP_302);
1070 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001071
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001072 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001073
Willy Tarreauefb453c2008-10-26 20:49:47 +01001074 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001075 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001076 return;
1077
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001078 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +01001079 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001080 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
1081 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001082 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001083
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001084 /* 3: add the request URI. Since it was already forwarded, we need
1085 * to temporarily rewind the buffer.
1086 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001087 txn = s->txn;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001088 b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001089
Willy Tarreauefb453c2008-10-26 20:49:47 +01001090 path = http_get_path(txn);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001091 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 +02001092
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001093 b_adv(s->req.buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001094
Willy Tarreauefb453c2008-10-26 20:49:47 +01001095 if (!path)
1096 return;
1097
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001098 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001099 return;
1100
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001101 memcpy(trash.str + trash.len, path, len);
1102 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001103
1104 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001105 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
1106 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001107 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001108 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
1109 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001110 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001111
1112 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001113 si_shutr(si);
1114 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001115 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001116 si->state = SI_ST_CLO;
1117
1118 /* send the message */
Willy Tarreau2019f952017-03-14 11:07:31 +01001119 txn->status = 302;
1120 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001121
1122 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001123 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001124 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001125}
1126
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001127/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001128 * that the server side is closed. Note that err_type is actually a
1129 * bitmask, where almost only aborts may be cumulated with other
1130 * values. We consider that aborted operations are more important
1131 * than timeouts or errors due to the fact that nobody else in the
1132 * logs might explain incomplete retries. All others should avoid
1133 * being cumulated. It should normally not be possible to have multiple
1134 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001135 * Note that connection errors appearing on the second request of a keep-alive
1136 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001137 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001138void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001139{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001140 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001141
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001142 /* set s->txn->status for http_error_message(s) */
1143 s->txn->status = 503;
1144
Willy Tarreauefb453c2008-10-26 20:49:47 +01001145 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001146 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001147 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001148 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001149 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001150 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001151 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001152 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001153 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau2019f952017-03-14 11:07:31 +01001154 http_error_message(s));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001155 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001156 http_server_error(s, si, SF_ERR_SRVCL, 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_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001159 http_server_error(s, si, SF_ERR_SRVTO, 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_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001163 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001164 (s->flags & SF_SRV_REUSED) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001165 http_error_message(s));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001166 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001167 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001168 (s->txn->flags & TX_NOT_FIRST) ? NULL :
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001169 http_error_message(s));
1170 else { /* SI_ET_CONN_OTHER and others */
1171 s->txn->status = 500;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001172 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau2019f952017-03-14 11:07:31 +01001173 http_error_message(s));
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001174 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001175}
1176
Willy Tarreau42250582007-04-01 01:30:43 +02001177extern const char sess_term_cond[8];
1178extern const char sess_fin_state[8];
1179extern const char *monthname[12];
Willy Tarreau63986c72015-04-03 22:55:33 +02001180struct pool_head *pool2_http_txn;
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001181struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001182struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001183struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001184
Willy Tarreau117f59e2007-03-04 18:17:17 +01001185/*
1186 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001187 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001188 */
1189void capture_headers(char *som, struct hdr_idx *idx,
1190 char **cap, struct cap_hdr *cap_hdr)
1191{
1192 char *eol, *sol, *col, *sov;
1193 int cur_idx;
1194 struct cap_hdr *h;
1195 int len;
1196
1197 sol = som + hdr_idx_first_pos(idx);
1198 cur_idx = hdr_idx_first_idx(idx);
1199
1200 while (cur_idx) {
1201 eol = sol + idx->v[cur_idx].len;
1202
1203 col = sol;
1204 while (col < eol && *col != ':')
1205 col++;
1206
1207 sov = col + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01001208 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001209 sov++;
1210
1211 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001212 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001213 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1214 if (cap[h->index] == NULL)
1215 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001216 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001217
1218 if (cap[h->index] == NULL) {
1219 Alert("HTTP capture : out of memory.\n");
1220 continue;
1221 }
1222
1223 len = eol - sov;
1224 if (len > h->len)
1225 len = h->len;
1226
1227 memcpy(cap[h->index], sov, len);
1228 cap[h->index][len]=0;
1229 }
1230 }
1231 sol = eol + idx->v[cur_idx].cr + 1;
1232 cur_idx = idx->v[cur_idx].next;
1233 }
1234}
1235
Willy Tarreaubaaee002006-06-26 02:48:02 +02001236/*
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001237 * Returns the data from Authorization header. Function may be called more
1238 * than once so data is stored in txn->auth_data. When no header is found
1239 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001240 * searching again for something we are unable to find anyway. However, if
1241 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +02001242 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001243 */
1244
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001245int
Willy Tarreau87b09662015-04-03 00:22:06 +02001246get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001247{
1248
Willy Tarreaueee5b512015-04-03 23:46:31 +02001249 struct http_txn *txn = s->txn;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001250 struct chunk auth_method;
1251 struct hdr_ctx ctx;
1252 char *h, *p;
1253 int len;
1254
1255#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +02001256 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001257#endif
1258
1259 if (txn->auth.method == HTTP_AUTH_WRONG)
1260 return 0;
1261
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001262 txn->auth.method = HTTP_AUTH_WRONG;
1263
1264 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001265
1266 if (txn->flags & TX_USE_PX_CONN) {
1267 h = "Proxy-Authorization";
1268 len = strlen(h);
1269 } else {
1270 h = "Authorization";
1271 len = strlen(h);
1272 }
1273
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001274 if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001275 return 0;
1276
1277 h = ctx.line + ctx.val;
1278
1279 p = memchr(h, ' ', ctx.vlen);
Willy Tarreau5c557d12016-03-13 08:17:02 +01001280 len = p - h;
1281 if (!p || len <= 0)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001282 return 0;
1283
David Carlier7365f7d2016-04-04 11:54:42 +01001284 if (chunk_initlen(&auth_method, h, 0, len) != 1)
1285 return 0;
1286
Willy Tarreau5c557d12016-03-13 08:17:02 +01001287 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001288
1289 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
Christopher Faulet6988f672017-07-27 15:18:52 +02001290 struct chunk *http_auth = get_trash_chunk();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001291
1292 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Christopher Faulet6988f672017-07-27 15:18:52 +02001293 http_auth->str, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001294
1295 if (len < 0)
1296 return 0;
1297
1298
Christopher Faulet6988f672017-07-27 15:18:52 +02001299 http_auth->str[len] = '\0';
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001300
Christopher Faulet6988f672017-07-27 15:18:52 +02001301 p = strchr(http_auth->str, ':');
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001302
1303 if (!p)
1304 return 0;
1305
Christopher Faulet6988f672017-07-27 15:18:52 +02001306 txn->auth.user = http_auth->str;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001307 *p = '\0';
1308 txn->auth.pass = p+1;
1309
1310 txn->auth.method = HTTP_AUTH_BASIC;
1311 return 1;
1312 }
1313
1314 return 0;
1315}
1316
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001317
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001318/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1319 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1320 * nothing is done and 1 is returned.
1321 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001322static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001323{
1324 int delta;
1325 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001326 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001327
1328 if (msg->sl.rq.v_l != 0)
1329 return 1;
1330
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001331 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1332 if (txn->meth != HTTP_METH_GET)
1333 return 0;
1334
Willy Tarreau9b28e032012-10-12 23:49:43 +02001335 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001336
1337 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001338 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1339 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001340 }
1341 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001342 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001343 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001344 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001345 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001346 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001347 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001348 NULL, NULL);
1349 if (unlikely(!cur_end))
1350 return 0;
1351
1352 /* we have a full HTTP/1.0 request now and we know that
1353 * we have either a CR or an LF at <ptr>.
1354 */
1355 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1356 return 1;
1357}
1358
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001359/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001360 * and "keep-alive" values. If we already know that some headers may safely
1361 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001362 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1363 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001364 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001365 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1366 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1367 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001368 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001369 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001370void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001371{
Willy Tarreau5b154472009-12-21 20:11:07 +01001372 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001373 const char *hdr_val = "Connection";
1374 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001375
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001376 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001377 return;
1378
Willy Tarreau88d349d2010-01-25 12:15:43 +01001379 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1380 hdr_val = "Proxy-Connection";
1381 hdr_len = 16;
1382 }
1383
Willy Tarreau5b154472009-12-21 20:11:07 +01001384 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001385 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001386 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001387 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1388 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001389 if (to_del & 2)
1390 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001391 else
1392 txn->flags |= TX_CON_KAL_SET;
1393 }
1394 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1395 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001396 if (to_del & 1)
1397 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001398 else
1399 txn->flags |= TX_CON_CLO_SET;
1400 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001401 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1402 txn->flags |= TX_HDR_CONN_UPG;
1403 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001404 }
1405
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001406 txn->flags |= TX_HDR_CONN_PRS;
1407 return;
1408}
Willy Tarreau5b154472009-12-21 20:11:07 +01001409
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001410/* Apply desired changes on the Connection: header. Values may be removed and/or
1411 * added depending on the <wanted> flags, which are exclusively composed of
1412 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1413 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1414 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001415void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001416{
1417 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001418 const char *hdr_val = "Connection";
1419 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001420
1421 ctx.idx = 0;
1422
Willy Tarreau88d349d2010-01-25 12:15:43 +01001423
1424 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1425 hdr_val = "Proxy-Connection";
1426 hdr_len = 16;
1427 }
1428
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001429 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001430 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001431 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1432 if (wanted & TX_CON_KAL_SET)
1433 txn->flags |= TX_CON_KAL_SET;
1434 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001435 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001436 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001437 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1438 if (wanted & TX_CON_CLO_SET)
1439 txn->flags |= TX_CON_CLO_SET;
1440 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001441 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001442 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001443 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001444
1445 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1446 return;
1447
1448 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1449 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001450 hdr_val = "Connection: close";
1451 hdr_len = 17;
1452 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1453 hdr_val = "Proxy-Connection: close";
1454 hdr_len = 23;
1455 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001456 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001457 }
1458
1459 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1460 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001461 hdr_val = "Connection: keep-alive";
1462 hdr_len = 22;
1463 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1464 hdr_val = "Proxy-Connection: keep-alive";
1465 hdr_len = 28;
1466 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001467 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001468 }
1469 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001470}
1471
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001472/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
1473 * value is larger than 1000, it is bound to 1000. The parser consumes up to
1474 * 1 digit, one dot and 3 digits and stops on the first invalid character.
1475 * Unparsable qvalues return 1000 as "q=1.000".
1476 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02001477int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001478{
1479 int q = 1000;
1480
Willy Tarreau506c69a2014-07-08 00:59:48 +02001481 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001482 goto out;
1483 q = (*qvalue++ - '0') * 1000;
1484
1485 if (*qvalue++ != '.')
1486 goto out;
1487
Willy Tarreau506c69a2014-07-08 00:59:48 +02001488 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001489 goto out;
1490 q += (*qvalue++ - '0') * 100;
1491
Willy Tarreau506c69a2014-07-08 00:59:48 +02001492 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001493 goto out;
1494 q += (*qvalue++ - '0') * 10;
1495
Willy Tarreau506c69a2014-07-08 00:59:48 +02001496 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001497 goto out;
1498 q += (*qvalue++ - '0') * 1;
1499 out:
1500 if (q > 1000)
1501 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02001502 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02001503 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01001504 return q;
1505}
William Lallemand82fe75c2012-10-23 10:25:10 +02001506
Willy Tarreau87b09662015-04-03 00:22:06 +02001507void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001508{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001509 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001510 int tmp = TX_CON_WANT_KAL;
1511
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001512 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
1513 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001514 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
1515 tmp = TX_CON_WANT_TUN;
1516
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001517 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001518 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
1519 tmp = TX_CON_WANT_TUN;
1520 }
1521
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001522 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001523 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
1524 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001525 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001526 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
1527 tmp = TX_CON_WANT_CLO;
1528 else
1529 tmp = TX_CON_WANT_SCL;
1530 }
1531
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001532 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001533 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
1534 tmp = TX_CON_WANT_CLO;
1535
1536 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
1537 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
1538
1539 if (!(txn->flags & TX_HDR_CONN_PRS) &&
1540 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
1541 /* parse the Connection header and possibly clean it */
1542 int to_del = 0;
1543 if ((msg->flags & HTTP_MSGF_VER_11) ||
1544 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001545 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001546 to_del |= 2; /* remove "keep-alive" */
1547 if (!(msg->flags & HTTP_MSGF_VER_11))
1548 to_del |= 1; /* remove "close" */
1549 http_parse_connection_header(txn, msg, to_del);
1550 }
1551
1552 /* check if client or config asks for explicit close in KAL/SCL */
1553 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
1554 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
1555 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
1556 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
1557 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001558 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02001559 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
1560}
William Lallemand82fe75c2012-10-23 10:25:10 +02001561
Willy Tarreaud787e662009-07-07 10:14:51 +02001562/* This stream analyser waits for a complete HTTP request. It returns 1 if the
1563 * processing can continue on next analysers, or zero if it either needs more
1564 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001565 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02001566 * when it has nothing left to do, and may remove any analyser when it wants to
1567 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001568 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001569int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001570{
Willy Tarreau59234e92008-11-30 23:51:27 +01001571 /*
1572 * We will parse the partial (or complete) lines.
1573 * We will check the request syntax, and also join multi-line
1574 * headers. An index of all the lines will be elaborated while
1575 * parsing.
1576 *
1577 * For the parsing, we use a 28 states FSM.
1578 *
1579 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02001580 * req->buf->p = beginning of request
1581 * req->buf->p + msg->eoh = end of processed headers / start of current one
1582 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02001583 * msg->eol = end of current header or line (LF or CRLF)
1584 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02001585 *
1586 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02001587 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02001588 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
1589 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01001590 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001591
Willy Tarreau59234e92008-11-30 23:51:27 +01001592 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001593 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001594 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01001595 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02001596 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001597
Willy Tarreau87b09662015-04-03 00:22:06 +02001598 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 +01001599 now_ms, __FUNCTION__,
1600 s,
1601 req,
1602 req->rex, req->wex,
1603 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001604 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01001605 req->analysers);
1606
Willy Tarreau52a0c602009-08-16 22:45:38 +02001607 /* we're speaking HTTP here, so let's speak HTTP to the client */
1608 s->srv_error = http_return_srv_error;
1609
Willy Tarreau83e3af02009-12-28 17:39:57 +01001610 /* There's a protected area at the end of the buffer for rewriting
1611 * purposes. We don't want to start to parse the request if the
1612 * protected area is affected, because we may have to move processed
1613 * data later, which is much more complicated.
1614 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001615 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02001616
1617 /* This point is executed when some data is avalaible for analysis,
1618 * so we log the end of the idle time. */
1619 if (s->logs.t_idle == -1)
1620 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
1621
Willy Tarreau379357a2013-06-08 12:55:46 +02001622 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01001623 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001624 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01001625 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01001626 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001627 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001628 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01001629 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01001630 return 0;
1631 }
Willy Tarreau379357a2013-06-08 12:55:46 +02001632 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
1633 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
1634 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001635 }
1636
Willy Tarreau9b28e032012-10-12 23:49:43 +02001637 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01001638 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001639 }
1640
Willy Tarreau59234e92008-11-30 23:51:27 +01001641 /* 1: we might have to print this header in debug mode */
1642 if (unlikely((global.mode & MODE_DEBUG) &&
1643 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02001644 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001645 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001646
Willy Tarreau9b28e032012-10-12 23:49:43 +02001647 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001648 /* this is a bit complex : in case of error on the request line,
1649 * we know that rq.l is still zero, so we display only the part
1650 * up to the end of the line (truncated by debug_hdr).
1651 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001652 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01001653 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01001654
Willy Tarreau59234e92008-11-30 23:51:27 +01001655 sol += hdr_idx_first_pos(&txn->hdr_idx);
1656 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001657
Willy Tarreau59234e92008-11-30 23:51:27 +01001658 while (cur_idx) {
1659 eol = sol + txn->hdr_idx.v[cur_idx].len;
1660 debug_hdr("clihdr", s, sol, eol);
1661 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
1662 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001663 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001664 }
1665
Willy Tarreau58f10d72006-12-04 02:26:12 +01001666
Willy Tarreau59234e92008-11-30 23:51:27 +01001667 /*
1668 * Now we quickly check if we have found a full valid request.
1669 * If not so, we check the FD and buffer states before leaving.
1670 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01001671 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001672 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02001673 * on a keep-alive stream, if we encounter and error, close, t/o,
1674 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001675 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02001676 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02001677 * Last, we may increase some tracked counters' http request errors on
1678 * the cases that are deliberately the client's fault. For instance,
1679 * a timeout or connection reset is not counted as an error. However
1680 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01001681 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001682
Willy Tarreau655dce92009-11-08 13:10:58 +01001683 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001684 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001685 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001686 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01001687 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001688 stream_inc_http_req_ctr(s);
1689 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001690 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001691 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01001692 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001693
Willy Tarreau59234e92008-11-30 23:51:27 +01001694 /* 1: Since we are in header mode, if there's no space
1695 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02001696 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01001697 * must terminate it now.
1698 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001699 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01001700 /* FIXME: check if URI is set and return Status
1701 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01001702 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001703 stream_inc_http_req_ctr(s);
1704 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001705 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02001706 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02001707 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01001708 goto return_bad_req;
1709 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001710
Willy Tarreau59234e92008-11-30 23:51:27 +01001711 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001712 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001713 if (!(s->flags & SF_ERR_MASK))
1714 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001715
Willy Tarreaufcffa692010-01-10 14:21:19 +01001716 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001717 goto failed_keep_alive;
1718
Willy Tarreau0f228a02015-05-01 15:37:53 +02001719 if (sess->fe->options & PR_O_IGNORE_PRB)
1720 goto failed_keep_alive;
1721
Willy Tarreau59234e92008-11-30 23:51:27 +01001722 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001723 if (msg->err_pos >= 0) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001724 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001725 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001726 }
1727
Willy Tarreaudc979f22012-12-04 10:39:01 +01001728 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001729 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001730 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001731 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01001732 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001733 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001734 proxy_inc_fe_req_ctr(sess->fe);
1735 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001736 if (sess->listener->counters)
1737 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001738
Willy Tarreaue7dff022015-04-03 01:14:29 +02001739 if (!(s->flags & SF_FINST_MASK))
1740 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001741 return 0;
1742 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02001743
Willy Tarreau59234e92008-11-30 23:51:27 +01001744 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001745 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001746 if (!(s->flags & SF_ERR_MASK))
1747 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001748
Willy Tarreaufcffa692010-01-10 14:21:19 +01001749 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001750 goto failed_keep_alive;
1751
Willy Tarreau0f228a02015-05-01 15:37:53 +02001752 if (sess->fe->options & PR_O_IGNORE_PRB)
1753 goto failed_keep_alive;
1754
Willy Tarreau59234e92008-11-30 23:51:27 +01001755 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02001756 if (msg->err_pos >= 0) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001757 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02001758 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02001759 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001760 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001761 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001762 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001763 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001764 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001765
Willy Tarreau87b09662015-04-03 00:22:06 +02001766 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001767 proxy_inc_fe_req_ctr(sess->fe);
1768 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001769 if (sess->listener->counters)
1770 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001771
Willy Tarreaue7dff022015-04-03 01:14:29 +02001772 if (!(s->flags & SF_FINST_MASK))
1773 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01001774 return 0;
1775 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02001776
Willy Tarreau59234e92008-11-30 23:51:27 +01001777 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001778 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001779 if (!(s->flags & SF_ERR_MASK))
1780 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01001781
Willy Tarreaufcffa692010-01-10 14:21:19 +01001782 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01001783 goto failed_keep_alive;
1784
Willy Tarreau0f228a02015-05-01 15:37:53 +02001785 if (sess->fe->options & PR_O_IGNORE_PRB)
1786 goto failed_keep_alive;
1787
Willy Tarreau4076a152009-04-02 15:18:36 +02001788 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001789 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01001790 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001791 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01001792 msg->msg_state = HTTP_MSG_ERROR;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001793 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet0184ea72017-01-05 14:06:34 +01001794 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02001795 stream_inc_http_err_ctr(s);
1796 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001797 proxy_inc_fe_req_ctr(sess->fe);
1798 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001799 if (sess->listener->counters)
1800 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001801
Willy Tarreaue7dff022015-04-03 01:14:29 +02001802 if (!(s->flags & SF_FINST_MASK))
1803 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02001804 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001805 }
1806
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001807 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001808 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001809 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01001810#ifdef TCP_QUICKACK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02001811 if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i &&
1812 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01001813 /* We need more data, we have to re-enable quick-ack in case we
1814 * previously disabled it, otherwise we might cause the client
1815 * to delay next data.
1816 */
Willy Tarreau585744b2017-08-24 14:31:19 +02001817 setsockopt(__objt_conn(sess->origin)->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01001818 }
1819#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01001820
Willy Tarreaufcffa692010-01-10 14:21:19 +01001821 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
1822 /* If the client starts to talk, let's fall back to
1823 * request timeout processing.
1824 */
1825 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01001826 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01001827 }
1828
Willy Tarreau59234e92008-11-30 23:51:27 +01001829 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01001830 if (!tick_isset(req->analyse_exp)) {
1831 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
1832 (txn->flags & TX_WAIT_NEXT_RQ) &&
1833 tick_isset(s->be->timeout.httpka))
1834 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
1835 else
1836 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
1837 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001838
Willy Tarreau59234e92008-11-30 23:51:27 +01001839 /* we're not ready yet */
1840 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001841
1842 failed_keep_alive:
1843 /* Here we process low-level errors for keep-alive requests. In
1844 * short, if the request is not the first one and it experiences
1845 * a timeout, read error or shutdown, we just silently close so
1846 * that the client can try again.
1847 */
1848 txn->status = 0;
1849 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01001850 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01001851 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02001852 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001853 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001854 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01001855 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01001856 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001857
Willy Tarreaud787e662009-07-07 10:14:51 +02001858 /* OK now we have a complete HTTP request with indexed headers. Let's
1859 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02001860 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01001861 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01001862 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001863 * byte after the last LF. msg->sov points to the first byte of data.
1864 * msg->eol cannot be trusted because it may have been left uninitialized
1865 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02001866 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02001867
Willy Tarreau87b09662015-04-03 00:22:06 +02001868 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001869 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01001870
Willy Tarreaub16a5742010-01-10 14:46:16 +01001871 if (txn->flags & TX_WAIT_NEXT_RQ) {
1872 /* kill the pending keep-alive timeout */
1873 txn->flags &= ~TX_WAIT_NEXT_RQ;
1874 req->analyse_exp = TICK_ETERNITY;
1875 }
1876
1877
Willy Tarreaud787e662009-07-07 10:14:51 +02001878 /* Maybe we found in invalid header name while we were configured not
1879 * to block on that, so we have to capture it now.
1880 */
1881 if (unlikely(msg->err_pos >= 0))
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001882 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02001883
Willy Tarreau59234e92008-11-30 23:51:27 +01001884 /*
1885 * 1: identify the method
1886 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001887 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01001888
1889 /* we can make use of server redirect on GET and HEAD */
1890 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001891 s->flags |= SF_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001892
Willy Tarreau59234e92008-11-30 23:51:27 +01001893 /*
1894 * 2: check if the URI matches the monitor_uri.
1895 * We have to do this for every request which gets in, because
1896 * the monitor-uri is defined by the frontend.
1897 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001898 if (unlikely((sess->fe->monitor_uri_len != 0) &&
1899 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02001900 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001901 sess->fe->monitor_uri,
1902 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01001903 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01001904 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01001905 */
Willy Tarreau59234e92008-11-30 23:51:27 +01001906 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001907
Willy Tarreaue7dff022015-04-03 01:14:29 +02001908 s->flags |= SF_MONITOR;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001909 sess->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001910
Willy Tarreau59234e92008-11-30 23:51:27 +01001911 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001912 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001913 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02001914
Willy Tarreau59234e92008-11-30 23:51:27 +01001915 ret = acl_pass(ret);
1916 if (cond->pol == ACL_COND_UNLESS)
1917 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001918
Willy Tarreau59234e92008-11-30 23:51:27 +01001919 if (ret) {
1920 /* we fail this request, let's return 503 service unavail */
1921 txn->status = 503;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001922 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001923 if (!(s->flags & SF_ERR_MASK))
1924 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001925 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01001926 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001927 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01001928
Willy Tarreau59234e92008-11-30 23:51:27 +01001929 /* nothing to fail, let's reply normaly */
1930 txn->status = 200;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02001931 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02001932 if (!(s->flags & SF_ERR_MASK))
1933 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01001934 goto return_prx_cond;
1935 }
1936
1937 /*
1938 * 3: Maybe we have to copy the original REQURI for the logs ?
1939 * Note: we cannot log anymore if the request has been
1940 * classified as invalid.
1941 */
1942 if (unlikely(s->logs.logwait & LW_REQ)) {
1943 /* we have a complete HTTP request that we must log */
1944 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
1945 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001946
Stéphane Cottin23e9e932017-05-18 08:58:41 +02001947 if (urilen >= global.tune.requri_len )
1948 urilen = global.tune.requri_len - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001949 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01001950 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001951
Willy Tarreaud79a3b22012-12-28 09:40:16 +01001952 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01001953 s->do_log(s);
1954 } else {
1955 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001956 }
Willy Tarreau59234e92008-11-30 23:51:27 +01001957 }
Willy Tarreau06619262006-12-17 08:37:22 +01001958
Willy Tarreau91852eb2015-05-01 13:26:00 +02001959 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
1960 * exactly one digit "." one digit. This check may be disabled using
1961 * option accept-invalid-http-request.
1962 */
1963 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
1964 if (msg->sl.rq.v_l != 8) {
1965 msg->err_pos = msg->sl.rq.v;
1966 goto return_bad_req;
1967 }
1968
1969 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
1970 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
1971 req->buf->p[msg->sl.rq.v + 6] != '.' ||
1972 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
1973 msg->err_pos = msg->sl.rq.v + 4;
1974 goto return_bad_req;
1975 }
1976 }
Willy Tarreau13317662015-05-01 13:47:08 +02001977 else {
1978 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
1979 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
1980 goto return_bad_req;
1981 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02001982
Willy Tarreau5b154472009-12-21 20:11:07 +01001983 /* ... and check if the request is HTTP/1.1 or above */
1984 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02001985 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
1986 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
1987 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01001988 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01001989
1990 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01001991 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 +01001992
Willy Tarreau88d349d2010-01-25 12:15:43 +01001993 /* if the frontend has "option http-use-proxy-header", we'll check if
1994 * we have what looks like a proxied connection instead of a connection,
1995 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
1996 * Note that this is *not* RFC-compliant, however browsers and proxies
1997 * happen to do that despite being non-standard :-(
1998 * We consider that a request not beginning with either '/' or '*' is
1999 * a proxied connection, which covers both "scheme://location" and
2000 * CONNECT ip:port.
2001 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002002 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002003 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002004 txn->flags |= TX_USE_PX_CONN;
2005
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002006 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002007 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002008
Willy Tarreau59234e92008-11-30 23:51:27 +01002009 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002010 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002011 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002012 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002013
Willy Tarreau557f1992015-05-01 10:05:17 +02002014 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
2015 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002016 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002017 * The length of a message body is determined by one of the following
2018 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02002019 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002020 * 1. Any response to a HEAD request and any response with a 1xx
2021 * (Informational), 204 (No Content), or 304 (Not Modified) status
2022 * code is always terminated by the first empty line after the
2023 * header fields, regardless of the header fields present in the
2024 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002025 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002026 * 2. Any 2xx (Successful) response to a CONNECT request implies that
2027 * the connection will become a tunnel immediately after the empty
2028 * line that concludes the header fields. A client MUST ignore any
2029 * Content-Length or Transfer-Encoding header fields received in
2030 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002031 *
Willy Tarreau557f1992015-05-01 10:05:17 +02002032 * 3. If a Transfer-Encoding header field is present and the chunked
2033 * transfer coding (Section 4.1) is the final encoding, the message
2034 * body length is determined by reading and decoding the chunked
2035 * data until the transfer coding indicates the data is complete.
2036 *
2037 * If a Transfer-Encoding header field is present in a response and
2038 * the chunked transfer coding is not the final encoding, the
2039 * message body length is determined by reading the connection until
2040 * it is closed by the server. If a Transfer-Encoding header field
2041 * is present in a request and the chunked transfer coding is not
2042 * the final encoding, the message body length cannot be determined
2043 * reliably; the server MUST respond with the 400 (Bad Request)
2044 * status code and then close the connection.
2045 *
2046 * If a message is received with both a Transfer-Encoding and a
2047 * Content-Length header field, the Transfer-Encoding overrides the
2048 * Content-Length. Such a message might indicate an attempt to
2049 * perform request smuggling (Section 9.5) or response splitting
2050 * (Section 9.4) and ought to be handled as an error. A sender MUST
2051 * remove the received Content-Length field prior to forwarding such
2052 * a message downstream.
2053 *
2054 * 4. If a message is received without Transfer-Encoding and with
2055 * either multiple Content-Length header fields having differing
2056 * field-values or a single Content-Length header field having an
2057 * invalid value, then the message framing is invalid and the
2058 * recipient MUST treat it as an unrecoverable error. If this is a
2059 * request message, the server MUST respond with a 400 (Bad Request)
2060 * status code and then close the connection. If this is a response
2061 * message received by a proxy, the proxy MUST close the connection
2062 * to the server, discard the received response, and send a 502 (Bad
2063 * Gateway) response to the client. If this is a response message
2064 * received by a user agent, the user agent MUST close the
2065 * connection to the server and discard the received response.
2066 *
2067 * 5. If a valid Content-Length header field is present without
2068 * Transfer-Encoding, its decimal value defines the expected message
2069 * body length in octets. If the sender closes the connection or
2070 * the recipient times out before the indicated number of octets are
2071 * received, the recipient MUST consider the message to be
2072 * incomplete and close the connection.
2073 *
2074 * 6. If this is a request message and none of the above are true, then
2075 * the message body length is zero (no message body is present).
2076 *
2077 * 7. Otherwise, this is a response message without a declared message
2078 * body length, so the message body length is determined by the
2079 * number of octets received prior to the server closing the
2080 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002081 */
2082
Willy Tarreau32b47f42009-10-18 20:55:02 +02002083 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002084 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau4979d5c2015-05-01 10:06:30 +02002085 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002086 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Christopher Fauletbe821b92017-03-30 11:21:53 +02002087 msg->flags |= HTTP_MSGF_TE_CHNK;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002088 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02002089 /* chunked not last, return badreq */
2090 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002091 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002092 }
2093
Willy Tarreau1c913912015-04-30 10:57:51 +02002094 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02002095 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02002096 if (msg->flags & HTTP_MSGF_TE_CHNK) {
2097 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
2098 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2099 }
Willy Tarreau34dfc602015-05-01 10:09:49 +02002100 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002101 signed long long cl;
2102
Willy Tarreauad14f752011-09-02 20:33:27 +02002103 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002104 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002105 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002106 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002107
Willy Tarreauad14f752011-09-02 20:33:27 +02002108 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002109 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002110 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002111 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002112
Willy Tarreauad14f752011-09-02 20:33:27 +02002113 if (cl < 0) {
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 Tarreaua36fc4d2012-02-17 17:39:37 +01002118 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != 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; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002121 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002122
Christopher Fauletbe821b92017-03-30 11:21:53 +02002123 msg->flags |= HTTP_MSGF_CNT_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002124 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002125 }
2126
Willy Tarreau34dfc602015-05-01 10:09:49 +02002127 /* even bodyless requests have a known length */
2128 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002129
Willy Tarreau179085c2014-04-28 16:48:56 +02002130 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
2131 * only change if both the request and the config reference something else.
2132 * Option httpclose by itself sets tunnel mode where headers are mangled.
2133 * However, if another mode is set, it will affect it (eg: server-close/
2134 * keep-alive + httpclose = close). Note that we avoid to redo the same work
2135 * if FE and BE have the same settings (common). The method consists in
2136 * checking if options changed between the two calls (implying that either
2137 * one is non-null, or one of them is non-null and we are there for the first
2138 * time.
2139 */
2140 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002141 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002142 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02002143
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02002144 /* we may have to wait for the request's body */
2145 if ((s->be->options & PR_O_WREQ_BODY) &&
2146 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
2147 req->analysers |= AN_REQ_HTTP_BODY;
2148
Willy Tarreaud787e662009-07-07 10:14:51 +02002149 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002150 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002151 req->analyse_exp = TICK_ETERNITY;
2152 return 1;
2153
2154 return_bad_req:
2155 /* We centralize bad requests processing here */
2156 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2157 /* we detected a parsing error. We want to archive this request
2158 * in the dedicated proxy area for later troubleshooting.
2159 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002160 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002161 }
2162
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002163 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02002164 txn->req.msg_state = HTTP_MSG_ERROR;
2165 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02002166 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002167
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002168 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002169 if (sess->listener->counters)
2170 sess->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002171
2172 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02002173 if (!(s->flags & SF_ERR_MASK))
2174 s->flags |= SF_ERR_PRXCOND;
2175 if (!(s->flags & SF_FINST_MASK))
2176 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02002177
Christopher Faulet0184ea72017-01-05 14:06:34 +01002178 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02002179 req->analyse_exp = TICK_ETERNITY;
2180 return 0;
2181}
2182
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002183
Willy Tarreau347a35d2013-11-22 17:51:09 +01002184/* This function prepares an applet to handle the stats. It can deal with the
2185 * "100-continue" expectation, check that admin rules are met for POST requests,
2186 * and program a response message if something was unexpected. It cannot fail
2187 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002188 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002189 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02002190 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002191 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002192int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002193{
2194 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01002195 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02002196 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002197 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002198 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002199 struct uri_auth *uri_auth = s->be->uri_auth;
2200 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002201 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002202
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002203 appctx = si_appctx(si);
2204 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2205 appctx->st1 = appctx->st2 = 0;
2206 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
2207 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02002208 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02002209 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002210
2211 uri = msg->chn->buf->p + msg->sl.rq.u;
2212 lookup = uri + uri_auth->uri_len;
2213
2214 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
2215 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002216 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002217 break;
2218 }
2219 }
2220
2221 if (uri_auth->refresh) {
2222 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
2223 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002224 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002225 break;
2226 }
2227 }
2228 }
2229
2230 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
2231 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002232 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002233 break;
2234 }
2235 }
2236
Willy Tarreau1e62df92016-01-11 18:57:53 +01002237 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
2238 if (memcmp(h, ";typed", 6) == 0) {
2239 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
2240 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
2241 break;
2242 }
2243 }
2244
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002245 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2246 if (memcmp(h, ";st=", 4) == 0) {
2247 int i;
2248 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002249 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002250 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
2251 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002252 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002253 break;
2254 }
2255 }
2256 break;
2257 }
2258 }
2259
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002260 appctx->ctx.stats.scope_str = 0;
2261 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002262 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2263 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
2264 int itx = 0;
2265 const char *h2;
2266 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
2267 const char *err;
2268
2269 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
2270 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002271 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002272 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
2273 itx++;
2274 h++;
2275 }
2276
2277 if (itx > STAT_SCOPE_TXT_MAXLEN)
2278 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002279 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002280
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002281 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002282 memcpy(scope_txt, h2, itx);
2283 scope_txt[itx] = '\0';
2284 err = invalid_char(scope_txt);
2285 if (err) {
2286 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002287 appctx->ctx.stats.scope_str = 0;
2288 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002289 }
2290 break;
2291 }
2292 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002293
2294 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002295 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002296 int ret = 1;
2297
2298 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002299 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 +01002300 ret = acl_pass(ret);
2301 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2302 ret = !ret;
2303 }
2304
2305 if (ret) {
2306 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002307 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002308 break;
2309 }
2310 }
2311
2312 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02002313 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002314 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02002315 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02002316 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
2317 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002318 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002319 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002320 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002321 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
2322 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002323 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002324 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01002325 else {
2326 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002327 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002328 }
2329
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002330 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002331 return 1;
2332}
2333
Lukas Tribus67db8df2013-06-23 17:37:13 +02002334/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
2335 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
2336 */
Vincent Bernat6e615892016-05-18 16:17:44 +02002337void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02002338{
2339#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02002340 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02002341 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2342#endif
2343#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02002344 if (from->ss_family == AF_INET6) {
2345 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02002346 /* v4-mapped addresses need IP_TOS */
2347 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
2348 else
2349 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
2350 }
2351#endif
2352}
2353
Willy Tarreau87b09662015-04-03 00:22:06 +02002354int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002355 const char* name, unsigned int name_len,
2356 const char *str, struct my_regex *re,
2357 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06002358{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002359 struct hdr_ctx ctx;
2360 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002361 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002362 int (*http_find_hdr_func)(const char *name, int len, char *sol,
2363 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002364 struct chunk *output = get_trash_chunk();
2365
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002366 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06002367
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002368 /* Choose the header browsing function. */
2369 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002370 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002371 http_find_hdr_func = http_find_header2;
2372 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002373 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002374 http_find_hdr_func = http_find_full_header2;
2375 break;
2376 default: /* impossible */
2377 return -1;
2378 }
2379
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002380 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
2381 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06002382 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002383 char *val = ctx.line + ctx.val;
2384 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06002385
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002386 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
2387 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06002388
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002389 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002390 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06002391 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06002392
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002393 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06002394
2395 hdr->len += delta;
2396 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01002397
2398 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002399 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06002400 }
2401
2402 return 0;
2403}
2404
Willy Tarreau87b09662015-04-03 00:22:06 +02002405static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002406 const char* name, unsigned int name_len,
2407 struct list *fmt, struct my_regex *re,
2408 int action)
2409{
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002410 struct chunk *replace;
2411 int ret = -1;
2412
2413 replace = alloc_trash_chunk();
2414 if (!replace)
2415 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002416
2417 replace->len = build_logline(s, replace->str, replace->size, fmt);
2418 if (replace->len >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002419 goto leave;
2420
2421 ret = http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002422
Christopher Faulet07a0fec2017-02-08 12:17:07 +01002423 leave:
2424 free_trash_chunk(replace);
2425 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01002426}
2427
Willy Tarreau87b09662015-04-03 00:22:06 +02002428/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02002429 * transaction <txn>. Returns the verdict of the first rule that prevents
2430 * further processing of the request (auth, deny, ...), and defaults to
2431 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2432 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02002433 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2434 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2435 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002436 */
Willy Tarreau0b748332014-04-29 00:13:29 +02002437enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02002438http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002439{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002440 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002441 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002442 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002443 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01002444 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02002445 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02002446 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02002447 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002448
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002449 /* If "the current_rule_list" match the executed rule list, we are in
2450 * resume condition. If a resume is needed it is always in the action
2451 * and never in the ACL or converters. In this case, we initialise the
2452 * current rule, and go to the action execution point.
2453 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002454 if (s->current_rule) {
2455 rule = s->current_rule;
2456 s->current_rule = NULL;
2457 if (s->current_rule_list == rules)
2458 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002459 }
2460 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002461
Willy Tarreauff011f22011-01-06 17:51:27 +01002462 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002463
Willy Tarreau96257ec2012-12-27 10:46:37 +01002464 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01002465 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01002466 int ret;
2467
Willy Tarreau192252e2015-04-04 01:47:55 +02002468 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002469 ret = acl_pass(ret);
2470
Willy Tarreauff011f22011-01-06 17:51:27 +01002471 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002472 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002473
2474 if (!ret) /* condition not matched */
2475 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002476 }
2477
Willy Tarreauacc98002015-09-27 23:34:39 +02002478 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002479resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01002480 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002481 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02002482 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002483
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002484 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02002485 if (deny_status)
2486 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02002487 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002488
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002489 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01002490 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02002491 if (deny_status)
2492 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02002493 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01002494
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002495 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02002496 /* Auth might be performed on regular http-req rules as well as on stats */
2497 auth_realm = rule->arg.auth.realm;
2498 if (!auth_realm) {
2499 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
2500 auth_realm = STATS_DEFAULT_REALM;
2501 else
2502 auth_realm = px->id;
2503 }
2504 /* send 401/407 depending on whether we use a proxy or not. We still
2505 * count one error, because normal browsing won't significantly
2506 * increase the counter but brute force attempts will.
2507 */
2508 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
2509 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002510 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02002511 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02002512 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002513
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002514 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02002515 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
2516 return HTTP_RULE_RES_BADREQ;
2517 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01002518
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002519 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002520 s->task->nice = rule->arg.nice;
2521 break;
2522
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002523 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002524 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002525 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002526 break;
2527
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002528 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002529#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002530 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002531 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002532#endif
2533 break;
2534
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002535 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002536 s->logs.level = rule->arg.loglevel;
2537 break;
2538
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002539 case ACT_HTTP_REPLACE_HDR:
2540 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002541 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
2542 rule->arg.hdr_add.name_len,
2543 &rule->arg.hdr_add.fmt,
2544 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06002545 return HTTP_RULE_RES_BADREQ;
2546 break;
2547
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002548 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01002549 ctx.idx = 0;
2550 /* remove all occurrences of the header */
2551 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2552 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
2553 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01002554 }
Willy Tarreau85603282015-01-21 20:39:27 +01002555 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01002556
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002557 case ACT_HTTP_SET_HDR:
2558 case ACT_HTTP_ADD_HDR:
Thierry Fournier4b788f72016-06-01 13:35:36 +02002559 /* The scope of the trash buffer must be limited to this function. The
2560 * build_logline() function can execute a lot of other function which
2561 * can use the trash buffer. So for limiting the scope of this global
2562 * buffer, we build first the header value using build_logline, and
2563 * after we store the header name.
2564 */
2565 len = rule->arg.hdr_add.name_len + 2,
2566 len += build_logline(s, trash.str + len, trash.size - len, &rule->arg.hdr_add.fmt);
Willy Tarreau96257ec2012-12-27 10:46:37 +01002567 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
Thierry Fournier4b788f72016-06-01 13:35:36 +02002568 trash.str[rule->arg.hdr_add.name_len] = ':';
2569 trash.str[rule->arg.hdr_add.name_len + 1] = ' ';
2570 trash.len = len;
Willy Tarreau85603282015-01-21 20:39:27 +01002571
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002572 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002573 /* remove all occurrences of the header */
2574 ctx.idx = 0;
2575 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2576 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
2577 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
2578 }
2579 }
2580
Willy Tarreau96257ec2012-12-27 10:46:37 +01002581 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
2582 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002583
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002584 case ACT_HTTP_DEL_ACL:
2585 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002586 struct pat_ref *ref;
2587 char *key;
2588 int len;
2589
2590 /* collect reference */
2591 ref = pat_ref_lookup(rule->arg.map.ref);
2592 if (!ref)
2593 continue;
2594
2595 /* collect key */
2596 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
2597 key = trash.str;
2598 key[len] = '\0';
2599
2600 /* perform update */
2601 /* returned code: 1=ok, 0=ko */
2602 pat_ref_delete(ref, key);
2603
2604 break;
2605 }
2606
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002607 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002608 struct pat_ref *ref;
2609 char *key;
2610 struct chunk *trash_key;
2611 int len;
2612
2613 trash_key = get_trash_chunk();
2614
2615 /* collect reference */
2616 ref = pat_ref_lookup(rule->arg.map.ref);
2617 if (!ref)
2618 continue;
2619
2620 /* collect key */
2621 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
2622 key = trash_key->str;
2623 key[len] = '\0';
2624
2625 /* perform update */
2626 /* add entry only if it does not already exist */
2627 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002628 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002629
2630 break;
2631 }
2632
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002633 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002634 struct pat_ref *ref;
2635 char *key, *value;
2636 struct chunk *trash_key, *trash_value;
2637 int len;
2638
2639 trash_key = get_trash_chunk();
2640 trash_value = get_trash_chunk();
2641
2642 /* collect reference */
2643 ref = pat_ref_lookup(rule->arg.map.ref);
2644 if (!ref)
2645 continue;
2646
2647 /* collect key */
2648 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
2649 key = trash_key->str;
2650 key[len] = '\0';
2651
2652 /* collect value */
2653 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
2654 value = trash_value->str;
2655 value[len] = '\0';
2656
2657 /* perform update */
2658 if (pat_ref_find_elt(ref, key) != NULL)
2659 /* update entry if it exists */
2660 pat_ref_set(ref, key, value, NULL);
2661 else
2662 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002663 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002664
2665 break;
2666 }
William Lallemand73025dd2014-04-24 14:38:37 +02002667
Thierry FOURNIER42148732015-09-02 17:17:33 +02002668 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02002669 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
2670 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002671
Willy Tarreauacc98002015-09-27 23:34:39 +02002672 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002673 case ACT_RET_ERR:
2674 case ACT_RET_CONT:
2675 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002676 case ACT_RET_STOP:
2677 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002678 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002679 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002680 return HTTP_RULE_RES_YIELD;
2681 }
William Lallemand73025dd2014-04-24 14:38:37 +02002682 break;
2683
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002684 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02002685 /* Note: only the first valid tracking parameter of each
2686 * applies.
2687 */
2688
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002689 if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02002690 struct stktable *t;
2691 struct stksess *ts;
2692 struct stktable_key *key;
2693 void *ptr;
2694
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02002695 t = rule->arg.trk_ctr.table.t;
2696 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 +02002697
2698 if (key && (ts = stktable_get_entry(t, key))) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002699 stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02002700
2701 /* let's count a new HTTP request as it's the first time we do it */
2702 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2703 if (ptr)
2704 stktable_data_cast(ptr, http_req_cnt)++;
2705
2706 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2707 if (ptr)
2708 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2709 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2710
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002711 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002712 if (sess->fe != s->be)
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002713 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02002714 }
2715 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02002716 break;
2717
Thierry FOURNIER22e49012015-08-05 19:13:48 +02002718 /* other flags exists, but normaly, they never be matched. */
2719 default:
2720 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002721 }
2722 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01002723
2724 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02002725 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002726}
2727
Willy Tarreau71241ab2012-12-27 11:30:54 +01002728
Willy Tarreau51d861a2015-05-22 17:30:48 +02002729/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2730 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2731 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2732 * is returned, the process can continue the evaluation of next rule list. If
2733 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2734 * is returned, it means the operation could not be processed and a server error
2735 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
2736 * deny rule. If *YIELD is returned, the caller must call again the function
2737 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002738 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002739static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002740http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002741{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002742 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02002743 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002744 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02002745 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002746 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02002747 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002748
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002749 /* If "the current_rule_list" match the executed rule list, we are in
2750 * resume condition. If a resume is needed it is always in the action
2751 * and never in the ACL or converters. In this case, we initialise the
2752 * current rule, and go to the action execution point.
2753 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02002754 if (s->current_rule) {
2755 rule = s->current_rule;
2756 s->current_rule = NULL;
2757 if (s->current_rule_list == rules)
2758 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002759 }
2760 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02002761
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002762 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002763
2764 /* check optional condition */
2765 if (rule->cond) {
2766 int ret;
2767
Willy Tarreau192252e2015-04-04 01:47:55 +02002768 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002769 ret = acl_pass(ret);
2770
2771 if (rule->cond->pol == ACL_COND_UNLESS)
2772 ret = !ret;
2773
2774 if (!ret) /* condition not matched */
2775 continue;
2776 }
2777
Willy Tarreauacc98002015-09-27 23:34:39 +02002778 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002779resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002780 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002781 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002782 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002783
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002784 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002785 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01002786 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002787
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002788 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02002789 s->task->nice = rule->arg.nice;
2790 break;
2791
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002792 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002793 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002794 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02002795 break;
2796
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002797 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02002798#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002799 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreau585744b2017-08-24 14:31:19 +02002800 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02002801#endif
2802 break;
2803
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002804 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02002805 s->logs.level = rule->arg.loglevel;
2806 break;
2807
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002808 case ACT_HTTP_REPLACE_HDR:
2809 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01002810 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
2811 rule->arg.hdr_add.name_len,
2812 &rule->arg.hdr_add.fmt,
2813 &rule->arg.hdr_add.re, rule->action))
Christopher Fauletcdade942017-02-08 12:41:31 +01002814 return HTTP_RULE_RES_BADREQ;
Sasha Pachev218f0642014-06-16 12:05:59 -06002815 break;
2816
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002817 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002818 ctx.idx = 0;
2819 /* remove all occurrences of the header */
2820 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2821 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
2822 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2823 }
Willy Tarreau85603282015-01-21 20:39:27 +01002824 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002825
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002826 case ACT_HTTP_SET_HDR:
2827 case ACT_HTTP_ADD_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002828 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
2829 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
2830 trash.len = rule->arg.hdr_add.name_len;
2831 trash.str[trash.len++] = ':';
2832 trash.str[trash.len++] = ' ';
2833 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
Willy Tarreau85603282015-01-21 20:39:27 +01002834
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002835 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01002836 /* remove all occurrences of the header */
2837 ctx.idx = 0;
2838 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
2839 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
2840 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
2841 }
2842 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02002843 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
2844 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002845
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002846 case ACT_HTTP_DEL_ACL:
2847 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002848 struct pat_ref *ref;
2849 char *key;
2850 int len;
2851
2852 /* collect reference */
2853 ref = pat_ref_lookup(rule->arg.map.ref);
2854 if (!ref)
2855 continue;
2856
2857 /* collect key */
2858 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
2859 key = trash.str;
2860 key[len] = '\0';
2861
2862 /* perform update */
2863 /* returned code: 1=ok, 0=ko */
2864 pat_ref_delete(ref, key);
2865
2866 break;
2867 }
2868
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002869 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002870 struct pat_ref *ref;
2871 char *key;
2872 struct chunk *trash_key;
2873 int len;
2874
2875 trash_key = get_trash_chunk();
2876
2877 /* collect reference */
2878 ref = pat_ref_lookup(rule->arg.map.ref);
2879 if (!ref)
2880 continue;
2881
2882 /* collect key */
2883 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
2884 key = trash_key->str;
2885 key[len] = '\0';
2886
2887 /* perform update */
2888 /* check if the entry already exists */
2889 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002890 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002891
2892 break;
2893 }
2894
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002895 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002896 struct pat_ref *ref;
2897 char *key, *value;
2898 struct chunk *trash_key, *trash_value;
2899 int len;
2900
2901 trash_key = get_trash_chunk();
2902 trash_value = get_trash_chunk();
2903
2904 /* collect reference */
2905 ref = pat_ref_lookup(rule->arg.map.ref);
2906 if (!ref)
2907 continue;
2908
2909 /* collect key */
2910 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
2911 key = trash_key->str;
2912 key[len] = '\0';
2913
2914 /* collect value */
2915 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
2916 value = trash_value->str;
2917 value[len] = '\0';
2918
2919 /* perform update */
2920 if (pat_ref_find_elt(ref, key) != NULL)
2921 /* update entry if it exists */
2922 pat_ref_set(ref, key, value, NULL);
2923 else
2924 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002925 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02002926
2927 break;
2928 }
William Lallemand73025dd2014-04-24 14:38:37 +02002929
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02002930 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02002931 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
2932 return HTTP_RULE_RES_BADREQ;
2933 return HTTP_RULE_RES_DONE;
2934
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002935 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
2936 /* Note: only the first valid tracking parameter of each
2937 * applies.
2938 */
2939
2940 if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) {
2941 struct stktable *t;
2942 struct stksess *ts;
2943 struct stktable_key *key;
2944 void *ptr;
2945
2946 t = rule->arg.trk_ctr.table.t;
2947 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
2948
2949 if (key && (ts = stktable_get_entry(t, key))) {
2950 stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts);
2951
2952 /* let's count a new HTTP request as it's the first time we do it */
2953 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
2954 if (ptr)
2955 stktable_data_cast(ptr, http_req_cnt)++;
2956
2957 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
2958 if (ptr)
2959 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
2960 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
2961
2962 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
2963 if (sess->fe != s->be)
2964 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
2965
2966 /* When the client triggers a 4xx from the server, it's most often due
2967 * to a missing object or permission. These events should be tracked
2968 * because if they happen often, it may indicate a brute force or a
2969 * vulnerability scan. Normally this is done when receiving the response
2970 * but here we're tracking after this ought to have been done so we have
2971 * to do it on purpose.
2972 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02002973 if ((unsigned)(txn->status - 400) < 100) {
2974 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
2975 if (ptr)
2976 stktable_data_cast(ptr, http_err_cnt)++;
2977
2978 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
2979 if (ptr)
2980 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
2981 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
2982 }
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08002983 }
2984 }
2985 break;
2986
Thierry FOURNIER42148732015-09-02 17:17:33 +02002987 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02002988 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
2989 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02002990
Willy Tarreauacc98002015-09-27 23:34:39 +02002991 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002992 case ACT_RET_ERR:
2993 case ACT_RET_CONT:
2994 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02002995 case ACT_RET_STOP:
2996 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02002997 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02002998 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01002999 return HTTP_RULE_RES_YIELD;
3000 }
William Lallemand73025dd2014-04-24 14:38:37 +02003001 break;
3002
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003003 /* other flags exists, but normaly, they never be matched. */
3004 default:
3005 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003006 }
3007 }
3008
3009 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003010 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003011}
3012
3013
Willy Tarreau71241ab2012-12-27 11:30:54 +01003014/* Perform an HTTP redirect based on the information in <rule>. The function
3015 * returns non-zero on success, or zero in case of a, irrecoverable error such
3016 * as too large a request to build a valid response.
3017 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003018static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01003019{
Willy Tarreaub329a312015-05-22 16:27:37 +02003020 struct http_msg *req = &txn->req;
3021 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003022 const char *msg_fmt;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003023 struct chunk *chunk;
3024 int ret = 0;
3025
3026 chunk = alloc_trash_chunk();
3027 if (!chunk)
3028 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003029
3030 /* build redirect message */
3031 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003032 case 308:
3033 msg_fmt = HTTP_308;
3034 break;
3035 case 307:
3036 msg_fmt = HTTP_307;
3037 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003038 case 303:
3039 msg_fmt = HTTP_303;
3040 break;
3041 case 301:
3042 msg_fmt = HTTP_301;
3043 break;
3044 case 302:
3045 default:
3046 msg_fmt = HTTP_302;
3047 break;
3048 }
3049
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003050 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
3051 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003052
3053 switch(rule->type) {
3054 case REDIRECT_TYPE_SCHEME: {
3055 const char *path;
3056 const char *host;
3057 struct hdr_ctx ctx;
3058 int pathlen;
3059 int hostlen;
3060
3061 host = "";
3062 hostlen = 0;
3063 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02003064 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003065 host = ctx.line + ctx.val;
3066 hostlen = ctx.vlen;
3067 }
3068
3069 path = http_get_path(txn);
3070 /* build message using path */
3071 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02003072 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003073 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3074 int qs = 0;
3075 while (qs < pathlen) {
3076 if (path[qs] == '?') {
3077 pathlen = qs;
3078 break;
3079 }
3080 qs++;
3081 }
3082 }
3083 } else {
3084 path = "/";
3085 pathlen = 1;
3086 }
3087
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003088 if (rule->rdr_str) { /* this is an old "redirect" rule */
3089 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003090 if (chunk->len + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
3091 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003092
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003093 /* add scheme */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003094 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3095 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003096 }
3097 else {
3098 /* add scheme with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003099 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003100
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003101 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003102 if (chunk->len + 3 + hostlen + pathlen > chunk->size - 4)
3103 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003104 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003105 /* add "://" */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003106 memcpy(chunk->str + chunk->len, "://", 3);
3107 chunk->len += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003108
3109 /* add host */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003110 memcpy(chunk->str + chunk->len, host, hostlen);
3111 chunk->len += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003112
3113 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003114 memcpy(chunk->str + chunk->len, path, pathlen);
3115 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003116
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003117 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003118 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003119 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003120 if (chunk->len > chunk->size - 5)
3121 goto leave;
3122 chunk->str[chunk->len] = '/';
3123 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003124 }
3125
3126 break;
3127 }
3128 case REDIRECT_TYPE_PREFIX: {
3129 const char *path;
3130 int pathlen;
3131
3132 path = http_get_path(txn);
3133 /* build message using path */
3134 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02003135 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003136 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3137 int qs = 0;
3138 while (qs < pathlen) {
3139 if (path[qs] == '?') {
3140 pathlen = qs;
3141 break;
3142 }
3143 qs++;
3144 }
3145 }
3146 } else {
3147 path = "/";
3148 pathlen = 1;
3149 }
3150
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003151 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003152 if (chunk->len + rule->rdr_len + pathlen > chunk->size - 4)
3153 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003154
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003155 /* add prefix. Note that if prefix == "/", we don't want to
3156 * add anything, otherwise it makes it hard for the user to
3157 * configure a self-redirection.
3158 */
3159 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003160 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3161 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003162 }
3163 }
3164 else {
3165 /* add prefix with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003166 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003167
3168 /* Check length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003169 if (chunk->len + pathlen > chunk->size - 4)
3170 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003171 }
3172
3173 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003174 memcpy(chunk->str + chunk->len, path, pathlen);
3175 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003176
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003177 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003178 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003179 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003180 if (chunk->len > chunk->size - 5)
3181 goto leave;
3182 chunk->str[chunk->len] = '/';
3183 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003184 }
3185
3186 break;
3187 }
3188 case REDIRECT_TYPE_LOCATION:
3189 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003190 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003191 if (chunk->len + rule->rdr_len > chunk->size - 4)
3192 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003193
3194 /* add location */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003195 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
3196 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003197 }
3198 else {
3199 /* add location with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003200 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003201
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003202 /* Check left length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003203 if (chunk->len > chunk->size - 4)
3204 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003205 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003206 break;
3207 }
3208
3209 if (rule->cookie_len) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003210 memcpy(chunk->str + chunk->len, "\r\nSet-Cookie: ", 14);
3211 chunk->len += 14;
3212 memcpy(chunk->str + chunk->len, rule->cookie_str, rule->cookie_len);
3213 chunk->len += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003214 }
3215
Willy Tarreau19b14122017-02-28 09:48:11 +01003216 /* add end of headers and the keep-alive/close status. */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003217 txn->status = rule->code;
3218 /* let's log the request time */
3219 s->logs.tv_request = now;
3220
Christopher Fauletbe821b92017-03-30 11:21:53 +02003221 if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003222 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3223 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3224 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02003225 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003226 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003227 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: keep-alive", 30);
3228 chunk->len += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003229 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003230 memcpy(chunk->str + chunk->len, "\r\nConnection: keep-alive", 24);
3231 chunk->len += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003232 }
3233 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003234 memcpy(chunk->str + chunk->len, "\r\n\r\n", 4);
3235 chunk->len += 4;
3236 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
Willy Tarreau06d80a92017-10-19 14:32:15 +02003237 co_inject(res->chn, chunk->str, chunk->len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003238 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02003239 bi_fast_delete(req->chn->buf, req->sov);
3240 req->next -= req->sov;
3241 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003242 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
Christopher Faulet014e39c2017-03-10 13:52:30 +01003243 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02003244 req->msg_state = HTTP_MSG_CLOSED;
3245 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02003246 /* Trim any possible response */
3247 res->chn->buf->i = 0;
3248 res->next = res->sov = 0;
Christopher Faulet5d468ca2017-09-11 09:27:29 +02003249 /* let the server side turn to SI_ST_CLO */
3250 channel_shutw_now(req->chn);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003251 } else {
3252 /* keep-alive not possible */
3253 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003254 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3255 chunk->len += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003256 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003257 memcpy(chunk->str + chunk->len, "\r\nConnection: close\r\n\r\n", 23);
3258 chunk->len += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003259 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003260 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01003261 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003262 }
3263
Willy Tarreaue7dff022015-04-03 01:14:29 +02003264 if (!(s->flags & SF_ERR_MASK))
3265 s->flags |= SF_ERR_LOCAL;
3266 if (!(s->flags & SF_FINST_MASK))
3267 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003268
Thierry FOURNIER0d945762017-01-28 07:39:53 +01003269 ret = 1;
3270 leave:
3271 free_trash_chunk(chunk);
3272 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003273}
3274
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003275/* This stream analyser runs all HTTP request processing which is common to
3276 * frontends and backends, which means blocking ACLs, filters, connection-close,
3277 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003278 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003279 * either needs more data or wants to immediately abort the request (eg: deny,
3280 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003281 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003282int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003283{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003284 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003285 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02003286 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003287 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003288 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02003289 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003290 int deny_status = HTTP_ERR_403;
Willy Tarreaud787e662009-07-07 10:14:51 +02003291
Willy Tarreau655dce92009-11-08 13:10:58 +01003292 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003293 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003294 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02003295 }
3296
Willy Tarreau87b09662015-04-03 00:22:06 +02003297 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 +02003298 now_ms, __FUNCTION__,
3299 s,
3300 req,
3301 req->rex, req->wex,
3302 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003303 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003304 req->analysers);
3305
Willy Tarreau65410832014-04-28 21:25:43 +02003306 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02003307 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02003308
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003309 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02003310 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02003311 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01003312
Willy Tarreau0b748332014-04-29 00:13:29 +02003313 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003314 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
3315 goto return_prx_yield;
3316
Willy Tarreau0b748332014-04-29 00:13:29 +02003317 case HTTP_RULE_RES_CONT:
3318 case HTTP_RULE_RES_STOP: /* nothing to do */
3319 break;
Willy Tarreau52542592014-04-28 18:33:26 +02003320
Willy Tarreau0b748332014-04-29 00:13:29 +02003321 case HTTP_RULE_RES_DENY: /* deny or tarpit */
3322 if (txn->flags & TX_CLTARPIT)
3323 goto tarpit;
3324 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003325
Willy Tarreau0b748332014-04-29 00:13:29 +02003326 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
3327 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02003328
Willy Tarreau0b748332014-04-29 00:13:29 +02003329 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02003330 goto done;
3331
Willy Tarreau0b748332014-04-29 00:13:29 +02003332 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
3333 goto return_bad_req;
3334 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003335 }
3336
Willy Tarreau52542592014-04-28 18:33:26 +02003337 /* OK at this stage, we know that the request was accepted according to
3338 * the http-request rules, we can check for the stats. Note that the
3339 * URI is detected *before* the req* rules in order not to be affected
3340 * by a possible reqrep, while they are processed *after* so that a
3341 * reqdeny can still block them. This clearly needs to change in 1.6!
3342 */
Willy Tarreau350f4872014-11-28 14:42:25 +01003343 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02003344 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01003345 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02003346 txn->status = 500;
3347 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003348 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003349
Willy Tarreaue7dff022015-04-03 01:14:29 +02003350 if (!(s->flags & SF_ERR_MASK))
3351 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02003352 goto return_prx_cond;
3353 }
3354
3355 /* parse the whole stats request and extract the relevant information */
3356 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02003357 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02003358 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003359
Willy Tarreau0b748332014-04-29 00:13:29 +02003360 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
3361 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003362
Willy Tarreau0b748332014-04-29 00:13:29 +02003363 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
3364 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003365 }
3366
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003367 /* evaluate the req* rules except reqadd */
3368 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003369 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003370 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003371
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003372 if (txn->flags & TX_CLDENY)
3373 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003374
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003375 if (txn->flags & TX_CLTARPIT) {
3376 deny_status = HTTP_ERR_500;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003377 goto tarpit;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003378 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003379 }
Willy Tarreau06619262006-12-17 08:37:22 +01003380
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003381 /* add request headers from the rule sets in the same order */
3382 list_for_each_entry(wl, &px->req_add, list) {
3383 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003384 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003385 ret = acl_pass(ret);
3386 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3387 ret = !ret;
3388 if (!ret)
3389 continue;
3390 }
3391
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003392 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003393 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003394 }
3395
Willy Tarreau52542592014-04-28 18:33:26 +02003396
3397 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003398 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003399 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003400 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
3401 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003402
Willy Tarreaue7dff022015-04-03 01:14:29 +02003403 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
3404 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
3405 if (!(s->flags & SF_FINST_MASK))
3406 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01003407
Willy Tarreau70730dd2014-04-24 18:06:27 +02003408 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01003409 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
3410 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003411 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003412 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003413 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003414
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003415 /* check whether we have some ACLs set to redirect this request */
3416 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003417 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003418 int ret;
3419
Willy Tarreau192252e2015-04-04 01:47:55 +02003420 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003421 ret = acl_pass(ret);
3422 if (rule->cond->pol == ACL_COND_UNLESS)
3423 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003424 if (!ret)
3425 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003426 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003427 if (!http_apply_redirect_rule(rule, s, txn))
3428 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003429 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003430 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003431
Willy Tarreau2be39392010-01-03 17:24:51 +01003432 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3433 * If this happens, then the data will not come immediately, so we must
3434 * send all what we have without waiting. Note that due to the small gain
3435 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003436 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003437 * itself once used.
3438 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003439 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003440
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003441 done: /* done with this analyser, continue with next ones that the calling
3442 * points will have set, if any.
3443 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003444 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003445 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
3446 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003447 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003448
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003449 tarpit:
Willy Tarreau6a0bca92017-06-11 17:56:27 +02003450 /* Allow cookie logging
3451 */
3452 if (s->be->cookie_name || sess->fe->capture_name)
3453 manage_client_side_cookies(s, req);
3454
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003455 /* When a connection is tarpitted, we use the tarpit timeout,
3456 * which may be the same as the connect timeout if unspecified.
3457 * If unset, then set it to zero because we really want it to
3458 * eventually expire. We build the tarpit as an analyser.
3459 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003460 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003461
3462 /* wipe the request out so that we can drop the connection early
3463 * if the client closes first.
3464 */
3465 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003466
Jarno Huuskonen800d1762017-03-06 14:56:36 +02003467 txn->status = http_err_codes[deny_status];
3468
Christopher Faulet0184ea72017-01-05 14:06:34 +01003469 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003470 req->analysers |= AN_REQ_HTTP_TARPIT;
3471 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3472 if (!req->analyse_exp)
3473 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02003474 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003475 sess->fe->fe_counters.denied_req++;
3476 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003477 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003478 if (sess->listener->counters)
3479 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02003480 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003481
3482 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003483
3484 /* Allow cookie logging
3485 */
3486 if (s->be->cookie_name || sess->fe->capture_name)
3487 manage_client_side_cookies(s, req);
3488
Willy Tarreau0b748332014-04-29 00:13:29 +02003489 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003490 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003491 s->logs.tv_request = now;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003492 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau87b09662015-04-03 00:22:06 +02003493 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003494 sess->fe->fe_counters.denied_req++;
3495 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003496 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003497 if (sess->listener->counters)
3498 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003499 goto return_prx_cond;
3500
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003501 return_bad_req:
3502 /* We centralize bad requests processing here */
3503 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3504 /* we detected a parsing error. We want to archive this request
3505 * in the dedicated proxy area for later troubleshooting.
3506 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003507 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003508 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003509
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003510 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003511 txn->req.msg_state = HTTP_MSG_ERROR;
3512 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003513 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003514
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003515 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003516 if (sess->listener->counters)
3517 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003518
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003519 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003520 if (!(s->flags & SF_ERR_MASK))
3521 s->flags |= SF_ERR_PRXCOND;
3522 if (!(s->flags & SF_FINST_MASK))
3523 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003524
Christopher Faulet0184ea72017-01-05 14:06:34 +01003525 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003526 req->analyse_exp = TICK_ETERNITY;
3527 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003528
3529 return_prx_yield:
3530 channel_dont_connect(req);
3531 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003532}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003533
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003534/* This function performs all the processing enabled for the current request.
3535 * It returns 1 if the processing can continue on next analysers, or zero if it
3536 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01003537 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003538 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003539int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003540{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003541 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003542 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003543 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02003544 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003545
Willy Tarreau655dce92009-11-08 13:10:58 +01003546 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003547 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003548 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003549 return 0;
3550 }
3551
Willy Tarreau87b09662015-04-03 00:22:06 +02003552 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 +02003553 now_ms, __FUNCTION__,
3554 s,
3555 req,
3556 req->rex, req->wex,
3557 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003558 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003559 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003560
Willy Tarreau59234e92008-11-30 23:51:27 +01003561 /*
3562 * Right now, we know that we have processed the entire headers
3563 * and that unwanted requests have been filtered out. We can do
3564 * whatever we want with the remaining request. Also, now we
3565 * may have separate values for ->fe, ->be.
3566 */
Willy Tarreau06619262006-12-17 08:37:22 +01003567
Willy Tarreau59234e92008-11-30 23:51:27 +01003568 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003569 * If HTTP PROXY is set we simply get remote server address parsing
3570 * incoming request. Note that this requires that a connection is
3571 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003572 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003573 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003574 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003575 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003576
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003577 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau973a5422015-08-05 21:47:23 +02003578 if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003579 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003580 txn->req.msg_state = HTTP_MSG_ERROR;
3581 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003582 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003583 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003584
Willy Tarreaue7dff022015-04-03 01:14:29 +02003585 if (!(s->flags & SF_ERR_MASK))
3586 s->flags |= SF_ERR_RESOURCE;
3587 if (!(s->flags & SF_FINST_MASK))
3588 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003589
3590 return 0;
3591 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003592
3593 path = http_get_path(txn);
3594 url2sa(req->buf->p + msg->sl.rq.u,
3595 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01003596 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003597 /* if the path was found, we have to remove everything between
3598 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
3599 * found, we need to replace from req->buf->p + msg->sl.rq.u for
3600 * u_l characters by a single "/".
3601 */
3602 if (path) {
3603 char *cur_ptr = req->buf->p;
3604 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3605 int delta;
3606
3607 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
3608 http_msg_move_end(&txn->req, delta);
3609 cur_end += delta;
3610 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3611 goto return_bad_req;
3612 }
3613 else {
3614 char *cur_ptr = req->buf->p;
3615 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3616 int delta;
3617
3618 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
3619 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
3620 http_msg_move_end(&txn->req, delta);
3621 cur_end += delta;
3622 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3623 goto return_bad_req;
3624 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003625 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003626
Willy Tarreau59234e92008-11-30 23:51:27 +01003627 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003628 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003629 * Note that doing so might move headers in the request, but
3630 * the fields will stay coherent and the URI will not move.
3631 * This should only be performed in the backend.
3632 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02003633 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003634 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003635
William Lallemanda73203e2012-03-12 12:48:57 +01003636 /* add unique-id if "header-unique-id" is specified */
3637
Thierry Fournierf4011dd2016-03-29 17:23:51 +02003638 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003639 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
3640 goto return_bad_req;
3641 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003642 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003643 }
William Lallemanda73203e2012-03-12 12:48:57 +01003644
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003645 if (sess->fe->header_unique_id && s->unique_id) {
3646 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003647 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003648 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003649 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003650 goto return_bad_req;
3651 }
3652
Cyril Bontéb21570a2009-11-29 20:04:48 +01003653 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003654 * 9: add X-Forwarded-For if either the frontend or the backend
3655 * asks for it.
3656 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003657 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003658 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003659 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
3660 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
3661 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003662 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003663 /* The header is set to be added only if none is present
3664 * and we found it, so don't do anything.
3665 */
3666 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003667 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003668 /* Add an X-Forwarded-For header unless the source IP is
3669 * in the 'except' network range.
3670 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003671 if ((!sess->fe->except_mask.s_addr ||
3672 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
3673 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01003674 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003675 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003676 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003677 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003678 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003679 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003680
3681 /* Note: we rely on the backend to get the header name to be used for
3682 * x-forwarded-for, because the header is really meant for the backends.
3683 * However, if the backend did not specify any option, we have to rely
3684 * on the frontend's header name.
3685 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003686 if (s->be->fwdfor_hdr_len) {
3687 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003688 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003689 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003690 len = sess->fe->fwdfor_hdr_len;
3691 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003692 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003693 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 +01003694
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003695 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003696 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003697 }
3698 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003699 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003700 /* FIXME: for the sake of completeness, we should also support
3701 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003702 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003703 int len;
3704 char pn[INET6_ADDRSTRLEN];
3705 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003706 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003707 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003708
Willy Tarreau59234e92008-11-30 23:51:27 +01003709 /* Note: we rely on the backend to get the header name to be used for
3710 * x-forwarded-for, because the header is really meant for the backends.
3711 * However, if the backend did not specify any option, we have to rely
3712 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003713 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003714 if (s->be->fwdfor_hdr_len) {
3715 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003716 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003717 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003718 len = sess->fe->fwdfor_hdr_len;
3719 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003720 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003721 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003722
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003723 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003724 goto return_bad_req;
3725 }
3726 }
3727
3728 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003729 * 10: add X-Original-To if either the frontend or the backend
3730 * asks for it.
3731 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003732 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003733
3734 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003735 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003736 /* Add an X-Original-To header unless the destination IP is
3737 * in the 'except' network range.
3738 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003739 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003740
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003741 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003742 ((!sess->fe->except_mask_to.s_addr ||
3743 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
3744 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003745 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003746 (((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 +02003747 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003748 int len;
3749 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003750 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003751
3752 /* Note: we rely on the backend to get the header name to be used for
3753 * x-original-to, because the header is really meant for the backends.
3754 * However, if the backend did not specify any option, we have to rely
3755 * on the frontend's header name.
3756 */
3757 if (s->be->orgto_hdr_len) {
3758 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003759 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003760 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003761 len = sess->fe->orgto_hdr_len;
3762 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003763 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02003764 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 +02003765
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003766 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003767 goto return_bad_req;
3768 }
3769 }
3770 }
3771
Willy Tarreau50fc7772012-11-11 22:19:57 +01003772 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3773 * If an "Upgrade" token is found, the header is left untouched in order not to have
3774 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3775 * "Upgrade" is present in the Connection header.
3776 */
3777 if (!(txn->flags & TX_HDR_CONN_UPG) &&
3778 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003779 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003780 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003781 unsigned int want_flags = 0;
3782
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003783 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003784 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003785 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003786 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003787 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003788 want_flags |= TX_CON_CLO_SET;
3789 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003790 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003791 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003792 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003793 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003794 want_flags |= TX_CON_KAL_SET;
3795 }
3796
3797 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003798 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003799 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003800
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003801
Willy Tarreau522d6c02009-12-06 18:49:18 +01003802 /* If we have no server assigned yet and we're balancing on url_param
3803 * with a POST request, we may be interested in checking the body for
3804 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003805 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02003806 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02003807 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003808 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003809 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003810 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003811 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003812
Christopher Fauletbe821b92017-03-30 11:21:53 +02003813 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
3814 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003815#ifdef TCP_QUICKACK
Christopher Fauletbe821b92017-03-30 11:21:53 +02003816 /* We expect some data from the client. Unless we know for sure
3817 * we already have a full request, we have to re-enable quick-ack
3818 * in case we previously disabled it, otherwise we might cause
3819 * the client to delay further data.
3820 */
3821 if ((sess->listener->options & LI_O_NOQUICKACK) &&
3822 cli_conn && conn_ctrl_ready(cli_conn) &&
3823 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
3824 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreau585744b2017-08-24 14:31:19 +02003825 setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003826#endif
Willy Tarreau03945942009-12-22 16:50:27 +01003827
Willy Tarreau59234e92008-11-30 23:51:27 +01003828 /*************************************************************
3829 * OK, that's finished for the headers. We have done what we *
3830 * could. Let's switch to the DATA state. *
3831 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003832 req->analyse_exp = TICK_ETERNITY;
3833 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003834
Willy Tarreau59234e92008-11-30 23:51:27 +01003835 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003836 /* OK let's go on with the BODY now */
3837 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003838
Willy Tarreau59234e92008-11-30 23:51:27 +01003839 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003840 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003841 /* we detected a parsing error. We want to archive this request
3842 * in the dedicated proxy area for later troubleshooting.
3843 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003844 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003845 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003846
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003847 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01003848 txn->req.msg_state = HTTP_MSG_ERROR;
3849 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01003850 req->analysers &= AN_REQ_FLT_END;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003851 http_reply_and_close(s, txn->status, http_error_message(s));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003852
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003853 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003854 if (sess->listener->counters)
3855 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003856
Willy Tarreaue7dff022015-04-03 01:14:29 +02003857 if (!(s->flags & SF_ERR_MASK))
3858 s->flags |= SF_ERR_PRXCOND;
3859 if (!(s->flags & SF_FINST_MASK))
3860 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003861 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003862}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003863
Willy Tarreau60b85b02008-11-30 23:28:40 +01003864/* This function is an analyser which processes the HTTP tarpit. It always
3865 * returns zero, at the beginning because it prevents any other processing
3866 * from occurring, and at the end because it terminates the request.
3867 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003868int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003869{
Willy Tarreaueee5b512015-04-03 23:46:31 +02003870 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003871
3872 /* This connection is being tarpitted. The CLIENT side has
3873 * already set the connect expiration date to the right
3874 * timeout. We just have to check that the client is still
3875 * there and that the timeout has not expired.
3876 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003877 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003878 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003879 !tick_is_expired(req->analyse_exp, now_ms))
3880 return 0;
3881
3882 /* We will set the queue timer to the time spent, just for
3883 * logging purposes. We fake a 500 server error, so that the
3884 * attacker will not suspect his connection has been tarpitted.
3885 * It will not cause trouble to the logs because we can exclude
3886 * the tarpitted connections by filtering on the 'PT' status flags.
3887 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003888 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3889
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003890 if (!(req->flags & CF_READ_ERROR))
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02003891 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003892
Christopher Faulet0184ea72017-01-05 14:06:34 +01003893 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003894 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003895
Willy Tarreaue7dff022015-04-03 01:14:29 +02003896 if (!(s->flags & SF_ERR_MASK))
3897 s->flags |= SF_ERR_PRXCOND;
3898 if (!(s->flags & SF_FINST_MASK))
3899 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003900 return 0;
3901}
3902
Willy Tarreau5a8f9472014-04-10 11:16:06 +02003903/* This function is an analyser which waits for the HTTP request body. It waits
3904 * for either the buffer to be full, or the full advertised contents to have
3905 * reached the buffer. It must only be called after the standard HTTP request
3906 * processing has occurred, because it expects the request to be parsed and will
3907 * look for the Expect header. It may send a 100-Continue interim response. It
3908 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
3909 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
3910 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01003911 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003912int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003913{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003914 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003915 struct http_txn *txn = s->txn;
3916 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003917
3918 /* We have to parse the HTTP request body to find any required data.
3919 * "balance url_param check_post" should have been the only way to get
3920 * into this. We were brought here after HTTP header analysis, so all
3921 * related structures are ready.
3922 */
3923
Willy Tarreau890988f2014-04-10 11:59:33 +02003924 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
3925 /* This is the first call */
3926 if (msg->msg_state < HTTP_MSG_BODY)
3927 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003928
Willy Tarreau890988f2014-04-10 11:59:33 +02003929 if (msg->msg_state < HTTP_MSG_100_SENT) {
3930 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3931 * send an HTTP/1.1 100 Continue intermediate response.
3932 */
3933 if (msg->flags & HTTP_MSGF_VER_11) {
3934 struct hdr_ctx ctx;
3935 ctx.idx = 0;
3936 /* Expect is allowed in 1.1, look for it */
3937 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
3938 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02003939 co_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01003940 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02003941 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003942 }
Willy Tarreau890988f2014-04-10 11:59:33 +02003943 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003944 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003945
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003946 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02003947 * req->buf->p still points to the beginning of the message. We
3948 * must save the body in msg->next because it survives buffer
3949 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003950 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003951 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003952
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003953 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003954 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3955 else
3956 msg->msg_state = HTTP_MSG_DATA;
3957 }
3958
Willy Tarreau890988f2014-04-10 11:59:33 +02003959 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
3960 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02003961 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02003962 goto missing_data;
3963
3964 /* OK we have everything we need now */
3965 goto http_end;
3966 }
3967
3968 /* OK here we're parsing a chunked-encoded message */
3969
Willy Tarreau522d6c02009-12-06 18:49:18 +01003970 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003971 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003972 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003973 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003974 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003975 unsigned int chunk;
3976 int ret = h1_parse_chunk_size(req->buf, msg->next, req->buf->i, &chunk);
Willy Tarreaud34af782008-11-30 23:36:37 +01003977
Willy Tarreau115acb92009-12-26 13:56:06 +01003978 if (!ret)
3979 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003980 else if (ret < 0) {
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003981 msg->err_pos = req->buf->i + ret;
3982 if (msg->err_pos < 0)
3983 msg->err_pos += req->buf->size;
Willy Tarreau87b09662015-04-03 00:22:06 +02003984 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003985 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003986 }
Willy Tarreaue56cdd32017-09-21 08:36:33 +02003987
3988 msg->chunk_len = chunk;
3989 msg->body_len += chunk;
3990
3991 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01003992 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003993 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01003994 }
3995
Willy Tarreaud98cf932009-12-27 22:54:55 +01003996 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02003997 * We have the first data byte is in msg->sov + msg->sol. We're waiting
3998 * for at least a whole chunk or the whole content length bytes after
3999 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004000 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004001 if (msg->msg_state == HTTP_MSG_TRAILERS)
4002 goto http_end;
4003
Willy Tarreaue115b492015-05-01 23:05:14 +02004004 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004005 goto http_end;
4006
4007 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004008 /* we get here if we need to wait for more data. If the buffer is full,
4009 * we have the maximum we can expect.
4010 */
4011 if (buffer_full(req->buf, global.tune.maxrewrite))
4012 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004013
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004014 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004015 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004016 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004017
Willy Tarreaue7dff022015-04-03 01:14:29 +02004018 if (!(s->flags & SF_ERR_MASK))
4019 s->flags |= SF_ERR_CLITO;
4020 if (!(s->flags & SF_FINST_MASK))
4021 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004022 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004023 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004024
4025 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004026 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004027 /* Not enough data. We'll re-use the http-request
4028 * timeout here. Ideally, we should set the timeout
4029 * relative to the accept() date. We just set the
4030 * request timeout once at the beginning of the
4031 * request.
4032 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004033 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004034 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004035 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004036 return 0;
4037 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004038
4039 http_end:
4040 /* The situation will not evolve, so let's give up on the analysis. */
4041 s->logs.tv_request = now; /* update the request timer to reflect full request */
4042 req->analysers &= ~an_bit;
4043 req->analyse_exp = TICK_ETERNITY;
4044 return 1;
4045
4046 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004047 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004048 txn->req.msg_state = HTTP_MSG_ERROR;
4049 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004050 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004051
Willy Tarreaue7dff022015-04-03 01:14:29 +02004052 if (!(s->flags & SF_ERR_MASK))
4053 s->flags |= SF_ERR_PRXCOND;
4054 if (!(s->flags & SF_FINST_MASK))
4055 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004056
Willy Tarreau522d6c02009-12-06 18:49:18 +01004057 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01004058 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004059 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004060 if (sess->listener->counters)
4061 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004062 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004063}
4064
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004065/* send a server's name with an outgoing request over an established connection.
4066 * Note: this function is designed to be called once the request has been scheduled
4067 * for being forwarded. This is the reason why it rewinds the buffer before
4068 * proceeding.
4069 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004070int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004071
4072 struct hdr_ctx ctx;
4073
Mark Lamourinec2247f02012-01-04 13:02:01 -05004074 char *hdr_name = be->server_id_hdr_name;
4075 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004076 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004077 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004078 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004079
William Lallemandd9e90662012-01-30 17:27:17 +01004080 ctx.idx = 0;
4081
Willy Tarreau211cdec2014-04-17 20:18:08 +02004082 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004083 if (old_o) {
4084 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004085 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004086 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004087 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004088 }
4089
Willy Tarreau9b28e032012-10-12 23:49:43 +02004090 old_i = chn->buf->i;
4091 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 -05004092 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004093 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004094 }
4095
4096 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004097 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004098 memcpy(hdr_val, hdr_name, hdr_name_len);
4099 hdr_val += hdr_name_len;
4100 *hdr_val++ = ':';
4101 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004102 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4103 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004104
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004105 if (old_o) {
4106 /* If this was a forwarded request, we must readjust the amount of
4107 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004108 * variations. Note that the current state is >= HTTP_MSG_BODY,
4109 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004110 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004111 old_o += chn->buf->i - old_i;
4112 b_adv(chn->buf, old_o);
4113 txn->req.next -= old_o;
4114 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004115 }
4116
Mark Lamourinec2247f02012-01-04 13:02:01 -05004117 return 0;
4118}
4119
Willy Tarreau610ecce2010-01-04 21:15:02 +01004120/* Terminate current transaction and prepare a new one. This is very tricky
4121 * right now but it works.
4122 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004123void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004124{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004125 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02004126 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01004127 struct proxy *be = s->be;
Willy Tarreau323a2d92015-08-04 19:00:17 +02004128 struct connection *srv_conn;
4129 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02004130 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01004131
Willy Tarreau610ecce2010-01-04 21:15:02 +01004132 /* FIXME: We need a more portable way of releasing a backend's and a
4133 * server's connections. We need a safer way to reinitialize buffer
4134 * flags. We also need a more accurate method for computing per-request
4135 * data.
4136 */
Willy Tarreau323a2d92015-08-04 19:00:17 +02004137 srv_conn = objt_conn(s->si[1].end);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004138
Willy Tarreau4213a112013-12-15 10:25:42 +01004139 /* unless we're doing keep-alive, we want to quickly close the connection
4140 * to the server.
4141 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004142 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004143 !si_conn_ready(&s->si[1])) {
4144 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4145 si_shutr(&s->si[1]);
4146 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01004147 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004148
Willy Tarreaue7dff022015-04-03 01:14:29 +02004149 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau858b1032015-12-07 17:04:59 +01004150 be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004151 if (unlikely(s->srv_conn))
4152 sess_change_server(s, NULL);
4153 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004154
4155 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02004156 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004157
Willy Tarreaueee5b512015-04-03 23:46:31 +02004158 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004159 int n;
4160
Willy Tarreaueee5b512015-04-03 23:46:31 +02004161 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004162 if (n < 1 || n > 5)
4163 n = 0;
4164
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004165 if (fe->mode == PR_MODE_HTTP) {
4166 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004167 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02004168 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01004169 (be->mode == PR_MODE_HTTP)) {
4170 be->be_counters.p.http.rsp[n]++;
4171 be->be_counters.p.http.cum_req++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004172 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004173 }
4174
4175 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004176 s->logs.bytes_in -= s->req.buf->i;
4177 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004178
4179 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004180 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02004181 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004182 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004183 s->do_log(s);
4184 }
4185
Willy Tarreaud713bcc2014-06-25 15:36:04 +02004186 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02004187 stream_stop_content_counters(s);
4188 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004189
Willy Tarreau610ecce2010-01-04 21:15:02 +01004190 s->logs.accept_date = date; /* user-visible date for logging */
4191 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02004192 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
4193 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004194 tv_zero(&s->logs.tv_request);
4195 s->logs.t_queue = -1;
4196 s->logs.t_connect = -1;
4197 s->logs.t_data = -1;
4198 s->logs.t_close = 0;
4199 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4200 s->logs.srv_queue_size = 0; /* we will get this number soon */
4201
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004202 s->logs.bytes_in = s->req.total = s->req.buf->i;
4203 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004204
4205 if (s->pend_pos)
4206 pendconn_free(s->pend_pos);
4207
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004208 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004209 if (s->flags & SF_CURR_SESS) {
4210 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004211 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004212 }
Willy Tarreau858b1032015-12-07 17:04:59 +01004213 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004214 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004215 }
4216
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004217 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004218
Willy Tarreau4213a112013-12-15 10:25:42 +01004219 /* only release our endpoint if we don't intend to reuse the
4220 * connection.
4221 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004222 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004223 !si_conn_ready(&s->si[1])) {
4224 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02004225 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004226 }
4227
Willy Tarreau350f4872014-11-28 14:42:25 +01004228 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
4229 s->si[1].err_type = SI_ET_NONE;
4230 s->si[1].conn_retries = 0; /* used for logging too */
4231 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02004232 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 +01004233 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);
4234 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 +02004235 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
4236 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
4237 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01004238
Willy Tarreaueee5b512015-04-03 23:46:31 +02004239 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004240 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02004241 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004242
4243 if (prev_status == 401 || prev_status == 407) {
4244 /* In HTTP keep-alive mode, if we receive a 401, we still have
4245 * a chance of being able to send the visitor again to the same
4246 * server over the same connection. This is required by some
4247 * broken protocols such as NTLM, and anyway whenever there is
4248 * an opportunity for sending the challenge to the proper place,
4249 * it's better to do it (at least it helps with debugging).
4250 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004251 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02004252 if (srv_conn)
4253 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01004254 }
4255
Willy Tarreau53f96852016-02-02 18:50:47 +01004256 /* Never ever allow to reuse a connection from a non-reuse backend */
4257 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
4258 srv_conn->flags |= CO_FL_PRIVATE;
4259
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004260 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01004261 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004262
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004263 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004264 s->req.flags |= CF_NEVER_WAIT;
4265 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004266 }
4267
Willy Tarreau714ea782015-11-25 20:11:11 +01004268 /* we're removing the analysers, we MUST re-enable events detection.
4269 * We don't enable close on the response channel since it's either
4270 * already closed, or in keep-alive with an idle connection handler.
4271 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004272 channel_auto_read(&s->req);
4273 channel_auto_close(&s->req);
4274 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004275
Willy Tarreau1c59bd52015-11-02 20:20:11 +01004276 /* we're in keep-alive with an idle connection, monitor it if not already done */
4277 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02004278 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02004279 if (!srv)
4280 si_idle_conn(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01004281 else if (srv_conn->flags & CO_FL_PRIVATE)
Willy Tarreau8dff9982015-08-04 20:45:52 +02004282 si_idle_conn(&s->si[1], &srv->priv_conns);
Willy Tarreau449d74a2015-08-05 17:16:33 +02004283 else if (prev_flags & TX_NOT_FIRST)
4284 /* note: we check the request, not the connection, but
4285 * this is valid for strategies SAFE and AGGR, and in
4286 * case of ALWS, we don't care anyway.
4287 */
4288 si_idle_conn(&s->si[1], &srv->safe_conns);
Willy Tarreau8dff9982015-08-04 20:45:52 +02004289 else
4290 si_idle_conn(&s->si[1], &srv->idle_conns);
Willy Tarreau4320eaa2015-08-05 11:08:30 +02004291 }
Christopher Faulete6006242017-03-10 11:52:44 +01004292 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
4293 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004294}
4295
4296
4297/* This function updates the request state machine according to the response
4298 * state machine and buffer flags. It returns 1 if it changes anything (flag
4299 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4300 * it is only used to find when a request/response couple is complete. Both
4301 * this function and its equivalent should loop until both return zero. It
4302 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4303 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004304int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004305{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004306 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004307 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004308 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004309 unsigned int old_state = txn->req.msg_state;
4310
Christopher Faulet4be98032017-07-18 10:48:24 +02004311 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004312 return 0;
4313
4314 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004315 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004316 * We can shut the read side unless we want to abort_on_close,
4317 * or we have a POST request. The issue with POST requests is
4318 * that some browsers still send a CRLF after the request, and
4319 * this CRLF must be read so that it does not remain in the kernel
4320 * buffers, otherwise a close could cause an RST on some systems
4321 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004322 * Note that if we're using keep-alive on the client side, we'd
4323 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02004324 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01004325 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004326 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004327 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4328 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4329 !(s->be->options & PR_O_ABRT_CLOSE) &&
4330 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004331 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004332
Willy Tarreau40f151a2012-12-20 12:10:09 +01004333 /* if the server closes the connection, we want to immediately react
4334 * and close the socket to save packets and syscalls.
4335 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004336 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01004337
Willy Tarreau7f876a12015-11-18 11:59:55 +01004338 /* In any case we've finished parsing the request so we must
4339 * disable Nagle when sending data because 1) we're not going
4340 * to shut this side, and 2) the server is waiting for us to
4341 * send pending data.
4342 */
4343 chn->flags |= CF_NEVER_WAIT;
4344
Willy Tarreau610ecce2010-01-04 21:15:02 +01004345 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4346 goto wait_other_side;
4347
4348 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4349 /* The server has not finished to respond, so we
4350 * don't want to move in order not to upset it.
4351 */
4352 goto wait_other_side;
4353 }
4354
Willy Tarreau610ecce2010-01-04 21:15:02 +01004355 /* When we get here, it means that both the request and the
4356 * response have finished receiving. Depending on the connection
4357 * mode, we'll have to wait for the last bytes to leave in either
4358 * direction, and sometimes for a close to be effective.
4359 */
4360
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004361 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4362 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004363 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4364 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004365 }
4366 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4367 /* Option forceclose is set, or either side wants to close,
4368 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004369 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004370 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004371 *
4372 * However, there is an exception if the response
4373 * length is undefined. In this case, we need to wait
4374 * the close from the server. The response will be
4375 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004376 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004377 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4378 txn->rsp.msg_state != HTTP_MSG_CLOSED)
4379 goto check_channel_flags;
4380
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004381 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4382 channel_shutr_now(chn);
4383 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004384 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004385 }
4386 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004387 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4388 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004389 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004390 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4391 channel_auto_read(chn);
4392 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004393 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004394 }
4395
Christopher Faulet4be98032017-07-18 10:48:24 +02004396 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004397 }
4398
4399 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4400 http_msg_closing:
4401 /* nothing else to forward, just waiting for the output buffer
4402 * to be empty and for the shutw_now to take effect.
4403 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004404 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004405 txn->req.msg_state = HTTP_MSG_CLOSED;
4406 goto http_msg_closed;
4407 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004408 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004409 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004410 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004411 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004412 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004413 }
4414
4415 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4416 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01004417 /* see above in MSG_DONE why we only do this in these states */
4418 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4419 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4420 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004421 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004422 goto wait_other_side;
4423 }
4424
Christopher Faulet4be98032017-07-18 10:48:24 +02004425 check_channel_flags:
4426 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4427 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4428 /* if we've just closed an output, let's switch */
4429 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4430 txn->req.msg_state = HTTP_MSG_CLOSING;
4431 goto http_msg_closing;
4432 }
4433
4434
Willy Tarreau610ecce2010-01-04 21:15:02 +01004435 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004436 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004437}
4438
4439
4440/* This function updates the response state machine according to the request
4441 * state machine and buffer flags. It returns 1 if it changes anything (flag
4442 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4443 * it is only used to find when a request/response couple is complete. Both
4444 * this function and its equivalent should loop until both return zero. It
4445 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4446 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004447int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004448{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004449 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004450 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004451 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004452 unsigned int old_state = txn->rsp.msg_state;
4453
Christopher Faulet4be98032017-07-18 10:48:24 +02004454 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004455 return 0;
4456
4457 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4458 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004459 * still monitor the server connection for a possible close
4460 * while the request is being uploaded, so we don't disable
4461 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004462 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004463 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004464
4465 if (txn->req.msg_state == HTTP_MSG_ERROR)
4466 goto wait_other_side;
4467
4468 if (txn->req.msg_state < HTTP_MSG_DONE) {
4469 /* The client seems to still be sending data, probably
4470 * because we got an error response during an upload.
4471 * We have the choice of either breaking the connection
4472 * or letting it pass through. Let's do the later.
4473 */
4474 goto wait_other_side;
4475 }
4476
Willy Tarreau610ecce2010-01-04 21:15:02 +01004477 /* When we get here, it means that both the request and the
4478 * response have finished receiving. Depending on the connection
4479 * mode, we'll have to wait for the last bytes to leave in either
4480 * direction, and sometimes for a close to be effective.
4481 */
4482
4483 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4484 /* Server-close mode : shut read and wait for the request
4485 * side to close its output buffer. The caller will detect
4486 * when we're in DONE and the other is in CLOSED and will
4487 * catch that for the final cleanup.
4488 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004489 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4490 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004491 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004492 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4493 /* Option forceclose is set, or either side wants to close,
4494 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004495 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004496 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004497 *
4498 * However, there is an exception if the response length
4499 * is undefined. In this case, we switch in TUNNEL mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004500 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004501 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN)) {
4502 channel_auto_read(chn);
4503 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4504 chn->flags |= CF_NEVER_WAIT;
4505 }
4506 else if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004507 channel_shutr_now(chn);
4508 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004509 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004510 }
4511 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004512 /* The last possible modes are keep-alive and tunnel. Tunnel will
4513 * need to forward remaining data. Keep-alive will need to monitor
4514 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004515 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004516 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004517 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004518 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4519 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004520 }
4521
Christopher Faulet4be98032017-07-18 10:48:24 +02004522 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004523 }
4524
4525 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4526 http_msg_closing:
4527 /* nothing else to forward, just waiting for the output buffer
4528 * to be empty and for the shutw_now to take effect.
4529 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004530 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004531 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4532 goto http_msg_closed;
4533 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004534 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02004535 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004536 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004537 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004538 if (objt_server(s->target))
4539 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004540 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004541 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004542 }
4543
4544 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4545 http_msg_closed:
4546 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01004547 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004548 channel_auto_close(chn);
4549 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004550 goto wait_other_side;
4551 }
4552
Christopher Faulet4be98032017-07-18 10:48:24 +02004553 check_channel_flags:
4554 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4555 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4556 /* if we've just closed an output, let's switch */
4557 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4558 goto http_msg_closing;
4559 }
4560
Willy Tarreau610ecce2010-01-04 21:15:02 +01004561 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004562 /* We force the response to leave immediately if we're waiting for the
4563 * other side, since there is no pending shutdown to push it out.
4564 */
4565 if (!channel_is_empty(chn))
4566 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004567 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004568}
4569
4570
Christopher Faulet894da4c2017-07-18 11:29:07 +02004571/* Resync the request and response state machines. */
4572void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004573{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004574 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004575#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01004576 int old_req_state = txn->req.msg_state;
4577 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004578#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01004579
Willy Tarreau610ecce2010-01-04 21:15:02 +01004580 http_sync_req_state(s);
4581 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004582 if (!http_sync_res_state(s))
4583 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004584 if (!http_sync_req_state(s))
4585 break;
4586 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004587
Christopher Faulet894da4c2017-07-18 11:29:07 +02004588 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
4589 "req->analysers=0x%08x res->analysers=0x%08x\n",
4590 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02004591 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
4592 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02004593 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02004594
4595
Willy Tarreau610ecce2010-01-04 21:15:02 +01004596 /* OK, both state machines agree on a compatible state.
4597 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01004598 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4599 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02004600 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
4601 * means we must abort the request.
4602 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
4603 * corresponding channel.
4604 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
4605 * on the response with server-close mode means we've completed one
4606 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004607 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02004608 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4609 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004610 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004611 channel_auto_close(&s->req);
4612 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004613 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004614 channel_auto_close(&s->res);
4615 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004616 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004617 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4618 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004619 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004620 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004621 channel_auto_close(&s->res);
4622 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004623 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004624 channel_abort(&s->req);
4625 channel_auto_close(&s->req);
4626 channel_auto_read(&s->req);
4627 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004628 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004629 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4630 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4631 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4632 s->req.analysers &= AN_REQ_FLT_END;
4633 if (HAS_REQ_DATA_FILTERS(s))
4634 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
4635 }
4636 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4637 s->res.analysers &= AN_RES_FLT_END;
4638 if (HAS_RSP_DATA_FILTERS(s))
4639 s->res.analysers |= AN_RES_FLT_XFER_DATA;
4640 }
4641 channel_auto_close(&s->req);
4642 channel_auto_read(&s->req);
4643 channel_auto_close(&s->res);
4644 channel_auto_read(&s->res);
4645 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004646 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4647 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004648 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004649 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4650 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4651 /* server-close/keep-alive: terminate this transaction,
4652 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004653 * a fresh-new transaction, but only once we're sure there's
4654 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02004655 * another request. They must not hold any pending output data
4656 * and the response buffer must realigned
4657 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01004658 */
Christopher Faulet894da4c2017-07-18 11:29:07 +02004659 if (s->req.buf->o)
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004660 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004661 else if (s->res.buf->o)
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004662 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02004663 else {
4664 s->req.analysers = AN_REQ_FLT_END;
4665 s->res.analysers = AN_RES_FLT_END;
4666 txn->flags |= TX_WAIT_CLEANUP;
4667 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004668 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004669}
4670
Willy Tarreaud98cf932009-12-27 22:54:55 +01004671/* This function is an analyser which forwards request body (including chunk
4672 * sizes if any). It is called as soon as we must forward, even if we forward
4673 * zero byte. The only situation where it must not be called is when we're in
4674 * tunnel mode and we want to forward till the close. It's used both to forward
4675 * remaining data and to resync after end of body. It expects the msg_state to
4676 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02004677 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01004678 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004679 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004680 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004681int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004682{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004683 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004684 struct http_txn *txn = s->txn;
4685 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01004686 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004687
Christopher Faulet814d2702017-03-30 11:33:44 +02004688 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
4689 now_ms, __FUNCTION__,
4690 s,
4691 req,
4692 req->rex, req->wex,
4693 req->flags,
4694 req->buf->i,
4695 req->analysers);
4696
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004697 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4698 return 0;
4699
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004700 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004701 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004702 /* Output closed while we were sending data. We must abort and
4703 * wake the other side up.
4704 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004705 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02004706 msg->msg_state = HTTP_MSG_ERROR;
4707 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004708 return 1;
4709 }
4710
Willy Tarreaud98cf932009-12-27 22:54:55 +01004711 /* Note that we don't have to send 100-continue back because we don't
4712 * need the data to complete our job, and it's up to the server to
4713 * decide whether to return 100, 417 or anything else in return of
4714 * an "Expect: 100-continue" header.
4715 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004716 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004717 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4718 ? HTTP_MSG_CHUNK_SIZE
4719 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004720
4721 /* TODO/filters: when http-buffer-request option is set or if a
4722 * rule on url_param exists, the first chunk size could be
4723 * already parsed. In that case, msg->next is after the chunk
4724 * size (including the CRLF after the size). So this case should
4725 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004726 }
4727
Willy Tarreau7ba23542014-04-17 21:50:00 +02004728 /* Some post-connect processing might want us to refrain from starting to
4729 * forward data. Currently, the only reason for this is "balance url_param"
4730 * whichs need to parse/process the request after we've enabled forwarding.
4731 */
4732 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004733 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004734 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004735 req->flags |= CF_WAKE_CONNECT;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004736 goto missing_data_or_waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004737 }
4738 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4739 }
4740
Willy Tarreau80a92c02014-03-12 10:41:13 +01004741 /* in most states, we should abort in case of early close */
4742 channel_auto_close(req);
4743
Willy Tarreauefdf0942014-04-24 20:08:57 +02004744 if (req->to_forward) {
4745 /* We can't process the buffer's contents yet */
4746 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004747 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004748 }
4749
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004750 if (msg->msg_state < HTTP_MSG_DONE) {
4751 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4752 ? http_msg_forward_chunked_body(s, msg)
4753 : http_msg_forward_body(s, msg));
4754 if (!ret)
4755 goto missing_data_or_waiting;
4756 if (ret < 0)
4757 goto return_bad_req;
4758 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004759
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004760 /* other states, DONE...TUNNEL */
4761 /* we don't want to forward closes on DONE except in tunnel mode. */
4762 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4763 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004764
Christopher Faulet894da4c2017-07-18 11:29:07 +02004765 http_resync_states(s);
4766 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004767 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4768 if (req->flags & CF_SHUTW) {
4769 /* request errors are most likely due to the
4770 * server aborting the transfer. */
4771 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004772 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004773 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004774 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004775 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004776 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004777 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004778 }
4779
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004780 /* If "option abortonclose" is set on the backend, we want to monitor
4781 * the client's connection and forward any shutdown notification to the
4782 * server, which will decide whether to close or to go on processing the
4783 * request. We only do that in tunnel mode, and not in other modes since
4784 * it can be abused to exhaust source ports. */
4785 if (s->be->options & PR_O_ABRT_CLOSE) {
4786 channel_auto_read(req);
4787 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4788 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4789 s->si[1].flags |= SI_FL_NOLINGER;
4790 channel_auto_close(req);
4791 }
4792 else if (s->txn->meth == HTTP_METH_POST) {
4793 /* POST requests may require to read extra CRLF sent by broken
4794 * browsers and which could cause an RST to be sent upon close
4795 * on some systems (eg: Linux). */
4796 channel_auto_read(req);
4797 }
4798 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004799
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004800 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004801 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02004802 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004803 if (!(s->flags & SF_ERR_MASK))
4804 s->flags |= SF_ERR_CLICL;
4805 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004806 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004807 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004808 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004809 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004810 }
4811
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004812 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004813 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004814 if (objt_server(s->target))
4815 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004816
4817 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004818 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004819
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004820 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004821 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004822 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004823
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004824 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004825 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004826 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004827 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004828 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004829
Willy Tarreau5c620922011-05-11 19:56:11 +02004830 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004831 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004832 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004833 * modes are already handled by the stream sock layer. We must not do
4834 * this in content-length mode because it could present the MSG_MORE
4835 * flag with the last block of forwarded data, which would cause an
4836 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004837 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004838 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004839 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004840
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004841 return 0;
4842
Willy Tarreaud98cf932009-12-27 22:54:55 +01004843 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004844 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004845 if (sess->listener->counters)
4846 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004847
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004848 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004849 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004850 txn->req.msg_state = HTTP_MSG_ERROR;
4851 if (txn->status) {
4852 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004853 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004854 } else {
4855 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004856 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004857 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004858 req->analysers &= AN_REQ_FLT_END;
4859 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 +01004860
Willy Tarreaue7dff022015-04-03 01:14:29 +02004861 if (!(s->flags & SF_ERR_MASK))
4862 s->flags |= SF_ERR_PRXCOND;
4863 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004864 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004865 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004866 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004867 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004868 }
4869 return 0;
4870
4871 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004872 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004873 txn->req.msg_state = HTTP_MSG_ERROR;
4874 if (txn->status) {
4875 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004876 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004877 } else {
4878 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004879 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004880 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004881 req->analysers &= AN_REQ_FLT_END;
4882 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 +01004883
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004884 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004885 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004886 if (objt_server(s->target))
4887 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004888
Willy Tarreaue7dff022015-04-03 01:14:29 +02004889 if (!(s->flags & SF_ERR_MASK))
4890 s->flags |= SF_ERR_SRVCL;
4891 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004892 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004893 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004894 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004895 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004896 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004897 return 0;
4898}
4899
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004900/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4901 * processing can continue on next analysers, or zero if it either needs more
4902 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004903 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004904 * when it has nothing left to do, and may remove any analyser when it wants to
4905 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004906 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004907int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004908{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004909 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004910 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004911 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004912 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004913 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004914 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004915 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004916
Willy Tarreau87b09662015-04-03 00:22:06 +02004917 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 +02004918 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004919 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004920 rep,
4921 rep->rex, rep->wex,
4922 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004923 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004924 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004925
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004926 /*
4927 * Now parse the partial (or complete) lines.
4928 * We will check the response syntax, and also join multi-line
4929 * headers. An index of all the lines will be elaborated while
4930 * parsing.
4931 *
4932 * For the parsing, we use a 28 states FSM.
4933 *
4934 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02004935 * rep->buf->p = beginning of response
4936 * rep->buf->p + msg->eoh = end of processed headers / start of current one
4937 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02004938 * msg->eol = end of current header or line (LF or CRLF)
4939 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004940 */
4941
Willy Tarreau628c40c2014-04-24 19:11:26 +02004942 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01004943 /* There's a protected area at the end of the buffer for rewriting
4944 * purposes. We don't want to start to parse the request if the
4945 * protected area is affected, because we may have to move processed
4946 * data later, which is much more complicated.
4947 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004948 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01004949 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02004950 /* some data has still not left the buffer, wake us once that's done */
4951 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
4952 goto abort_response;
4953 channel_dont_close(rep);
4954 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004955 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02004956 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01004957 }
4958
Willy Tarreau379357a2013-06-08 12:55:46 +02004959 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
4960 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
4961 buffer_slow_realign(rep->buf);
4962
Willy Tarreau9b28e032012-10-12 23:49:43 +02004963 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01004964 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004965 }
4966
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004967 /* 1: we might have to print this header in debug mode */
4968 if (unlikely((global.mode & MODE_DEBUG) &&
4969 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02004970 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004971 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004972
Willy Tarreau9b28e032012-10-12 23:49:43 +02004973 sol = rep->buf->p;
4974 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004975 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004976
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004977 sol += hdr_idx_first_pos(&txn->hdr_idx);
4978 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004979
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004980 while (cur_idx) {
4981 eol = sol + txn->hdr_idx.v[cur_idx].len;
4982 debug_hdr("srvhdr", s, sol, eol);
4983 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4984 cur_idx = txn->hdr_idx.v[cur_idx].next;
4985 }
4986 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004987
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004988 /*
4989 * Now we quickly check if we have found a full valid response.
4990 * If not so, we check the FD and buffer states before leaving.
4991 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004992 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004993 * responses are checked first.
4994 *
4995 * Depending on whether the client is still there or not, we
4996 * may send an error response back or not. Note that normally
4997 * we should only check for HTTP status there, and check I/O
4998 * errors somewhere else.
4999 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005000
Willy Tarreau655dce92009-11-08 13:10:58 +01005001 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005002 /* Invalid response */
5003 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5004 /* we detected a parsing error. We want to archive this response
5005 * in the dedicated proxy area for later troubleshooting.
5006 */
5007 hdr_response_bad:
5008 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005009 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005010
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005011 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005012 if (objt_server(s->target)) {
5013 objt_server(s->target)->counters.failed_resp++;
5014 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005015 }
Willy Tarreau64648412010-03-05 10:41:54 +01005016 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005017 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005018 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005019 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005020 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005021 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005022 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005023
Willy Tarreaue7dff022015-04-03 01:14:29 +02005024 if (!(s->flags & SF_ERR_MASK))
5025 s->flags |= SF_ERR_PRXCOND;
5026 if (!(s->flags & SF_FINST_MASK))
5027 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005028
5029 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005030 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005031
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005032 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005033 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005034 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005035 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005036 goto hdr_response_bad;
5037 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005038
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005039 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005040 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005041 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005042 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005043 else if (txn->flags & TX_NOT_FIRST)
5044 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005045
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005046 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005047 if (objt_server(s->target)) {
5048 objt_server(s->target)->counters.failed_resp++;
5049 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005050 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005051
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005052 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005053 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005054 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005055 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005056 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005057 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02005058
Willy Tarreaue7dff022015-04-03 01:14:29 +02005059 if (!(s->flags & SF_ERR_MASK))
5060 s->flags |= SF_ERR_SRVCL;
5061 if (!(s->flags & SF_FINST_MASK))
5062 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005063 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005064 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005065
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005066 /* read timeout : return a 504 to the client. */
5067 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005068 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005069 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005070
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005071 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005072 if (objt_server(s->target)) {
5073 objt_server(s->target)->counters.failed_resp++;
5074 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005075 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005076
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005077 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005078 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005079 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01005080 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005081 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005082 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02005083
Willy Tarreaue7dff022015-04-03 01:14:29 +02005084 if (!(s->flags & SF_ERR_MASK))
5085 s->flags |= SF_ERR_SRVTO;
5086 if (!(s->flags & SF_FINST_MASK))
5087 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005088 return 0;
5089 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005090
Willy Tarreauf003d372012-11-26 13:35:37 +01005091 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005092 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005093 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01005094 s->be->be_counters.cli_aborts++;
5095 if (objt_server(s->target))
5096 objt_server(s->target)->counters.cli_aborts++;
5097
Christopher Faulet0184ea72017-01-05 14:06:34 +01005098 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01005099 channel_auto_close(rep);
5100
5101 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01005102 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005103 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01005104
Willy Tarreaue7dff022015-04-03 01:14:29 +02005105 if (!(s->flags & SF_ERR_MASK))
5106 s->flags |= SF_ERR_CLICL;
5107 if (!(s->flags & SF_FINST_MASK))
5108 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01005109
Willy Tarreau87b09662015-04-03 00:22:06 +02005110 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01005111 return 0;
5112 }
5113
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005114 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005115 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005116 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005117 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005118 else if (txn->flags & TX_NOT_FIRST)
5119 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005120
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005121 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005122 if (objt_server(s->target)) {
5123 objt_server(s->target)->counters.failed_resp++;
5124 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005125 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005126
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005127 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005128 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005129 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005130 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005131 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005132 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005133
Willy Tarreaue7dff022015-04-03 01:14:29 +02005134 if (!(s->flags & SF_ERR_MASK))
5135 s->flags |= SF_ERR_SRVCL;
5136 if (!(s->flags & SF_FINST_MASK))
5137 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005138 return 0;
5139 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005140
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005141 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005142 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005143 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005144 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005145 else if (txn->flags & TX_NOT_FIRST)
5146 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005147
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005148 s->be->be_counters.failed_resp++;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005149 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005150 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005151
Willy Tarreaue7dff022015-04-03 01:14:29 +02005152 if (!(s->flags & SF_ERR_MASK))
5153 s->flags |= SF_ERR_CLICL;
5154 if (!(s->flags & SF_FINST_MASK))
5155 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005156
Willy Tarreau87b09662015-04-03 00:22:06 +02005157 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005158 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005159 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005160
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005161 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005162 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005163 return 0;
5164 }
5165
5166 /* More interesting part now : we know that we have a complete
5167 * response which at least looks like HTTP. We have an indicator
5168 * of each header's length, so we can parse them quickly.
5169 */
5170
5171 if (unlikely(msg->err_pos >= 0))
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005172 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005173
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005174 /*
5175 * 1: get the status code
5176 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005177 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005178 if (n < 1 || n > 5)
5179 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005180 /* when the client triggers a 4xx from the server, it's most often due
5181 * to a missing object or permission. These events should be tracked
5182 * because if they happen often, it may indicate a brute force or a
5183 * vulnerability scan.
5184 */
5185 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02005186 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02005187
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005188 if (objt_server(s->target))
5189 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005190
Willy Tarreau91852eb2015-05-01 13:26:00 +02005191 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
5192 * exactly one digit "." one digit. This check may be disabled using
5193 * option accept-invalid-http-response.
5194 */
5195 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
5196 if (msg->sl.st.v_l != 8) {
5197 msg->err_pos = 0;
5198 goto hdr_response_bad;
5199 }
5200
5201 if (rep->buf->p[4] != '/' ||
5202 !isdigit((unsigned char)rep->buf->p[5]) ||
5203 rep->buf->p[6] != '.' ||
5204 !isdigit((unsigned char)rep->buf->p[7])) {
5205 msg->err_pos = 4;
5206 goto hdr_response_bad;
5207 }
5208 }
5209
Willy Tarreau5b154472009-12-21 20:11:07 +01005210 /* check if the response is HTTP/1.1 or above */
5211 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005212 ((rep->buf->p[5] > '1') ||
5213 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005214 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005215
5216 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005217 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 +01005218
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005219 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005220 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005221
Willy Tarreau9b28e032012-10-12 23:49:43 +02005222 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005223
Willy Tarreau39650402010-03-15 19:44:39 +01005224 /* Adjust server's health based on status code. Note: status codes 501
5225 * and 505 are triggered on demand by client request, so we must not
5226 * count them as server failures.
5227 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005228 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005229 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005230 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005231 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005232 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005233 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005234
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005235 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02005236 * We may be facing a 100-continue response, or any other informational
5237 * 1xx response which is non-final, in which case this is not the right
5238 * response, and we're waiting for the next one. Let's allow this response
5239 * to go to the client and wait for the next one. There's an exception for
5240 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005241 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02005242 if (txn->status < 200 &&
5243 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02005244 hdr_idx_init(&txn->hdr_idx);
5245 msg->next -= channel_forward(rep, msg->next);
5246 msg->msg_state = HTTP_MSG_RPBEFORE;
5247 txn->status = 0;
5248 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01005249 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02005250 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02005251 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02005252
Willy Tarreaua14ad722017-07-07 11:36:32 +02005253 /*
5254 * 2: check for cacheability.
5255 */
5256
5257 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005258 case 200:
5259 case 203:
5260 case 206:
5261 case 300:
5262 case 301:
5263 case 410:
5264 /* RFC2616 @13.4:
5265 * "A response received with a status code of
5266 * 200, 203, 206, 300, 301 or 410 MAY be stored
5267 * by a cache (...) unless a cache-control
5268 * directive prohibits caching."
5269 *
5270 * RFC2616 @9.5: POST method :
5271 * "Responses to this method are not cacheable,
5272 * unless the response includes appropriate
5273 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005274 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005275 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005276 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005277 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5278 break;
5279 default:
5280 break;
5281 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005282
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005283 /*
5284 * 3: we may need to capture headers
5285 */
5286 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005287 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005288 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005289 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005290
Willy Tarreau557f1992015-05-01 10:05:17 +02005291 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
5292 * by RFC7230#3.3.3 :
5293 *
5294 * The length of a message body is determined by one of the following
5295 * (in order of precedence):
5296 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005297 * 1. Any 2xx (Successful) response to a CONNECT request implies that
5298 * the connection will become a tunnel immediately after the empty
5299 * line that concludes the header fields. A client MUST ignore
5300 * any Content-Length or Transfer-Encoding header fields received
5301 * in such a message. Any 101 response (Switching Protocols) is
5302 * managed in the same manner.
5303 *
5304 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02005305 * (Informational), 204 (No Content), or 304 (Not Modified) status
5306 * code is always terminated by the first empty line after the
5307 * header fields, regardless of the header fields present in the
5308 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005309 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005310 * 3. If a Transfer-Encoding header field is present and the chunked
5311 * transfer coding (Section 4.1) is the final encoding, the message
5312 * body length is determined by reading and decoding the chunked
5313 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005314 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005315 * If a Transfer-Encoding header field is present in a response and
5316 * the chunked transfer coding is not the final encoding, the
5317 * message body length is determined by reading the connection until
5318 * it is closed by the server. If a Transfer-Encoding header field
5319 * is present in a request and the chunked transfer coding is not
5320 * the final encoding, the message body length cannot be determined
5321 * reliably; the server MUST respond with the 400 (Bad Request)
5322 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005323 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005324 * If a message is received with both a Transfer-Encoding and a
5325 * Content-Length header field, the Transfer-Encoding overrides the
5326 * Content-Length. Such a message might indicate an attempt to
5327 * perform request smuggling (Section 9.5) or response splitting
5328 * (Section 9.4) and ought to be handled as an error. A sender MUST
5329 * remove the received Content-Length field prior to forwarding such
5330 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005331 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005332 * 4. If a message is received without Transfer-Encoding and with
5333 * either multiple Content-Length header fields having differing
5334 * field-values or a single Content-Length header field having an
5335 * invalid value, then the message framing is invalid and the
5336 * recipient MUST treat it as an unrecoverable error. If this is a
5337 * request message, the server MUST respond with a 400 (Bad Request)
5338 * status code and then close the connection. If this is a response
5339 * message received by a proxy, the proxy MUST close the connection
5340 * to the server, discard the received response, and send a 502 (Bad
5341 * Gateway) response to the client. If this is a response message
5342 * received by a user agent, the user agent MUST close the
5343 * connection to the server and discard the received response.
5344 *
5345 * 5. If a valid Content-Length header field is present without
5346 * Transfer-Encoding, its decimal value defines the expected message
5347 * body length in octets. If the sender closes the connection or
5348 * the recipient times out before the indicated number of octets are
5349 * received, the recipient MUST consider the message to be
5350 * incomplete and close the connection.
5351 *
5352 * 6. If this is a request message and none of the above are true, then
5353 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005354 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005355 * 7. Otherwise, this is a response message without a declared message
5356 * body length, so the message body length is determined by the
5357 * number of octets received prior to the server closing the
5358 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005359 */
5360
5361 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005362 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005363 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005364 * FIXME: should we parse anyway and return an error on chunked encoding ?
5365 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005366 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5367 txn->status == 101)) {
5368 /* Either we've established an explicit tunnel, or we're
5369 * switching the protocol. In both cases, we're very unlikely
5370 * to understand the next protocols. We have to switch to tunnel
5371 * mode, so that we transfer the request and responses then let
5372 * this protocol pass unmodified. When we later implement specific
5373 * parsers for such protocols, we'll want to check the Upgrade
5374 * header which contains information about that protocol for
5375 * responses with status 101 (eg: see RFC2817 about TLS).
5376 */
5377 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5378 msg->flags |= HTTP_MSGF_XFER_LEN;
5379 goto end;
5380 }
5381
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005382 if (txn->meth == HTTP_METH_HEAD ||
5383 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005384 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005385 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005386 goto skip_content_length;
5387 }
5388
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005389 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005390 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02005391 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005392 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005393 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5394 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005395 /* bad transfer-encoding (chunked followed by something else) */
5396 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005397 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005398 break;
5399 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005400 }
5401
Willy Tarreau1c913912015-04-30 10:57:51 +02005402 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005403 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005404 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02005405 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
5406 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5407 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005408 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005409 signed long long cl;
5410
Willy Tarreauad14f752011-09-02 20:33:27 +02005411 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005412 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005413 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005414 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005415
Willy Tarreauad14f752011-09-02 20:33:27 +02005416 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005417 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005418 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005419 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005420
Willy Tarreauad14f752011-09-02 20:33:27 +02005421 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005422 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005423 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005424 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005425
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005426 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005427 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005428 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005429 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005430
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005431 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005432 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005433 }
5434
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005435 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01005436 /* Now we have to check if we need to modify the Connection header.
5437 * This is more difficult on the response than it is on the request,
5438 * because we can have two different HTTP versions and we don't know
5439 * how the client will interprete a response. For instance, let's say
5440 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5441 * HTTP/1.1 response without any header. Maybe it will bound itself to
5442 * HTTP/1.0 because it only knows about it, and will consider the lack
5443 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5444 * the lack of header as a keep-alive. Thus we will use two flags
5445 * indicating how a request MAY be understood by the client. In case
5446 * of multiple possibilities, we'll fix the header to be explicit. If
5447 * ambiguous cases such as both close and keepalive are seen, then we
5448 * will fall back to explicit close. Note that we won't take risks with
5449 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005450 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005451 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005452 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5453 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5454 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5455 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005456 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005457
Willy Tarreau70dffda2014-01-30 03:07:23 +01005458 /* this situation happens when combining pretend-keepalive with httpclose. */
5459 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005460 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005461 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01005462 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
5463
Willy Tarreau60466522010-01-18 19:08:45 +01005464 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005465 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005466 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5467 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005468
Willy Tarreau60466522010-01-18 19:08:45 +01005469 /* now adjust header transformations depending on current state */
5470 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5471 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5472 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005473 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005474 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005475 }
Willy Tarreau60466522010-01-18 19:08:45 +01005476 else { /* SCL / KAL */
5477 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005478 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005479 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005480 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005481
Willy Tarreau60466522010-01-18 19:08:45 +01005482 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005483 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005484
Willy Tarreau60466522010-01-18 19:08:45 +01005485 /* Some keep-alive responses are converted to Server-close if
5486 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005487 */
Willy Tarreau60466522010-01-18 19:08:45 +01005488 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5489 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005490 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005491 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005492 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005493 }
5494
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005495 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02005496 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005497 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005498
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005499 /* end of job, return OK */
5500 rep->analysers &= ~an_bit;
5501 rep->analyse_exp = TICK_ETERNITY;
5502 channel_auto_close(rep);
5503 return 1;
5504
5505 abort_keep_alive:
5506 /* A keep-alive request to the server failed on a network error.
5507 * The client is required to retry. We need to close without returning
5508 * any other information so that the client retries.
5509 */
5510 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005511 rep->analysers &= AN_RES_FLT_END;
5512 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005513 channel_auto_close(rep);
5514 s->logs.logwait = 0;
5515 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005516 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01005517 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005518 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005519 return 0;
5520}
5521
5522/* This function performs all the processing enabled for the current response.
5523 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005524 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005525 * other functions. It works like process_request (see indications above).
5526 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005527int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005528{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005529 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005530 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005531 struct http_msg *msg = &txn->rsp;
5532 struct proxy *cur_proxy;
5533 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01005534 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005535
Willy Tarreau87b09662015-04-03 00:22:06 +02005536 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 +02005537 now_ms, __FUNCTION__,
5538 s,
5539 rep,
5540 rep->rex, rep->wex,
5541 rep->flags,
5542 rep->buf->i,
5543 rep->analysers);
5544
5545 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
5546 return 0;
5547
Willy Tarreau70730dd2014-04-24 18:06:27 +02005548 /* The stats applet needs to adjust the Connection header but we don't
5549 * apply any filter there.
5550 */
Willy Tarreau612adb82015-03-10 15:25:54 +01005551 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
5552 rep->analysers &= ~an_bit;
5553 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02005554 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01005555 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02005556
Willy Tarreau58975672014-04-24 21:13:57 +02005557 /*
5558 * We will have to evaluate the filters.
5559 * As opposed to version 1.2, now they will be evaluated in the
5560 * filters order and not in the header order. This means that
5561 * each filter has to be validated among all headers.
5562 *
5563 * Filters are tried with ->be first, then with ->fe if it is
5564 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005565 *
5566 * Maybe we are in resume condiion. In this case I choose the
5567 * "struct proxy" which contains the rule list matching the resume
5568 * pointer. If none of theses "struct proxy" match, I initialise
5569 * the process with the first one.
5570 *
5571 * In fact, I check only correspondance betwwen the current list
5572 * pointer and the ->fe rule list. If it doesn't match, I initialize
5573 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02005574 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005575 if (s->current_rule_list == &sess->fe->http_res_rules)
5576 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005577 else
5578 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02005579 while (1) {
5580 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005581
Willy Tarreau58975672014-04-24 21:13:57 +02005582 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02005583 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005584 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005585
Willy Tarreau51d861a2015-05-22 17:30:48 +02005586 if (ret == HTTP_RULE_RES_BADREQ)
5587 goto return_srv_prx_502;
5588
5589 if (ret == HTTP_RULE_RES_DONE) {
5590 rep->analysers &= ~an_bit;
5591 rep->analyse_exp = TICK_ETERNITY;
5592 return 1;
5593 }
5594 }
5595
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005596 /* we need to be called again. */
5597 if (ret == HTTP_RULE_RES_YIELD) {
5598 channel_dont_close(rep);
5599 return 0;
5600 }
5601
Willy Tarreau58975672014-04-24 21:13:57 +02005602 /* try headers filters */
5603 if (rule_set->rsp_exp != NULL) {
5604 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5605 return_bad_resp:
5606 if (objt_server(s->target)) {
5607 objt_server(s->target)->counters.failed_resp++;
5608 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005609 }
Willy Tarreau58975672014-04-24 21:13:57 +02005610 s->be->be_counters.failed_resp++;
5611 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005612 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02005613 txn->status = 502;
5614 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01005615 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005616 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005617 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02005618 if (!(s->flags & SF_ERR_MASK))
5619 s->flags |= SF_ERR_PRXCOND;
5620 if (!(s->flags & SF_FINST_MASK))
5621 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02005622 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005623 }
Willy Tarreau58975672014-04-24 21:13:57 +02005624 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005625
Willy Tarreau58975672014-04-24 21:13:57 +02005626 /* has the response been denied ? */
5627 if (txn->flags & TX_SVDENY) {
5628 if (objt_server(s->target))
5629 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005630
Willy Tarreau58975672014-04-24 21:13:57 +02005631 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005632 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005633 if (sess->listener->counters)
5634 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005635
Willy Tarreau58975672014-04-24 21:13:57 +02005636 goto return_srv_prx_502;
5637 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005638
Willy Tarreau58975672014-04-24 21:13:57 +02005639 /* add response headers from the rule sets in the same order */
5640 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02005641 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005642 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005643 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005644 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02005645 ret = acl_pass(ret);
5646 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5647 ret = !ret;
5648 if (!ret)
5649 continue;
5650 }
5651 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
5652 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005653 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005654
Willy Tarreau58975672014-04-24 21:13:57 +02005655 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005656 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02005657 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005658 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02005659 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005660
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005661 /* After this point, this anayzer can't return yield, so we can
5662 * remove the bit corresponding to this analyzer from the list.
5663 *
5664 * Note that the intermediate returns and goto found previously
5665 * reset the analyzers.
5666 */
5667 rep->analysers &= ~an_bit;
5668 rep->analyse_exp = TICK_ETERNITY;
5669
Willy Tarreau58975672014-04-24 21:13:57 +02005670 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005671 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005672 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005673
Willy Tarreau58975672014-04-24 21:13:57 +02005674 /*
5675 * Now check for a server cookie.
5676 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005677 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005678 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005679
Willy Tarreau58975672014-04-24 21:13:57 +02005680 /*
5681 * Check for cache-control or pragma headers if required.
5682 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005683 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 +02005684 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005685
Willy Tarreau58975672014-04-24 21:13:57 +02005686 /*
5687 * Add server cookie in the response if needed
5688 */
5689 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5690 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005691 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005692 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5693 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5694 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5695 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5696 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005697 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005698 /* the server is known, it's not the one the client requested, or the
5699 * cookie's last seen date needs to be refreshed. We have to
5700 * insert a set-cookie here, except if we want to insert only on POST
5701 * requests and this one isn't. Note that servers which don't have cookies
5702 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005703 */
Willy Tarreau58975672014-04-24 21:13:57 +02005704 if (!objt_server(s->target)->cookie) {
5705 chunk_printf(&trash,
5706 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5707 s->be->cookie_name);
5708 }
5709 else {
5710 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005711
Willy Tarreau58975672014-04-24 21:13:57 +02005712 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5713 /* emit last_date, which is mandatory */
5714 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5715 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5716 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005717
Willy Tarreau58975672014-04-24 21:13:57 +02005718 if (s->be->cookie_maxlife) {
5719 /* emit first_date, which is either the original one or
5720 * the current date.
5721 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005722 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005723 s30tob64(txn->cookie_first_date ?
5724 txn->cookie_first_date >> 2 :
5725 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005726 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005727 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005728 }
Willy Tarreau58975672014-04-24 21:13:57 +02005729 chunk_appendf(&trash, "; path=/");
5730 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005731
Willy Tarreau58975672014-04-24 21:13:57 +02005732 if (s->be->cookie_domain)
5733 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005734
Willy Tarreau58975672014-04-24 21:13:57 +02005735 if (s->be->ck_opts & PR_CK_HTTPONLY)
5736 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005737
Willy Tarreau58975672014-04-24 21:13:57 +02005738 if (s->be->ck_opts & PR_CK_SECURE)
5739 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005740
Willy Tarreau58975672014-04-24 21:13:57 +02005741 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
5742 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005743
Willy Tarreau58975672014-04-24 21:13:57 +02005744 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005745 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005746 /* the server did not change, only the date was updated */
5747 txn->flags |= TX_SCK_UPDATED;
5748 else
5749 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005750
Willy Tarreau58975672014-04-24 21:13:57 +02005751 /* Here, we will tell an eventual cache on the client side that we don't
5752 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5753 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5754 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005755 */
Willy Tarreau58975672014-04-24 21:13:57 +02005756 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005757
Willy Tarreau58975672014-04-24 21:13:57 +02005758 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005759
Willy Tarreau58975672014-04-24 21:13:57 +02005760 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5761 "Cache-control: private", 22) < 0))
5762 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005763 }
Willy Tarreau58975672014-04-24 21:13:57 +02005764 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005765
Willy Tarreau58975672014-04-24 21:13:57 +02005766 /*
5767 * Check if result will be cacheable with a cookie.
5768 * We'll block the response if security checks have caught
5769 * nasty things such as a cacheable cookie.
5770 */
5771 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5772 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5773 (s->be->options & PR_O_CHK_CACHE)) {
5774 /* we're in presence of a cacheable response containing
5775 * a set-cookie header. We'll block it as requested by
5776 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005777 */
Willy Tarreau58975672014-04-24 21:13:57 +02005778 if (objt_server(s->target))
5779 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01005780
Willy Tarreau58975672014-04-24 21:13:57 +02005781 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005782 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005783 if (sess->listener->counters)
5784 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005785
Willy Tarreau58975672014-04-24 21:13:57 +02005786 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5787 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5788 send_log(s->be, LOG_ALERT,
5789 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5790 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5791 goto return_srv_prx_502;
5792 }
Willy Tarreau03945942009-12-22 16:50:27 +01005793
Willy Tarreau70730dd2014-04-24 18:06:27 +02005794 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005795 /*
5796 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5797 * If an "Upgrade" token is found, the header is left untouched in order
5798 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005799 * if anything but "Upgrade" is present in the Connection header. We don't
5800 * want to touch any 101 response either since it's switching to another
5801 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005802 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005803 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02005804 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005805 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02005806 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
5807 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005808
Willy Tarreau58975672014-04-24 21:13:57 +02005809 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5810 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5811 /* we want a keep-alive response here. Keep-alive header
5812 * required if either side is not 1.1.
5813 */
5814 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5815 want_flags |= TX_CON_KAL_SET;
5816 }
5817 else {
5818 /* we want a close response here. Close header required if
5819 * the server is 1.1, regardless of the client.
5820 */
5821 if (msg->flags & HTTP_MSGF_VER_11)
5822 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005823 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005824
Willy Tarreau58975672014-04-24 21:13:57 +02005825 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5826 http_change_connection_header(txn, msg, want_flags);
5827 }
5828
5829 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02005830 /* Always enter in the body analyzer */
5831 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
5832 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005833
Willy Tarreau58975672014-04-24 21:13:57 +02005834 /* if the user wants to log as soon as possible, without counting
5835 * bytes from the server, then this is the right moment. We have
5836 * to temporarily assign bytes_out to log what we currently have.
5837 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005838 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005839 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
5840 s->logs.bytes_out = txn->rsp.eoh;
5841 s->do_log(s);
5842 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005843 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005844 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005845}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005846
Willy Tarreaud98cf932009-12-27 22:54:55 +01005847/* This function is an analyser which forwards response body (including chunk
5848 * sizes if any). It is called as soon as we must forward, even if we forward
5849 * zero byte. The only situation where it must not be called is when we're in
5850 * tunnel mode and we want to forward till the close. It's used both to forward
5851 * remaining data and to resync after end of body. It expects the msg_state to
5852 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005853 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02005854 *
5855 * It is capable of compressing response data both in content-length mode and
5856 * in chunked mode. The state machines follows different flows depending on
5857 * whether content-length and chunked modes are used, since there are no
5858 * trailers in content-length :
5859 *
5860 * chk-mode cl-mode
5861 * ,----- BODY -----.
5862 * / \
5863 * V size > 0 V chk-mode
5864 * .--> SIZE -------------> DATA -------------> CRLF
5865 * | | size == 0 | last byte |
5866 * | v final crlf v inspected |
5867 * | TRAILERS -----------> DONE |
5868 * | |
5869 * `----------------------------------------------'
5870 *
5871 * Compression only happens in the DATA state, and must be flushed in final
5872 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
5873 * is performed at once on final states for all bytes parsed, or when leaving
5874 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005875 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005876int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005877{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005878 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005879 struct http_txn *txn = s->txn;
5880 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01005881 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005882
Christopher Faulet814d2702017-03-30 11:33:44 +02005883 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
5884 now_ms, __FUNCTION__,
5885 s,
5886 res,
5887 res->rex, res->wex,
5888 res->flags,
5889 res->buf->i,
5890 res->analysers);
5891
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005892 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5893 return 0;
5894
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005895 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005896 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02005897 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005898 /* Output closed while we were sending data. We must abort and
5899 * wake the other side up.
5900 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005901 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005902 msg->msg_state = HTTP_MSG_ERROR;
5903 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005904 return 1;
5905 }
5906
Willy Tarreau4fe41902010-06-07 22:27:41 +02005907 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005908 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005909
Christopher Fauletd7c91962015-04-30 11:48:27 +02005910 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005911 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5912 ? HTTP_MSG_CHUNK_SIZE
5913 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005914 }
5915
Willy Tarreauefdf0942014-04-24 20:08:57 +02005916 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005917 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02005918 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005919 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005920 }
5921
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005922 if (msg->msg_state < HTTP_MSG_DONE) {
5923 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5924 ? http_msg_forward_chunked_body(s, msg)
5925 : http_msg_forward_body(s, msg));
5926 if (!ret)
5927 goto missing_data_or_waiting;
5928 if (ret < 0)
5929 goto return_bad_res;
5930 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005931
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005932 /* other states, DONE...TUNNEL */
5933 /* for keep-alive we don't want to forward closes on DONE */
5934 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5935 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5936 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005937
Christopher Faulet894da4c2017-07-18 11:29:07 +02005938 http_resync_states(s);
5939 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005940 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5941 if (res->flags & CF_SHUTW) {
5942 /* response errors are most likely due to the
5943 * client aborting the transfer. */
5944 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01005945 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005946 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005947 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005948 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005949 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005950 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005951 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02005952 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005953
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005954 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01005955 if (res->flags & CF_SHUTW)
5956 goto aborted_xfer;
5957
5958 /* stop waiting for data if the input is closed before the end. If the
5959 * client side was already closed, it means that the client has aborted,
5960 * so we don't want to count this as a server abort. Otherwise it's a
5961 * server abort.
5962 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02005963 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005964 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01005965 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005966 /* If we have some pending data, we continue the processing */
5967 if (!buffer_pending(res->buf)) {
5968 if (!(s->flags & SF_ERR_MASK))
5969 s->flags |= SF_ERR_SRVCL;
5970 s->be->be_counters.srv_aborts++;
5971 if (objt_server(s->target))
5972 objt_server(s->target)->counters.srv_aborts++;
5973 goto return_bad_res_stats_ok;
5974 }
Willy Tarreau40dba092010-03-04 18:14:51 +01005975 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005976
Willy Tarreau40dba092010-03-04 18:14:51 +01005977 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005978 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005979 goto return_bad_res;
5980
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005981 /* When TE: chunked is used, we need to get there again to parse
5982 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02005983 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
5984 * or if there are filters registered on the stream, we don't want to
5985 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005986 */
Christopher Faulet69744d92017-03-30 10:54:35 +02005987 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005988 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005989 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5990 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005991 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005992
Willy Tarreau5c620922011-05-11 19:56:11 +02005993 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005994 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005995 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005996 * modes are already handled by the stream sock layer. We must not do
5997 * this in content-length mode because it could present the MSG_MORE
5998 * flag with the last block of forwarded data, which would cause an
5999 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006000 */
Christopher Faulet92d36382015-11-05 13:35:03 +01006001 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006002 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006003
Willy Tarreau87b09662015-04-03 00:22:06 +02006004 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006005 return 0;
6006
Willy Tarreau40dba092010-03-04 18:14:51 +01006007 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006008 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006009 if (objt_server(s->target))
6010 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006011
6012 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006013 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006014 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006015 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006016 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006017 res->analysers &= AN_RES_FLT_END;
6018 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 +01006019 if (objt_server(s->target))
6020 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006021
Willy Tarreaue7dff022015-04-03 01:14:29 +02006022 if (!(s->flags & SF_ERR_MASK))
6023 s->flags |= SF_ERR_PRXCOND;
6024 if (!(s->flags & SF_FINST_MASK))
6025 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006026 return 0;
6027
6028 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006029 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006030 txn->rsp.msg_state = HTTP_MSG_ERROR;
6031 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006032 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006033 res->analysers &= AN_RES_FLT_END;
6034 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 +01006035
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006036 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006037 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006038 if (objt_server(s->target))
6039 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006040
Willy Tarreaue7dff022015-04-03 01:14:29 +02006041 if (!(s->flags & SF_ERR_MASK))
6042 s->flags |= SF_ERR_CLICL;
6043 if (!(s->flags & SF_FINST_MASK))
6044 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006045 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006046}
6047
6048
6049static inline int
6050http_msg_forward_body(struct stream *s, struct http_msg *msg)
6051{
6052 struct channel *chn = msg->chn;
6053 int ret;
6054
6055 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
6056
6057 if (msg->msg_state == HTTP_MSG_ENDING)
6058 goto ending;
6059
6060 /* Neither content-length, nor transfer-encoding was found, so we must
6061 * read the body until the server connection is closed. In that case, we
6062 * eat data as they come. Of course, this happens for response only. */
6063 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6064 unsigned long long len = (chn->buf->i - msg->next);
6065 msg->chunk_len += len;
6066 msg->body_len += len;
6067 }
Christopher Fauletda02e172015-12-04 09:25:05 +01006068 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
6069 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6070 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006071 msg->next += ret;
6072 msg->chunk_len -= ret;
6073 if (msg->chunk_len) {
6074 /* input empty or output full */
6075 if (chn->buf->i > msg->next)
6076 chn->flags |= CF_WAKE_WRITE;
6077 goto missing_data_or_waiting;
6078 }
6079
Christopher Faulet1486b0a2017-07-18 11:42:08 +02006080 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
6081 * always set for a request. */
6082 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6083 /* The server still sending data that should be filtered */
6084 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
6085 goto missing_data_or_waiting;
6086 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006087
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006088 msg->msg_state = HTTP_MSG_ENDING;
6089
6090 ending:
6091 /* we may have some pending data starting at res->buf->p such as a last
6092 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006093 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6094 /* default_ret */ msg->next,
6095 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006096 b_adv(chn->buf, ret);
6097 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006098 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6099 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006100 if (msg->next)
6101 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006102
Christopher Fauletda02e172015-12-04 09:25:05 +01006103 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
6104 /* default_ret */ 1,
6105 /* on_error */ goto error,
6106 /* on_wait */ goto waiting);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006107 msg->msg_state = HTTP_MSG_DONE;
6108 return 1;
6109
6110 missing_data_or_waiting:
6111 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006112 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6113 /* default_ret */ msg->next,
6114 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006115 b_adv(chn->buf, ret);
6116 msg->next -= ret;
6117 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6118 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006119 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006120 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006121 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006122 return 0;
6123 error:
6124 return -1;
6125}
6126
6127static inline int
6128http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
6129{
6130 struct channel *chn = msg->chn;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006131 unsigned int chunk;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006132 int ret;
6133
6134 /* Here we have the guarantee to be in one of the following state:
6135 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
6136 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
6137
6138 switch_states:
6139 switch (msg->msg_state) {
6140 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01006141 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
6142 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6143 /* on_error */ goto error);
6144 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006145 msg->chunk_len -= ret;
6146 if (msg->chunk_len) {
6147 /* input empty or output full */
6148 if (chn->buf->i > msg->next)
6149 chn->flags |= CF_WAKE_WRITE;
6150 goto missing_data_or_waiting;
6151 }
6152
6153 /* nothing left to forward for this chunk*/
6154 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
6155 /* fall through for HTTP_MSG_CHUNK_CRLF */
6156
6157 case HTTP_MSG_CHUNK_CRLF:
6158 /* we want the CRLF after the data */
Willy Tarreaub2892562017-09-21 11:33:54 +02006159 ret = h1_skip_chunk_crlf(chn->buf, msg->next, chn->buf->i);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006160 if (ret == 0)
6161 goto missing_data_or_waiting;
Willy Tarreaub2892562017-09-21 11:33:54 +02006162 if (ret < 0) {
6163 msg->err_pos = chn->buf->i + ret;
6164 if (msg->err_pos < 0)
6165 msg->err_pos += chn->buf->size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006166 goto chunk_parsing_error;
Willy Tarreaub2892562017-09-21 11:33:54 +02006167 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01006168 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006169 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6170 /* fall through for HTTP_MSG_CHUNK_SIZE */
6171
6172 case HTTP_MSG_CHUNK_SIZE:
6173 /* read the chunk size and assign it to ->chunk_len,
6174 * then set ->next to point to the body and switch to
6175 * DATA or TRAILERS state.
6176 */
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006177 ret = h1_parse_chunk_size(chn->buf, msg->next, chn->buf->i, &chunk);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006178 if (ret == 0)
6179 goto missing_data_or_waiting;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006180 if (ret < 0) {
6181 msg->err_pos = chn->buf->i + ret;
6182 if (msg->err_pos < 0)
6183 msg->err_pos += chn->buf->size;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006184 goto chunk_parsing_error;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006185 }
6186
6187 msg->sol = ret;
Christopher Faulet113f7de2015-12-14 14:52:13 +01006188 msg->next += ret;
Willy Tarreaue56cdd32017-09-21 08:36:33 +02006189 msg->chunk_len = chunk;
6190 msg->body_len += chunk;
6191
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006192 if (msg->chunk_len) {
6193 msg->msg_state = HTTP_MSG_DATA;
6194 goto switch_states;
6195 }
6196 msg->msg_state = HTTP_MSG_TRAILERS;
6197 /* fall through for HTTP_MSG_TRAILERS */
6198
6199 case HTTP_MSG_TRAILERS:
6200 ret = http_forward_trailers(msg);
6201 if (ret < 0)
6202 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01006203 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
6204 /* default_ret */ 1,
6205 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006206 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006207 if (!ret)
6208 goto missing_data_or_waiting;
6209 break;
6210
6211 case HTTP_MSG_ENDING:
6212 goto ending;
6213
6214 default:
6215 /* This should no happen in this function */
6216 goto error;
6217 }
6218
6219 msg->msg_state = HTTP_MSG_ENDING;
6220 ending:
6221 /* we may have some pending data starting at res->buf->p such as a last
6222 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006223 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006224 /* default_ret */ msg->next,
6225 /* on_error */ goto error);
6226 b_adv(chn->buf, ret);
6227 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006228 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6229 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006230 if (msg->next)
6231 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006232
Christopher Fauletda02e172015-12-04 09:25:05 +01006233 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006234 /* default_ret */ 1,
6235 /* on_error */ goto error,
6236 /* on_wait */ goto waiting);
6237 msg->msg_state = HTTP_MSG_DONE;
6238 return 1;
6239
6240 missing_data_or_waiting:
6241 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006242 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006243 /* default_ret */ msg->next,
6244 /* on_error */ goto error);
6245 b_adv(chn->buf, ret);
6246 msg->next -= ret;
6247 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6248 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006249 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006250 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006251 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006252 return 0;
6253
6254 chunk_parsing_error:
6255 if (msg->err_pos >= 0) {
6256 if (chn->flags & CF_ISRESP)
6257 http_capture_bad_message(&s->be->invalid_rep, s, msg,
6258 msg->msg_state, strm_fe(s));
6259 else
6260 http_capture_bad_message(&strm_fe(s)->invalid_req, s,
6261 msg, msg->msg_state, s->be);
6262 }
6263 error:
6264 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006265}
6266
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006267
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006268/* Iterate the same filter through all request headers.
6269 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006270 * Since it can manage the switch to another backend, it updates the per-proxy
6271 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006272 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006273int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006274{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006275 char *cur_ptr, *cur_end, *cur_next;
6276 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006277 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006278 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006279 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006280
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006281 last_hdr = 0;
6282
Willy Tarreau9b28e032012-10-12 23:49:43 +02006283 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006284 old_idx = 0;
6285
6286 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006287 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006288 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006289 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006290 (exp->action == ACT_ALLOW ||
6291 exp->action == ACT_DENY ||
6292 exp->action == ACT_TARPIT))
6293 return 0;
6294
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006295 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006296 if (!cur_idx)
6297 break;
6298
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006299 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006300 cur_ptr = cur_next;
6301 cur_end = cur_ptr + cur_hdr->len;
6302 cur_next = cur_end + cur_hdr->cr + 1;
6303
6304 /* Now we have one header between cur_ptr and cur_end,
6305 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006306 */
6307
Willy Tarreau15a53a42015-01-21 13:39:42 +01006308 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006309 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006310 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006311 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006312 last_hdr = 1;
6313 break;
6314
6315 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006316 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006317 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006318 break;
6319
6320 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006321 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006322 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006323 break;
6324
6325 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006326 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6327 if (trash.len < 0)
6328 return -1;
6329
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006330 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006331 /* FIXME: if the user adds a newline in the replacement, the
6332 * index will not be recalculated for now, and the new line
6333 * will not be counted as a new header.
6334 */
6335
6336 cur_end += delta;
6337 cur_next += delta;
6338 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006339 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006340 break;
6341
6342 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006343 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006344 cur_next += delta;
6345
Willy Tarreaufa355d42009-11-29 18:12:29 +01006346 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006347 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6348 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006349 cur_hdr->len = 0;
6350 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006351 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006352 break;
6353
6354 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006355 }
6356
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006357 /* keep the link from this header to next one in case of later
6358 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006359 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006360 old_idx = cur_idx;
6361 }
6362 return 0;
6363}
6364
6365
6366/* Apply the filter to the request line.
6367 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6368 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006369 * Since it can manage the switch to another backend, it updates the per-proxy
6370 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006371 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006372int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006373{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006374 char *cur_ptr, *cur_end;
6375 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006376 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006377 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006378
Willy Tarreau3d300592007-03-18 18:34:41 +01006379 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006380 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006381 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006382 (exp->action == ACT_ALLOW ||
6383 exp->action == ACT_DENY ||
6384 exp->action == ACT_TARPIT))
6385 return 0;
6386 else if (exp->action == ACT_REMOVE)
6387 return 0;
6388
6389 done = 0;
6390
Willy Tarreau9b28e032012-10-12 23:49:43 +02006391 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006392 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006393
6394 /* Now we have the request line between cur_ptr and cur_end */
6395
Willy Tarreau15a53a42015-01-21 13:39:42 +01006396 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006397 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006398 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006399 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006400 done = 1;
6401 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006402
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006403 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006404 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006405 done = 1;
6406 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006407
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006408 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006409 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006410 done = 1;
6411 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006412
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006413 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006414 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6415 if (trash.len < 0)
6416 return -1;
6417
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006418 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006419 /* FIXME: if the user adds a newline in the replacement, the
6420 * index will not be recalculated for now, and the new line
6421 * will not be counted as a new header.
6422 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006423
Willy Tarreaufa355d42009-11-29 18:12:29 +01006424 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006425 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006426 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006427 HTTP_MSG_RQMETH,
6428 cur_ptr, cur_end + 1,
6429 NULL, NULL);
6430 if (unlikely(!cur_end))
6431 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006432
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006433 /* we have a full request and we know that we have either a CR
6434 * or an LF at <ptr>.
6435 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006436 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6437 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006438 /* there is no point trying this regex on headers */
6439 return 1;
6440 }
6441 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006442 return done;
6443}
Willy Tarreau97de6242006-12-27 17:18:38 +01006444
Willy Tarreau58f10d72006-12-04 02:26:12 +01006445
Willy Tarreau58f10d72006-12-04 02:26:12 +01006446
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006447/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006448 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006449 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006450 * unparsable request. Since it can manage the switch to another backend, it
6451 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006452 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006453int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006454{
Willy Tarreau192252e2015-04-04 01:47:55 +02006455 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006456 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006457 struct hdr_exp *exp;
6458
6459 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006460 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006461
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006462 /*
6463 * The interleaving of transformations and verdicts
6464 * makes it difficult to decide to continue or stop
6465 * the evaluation.
6466 */
6467
Willy Tarreau6c123b12010-01-28 20:22:06 +01006468 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6469 break;
6470
Willy Tarreau3d300592007-03-18 18:34:41 +01006471 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006472 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006473 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006474 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006475
6476 /* if this filter had a condition, evaluate it now and skip to
6477 * next filter if the condition does not match.
6478 */
6479 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006480 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006481 ret = acl_pass(ret);
6482 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6483 ret = !ret;
6484
6485 if (!ret)
6486 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006487 }
6488
6489 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006490 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006491 if (unlikely(ret < 0))
6492 return -1;
6493
6494 if (likely(ret == 0)) {
6495 /* The filter did not match the request, it can be
6496 * iterated through all headers.
6497 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01006498 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
6499 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006500 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006501 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006502 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006503}
6504
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006505
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006506/* Find the end of a cookie value contained between <s> and <e>. It works the
6507 * same way as with headers above except that the semi-colon also ends a token.
6508 * See RFC2965 for more information. Note that it requires a valid header to
6509 * return a valid result.
6510 */
6511char *find_cookie_value_end(char *s, const char *e)
6512{
6513 int quoted, qdpair;
6514
6515 quoted = qdpair = 0;
6516 for (; s < e; s++) {
6517 if (qdpair) qdpair = 0;
6518 else if (quoted) {
6519 if (*s == '\\') qdpair = 1;
6520 else if (*s == '"') quoted = 0;
6521 }
6522 else if (*s == '"') quoted = 1;
6523 else if (*s == ',' || *s == ';') return s;
6524 }
6525 return s;
6526}
6527
6528/* Delete a value in a header between delimiters <from> and <next> in buffer
6529 * <buf>. The number of characters displaced is returned, and the pointer to
6530 * the first delimiter is updated if required. The function tries as much as
6531 * possible to respect the following principles :
6532 * - replace <from> delimiter by the <next> one unless <from> points to a
6533 * colon, in which case <next> is simply removed
6534 * - set exactly one space character after the new first delimiter, unless
6535 * there are not enough characters in the block being moved to do so.
6536 * - remove unneeded spaces before the previous delimiter and after the new
6537 * one.
6538 *
6539 * It is the caller's responsibility to ensure that :
6540 * - <from> points to a valid delimiter or the colon ;
6541 * - <next> points to a valid delimiter or the final CR/LF ;
6542 * - there are non-space chars before <from> ;
6543 * - there is a CR/LF at or after <next>.
6544 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006545int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006546{
6547 char *prev = *from;
6548
6549 if (*prev == ':') {
6550 /* We're removing the first value, preserve the colon and add a
6551 * space if possible.
6552 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006553 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006554 next++;
6555 prev++;
6556 if (prev < next)
6557 *prev++ = ' ';
6558
Willy Tarreau2235b262016-11-05 15:50:20 +01006559 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006560 next++;
6561 } else {
6562 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01006563 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006564 prev--;
6565 *from = prev;
6566
6567 /* copy the delimiter and if possible a space if we're
6568 * not at the end of the line.
6569 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006570 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006571 *prev++ = *next++;
6572 if (prev + 1 < next)
6573 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01006574 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006575 next++;
6576 }
6577 }
6578 return buffer_replace2(buf, prev, next, NULL, 0);
6579}
6580
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006581/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006582 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006583 * desirable to call it only when needed. This code is quite complex because
6584 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6585 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006586 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006587void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006588{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006589 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006590 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006591 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006592 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006593 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6594 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006595
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006596 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006597 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006598 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006599
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006600 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006601 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006602 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006603
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006604 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006605 hdr_beg = hdr_next;
6606 hdr_end = hdr_beg + cur_hdr->len;
6607 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006608
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006609 /* We have one full header between hdr_beg and hdr_end, and the
6610 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006611 * "Cookie:" headers.
6612 */
6613
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006614 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006615 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006616 old_idx = cur_idx;
6617 continue;
6618 }
6619
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006620 del_from = NULL; /* nothing to be deleted */
6621 preserve_hdr = 0; /* assume we may kill the whole header */
6622
Willy Tarreau58f10d72006-12-04 02:26:12 +01006623 /* Now look for cookies. Conforming to RFC2109, we have to support
6624 * attributes whose name begin with a '$', and associate them with
6625 * the right cookie, if we want to delete this cookie.
6626 * So there are 3 cases for each cookie read :
6627 * 1) it's a special attribute, beginning with a '$' : ignore it.
6628 * 2) it's a server id cookie that we *MAY* want to delete : save
6629 * some pointers on it (last semi-colon, beginning of cookie...)
6630 * 3) it's an application cookie : we *MAY* have to delete a previous
6631 * "special" cookie.
6632 * At the end of loop, if a "special" cookie remains, we may have to
6633 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006634 * *MUST* delete it.
6635 *
6636 * Note: RFC2965 is unclear about the processing of spaces around
6637 * the equal sign in the ATTR=VALUE form. A careful inspection of
6638 * the RFC explicitly allows spaces before it, and not within the
6639 * tokens (attrs or values). An inspection of RFC2109 allows that
6640 * too but section 10.1.3 lets one think that spaces may be allowed
6641 * after the equal sign too, resulting in some (rare) buggy
6642 * implementations trying to do that. So let's do what servers do.
6643 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6644 * allowed quoted strings in values, with any possible character
6645 * after a backslash, including control chars and delimitors, which
6646 * causes parsing to become ambiguous. Browsers also allow spaces
6647 * within values even without quotes.
6648 *
6649 * We have to keep multiple pointers in order to support cookie
6650 * removal at the beginning, middle or end of header without
6651 * corrupting the header. All of these headers are valid :
6652 *
6653 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6654 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6655 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6656 * | | | | | | | | |
6657 * | | | | | | | | hdr_end <--+
6658 * | | | | | | | +--> next
6659 * | | | | | | +----> val_end
6660 * | | | | | +-----------> val_beg
6661 * | | | | +--------------> equal
6662 * | | | +----------------> att_end
6663 * | | +---------------------> att_beg
6664 * | +--------------------------> prev
6665 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006666 */
6667
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006668 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6669 /* Iterate through all cookies on this line */
6670
6671 /* find att_beg */
6672 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006673 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006674 att_beg++;
6675
6676 /* find att_end : this is the first character after the last non
6677 * space before the equal. It may be equal to hdr_end.
6678 */
6679 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006680
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006681 while (equal < hdr_end) {
6682 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006683 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006684 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006685 continue;
6686 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006687 }
6688
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006689 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6690 * is between <att_beg> and <equal>, both may be identical.
6691 */
6692
6693 /* look for end of cookie if there is an equal sign */
6694 if (equal < hdr_end && *equal == '=') {
6695 /* look for the beginning of the value */
6696 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006697 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006698 val_beg++;
6699
6700 /* find the end of the value, respecting quotes */
6701 next = find_cookie_value_end(val_beg, hdr_end);
6702
6703 /* make val_end point to the first white space or delimitor after the value */
6704 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006705 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006706 val_end--;
6707 } else {
6708 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006709 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006710
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006711 /* We have nothing to do with attributes beginning with '$'. However,
6712 * they will automatically be removed if a header before them is removed,
6713 * since they're supposed to be linked together.
6714 */
6715 if (*att_beg == '$')
6716 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006717
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006718 /* Ignore cookies with no equal sign */
6719 if (equal == next) {
6720 /* This is not our cookie, so we must preserve it. But if we already
6721 * scheduled another cookie for removal, we cannot remove the
6722 * complete header, but we can remove the previous block itself.
6723 */
6724 preserve_hdr = 1;
6725 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006726 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006727 val_end += delta;
6728 next += delta;
6729 hdr_end += delta;
6730 hdr_next += delta;
6731 cur_hdr->len += delta;
6732 http_msg_move_end(&txn->req, delta);
6733 prev = del_from;
6734 del_from = NULL;
6735 }
6736 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006737 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006738
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006739 /* if there are spaces around the equal sign, we need to
6740 * strip them otherwise we'll get trouble for cookie captures,
6741 * or even for rewrites. Since this happens extremely rarely,
6742 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006743 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006744 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6745 int stripped_before = 0;
6746 int stripped_after = 0;
6747
6748 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006749 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006750 equal += stripped_before;
6751 val_beg += stripped_before;
6752 }
6753
6754 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006755 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006756 val_beg += stripped_after;
6757 stripped_before += stripped_after;
6758 }
6759
6760 val_end += stripped_before;
6761 next += stripped_before;
6762 hdr_end += stripped_before;
6763 hdr_next += stripped_before;
6764 cur_hdr->len += stripped_before;
6765 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006766 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006767 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006768
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006769 /* First, let's see if we want to capture this cookie. We check
6770 * that we don't already have a client side cookie, because we
6771 * can only capture one. Also as an optimisation, we ignore
6772 * cookies shorter than the declared name.
6773 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006774 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6775 (val_end - att_beg >= sess->fe->capture_namelen) &&
6776 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006777 int log_len = val_end - att_beg;
6778
6779 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6780 Alert("HTTP logging : out of memory.\n");
6781 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006782 if (log_len > sess->fe->capture_len)
6783 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006784 memcpy(txn->cli_cookie, att_beg, log_len);
6785 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006786 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006787 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006788
Willy Tarreaubca99692010-10-06 19:25:55 +02006789 /* Persistence cookies in passive, rewrite or insert mode have the
6790 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006791 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006792 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006793 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006794 * For cookies in prefix mode, the form is :
6795 *
6796 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006797 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006798 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6799 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6800 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006801 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006802
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006803 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6804 * have the server ID between val_beg and delim, and the original cookie between
6805 * delim+1 and val_end. Otherwise, delim==val_end :
6806 *
6807 * Cookie: NAME=SRV; # in all but prefix modes
6808 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6809 * | || || | |+-> next
6810 * | || || | +--> val_end
6811 * | || || +---------> delim
6812 * | || |+------------> val_beg
6813 * | || +-------------> att_end = equal
6814 * | |+-----------------> att_beg
6815 * | +------------------> prev
6816 * +-------------------------> hdr_beg
6817 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006818
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006819 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006820 for (delim = val_beg; delim < val_end; delim++)
6821 if (*delim == COOKIE_DELIM)
6822 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006823 } else {
6824 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006825 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006826 /* Now check if the cookie contains a date field, which would
6827 * appear after a vertical bar ('|') just after the server name
6828 * and before the delimiter.
6829 */
6830 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6831 if (vbar1) {
6832 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006833 * right is the last seen date. It is a base64 encoded
6834 * 30-bit value representing the UNIX date since the
6835 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006836 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006837 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006838 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006839 if (val_end - vbar1 >= 5) {
6840 val = b64tos30(vbar1);
6841 if (val > 0)
6842 txn->cookie_last_date = val << 2;
6843 }
6844 /* look for a second vertical bar */
6845 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6846 if (vbar1 && (val_end - vbar1 > 5)) {
6847 val = b64tos30(vbar1 + 1);
6848 if (val > 0)
6849 txn->cookie_first_date = val << 2;
6850 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006851 }
6852 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006853
Willy Tarreauf64d1412010-10-07 20:06:11 +02006854 /* if the cookie has an expiration date and the proxy wants to check
6855 * it, then we do that now. We first check if the cookie is too old,
6856 * then only if it has expired. We detect strict overflow because the
6857 * time resolution here is not great (4 seconds). Cookies with dates
6858 * in the future are ignored if their offset is beyond one day. This
6859 * allows an admin to fix timezone issues without expiring everyone
6860 * and at the same time avoids keeping unwanted side effects for too
6861 * long.
6862 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006863 if (txn->cookie_first_date && s->be->cookie_maxlife &&
6864 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006865 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006866 txn->flags &= ~TX_CK_MASK;
6867 txn->flags |= TX_CK_OLD;
6868 delim = val_beg; // let's pretend we have not found the cookie
6869 txn->cookie_first_date = 0;
6870 txn->cookie_last_date = 0;
6871 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006872 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
6873 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006874 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006875 txn->flags &= ~TX_CK_MASK;
6876 txn->flags |= TX_CK_EXPIRED;
6877 delim = val_beg; // let's pretend we have not found the cookie
6878 txn->cookie_first_date = 0;
6879 txn->cookie_last_date = 0;
6880 }
6881
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006882 /* Here, we'll look for the first running server which supports the cookie.
6883 * This allows to share a same cookie between several servers, for example
6884 * to dedicate backup servers to specific servers only.
6885 * However, to prevent clients from sticking to cookie-less backup server
6886 * when they have incidentely learned an empty cookie, we simply ignore
6887 * empty cookies and mark them as invalid.
6888 * The same behaviour is applied when persistence must be ignored.
6889 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006890 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006891 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006892
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006893 while (srv) {
6894 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6895 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02006896 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006897 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006898 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006899 /* we found the server and we can use it */
6900 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02006901 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006902 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006903 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006904 break;
6905 } else {
6906 /* we found a server, but it's down,
6907 * mark it as such and go on in case
6908 * another one is available.
6909 */
6910 txn->flags &= ~TX_CK_MASK;
6911 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006912 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006913 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006914 srv = srv->next;
6915 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006916
Willy Tarreauf64d1412010-10-07 20:06:11 +02006917 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006918 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006919 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006920 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006921 txn->flags |= TX_CK_UNUSED;
6922 else
6923 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006924 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006925
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006926 /* depending on the cookie mode, we may have to either :
6927 * - delete the complete cookie if we're in insert+indirect mode, so that
6928 * the server never sees it ;
6929 * - remove the server id from the cookie value, and tag the cookie as an
6930 * application cookie so that it does not get accidentely removed later,
6931 * if we're in cookie prefix mode
6932 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006933 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006934 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006935
Willy Tarreau9b28e032012-10-12 23:49:43 +02006936 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006937 val_end += delta;
6938 next += delta;
6939 hdr_end += delta;
6940 hdr_next += delta;
6941 cur_hdr->len += delta;
6942 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006943
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006944 del_from = NULL;
6945 preserve_hdr = 1; /* we want to keep this cookie */
6946 }
6947 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006948 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006949 del_from = prev;
6950 }
6951 } else {
6952 /* This is not our cookie, so we must preserve it. But if we already
6953 * scheduled another cookie for removal, we cannot remove the
6954 * complete header, but we can remove the previous block itself.
6955 */
6956 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006957
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006958 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006959 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006960 if (att_beg >= del_from)
6961 att_beg += delta;
6962 if (att_end >= del_from)
6963 att_end += delta;
6964 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006965 val_end += delta;
6966 next += delta;
6967 hdr_end += delta;
6968 hdr_next += delta;
6969 cur_hdr->len += delta;
6970 http_msg_move_end(&txn->req, delta);
6971 prev = del_from;
6972 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006973 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006974 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006975
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006976 /* continue with next cookie on this header line */
6977 att_beg = next;
6978 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006979
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006980 /* There are no more cookies on this line.
6981 * We may still have one (or several) marked for deletion at the
6982 * end of the line. We must do this now in two ways :
6983 * - if some cookies must be preserved, we only delete from the
6984 * mark to the end of line ;
6985 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006986 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006987 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006988 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006989 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006990 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006991 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006992 cur_hdr->len += delta;
6993 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006994 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006995
6996 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006997 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6998 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006999 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007000 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007001 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007002 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007003 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007004 }
7005
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007006 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007007 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007008 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007009}
7010
7011
Willy Tarreaua15645d2007-03-18 16:22:39 +01007012/* Iterate the same filter through all response headers contained in <rtr>.
7013 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7014 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007015int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007016{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007017 char *cur_ptr, *cur_end, *cur_next;
7018 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007019 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007020 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007021 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007022
7023 last_hdr = 0;
7024
Willy Tarreau9b28e032012-10-12 23:49:43 +02007025 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007026 old_idx = 0;
7027
7028 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007029 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007030 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007031 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007032 (exp->action == ACT_ALLOW ||
7033 exp->action == ACT_DENY))
7034 return 0;
7035
7036 cur_idx = txn->hdr_idx.v[old_idx].next;
7037 if (!cur_idx)
7038 break;
7039
7040 cur_hdr = &txn->hdr_idx.v[cur_idx];
7041 cur_ptr = cur_next;
7042 cur_end = cur_ptr + cur_hdr->len;
7043 cur_next = cur_end + cur_hdr->cr + 1;
7044
7045 /* Now we have one header between cur_ptr and cur_end,
7046 * and the next header starts at cur_next.
7047 */
7048
Willy Tarreau15a53a42015-01-21 13:39:42 +01007049 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007050 switch (exp->action) {
7051 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007052 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007053 last_hdr = 1;
7054 break;
7055
7056 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007057 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007058 last_hdr = 1;
7059 break;
7060
7061 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007062 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7063 if (trash.len < 0)
7064 return -1;
7065
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007066 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007067 /* FIXME: if the user adds a newline in the replacement, the
7068 * index will not be recalculated for now, and the new line
7069 * will not be counted as a new header.
7070 */
7071
7072 cur_end += delta;
7073 cur_next += delta;
7074 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007075 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007076 break;
7077
7078 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007079 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007080 cur_next += delta;
7081
Willy Tarreaufa355d42009-11-29 18:12:29 +01007082 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007083 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7084 txn->hdr_idx.used--;
7085 cur_hdr->len = 0;
7086 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007087 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007088 break;
7089
7090 }
7091 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007092
7093 /* keep the link from this header to next one in case of later
7094 * removal of next header.
7095 */
7096 old_idx = cur_idx;
7097 }
7098 return 0;
7099}
7100
7101
7102/* Apply the filter to the status line in the response buffer <rtr>.
7103 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7104 * or -1 if a replacement resulted in an invalid status line.
7105 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007106int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007107{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007108 char *cur_ptr, *cur_end;
7109 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007110 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007111 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007112
7113
Willy Tarreau3d300592007-03-18 18:34:41 +01007114 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007115 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007116 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007117 (exp->action == ACT_ALLOW ||
7118 exp->action == ACT_DENY))
7119 return 0;
7120 else if (exp->action == ACT_REMOVE)
7121 return 0;
7122
7123 done = 0;
7124
Willy Tarreau9b28e032012-10-12 23:49:43 +02007125 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007126 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007127
7128 /* Now we have the status line between cur_ptr and cur_end */
7129
Willy Tarreau15a53a42015-01-21 13:39:42 +01007130 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007131 switch (exp->action) {
7132 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007133 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007134 done = 1;
7135 break;
7136
7137 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007138 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007139 done = 1;
7140 break;
7141
7142 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007143 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7144 if (trash.len < 0)
7145 return -1;
7146
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007147 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007148 /* FIXME: if the user adds a newline in the replacement, the
7149 * index will not be recalculated for now, and the new line
7150 * will not be counted as a new header.
7151 */
7152
Willy Tarreaufa355d42009-11-29 18:12:29 +01007153 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007154 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007155 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007156 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007157 cur_ptr, cur_end + 1,
7158 NULL, NULL);
7159 if (unlikely(!cur_end))
7160 return -1;
7161
7162 /* we have a full respnse and we know that we have either a CR
7163 * or an LF at <ptr>.
7164 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007165 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007166 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007167 /* there is no point trying this regex on headers */
7168 return 1;
7169 }
7170 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007171 return done;
7172}
7173
7174
7175
7176/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007177 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007178 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7179 * unparsable response.
7180 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007181int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007182{
Willy Tarreau192252e2015-04-04 01:47:55 +02007183 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007184 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007185 struct hdr_exp *exp;
7186
7187 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007188 int ret;
7189
7190 /*
7191 * The interleaving of transformations and verdicts
7192 * makes it difficult to decide to continue or stop
7193 * the evaluation.
7194 */
7195
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007196 if (txn->flags & TX_SVDENY)
7197 break;
7198
Willy Tarreau3d300592007-03-18 18:34:41 +01007199 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007200 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7201 exp->action == ACT_PASS)) {
7202 exp = exp->next;
7203 continue;
7204 }
7205
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007206 /* if this filter had a condition, evaluate it now and skip to
7207 * next filter if the condition does not match.
7208 */
7209 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007210 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007211 ret = acl_pass(ret);
7212 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7213 ret = !ret;
7214 if (!ret)
7215 continue;
7216 }
7217
Willy Tarreaua15645d2007-03-18 16:22:39 +01007218 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007219 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007220 if (unlikely(ret < 0))
7221 return -1;
7222
7223 if (likely(ret == 0)) {
7224 /* The filter did not match the response, it can be
7225 * iterated through all headers.
7226 */
Sasha Pachevc6002042014-05-26 12:33:48 -06007227 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
7228 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007229 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007230 }
7231 return 0;
7232}
7233
7234
Willy Tarreaua15645d2007-03-18 16:22:39 +01007235/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007236 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007237 * desirable to call it only when needed. This function is also used when we
7238 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007239 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007240void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007241{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007242 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007243 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01007244 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007245 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007246 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007247 char *hdr_beg, *hdr_end, *hdr_next;
7248 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007249
Willy Tarreaua15645d2007-03-18 16:22:39 +01007250 /* Iterate through the headers.
7251 * we start with the start line.
7252 */
7253 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007254 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007255
7256 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7257 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007258 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007259
7260 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007261 hdr_beg = hdr_next;
7262 hdr_end = hdr_beg + cur_hdr->len;
7263 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007264
Willy Tarreau24581ba2010-08-31 22:39:35 +02007265 /* We have one full header between hdr_beg and hdr_end, and the
7266 * next header starts at hdr_next. We're only interested in
7267 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007268 */
7269
Willy Tarreau24581ba2010-08-31 22:39:35 +02007270 is_cookie2 = 0;
7271 prev = hdr_beg + 10;
7272 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007273 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007274 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7275 if (!val) {
7276 old_idx = cur_idx;
7277 continue;
7278 }
7279 is_cookie2 = 1;
7280 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007281 }
7282
Willy Tarreau24581ba2010-08-31 22:39:35 +02007283 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7284 * <prev> points to the colon.
7285 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007286 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007287
Willy Tarreau24581ba2010-08-31 22:39:35 +02007288 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7289 * check-cache is enabled) and we are not interested in checking
7290 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007291 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02007292 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007293 return;
7294
Willy Tarreau24581ba2010-08-31 22:39:35 +02007295 /* OK so now we know we have to process this response cookie.
7296 * The format of the Set-Cookie header is slightly different
7297 * from the format of the Cookie header in that it does not
7298 * support the comma as a cookie delimiter (thus the header
7299 * cannot be folded) because the Expires attribute described in
7300 * the original Netscape's spec may contain an unquoted date
7301 * with a comma inside. We have to live with this because
7302 * many browsers don't support Max-Age and some browsers don't
7303 * support quoted strings. However the Set-Cookie2 header is
7304 * clean.
7305 *
7306 * We have to keep multiple pointers in order to support cookie
7307 * removal at the beginning, middle or end of header without
7308 * corrupting the header (in case of set-cookie2). A special
7309 * pointer, <scav> points to the beginning of the set-cookie-av
7310 * fields after the first semi-colon. The <next> pointer points
7311 * either to the end of line (set-cookie) or next unquoted comma
7312 * (set-cookie2). All of these headers are valid :
7313 *
7314 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7315 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7316 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7317 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7318 * | | | | | | | | | |
7319 * | | | | | | | | +-> next hdr_end <--+
7320 * | | | | | | | +------------> scav
7321 * | | | | | | +--------------> val_end
7322 * | | | | | +--------------------> val_beg
7323 * | | | | +----------------------> equal
7324 * | | | +------------------------> att_end
7325 * | | +----------------------------> att_beg
7326 * | +------------------------------> prev
7327 * +-----------------------------------------> hdr_beg
7328 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007329
Willy Tarreau24581ba2010-08-31 22:39:35 +02007330 for (; prev < hdr_end; prev = next) {
7331 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007332
Willy Tarreau24581ba2010-08-31 22:39:35 +02007333 /* find att_beg */
7334 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007335 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007336 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007337
Willy Tarreau24581ba2010-08-31 22:39:35 +02007338 /* find att_end : this is the first character after the last non
7339 * space before the equal. It may be equal to hdr_end.
7340 */
7341 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007342
Willy Tarreau24581ba2010-08-31 22:39:35 +02007343 while (equal < hdr_end) {
7344 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7345 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007346 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007347 continue;
7348 att_end = equal;
7349 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007350
Willy Tarreau24581ba2010-08-31 22:39:35 +02007351 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7352 * is between <att_beg> and <equal>, both may be identical.
7353 */
7354
7355 /* look for end of cookie if there is an equal sign */
7356 if (equal < hdr_end && *equal == '=') {
7357 /* look for the beginning of the value */
7358 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007359 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007360 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007361
Willy Tarreau24581ba2010-08-31 22:39:35 +02007362 /* find the end of the value, respecting quotes */
7363 next = find_cookie_value_end(val_beg, hdr_end);
7364
7365 /* make val_end point to the first white space or delimitor after the value */
7366 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007367 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007368 val_end--;
7369 } else {
7370 /* <equal> points to next comma, semi-colon or EOL */
7371 val_beg = val_end = next = equal;
7372 }
7373
7374 if (next < hdr_end) {
7375 /* Set-Cookie2 supports multiple cookies, and <next> points to
7376 * a colon or semi-colon before the end. So skip all attr-value
7377 * pairs and look for the next comma. For Set-Cookie, since
7378 * commas are permitted in values, skip to the end.
7379 */
7380 if (is_cookie2)
7381 next = find_hdr_value_end(next, hdr_end);
7382 else
7383 next = hdr_end;
7384 }
7385
7386 /* Now everything is as on the diagram above */
7387
7388 /* Ignore cookies with no equal sign */
7389 if (equal == val_end)
7390 continue;
7391
7392 /* If there are spaces around the equal sign, we need to
7393 * strip them otherwise we'll get trouble for cookie captures,
7394 * or even for rewrites. Since this happens extremely rarely,
7395 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007396 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007397 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7398 int stripped_before = 0;
7399 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007400
Willy Tarreau24581ba2010-08-31 22:39:35 +02007401 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007402 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007403 equal += stripped_before;
7404 val_beg += stripped_before;
7405 }
7406
7407 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007408 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007409 val_beg += stripped_after;
7410 stripped_before += stripped_after;
7411 }
7412
7413 val_end += stripped_before;
7414 next += stripped_before;
7415 hdr_end += stripped_before;
7416 hdr_next += stripped_before;
7417 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007418 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007419 }
7420
7421 /* First, let's see if we want to capture this cookie. We check
7422 * that we don't already have a server side cookie, because we
7423 * can only capture one. Also as an optimisation, we ignore
7424 * cookies shorter than the declared name.
7425 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007426 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007427 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007428 (val_end - att_beg >= sess->fe->capture_namelen) &&
7429 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007430 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007431 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007432 Alert("HTTP logging : out of memory.\n");
7433 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007434 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007435 if (log_len > sess->fe->capture_len)
7436 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01007437 memcpy(txn->srv_cookie, att_beg, log_len);
7438 txn->srv_cookie[log_len] = 0;
7439 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007440 }
7441
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007442 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007443 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007444 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007445 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7446 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007447 /* assume passive cookie by default */
7448 txn->flags &= ~TX_SCK_MASK;
7449 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007450
7451 /* If the cookie is in insert mode on a known server, we'll delete
7452 * this occurrence because we'll insert another one later.
7453 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007454 * a direct access.
7455 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007456 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007457 /* The "preserve" flag was set, we don't want to touch the
7458 * server's cookie.
7459 */
7460 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007461 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007462 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007463 /* this cookie must be deleted */
7464 if (*prev == ':' && next == hdr_end) {
7465 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007466 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007467 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7468 txn->hdr_idx.used--;
7469 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007470 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007471 hdr_next += delta;
7472 http_msg_move_end(&txn->rsp, delta);
7473 /* note: while both invalid now, <next> and <hdr_end>
7474 * are still equal, so the for() will stop as expected.
7475 */
7476 } else {
7477 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007478 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007479 next = prev;
7480 hdr_end += delta;
7481 hdr_next += delta;
7482 cur_hdr->len += delta;
7483 http_msg_move_end(&txn->rsp, delta);
7484 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007485 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007486 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007487 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007488 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007489 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007490 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007491 * with this server since we know it.
7492 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007493 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007494 next += delta;
7495 hdr_end += delta;
7496 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007497 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007498 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007499
Willy Tarreauf1348312010-10-07 15:54:11 +02007500 txn->flags &= ~TX_SCK_MASK;
7501 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007502 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007503 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007504 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007505 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007506 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007507 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007508 next += delta;
7509 hdr_end += delta;
7510 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007511 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007512 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007513
Willy Tarreau827aee92011-03-10 16:55:02 +01007514 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007515 txn->flags &= ~TX_SCK_MASK;
7516 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007517 }
7518 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007519 /* that's done for this cookie, check the next one on the same
7520 * line when next != hdr_end (only if is_cookie2).
7521 */
7522 }
7523 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007524 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007525 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007526}
7527
7528
Willy Tarreaua15645d2007-03-18 16:22:39 +01007529/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007530 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007531 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007532void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007533{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007534 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007535 char *p1, *p2;
7536
7537 char *cur_ptr, *cur_end, *cur_next;
7538 int cur_idx;
7539
Willy Tarreau5df51872007-11-25 16:20:08 +01007540 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007541 return;
7542
7543 /* Iterate through the headers.
7544 * we start with the start line.
7545 */
7546 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007547 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007548
7549 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7550 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007551 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007552
7553 cur_hdr = &txn->hdr_idx.v[cur_idx];
7554 cur_ptr = cur_next;
7555 cur_end = cur_ptr + cur_hdr->len;
7556 cur_next = cur_end + cur_hdr->cr + 1;
7557
7558 /* We have one full header between cur_ptr and cur_end, and the
7559 * next header starts at cur_next. We're only interested in
7560 * "Cookie:" headers.
7561 */
7562
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007563 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7564 if (val) {
7565 if ((cur_end - (cur_ptr + val) >= 8) &&
7566 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7567 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7568 return;
7569 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007570 }
7571
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007572 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7573 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007574 continue;
7575
7576 /* OK, right now we know we have a cache-control header at cur_ptr */
7577
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007578 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007579
7580 if (p1 >= cur_end) /* no more info */
7581 continue;
7582
7583 /* p1 is at the beginning of the value */
7584 p2 = p1;
7585
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007586 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007587 p2++;
7588
7589 /* we have a complete value between p1 and p2 */
7590 if (p2 < cur_end && *p2 == '=') {
7591 /* we have something of the form no-cache="set-cookie" */
7592 if ((cur_end - p1 >= 21) &&
7593 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7594 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007595 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007596 continue;
7597 }
7598
7599 /* OK, so we know that either p2 points to the end of string or to a comma */
7600 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007601 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01007602 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7603 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7604 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007605 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007606 return;
7607 }
7608
7609 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007610 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007611 continue;
7612 }
7613 }
7614}
7615
Willy Tarreau58f10d72006-12-04 02:26:12 +01007616
Willy Tarreaub2513902006-12-17 14:52:38 +01007617/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007618 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007619 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007620 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007621 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007622 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007623 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007624 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007625 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007626int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007627{
7628 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007629 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007630 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007631
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007632 if (!uri_auth)
7633 return 0;
7634
Cyril Bonté70be45d2010-10-12 00:14:35 +02007635 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007636 return 0;
7637
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007638 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007639 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007640 return 0;
7641
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007642 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007643 return 0;
7644
Willy Tarreaub2513902006-12-17 14:52:38 +01007645 return 1;
7646}
7647
Willy Tarreau4076a152009-04-02 15:18:36 +02007648/*
7649 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007650 * By default it tries to report the error position as msg->err_pos. However if
7651 * this one is not set, it will then report msg->next, which is the last known
7652 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007653 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007654 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007655void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007656 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007657 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007658{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007659 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007660 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007661 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007662
Willy Tarreauf3764b72016-03-31 13:45:10 +02007663 es->len = MIN(chn->buf->i, global.tune.bufsize);
Willy Tarreau9b28e032012-10-12 23:49:43 +02007664 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007665 len1 = MIN(len1, es->len);
7666 len2 = es->len - len1; /* remaining data if buffer wraps */
7667
Willy Tarreauf3764b72016-03-31 13:45:10 +02007668 if (!es->buf)
7669 es->buf = malloc(global.tune.bufsize);
7670
7671 if (es->buf) {
7672 memcpy(es->buf, chn->buf->p, len1);
7673 if (len2)
7674 memcpy(es->buf + len1, chn->buf->data, len2);
7675 }
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007676
Willy Tarreau4076a152009-04-02 15:18:36 +02007677 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007678 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007679 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007680 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007681
Willy Tarreau4076a152009-04-02 15:18:36 +02007682 es->when = date; // user-visible date
7683 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007684 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007685 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007686 if (objt_conn(sess->origin))
7687 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007688 else
7689 memset(&es->src, 0, sizeof(es->src));
7690
Willy Tarreau078272e2010-12-12 12:46:33 +01007691 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007692 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007693 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007694 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007695 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007696 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007697 es->b_out = chn->buf->o;
7698 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007699 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007700 es->m_clen = msg->chunk_len;
7701 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02007702}
Willy Tarreaub2513902006-12-17 14:52:38 +01007703
Willy Tarreau294c4732011-12-16 21:35:50 +01007704/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7705 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7706 * performed over the whole headers. Otherwise it must contain a valid header
7707 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7708 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7709 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7710 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007711 * -1. The value fetch stops at commas, so this function is suited for use with
7712 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007713 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007714 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007715unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007716 struct hdr_idx *idx, int occ,
7717 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007718{
Willy Tarreau294c4732011-12-16 21:35:50 +01007719 struct hdr_ctx local_ctx;
7720 char *ptr_hist[MAX_HDR_HISTORY];
7721 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007722 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007723 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007724
Willy Tarreau294c4732011-12-16 21:35:50 +01007725 if (!ctx) {
7726 local_ctx.idx = 0;
7727 ctx = &local_ctx;
7728 }
7729
Willy Tarreaubce70882009-09-07 11:51:47 +02007730 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007731 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007732 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007733 occ--;
7734 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007735 *vptr = ctx->line + ctx->val;
7736 *vlen = ctx->vlen;
7737 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007738 }
7739 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007740 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007741 }
7742
7743 /* negative occurrence, we scan all the list then walk back */
7744 if (-occ > MAX_HDR_HISTORY)
7745 return 0;
7746
Willy Tarreau294c4732011-12-16 21:35:50 +01007747 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007748 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007749 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7750 len_hist[hist_ptr] = ctx->vlen;
7751 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007752 hist_ptr = 0;
7753 found++;
7754 }
7755 if (-occ > found)
7756 return 0;
7757 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02007758 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7759 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7760 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02007761 */
Willy Tarreau67dad272013-06-12 22:27:44 +02007762 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02007763 if (hist_ptr >= MAX_HDR_HISTORY)
7764 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007765 *vptr = ptr_hist[hist_ptr];
7766 *vlen = len_hist[hist_ptr];
7767 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007768}
7769
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007770/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7771 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7772 * performed over the whole headers. Otherwise it must contain a valid header
7773 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7774 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7775 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7776 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7777 * -1. This function differs from http_get_hdr() in that it only returns full
7778 * line header values and does not stop at commas.
7779 * The return value is 0 if nothing was found, or non-zero otherwise.
7780 */
7781unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
7782 struct hdr_idx *idx, int occ,
7783 struct hdr_ctx *ctx, char **vptr, int *vlen)
7784{
7785 struct hdr_ctx local_ctx;
7786 char *ptr_hist[MAX_HDR_HISTORY];
7787 int len_hist[MAX_HDR_HISTORY];
7788 unsigned int hist_ptr;
7789 int found;
7790
7791 if (!ctx) {
7792 local_ctx.idx = 0;
7793 ctx = &local_ctx;
7794 }
7795
7796 if (occ >= 0) {
7797 /* search from the beginning */
7798 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
7799 occ--;
7800 if (occ <= 0) {
7801 *vptr = ctx->line + ctx->val;
7802 *vlen = ctx->vlen;
7803 return 1;
7804 }
7805 }
7806 return 0;
7807 }
7808
7809 /* negative occurrence, we scan all the list then walk back */
7810 if (-occ > MAX_HDR_HISTORY)
7811 return 0;
7812
7813 found = hist_ptr = 0;
7814 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
7815 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7816 len_hist[hist_ptr] = ctx->vlen;
7817 if (++hist_ptr >= MAX_HDR_HISTORY)
7818 hist_ptr = 0;
7819 found++;
7820 }
7821 if (-occ > found)
7822 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007823
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007824 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007825 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7826 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7827 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007828 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007829 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007830 if (hist_ptr >= MAX_HDR_HISTORY)
7831 hist_ptr -= MAX_HDR_HISTORY;
7832 *vptr = ptr_hist[hist_ptr];
7833 *vlen = len_hist[hist_ptr];
7834 return 1;
7835}
7836
Willy Tarreaubaaee002006-06-26 02:48:02 +02007837/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007838 * Print a debug line with a header. Always stop at the first CR or LF char,
7839 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7840 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007841 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007842void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007843{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007844 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007845 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007846
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007847 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007848 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02007849 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
7850 objt_conn(s->si[1].end) ? (unsigned short)objt_conn(s->si[1].end)->handle.fd : -1);
Willy Tarreaue92693a2012-09-24 21:13:39 +02007851
7852 for (max = 0; start + max < end; max++)
7853 if (start[max] == '\r' || start[max] == '\n')
7854 break;
7855
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007856 UBOUND(max, trash.size - trash.len - 3);
7857 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
7858 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01007859 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007860}
7861
Willy Tarreaueee5b512015-04-03 23:46:31 +02007862
7863/* Allocate a new HTTP transaction for stream <s> unless there is one already.
7864 * The hdr_idx is allocated as well. In case of allocation failure, everything
7865 * allocated is freed and NULL is returned. Otherwise the new transaction is
7866 * assigned to the stream and returned.
7867 */
7868struct http_txn *http_alloc_txn(struct stream *s)
7869{
7870 struct http_txn *txn = s->txn;
7871
7872 if (txn)
7873 return txn;
7874
7875 txn = pool_alloc2(pool2_http_txn);
7876 if (!txn)
7877 return txn;
7878
7879 txn->hdr_idx.size = global.tune.max_http_hdr;
7880 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
7881 if (!txn->hdr_idx.v) {
7882 pool_free2(pool2_http_txn, txn);
7883 return NULL;
7884 }
7885
7886 s->txn = txn;
7887 return txn;
7888}
7889
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007890void http_txn_reset_req(struct http_txn *txn)
7891{
7892 txn->req.flags = 0;
7893 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
7894 txn->req.next = 0;
7895 txn->req.chunk_len = 0LL;
7896 txn->req.body_len = 0LL;
7897 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7898}
7899
7900void http_txn_reset_res(struct http_txn *txn)
7901{
7902 txn->rsp.flags = 0;
7903 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
7904 txn->rsp.next = 0;
7905 txn->rsp.chunk_len = 0LL;
7906 txn->rsp.body_len = 0LL;
7907 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
7908}
7909
Willy Tarreau0937bc42009-12-22 15:03:09 +01007910/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007911 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01007912 * the required fields are properly allocated and that we only need to (re)init
7913 * them. This should be used before processing any new request.
7914 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007915void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007916{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007917 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007918 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007919
7920 txn->flags = 0;
7921 txn->status = -1;
7922
Willy Tarreauf64d1412010-10-07 20:06:11 +02007923 txn->cookie_first_date = 0;
7924 txn->cookie_last_date = 0;
7925
Willy Tarreaueee5b512015-04-03 23:46:31 +02007926 txn->srv_cookie = NULL;
7927 txn->cli_cookie = NULL;
7928 txn->uri = NULL;
7929
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007930 http_txn_reset_req(txn);
7931 http_txn_reset_res(txn);
7932
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007933 txn->req.chn = &s->req;
7934 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007935
7936 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007937
7938 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7939 if (fe->options2 & PR_O2_REQBUG_OK)
7940 txn->req.err_pos = -1; /* let buggy requests pass */
7941
Willy Tarreau0937bc42009-12-22 15:03:09 +01007942 if (txn->hdr_idx.v)
7943 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02007944
7945 vars_init(&s->vars_txn, SCOPE_TXN);
7946 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007947}
7948
7949/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02007950void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007951{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007952 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007953 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007954
7955 /* these ones will have been dynamically allocated */
7956 pool_free2(pool2_requri, txn->uri);
7957 pool_free2(pool2_capture, txn->cli_cookie);
7958 pool_free2(pool2_capture, txn->srv_cookie);
William Lallemanda73203e2012-03-12 12:48:57 +01007959 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007960
William Lallemanda73203e2012-03-12 12:48:57 +01007961 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007962 txn->uri = NULL;
7963 txn->srv_cookie = NULL;
7964 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007965
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007966 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007967 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007968 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007969 pool_free2(h->pool, s->req_cap[h->index]);
7970 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007971 }
7972
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007973 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007974 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007975 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007976 pool_free2(h->pool, s->res_cap[h->index]);
7977 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007978 }
7979
Willy Tarreau6204cd92016-03-10 16:33:04 +01007980 vars_prune(&s->vars_txn, s->sess, s);
7981 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007982}
7983
7984/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02007985void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007986{
7987 http_end_txn(s);
7988 http_init_txn(s);
7989
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01007990 /* reinitialise the current rule list pointer to NULL. We are sure that
7991 * any rulelist match the NULL pointer.
7992 */
7993 s->current_rule_list = NULL;
7994
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007995 s->be = strm_fe(s);
7996 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02007997 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02007998 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007999 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008000 /* re-init store persistence */
8001 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008002 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008003
Willy Tarreau0937bc42009-12-22 15:03:09 +01008004 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008005
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008006 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008007
Willy Tarreau739cfba2010-01-25 23:11:14 +01008008 /* We must trim any excess data from the response buffer, because we
8009 * may have blocked an invalid response from a server that we don't
8010 * want to accidentely forward once we disable the analysers, nor do
8011 * we want those data to come along with next response. A typical
8012 * example of such data would be from a buggy server responding to
8013 * a HEAD with some data, or sending more than the advertised
8014 * content-length.
8015 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008016 if (unlikely(s->res.buf->i))
8017 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008018
Christopher Fauletc0c672a2017-03-28 11:51:33 +02008019 /* Now we can realign the response buffer */
8020 buffer_realign(s->res.buf);
8021
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008022 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008023 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008024
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008025 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008026 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008027
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008028 s->req.rex = TICK_ETERNITY;
8029 s->req.wex = TICK_ETERNITY;
8030 s->req.analyse_exp = TICK_ETERNITY;
8031 s->res.rex = TICK_ETERNITY;
8032 s->res.wex = TICK_ETERNITY;
8033 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01008034 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008035}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008036
Sasha Pachev218f0642014-06-16 12:05:59 -06008037void free_http_res_rules(struct list *r)
8038{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008039 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06008040
8041 list_for_each_entry_safe(pr, tr, r, list) {
8042 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008043 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008044 free(pr);
8045 }
8046}
8047
8048void free_http_req_rules(struct list *r)
8049{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008050 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01008051
8052 list_for_each_entry_safe(pr, tr, r, list) {
8053 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008054 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008055 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008056
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008057 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008058 free(pr);
8059 }
8060}
8061
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008062/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008063struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01008064{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008065 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008066 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008067 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008068 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008069
Vincent Bernat02779b62016-04-03 13:48:43 +02008070 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01008071 if (!rule) {
8072 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008073 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008074 }
8075
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008076 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008077 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01008078 cur_arg = 1;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008079 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block") || !strcmp(args[0], "tarpit")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04008080 int code;
8081 int hc;
8082
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008083 if (!strcmp(args[0], "tarpit")) {
8084 rule->action = ACT_HTTP_REQ_TARPIT;
8085 rule->deny_status = HTTP_ERR_500;
8086 }
8087 else {
8088 rule->action = ACT_ACTION_DENY;
8089 rule->deny_status = HTTP_ERR_403;
8090 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008091 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04008092 if (strcmp(args[cur_arg], "deny_status") == 0) {
8093 cur_arg++;
8094 if (!args[cur_arg]) {
8095 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
8096 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
8097 goto out_err;
8098 }
8099
8100 code = atol(args[cur_arg]);
8101 cur_arg++;
8102 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
8103 if (http_err_codes[hc] == code) {
8104 rule->deny_status = hc;
8105 break;
8106 }
8107 }
8108
8109 if (hc >= HTTP_ERR_SIZE) {
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008110 Warning("parsing [%s:%d] : status code %d not handled, using default code %d.\n",
8111 file, linenum, code, http_err_codes[rule->deny_status]);
CJ Ess108b1dd2015-04-07 12:03:37 -04008112 }
8113 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008114 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008115 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008116 cur_arg = 1;
8117
8118 while(*args[cur_arg]) {
8119 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008120 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008121 cur_arg+=2;
8122 continue;
8123 } else
8124 break;
8125 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008126 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008127 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008128 cur_arg = 1;
8129
8130 if (!*args[cur_arg] ||
8131 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8132 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8133 file, linenum, args[0]);
8134 goto out_err;
8135 }
8136 rule->arg.nice = atoi(args[cur_arg]);
8137 if (rule->arg.nice < -1024)
8138 rule->arg.nice = -1024;
8139 else if (rule->arg.nice > 1024)
8140 rule->arg.nice = 1024;
8141 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008142 } else if (!strcmp(args[0], "set-tos")) {
8143#ifdef IP_TOS
8144 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008145 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008146 cur_arg = 1;
8147
8148 if (!*args[cur_arg] ||
8149 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8150 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8151 file, linenum, args[0]);
8152 goto out_err;
8153 }
8154
8155 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8156 if (err && *err != '\0') {
8157 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8158 file, linenum, err, args[0]);
8159 goto out_err;
8160 }
8161 cur_arg++;
8162#else
8163 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8164 goto out_err;
8165#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008166 } else if (!strcmp(args[0], "set-mark")) {
8167#ifdef SO_MARK
8168 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008169 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008170 cur_arg = 1;
8171
8172 if (!*args[cur_arg] ||
8173 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8174 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8175 file, linenum, args[0]);
8176 goto out_err;
8177 }
8178
8179 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8180 if (err && *err != '\0') {
8181 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8182 file, linenum, err, args[0]);
8183 goto out_err;
8184 }
8185 cur_arg++;
8186 global.last_checks |= LSTCHK_NETADM;
8187#else
8188 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8189 goto out_err;
8190#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008191 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008192 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008193 cur_arg = 1;
8194
8195 if (!*args[cur_arg] ||
8196 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8197 bad_log_level:
8198 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8199 file, linenum, args[0]);
8200 goto out_err;
8201 }
8202 if (strcmp(args[cur_arg], "silent") == 0)
8203 rule->arg.loglevel = -1;
8204 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8205 goto bad_log_level;
8206 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008207 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008208 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008209 cur_arg = 1;
8210
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008211 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8212 (*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 +01008213 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8214 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008215 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008216 }
8217
8218 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8219 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8220 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008221
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008222 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008223 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008224 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 +01008225 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8226 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8227 file, linenum, args[0], error);
8228 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008229 goto out_err;
8230 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008231 free(proxy->conf.lfs_file);
8232 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8233 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008234 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02008235 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008236 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008237 cur_arg = 1;
8238
8239 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02008240 (*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 -06008241 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
8242 file, linenum, args[0]);
8243 goto out_err;
8244 }
8245
8246 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8247 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8248 LIST_INIT(&rule->arg.hdr_add.fmt);
8249
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008250 error = NULL;
8251 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
8252 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8253 args[cur_arg + 1], error);
8254 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008255 goto out_err;
8256 }
8257
8258 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008259 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008260 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 +01008261 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8262 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8263 file, linenum, args[0], error);
8264 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008265 goto out_err;
8266 }
Sasha Pachev218f0642014-06-16 12:05:59 -06008267
8268 free(proxy->conf.lfs_file);
8269 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8270 proxy->conf.lfs_line = proxy->conf.args.line;
8271 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008272 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008273 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008274 cur_arg = 1;
8275
8276 if (!*args[cur_arg] ||
8277 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8278 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8279 file, linenum, args[0]);
8280 goto out_err;
8281 }
8282
8283 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8284 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8285
8286 proxy->conf.args.ctx = ARGC_HRQ;
8287 free(proxy->conf.lfs_file);
8288 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8289 proxy->conf.lfs_line = proxy->conf.args.line;
8290 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02008291 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
8292 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02008293 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02008294 struct sample_expr *expr;
8295 unsigned int where;
8296 char *err = NULL;
8297
8298 cur_arg = 1;
8299 proxy->conf.args.ctx = ARGC_TRK;
8300
8301 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
8302 if (!expr) {
8303 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8304 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
8305 free(err);
8306 goto out_err;
8307 }
8308
8309 where = 0;
8310 if (proxy->cap & PR_CAP_FE)
8311 where |= SMP_VAL_FE_HRQ_HDR;
8312 if (proxy->cap & PR_CAP_BE)
8313 where |= SMP_VAL_BE_HRQ_HDR;
8314
8315 if (!(expr->fetch->val & where)) {
8316 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
8317 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
8318 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
8319 args[cur_arg-1], sample_src_names(expr->fetch->use));
8320 free(expr);
8321 goto out_err;
8322 }
8323
8324 if (strcmp(args[cur_arg], "table") == 0) {
8325 cur_arg++;
8326 if (!args[cur_arg]) {
8327 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
8328 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
8329 free(expr);
8330 goto out_err;
8331 }
8332 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008333 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02008334 cur_arg++;
8335 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008336 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008337 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01008338 } else if (strcmp(args[0], "redirect") == 0) {
8339 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008340 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008341
Willy Tarreaube4653b2015-05-28 15:26:58 +02008342 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01008343 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8344 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8345 goto out_err;
8346 }
8347
8348 /* this redirect rule might already contain a parsed condition which
8349 * we'll pass to the http-request rule.
8350 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008351 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008352 rule->arg.redir = redir;
8353 rule->cond = redir->cond;
8354 redir->cond = NULL;
8355 cur_arg = 2;
8356 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008357 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8358 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008359 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008360 /*
8361 * '+ 8' for 'add-acl('
8362 * '- 9' for 'add-acl(' + trailing ')'
8363 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008364 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008365
8366 cur_arg = 1;
8367
8368 if (!*args[cur_arg] ||
8369 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8370 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8371 file, linenum, args[0]);
8372 goto out_err;
8373 }
8374
8375 LIST_INIT(&rule->arg.map.key);
8376 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008377 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008378 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008379 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8380 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8381 file, linenum, args[0], error);
8382 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008383 goto out_err;
8384 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008385 free(proxy->conf.lfs_file);
8386 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8387 proxy->conf.lfs_line = proxy->conf.args.line;
8388 cur_arg += 1;
8389 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8390 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008391 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008392 /*
8393 * '+ 8' for 'del-acl('
8394 * '- 9' for 'del-acl(' + trailing ')'
8395 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008396 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008397
8398 cur_arg = 1;
8399
8400 if (!*args[cur_arg] ||
8401 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8402 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8403 file, linenum, args[0]);
8404 goto out_err;
8405 }
8406
8407 LIST_INIT(&rule->arg.map.key);
8408 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008409 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008410 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008411 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8412 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8413 file, linenum, args[0], error);
8414 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008415 goto out_err;
8416 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008417 free(proxy->conf.lfs_file);
8418 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8419 proxy->conf.lfs_line = proxy->conf.args.line;
8420 cur_arg += 1;
8421 } else if (strncmp(args[0], "del-map", 7) == 0) {
8422 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008423 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008424 /*
8425 * '+ 8' for 'del-map('
8426 * '- 9' for 'del-map(' + trailing ')'
8427 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008428 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008429
8430 cur_arg = 1;
8431
8432 if (!*args[cur_arg] ||
8433 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8434 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8435 file, linenum, args[0]);
8436 goto out_err;
8437 }
8438
8439 LIST_INIT(&rule->arg.map.key);
8440 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008441 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008442 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008443 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8444 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8445 file, linenum, args[0], error);
8446 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008447 goto out_err;
8448 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008449 free(proxy->conf.lfs_file);
8450 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8451 proxy->conf.lfs_line = proxy->conf.args.line;
8452 cur_arg += 1;
8453 } else if (strncmp(args[0], "set-map", 7) == 0) {
8454 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008455 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008456 /*
8457 * '+ 8' for 'set-map('
8458 * '- 9' for 'set-map(' + trailing ')'
8459 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008460 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008461
8462 cur_arg = 1;
8463
8464 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8465 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8466 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8467 file, linenum, args[0]);
8468 goto out_err;
8469 }
8470
8471 LIST_INIT(&rule->arg.map.key);
8472 LIST_INIT(&rule->arg.map.value);
8473 proxy->conf.args.ctx = ARGC_HRQ;
8474
8475 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008476 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008477 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008478 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8479 Alert("parsing [%s:%d]: 'http-request %s' key: %s.\n",
8480 file, linenum, args[0], error);
8481 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008482 goto out_err;
8483 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008484
8485 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008486 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008487 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 +01008488 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8489 Alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n",
8490 file, linenum, args[0], error);
8491 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008492 goto out_err;
8493 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008494 free(proxy->conf.lfs_file);
8495 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8496 proxy->conf.lfs_line = proxy->conf.args.line;
8497
8498 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02008499 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
8500 char *errmsg = NULL;
8501 cur_arg = 1;
8502 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02008503 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02008504 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02008505 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02008506 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8507 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8508 free(errmsg);
8509 goto out_err;
8510 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008511 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02008512 action_build_list(&http_req_keywords.list, &trash);
8513 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
8514 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08008515 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02008516 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02008517 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008518 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008519 }
8520
8521 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8522 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008523 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008524
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008525 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8526 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8527 file, linenum, args[0], errmsg);
8528 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008529 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008530 }
8531 rule->cond = cond;
8532 }
8533 else if (*args[cur_arg]) {
8534 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8535 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8536 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008537 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008538 }
8539
8540 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008541 out_err:
8542 free(rule);
8543 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008544}
8545
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008546/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008547struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008548{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008549 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008550 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008551 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008552 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008553
8554 rule = calloc(1, sizeof(*rule));
8555 if (!rule) {
8556 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
8557 goto out_err;
8558 }
8559
8560 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008561 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008562 cur_arg = 1;
8563 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008564 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008565 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008566 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008567 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008568 cur_arg = 1;
8569
8570 if (!*args[cur_arg] ||
8571 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8572 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8573 file, linenum, args[0]);
8574 goto out_err;
8575 }
8576 rule->arg.nice = atoi(args[cur_arg]);
8577 if (rule->arg.nice < -1024)
8578 rule->arg.nice = -1024;
8579 else if (rule->arg.nice > 1024)
8580 rule->arg.nice = 1024;
8581 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008582 } else if (!strcmp(args[0], "set-tos")) {
8583#ifdef IP_TOS
8584 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008585 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008586 cur_arg = 1;
8587
8588 if (!*args[cur_arg] ||
8589 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8590 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8591 file, linenum, args[0]);
8592 goto out_err;
8593 }
8594
8595 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8596 if (err && *err != '\0') {
8597 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8598 file, linenum, err, args[0]);
8599 goto out_err;
8600 }
8601 cur_arg++;
8602#else
8603 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8604 goto out_err;
8605#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008606 } else if (!strcmp(args[0], "set-mark")) {
8607#ifdef SO_MARK
8608 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008609 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008610 cur_arg = 1;
8611
8612 if (!*args[cur_arg] ||
8613 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8614 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8615 file, linenum, args[0]);
8616 goto out_err;
8617 }
8618
8619 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8620 if (err && *err != '\0') {
8621 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8622 file, linenum, err, args[0]);
8623 goto out_err;
8624 }
8625 cur_arg++;
8626 global.last_checks |= LSTCHK_NETADM;
8627#else
8628 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8629 goto out_err;
8630#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008631 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008632 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008633 cur_arg = 1;
8634
8635 if (!*args[cur_arg] ||
8636 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8637 bad_log_level:
8638 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8639 file, linenum, args[0]);
8640 goto out_err;
8641 }
8642 if (strcmp(args[cur_arg], "silent") == 0)
8643 rule->arg.loglevel = -1;
Ruoshan Huangdd016782016-06-15 22:16:03 +08008644 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008645 goto bad_log_level;
8646 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008647 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008648 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008649 cur_arg = 1;
8650
8651 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8652 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8653 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8654 file, linenum, args[0]);
8655 goto out_err;
8656 }
8657
8658 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8659 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8660 LIST_INIT(&rule->arg.hdr_add.fmt);
8661
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008662 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008663 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008664 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 +01008665 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8666 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8667 file, linenum, args[0], error);
8668 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008669 goto out_err;
8670 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008671 free(proxy->conf.lfs_file);
8672 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8673 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008674 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06008675 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008676 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008677 cur_arg = 1;
8678
8679 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02008680 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
8681 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06008682 file, linenum, args[0]);
8683 goto out_err;
8684 }
8685
8686 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8687 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8688 LIST_INIT(&rule->arg.hdr_add.fmt);
8689
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008690 error = NULL;
8691 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
8692 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8693 args[cur_arg + 1], error);
8694 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008695 goto out_err;
8696 }
8697
8698 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008699 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008700 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 +01008701 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8702 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8703 file, linenum, args[0], error);
8704 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008705 goto out_err;
8706 }
Sasha Pachev218f0642014-06-16 12:05:59 -06008707
8708 free(proxy->conf.lfs_file);
8709 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8710 proxy->conf.lfs_line = proxy->conf.args.line;
8711 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008712 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008713 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008714 cur_arg = 1;
8715
8716 if (!*args[cur_arg] ||
8717 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8718 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8719 file, linenum, args[0]);
8720 goto out_err;
8721 }
8722
8723 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8724 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8725
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008726 proxy->conf.args.ctx = ARGC_HRS;
8727 free(proxy->conf.lfs_file);
8728 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8729 proxy->conf.lfs_line = proxy->conf.args.line;
8730 cur_arg += 1;
8731 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8732 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008733 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008734 /*
8735 * '+ 8' for 'add-acl('
8736 * '- 9' for 'add-acl(' + trailing ')'
8737 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008738 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008739
8740 cur_arg = 1;
8741
8742 if (!*args[cur_arg] ||
8743 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8744 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8745 file, linenum, args[0]);
8746 goto out_err;
8747 }
8748
8749 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008750 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008751 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008752 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008753 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8754 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8755 file, linenum, args[0], error);
8756 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008757 goto out_err;
8758 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008759 free(proxy->conf.lfs_file);
8760 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8761 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008762
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008763 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008764 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8765 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008766 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008767 /*
8768 * '+ 8' for 'del-acl('
8769 * '- 9' for 'del-acl(' + trailing ')'
8770 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008771 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008772
8773 cur_arg = 1;
8774
8775 if (!*args[cur_arg] ||
8776 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8777 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8778 file, linenum, args[0]);
8779 goto out_err;
8780 }
8781
8782 LIST_INIT(&rule->arg.map.key);
8783 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008784 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008785 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008786 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8787 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8788 file, linenum, args[0], error);
8789 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008790 goto out_err;
8791 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008792 free(proxy->conf.lfs_file);
8793 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8794 proxy->conf.lfs_line = proxy->conf.args.line;
8795 cur_arg += 1;
8796 } else if (strncmp(args[0], "del-map", 7) == 0) {
8797 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008798 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008799 /*
8800 * '+ 8' for 'del-map('
8801 * '- 9' for 'del-map(' + trailing ')'
8802 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008803 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008804
8805 cur_arg = 1;
8806
8807 if (!*args[cur_arg] ||
8808 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8809 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8810 file, linenum, args[0]);
8811 goto out_err;
8812 }
8813
8814 LIST_INIT(&rule->arg.map.key);
8815 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008816 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008817 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008818 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8819 Alert("parsing [%s:%d]: 'http-response %s' %s.\n",
8820 file, linenum, args[0], error);
8821 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008822 goto out_err;
8823 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008824 free(proxy->conf.lfs_file);
8825 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8826 proxy->conf.lfs_line = proxy->conf.args.line;
8827 cur_arg += 1;
8828 } else if (strncmp(args[0], "set-map", 7) == 0) {
8829 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008830 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008831 /*
8832 * '+ 8' for 'set-map('
8833 * '- 9' for 'set-map(' + trailing ')'
8834 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008835 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008836
8837 cur_arg = 1;
8838
8839 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8840 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8841 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8842 file, linenum, args[0]);
8843 goto out_err;
8844 }
8845
8846 LIST_INIT(&rule->arg.map.key);
8847 LIST_INIT(&rule->arg.map.value);
8848
8849 proxy->conf.args.ctx = ARGC_HRS;
8850
8851 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008852 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008853 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008854 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8855 Alert("parsing [%s:%d]: 'http-response %s' name: %s.\n",
8856 file, linenum, args[0], error);
8857 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008858 goto out_err;
8859 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008860
8861 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008862 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008863 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 +01008864 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8865 Alert("parsing [%s:%d]: 'http-response %s' value: %s.\n",
8866 file, linenum, args[0], error);
8867 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008868 goto out_err;
8869 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008870
8871 free(proxy->conf.lfs_file);
8872 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8873 proxy->conf.lfs_line = proxy->conf.args.line;
8874
8875 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02008876 } else if (strcmp(args[0], "redirect") == 0) {
8877 struct redirect_rule *redir;
8878 char *errmsg = NULL;
8879
8880 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
8881 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
8882 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8883 goto out_err;
8884 }
8885
8886 /* this redirect rule might already contain a parsed condition which
8887 * we'll pass to the http-request rule.
8888 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008889 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02008890 rule->arg.redir = redir;
8891 rule->cond = redir->cond;
8892 redir->cond = NULL;
8893 cur_arg = 2;
8894 return rule;
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08008895 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
8896 args[0][9] == '\0' && args[0][8] >= '0' &&
8897 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
8898 struct sample_expr *expr;
8899 unsigned int where;
8900 char *err = NULL;
8901
8902 cur_arg = 1;
8903 proxy->conf.args.ctx = ARGC_TRK;
8904
8905 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
8906 if (!expr) {
8907 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
8908 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
8909 free(err);
8910 goto out_err;
8911 }
8912
8913 where = 0;
8914 if (proxy->cap & PR_CAP_FE)
8915 where |= SMP_VAL_FE_HRS_HDR;
8916 if (proxy->cap & PR_CAP_BE)
8917 where |= SMP_VAL_BE_HRS_HDR;
8918
8919 if (!(expr->fetch->val & where)) {
8920 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :"
8921 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
8922 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
8923 args[cur_arg-1], sample_src_names(expr->fetch->use));
8924 free(expr);
8925 goto out_err;
8926 }
8927
8928 if (strcmp(args[cur_arg], "table") == 0) {
8929 cur_arg++;
8930 if (!args[cur_arg]) {
8931 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n",
8932 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
8933 free(expr);
8934 goto out_err;
8935 }
8936 /* we copy the table name for now, it will be resolved later */
8937 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
8938 cur_arg++;
8939 }
8940 rule->arg.trk_ctr.expr = expr;
8941 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
William Lallemand73025dd2014-04-24 14:38:37 +02008942 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
8943 char *errmsg = NULL;
8944 cur_arg = 1;
8945 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02008946 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02008947 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02008948 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02008949 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
8950 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8951 free(errmsg);
8952 goto out_err;
8953 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008954 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02008955 action_build_list(&http_res_keywords.list, &trash);
8956 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
8957 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08008958 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02008959 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02008960 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008961 goto out_err;
8962 }
8963
8964 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8965 struct acl_cond *cond;
8966 char *errmsg = NULL;
8967
8968 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8969 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
8970 file, linenum, args[0], errmsg);
8971 free(errmsg);
8972 goto out_err;
8973 }
8974 rule->cond = cond;
8975 }
8976 else if (*args[cur_arg]) {
8977 Alert("parsing [%s:%d]: 'http-response %s' expects"
8978 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8979 file, linenum, args[0], args[cur_arg]);
8980 goto out_err;
8981 }
8982
8983 return rule;
8984 out_err:
8985 free(rule);
8986 return NULL;
8987}
8988
Willy Tarreau4baae242012-12-27 12:00:31 +01008989/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008990 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02008991 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
8992 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01008993 */
8994struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02008995 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01008996{
8997 struct redirect_rule *rule;
8998 int cur_arg;
8999 int type = REDIRECT_TYPE_NONE;
9000 int code = 302;
9001 const char *destination = NULL;
9002 const char *cookie = NULL;
9003 int cookie_set = 0;
9004 unsigned int flags = REDIRECT_FLAG_NONE;
9005 struct acl_cond *cond = NULL;
9006
9007 cur_arg = 0;
9008 while (*(args[cur_arg])) {
9009 if (strcmp(args[cur_arg], "location") == 0) {
9010 if (!*args[cur_arg + 1])
9011 goto missing_arg;
9012
9013 type = REDIRECT_TYPE_LOCATION;
9014 cur_arg++;
9015 destination = args[cur_arg];
9016 }
9017 else if (strcmp(args[cur_arg], "prefix") == 0) {
9018 if (!*args[cur_arg + 1])
9019 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009020 type = REDIRECT_TYPE_PREFIX;
9021 cur_arg++;
9022 destination = args[cur_arg];
9023 }
9024 else if (strcmp(args[cur_arg], "scheme") == 0) {
9025 if (!*args[cur_arg + 1])
9026 goto missing_arg;
9027
9028 type = REDIRECT_TYPE_SCHEME;
9029 cur_arg++;
9030 destination = args[cur_arg];
9031 }
9032 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9033 if (!*args[cur_arg + 1])
9034 goto missing_arg;
9035
9036 cur_arg++;
9037 cookie = args[cur_arg];
9038 cookie_set = 1;
9039 }
9040 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9041 if (!*args[cur_arg + 1])
9042 goto missing_arg;
9043
9044 cur_arg++;
9045 cookie = args[cur_arg];
9046 cookie_set = 0;
9047 }
9048 else if (strcmp(args[cur_arg], "code") == 0) {
9049 if (!*args[cur_arg + 1])
9050 goto missing_arg;
9051
9052 cur_arg++;
9053 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009054 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009055 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009056 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009057 args[cur_arg - 1], args[cur_arg]);
9058 return NULL;
9059 }
9060 }
9061 else if (!strcmp(args[cur_arg],"drop-query")) {
9062 flags |= REDIRECT_FLAG_DROP_QS;
9063 }
9064 else if (!strcmp(args[cur_arg],"append-slash")) {
9065 flags |= REDIRECT_FLAG_APPEND_SLASH;
9066 }
9067 else if (strcmp(args[cur_arg], "if") == 0 ||
9068 strcmp(args[cur_arg], "unless") == 0) {
9069 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9070 if (!cond) {
9071 memprintf(errmsg, "error in condition: %s", *errmsg);
9072 return NULL;
9073 }
9074 break;
9075 }
9076 else {
9077 memprintf(errmsg,
9078 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9079 args[cur_arg]);
9080 return NULL;
9081 }
9082 cur_arg++;
9083 }
9084
9085 if (type == REDIRECT_TYPE_NONE) {
9086 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9087 return NULL;
9088 }
9089
Willy Tarreaube4653b2015-05-28 15:26:58 +02009090 if (dir && type != REDIRECT_TYPE_LOCATION) {
9091 memprintf(errmsg, "response only supports redirect type 'location'");
9092 return NULL;
9093 }
9094
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02009095 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +01009096 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009097 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009098
9099 if (!use_fmt) {
9100 /* old-style static redirect rule */
9101 rule->rdr_str = strdup(destination);
9102 rule->rdr_len = strlen(destination);
9103 }
9104 else {
9105 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009106
9107 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9108 * if prefix == "/", we don't want to add anything, otherwise it
9109 * makes it hard for the user to configure a self-redirection.
9110 */
Godbachd9722032014-12-18 15:44:58 +08009111 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009112 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009113 if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
9114 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 +01009115 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9116 errmsg)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009117 return NULL;
9118 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009119 free(curproxy->conf.lfs_file);
9120 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9121 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009122 }
9123 }
9124
Willy Tarreau4baae242012-12-27 12:00:31 +01009125 if (cookie) {
9126 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9127 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9128 */
9129 rule->cookie_len = strlen(cookie);
9130 if (cookie_set) {
9131 rule->cookie_str = malloc(rule->cookie_len + 10);
9132 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9133 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9134 rule->cookie_len += 9;
9135 } else {
9136 rule->cookie_str = malloc(rule->cookie_len + 21);
9137 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9138 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9139 rule->cookie_len += 20;
9140 }
9141 }
9142 rule->type = type;
9143 rule->code = code;
9144 rule->flags = flags;
9145 LIST_INIT(&rule->list);
9146 return rule;
9147
9148 missing_arg:
9149 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9150 return NULL;
9151}
9152
Willy Tarreau8797c062007-05-07 00:55:35 +02009153/************************************************************************/
9154/* The code below is dedicated to ACL parsing and matching */
9155/************************************************************************/
9156
9157
Willy Tarreau14174bc2012-04-16 14:34:04 +02009158/* This function ensures that the prerequisites for an L7 fetch are ready,
9159 * which means that a request or response is ready. If some data is missing,
9160 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009161 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9162 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009163 *
9164 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009165 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9166 * decide whether or not an HTTP message is present ;
9167 * 0 if the requested data cannot be fetched or if it is certain that
9168 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009169 * 1 if an HTTP message is ready
9170 */
James Rosewell91a41cb2015-09-18 17:11:16 +01009171int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009172 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009173{
Willy Tarreau192252e2015-04-04 01:47:55 +02009174 struct http_txn *txn;
9175 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009176
Willy Tarreaube508f12016-03-10 11:47:01 +01009177 /* Note: it is possible that <s> is NULL when called before stream
9178 * initialization (eg: tcp-request connection), so this function is the
9179 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009180 */
Willy Tarreau192252e2015-04-04 01:47:55 +02009181 if (!s)
9182 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +01009183
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +02009184 if (!s->txn) {
9185 if (unlikely(!http_alloc_txn(s)))
9186 return 0; /* not enough memory */
9187 http_init_txn(s);
9188 }
Willy Tarreau192252e2015-04-04 01:47:55 +02009189 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +02009190 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009191
9192 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009193 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009194
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009195 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009196 /* If the buffer does not leave enough free space at the end,
9197 * we must first realign it.
9198 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009199 if (s->req.buf->p > s->req.buf->data &&
9200 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
9201 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +01009202
Willy Tarreau14174bc2012-04-16 14:34:04 +02009203 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009204 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009205 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009206
9207 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009208 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009209 http_msg_analyzer(msg, &txn->hdr_idx);
9210
9211 /* Still no valid request ? */
9212 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009213 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009214 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009215 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009216 }
9217 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009218 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009219 return 0;
9220 }
9221
9222 /* OK we just got a valid HTTP request. We have some minor
9223 * preparation to perform so that further checks can rely
9224 * on HTTP tests.
9225 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009226
9227 /* If the request was parsed but was too large, we must absolutely
9228 * return an error so that it is not processed. At the moment this
9229 * cannot happen, but if the parsers are to change in the future,
9230 * we want this check to be maintained.
9231 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009232 if (unlikely(s->req.buf->i + s->req.buf->p >
9233 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01009234 msg->err_state = msg->msg_state;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009235 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009236 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009237 return 1;
9238 }
9239
Willy Tarreau9b28e032012-10-12 23:49:43 +02009240 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009241 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02009242 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009243
Willy Tarreau506d0502013-07-06 13:29:24 +02009244 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9245 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009246 }
9247
Willy Tarreau506d0502013-07-06 13:29:24 +02009248 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009249 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009250 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009251
9252 /* otherwise everything's ready for the request */
9253 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009254 else {
9255 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009256 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9257 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009258 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009259 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009260 }
9261
9262 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009263 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009264 return 1;
9265}
Willy Tarreau8797c062007-05-07 00:55:35 +02009266
Willy Tarreau8797c062007-05-07 00:55:35 +02009267/* 1. Check on METHOD
9268 * We use the pre-parsed method if it is known, and store its number as an
9269 * integer. If it is unknown, we use the pointer and the length.
9270 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009271static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009272{
9273 int len, meth;
9274
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009275 len = strlen(text);
9276 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009277
9278 pattern->val.i = meth;
9279 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +02009280 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009281 pattern->len = len;
9282 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009283 else {
9284 pattern->ptr.str = NULL;
9285 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009286 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009287 return 1;
9288}
9289
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009290/* This function fetches the method of current HTTP request and stores
9291 * it in the global pattern struct as a chunk. There are two possibilities :
9292 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9293 * in <len> and <ptr> is NULL ;
9294 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9295 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009296 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009297 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009298static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009299smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009300{
9301 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +01009302 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009303
Willy Tarreau24e32d82012-04-23 23:55:44 +02009304 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009305
Willy Tarreaube508f12016-03-10 11:47:01 +01009306 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009307 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009308 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009309 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009310 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009311 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9312 /* ensure the indexes are not affected */
9313 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009314 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009315 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
9316 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009317 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009318 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009319 return 1;
9320}
9321
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009322/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009323static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009324{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009325 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009326 struct pattern_list *lst;
9327 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009328
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009329 list_for_each_entry(lst, &expr->patterns, list) {
9330 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009331
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009332 /* well-known method */
9333 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009334 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009335 return pattern;
9336 else
9337 continue;
9338 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009339
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009340 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009341 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009342 continue;
9343
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009344 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009345 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
9346 (!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 +01009347 return pattern;
9348 }
9349 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009350}
9351
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009352static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009353smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009354{
Willy Tarreaube508f12016-03-10 11:47:01 +01009355 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009356 char *ptr;
9357 int len;
9358
Willy Tarreauc0239e02012-04-16 14:42:55 +02009359 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009360
Willy Tarreaube508f12016-03-10 11:47:01 +01009361 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009362 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009363 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009364
9365 while ((len-- > 0) && (*ptr++ != '/'));
9366 if (len <= 0)
9367 return 0;
9368
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009369 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009370 smp->data.u.str.str = ptr;
9371 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009372
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009373 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009374 return 1;
9375}
9376
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009377static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009378smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009379{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009380 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009381 char *ptr;
9382 int len;
9383
Willy Tarreauc0239e02012-04-16 14:42:55 +02009384 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009385
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009386 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009387 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9388 return 0;
9389
Willy Tarreau8797c062007-05-07 00:55:35 +02009390 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009391 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009392
9393 while ((len-- > 0) && (*ptr++ != '/'));
9394 if (len <= 0)
9395 return 0;
9396
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009397 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009398 smp->data.u.str.str = ptr;
9399 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009400
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009401 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009402 return 1;
9403}
9404
9405/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009406static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009407smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009408{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009409 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009410 char *ptr;
9411 int len;
9412
Willy Tarreauc0239e02012-04-16 14:42:55 +02009413 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009414
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009415 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009416 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9417 return 0;
9418
Willy Tarreau8797c062007-05-07 00:55:35 +02009419 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009420 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009421
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009422 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009423 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009424 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009425 return 1;
9426}
9427
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009428static int
9429smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
9430{
9431 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
9432 return 0;
9433
9434 if (!smp->strm->unique_id) {
9435 if ((smp->strm->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
9436 return 0;
9437 smp->strm->unique_id[0] = '\0';
9438 }
9439 smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id,
9440 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
9441
9442 smp->data.type = SMP_T_STR;
9443 smp->data.u.str.str = smp->strm->unique_id;
9444 smp->flags = SMP_F_CONST;
9445 return 1;
9446}
9447
Thierry FOURNIERd7d88812017-04-19 15:15:14 +02009448/* Returns a string block containing all headers including the
9449 * empty line wich separes headers from the body. This is useful
9450 * form some headers analysis.
9451 */
9452static int
9453smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
9454{
9455 struct http_msg *msg;
9456 struct hdr_idx *idx;
9457 struct http_txn *txn;
9458
9459 CHECK_HTTP_MESSAGE_FIRST();
9460
9461 txn = smp->strm->txn;
9462 idx = &txn->hdr_idx;
9463 msg = &txn->req;
9464
9465 smp->data.type = SMP_T_STR;
9466 smp->data.u.str.str = msg->chn->buf->p + hdr_idx_first_pos(idx);
9467 smp->data.u.str.len = msg->eoh - hdr_idx_first_pos(idx) + 1 +
9468 (msg->chn->buf->p[msg->eoh] == '\r');
9469
9470 return 1;
9471}
9472
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009473/* Returns the header request in a length/value encoded format.
9474 * This is useful for exchanges with the SPOE.
9475 *
9476 * A "length value" is a multibyte code encoding numbers. It uses the
9477 * SPOE format. The encoding is the following:
9478 *
9479 * Each couple "header name" / "header value" is composed
9480 * like this:
9481 * "length value" "header name bytes"
9482 * "length value" "header value bytes"
9483 * When the last header is reached, the header name and the header
9484 * value are empty. Their length are 0
9485 */
9486static int
9487smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
9488{
9489 struct http_msg *msg;
9490 struct chunk *temp;
9491 struct hdr_idx *idx;
9492 const char *cur_ptr, *cur_next, *p;
9493 int old_idx, cur_idx;
9494 struct hdr_idx_elem *cur_hdr;
9495 const char *hn, *hv;
9496 int hnl, hvl;
9497 int ret;
9498 struct http_txn *txn;
9499 char *buf;
9500 char *end;
9501
9502 CHECK_HTTP_MESSAGE_FIRST();
9503
9504 temp = get_trash_chunk();
9505 buf = temp->str;
9506 end = temp->str + temp->size;
9507
9508 txn = smp->strm->txn;
9509 idx = &txn->hdr_idx;
9510 msg = &txn->req;
9511
9512 /* Build array of headers. */
9513 old_idx = 0;
9514 cur_next = msg->chn->buf->p + hdr_idx_first_pos(idx);
9515 while (1) {
9516 cur_idx = idx->v[old_idx].next;
9517 if (!cur_idx)
9518 break;
9519 old_idx = cur_idx;
9520
9521 cur_hdr = &idx->v[cur_idx];
9522 cur_ptr = cur_next;
9523 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
9524
9525 /* Now we have one full header at cur_ptr of len cur_hdr->len,
9526 * and the next header starts at cur_next. We'll check
9527 * this header in the list as well as against the default
9528 * rule.
9529 */
9530
9531 /* look for ': *'. */
9532 hn = cur_ptr;
9533 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
9534 if (p >= cur_ptr+cur_hdr->len)
9535 continue;
9536 hnl = p - hn;
9537 p++;
9538 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
9539 p++;
9540 if (p >= cur_ptr + cur_hdr->len)
9541 continue;
9542 hv = p;
9543 hvl = cur_ptr + cur_hdr->len-p;
9544
9545 /* encode the header name. */
9546 ret = encode_varint(hnl, &buf, end);
9547 if (ret == -1)
9548 return 0;
9549 if (buf + hnl > end)
9550 return 0;
9551 memcpy(buf, hn, hnl);
9552 buf += hnl;
9553
9554 /* encode and copy the value. */
9555 ret = encode_varint(hvl, &buf, end);
9556 if (ret == -1)
9557 return 0;
9558 if (buf + hvl > end)
9559 return 0;
9560 memcpy(buf, hv, hvl);
9561 buf += hvl;
9562 }
9563
9564 /* encode the end of the header list with empty
9565 * header name and header value.
9566 */
9567 ret = encode_varint(0, &buf, end);
9568 if (ret == -1)
9569 return 0;
9570 ret = encode_varint(0, &buf, end);
9571 if (ret == -1)
9572 return 0;
9573
9574 /* Initialise sample data which will be filled. */
9575 smp->data.type = SMP_T_BIN;
9576 smp->data.u.str.str = temp->str;
9577 smp->data.u.str.len = buf - temp->str;
9578 smp->data.u.str.size = temp->size;
9579
9580 return 1;
9581}
9582
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009583/* returns the longest available part of the body. This requires that the body
9584 * has been waited for using http-buffer-request.
9585 */
9586static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009587smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009588{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009589 struct http_msg *msg;
9590 unsigned long len;
9591 unsigned long block1;
9592 char *body;
9593 struct chunk *temp;
9594
9595 CHECK_HTTP_MESSAGE_FIRST();
9596
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009597 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009598 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009599 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009600 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009601
9602 len = http_body_bytes(msg);
9603 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
9604
9605 block1 = len;
9606 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
9607 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
9608
9609 if (block1 == len) {
9610 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009611 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009612 smp->data.u.str.str = body;
9613 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009614 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
9615 }
9616 else {
9617 /* buffer is wrapped, we need to defragment it */
9618 temp = get_trash_chunk();
9619 memcpy(temp->str, body, block1);
9620 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009621 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009622 smp->data.u.str.str = temp->str;
9623 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009624 smp->flags = SMP_F_VOL_TEST;
9625 }
9626 return 1;
9627}
9628
9629
9630/* returns the available length of the body. This requires that the body
9631 * has been waited for using http-buffer-request.
9632 */
9633static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009634smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009635{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009636 struct http_msg *msg;
9637
9638 CHECK_HTTP_MESSAGE_FIRST();
9639
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009640 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009641 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009642 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009643 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009644
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009645 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009646 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009647
9648 smp->flags = SMP_F_VOL_TEST;
9649 return 1;
9650}
9651
9652
9653/* returns the advertised length of the body, or the advertised size of the
9654 * chunks available in the buffer. This requires that the body has been waited
9655 * for using http-buffer-request.
9656 */
9657static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009658smp_fetch_body_size(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
9662 CHECK_HTTP_MESSAGE_FIRST();
9663
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009664 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009665 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009666 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009667 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009668
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009669 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009670 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009671
9672 smp->flags = SMP_F_VOL_TEST;
9673 return 1;
9674}
9675
9676
Willy Tarreau8797c062007-05-07 00:55:35 +02009677/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009678static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009679smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009680{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009681 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009682
Willy Tarreauc0239e02012-04-16 14:42:55 +02009683 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009684
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009685 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009686 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009687 smp->data.u.str.len = txn->req.sl.rq.u_l;
9688 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009689 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009690 return 1;
9691}
9692
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009693static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009694smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009695{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009696 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009697 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009698
Willy Tarreauc0239e02012-04-16 14:42:55 +02009699 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009700
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009701 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009702 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 +02009703 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009704 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009705
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009706 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009707 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009708 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009709 return 1;
9710}
9711
9712static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009713smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009714{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009715 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009716 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009717
Willy Tarreauc0239e02012-04-16 14:42:55 +02009718 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009719
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009720 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009721 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 +02009722 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
9723 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009724
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009725 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009726 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009727 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009728 return 1;
9729}
9730
Willy Tarreau185b5c42012-04-26 15:11:51 +02009731/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9732 * Accepts an optional argument of type string containing the header field name,
9733 * and an optional argument of type signed or unsigned integer to request an
9734 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009735 * headers are considered from the first one. It does not stop on commas and
9736 * returns full lines instead (useful for User-Agent or Date for example).
9737 */
9738static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009739smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009740{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009741 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009742 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +02009743 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009744 int occ = 0;
9745 const char *name_str = NULL;
9746 int name_len = 0;
9747
9748 if (!ctx) {
9749 /* first call */
9750 ctx = &static_hdr_ctx;
9751 ctx->idx = 0;
9752 smp->ctx.a[0] = ctx;
9753 }
9754
9755 if (args) {
9756 if (args[0].type != ARGT_STR)
9757 return 0;
9758 name_str = args[0].data.str.str;
9759 name_len = args[0].data.str.len;
9760
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02009761 if (args[1].type == ARGT_SINT)
9762 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009763 }
9764
9765 CHECK_HTTP_MESSAGE_FIRST();
9766
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009767 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009768 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 +02009769
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009770 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9771 /* search for header from the beginning */
9772 ctx->idx = 0;
9773
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009774 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009775 /* no explicit occurrence and single fetch => last header by default */
9776 occ = -1;
9777
9778 if (!occ)
9779 /* prepare to report multiple occurrences for ACL fetches */
9780 smp->flags |= SMP_F_NOT_LAST;
9781
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009782 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009783 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009784 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 +02009785 return 1;
9786
9787 smp->flags &= ~SMP_F_NOT_LAST;
9788 return 0;
9789}
9790
9791/* 6. Check on HTTP header count. The number of occurrences is returned.
9792 * Accepts exactly 1 argument of type string. It does not stop on commas and
9793 * returns full lines instead (useful for User-Agent or Date for example).
9794 */
9795static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009796smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009797{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009798 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009799 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +02009800 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009801 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +02009802 const char *name = NULL;
9803 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009804
Willy Tarreau601a4d12015-04-01 19:16:09 +02009805 if (args && args->type == ARGT_STR) {
9806 name = args->data.str.str;
9807 len = args->data.str.len;
9808 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009809
9810 CHECK_HTTP_MESSAGE_FIRST();
9811
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009812 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009813 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 +02009814
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009815 ctx.idx = 0;
9816 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +02009817 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009818 cnt++;
9819
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009820 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009821 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009822 smp->flags = SMP_F_VOL_HDR;
9823 return 1;
9824}
9825
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009826static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009827smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009828{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009829 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009830 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +02009831 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009832 struct chunk *temp;
9833 char del = ',';
9834
9835 if (args && args->type == ARGT_STR)
9836 del = *args[0].data.str.str;
9837
9838 CHECK_HTTP_MESSAGE_FIRST();
9839
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009840 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009841 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 +02009842
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009843 temp = get_trash_chunk();
9844
9845 ctx.idx = 0;
9846 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
9847 if (temp->len)
9848 temp->str[temp->len++] = del;
9849 memcpy(temp->str + temp->len, ctx.line, ctx.del);
9850 temp->len += ctx.del;
9851 }
9852
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009853 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009854 smp->data.u.str.str = temp->str;
9855 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009856 smp->flags = SMP_F_VOL_HDR;
9857 return 1;
9858}
9859
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009860/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9861 * Accepts an optional argument of type string containing the header field name,
9862 * and an optional argument of type signed or unsigned integer to request an
9863 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009864 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009865 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009866static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009867smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009868{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009869 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009870 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +02009871 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009872 int occ = 0;
9873 const char *name_str = NULL;
9874 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009875
Willy Tarreaua890d072013-04-02 12:01:06 +02009876 if (!ctx) {
9877 /* first call */
9878 ctx = &static_hdr_ctx;
9879 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02009880 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009881 }
9882
Willy Tarreau185b5c42012-04-26 15:11:51 +02009883 if (args) {
9884 if (args[0].type != ARGT_STR)
9885 return 0;
9886 name_str = args[0].data.str.str;
9887 name_len = args[0].data.str.len;
9888
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02009889 if (args[1].type == ARGT_SINT)
9890 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009891 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009892
Willy Tarreaue333ec92012-04-16 16:26:40 +02009893 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02009894
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009895 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009896 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 +02009897
Willy Tarreau185b5c42012-04-26 15:11:51 +02009898 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009899 /* search for header from the beginning */
9900 ctx->idx = 0;
9901
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009902 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +02009903 /* no explicit occurrence and single fetch => last header by default */
9904 occ = -1;
9905
9906 if (!occ)
9907 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02009908 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01009909
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009910 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009911 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009912 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 +02009913 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009914
Willy Tarreau37406352012-04-23 16:16:37 +02009915 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009916 return 0;
9917}
9918
Willy Tarreauc11416f2007-06-17 16:58:38 +02009919/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02009920 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009921 */
9922static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009923smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009924{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009925 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009926 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +02009927 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009928 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +02009929 const char *name = NULL;
9930 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02009931
Willy Tarreau601a4d12015-04-01 19:16:09 +02009932 if (args && args->type == ARGT_STR) {
9933 name = args->data.str.str;
9934 len = args->data.str.len;
9935 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009936
Willy Tarreaue333ec92012-04-16 16:26:40 +02009937 CHECK_HTTP_MESSAGE_FIRST();
9938
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009939 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009940 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 +02009941
Willy Tarreau33a7e692007-06-10 19:45:56 +02009942 ctx.idx = 0;
9943 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +02009944 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009945 cnt++;
9946
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009947 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009948 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009949 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009950 return 1;
9951}
9952
Willy Tarreau185b5c42012-04-26 15:11:51 +02009953/* Fetch an HTTP header's integer value. The integer value is returned. It
9954 * takes a mandatory argument of type string and an optional one of type int
9955 * to designate a specific occurrence. It returns an unsigned integer, which
9956 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02009957 */
9958static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009959smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009960{
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009961 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +02009962
Willy Tarreauf853c462012-04-23 18:53:56 +02009963 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009964 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009965 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +02009966 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02009967
Willy Tarreaud53e2422012-04-16 17:21:11 +02009968 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009969}
9970
Cyril Bonté69fa9922012-10-25 00:01:06 +02009971/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
9972 * and an optional one of type int to designate a specific occurrence.
9973 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02009974 */
9975static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009976smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +02009977{
Willy Tarreaud53e2422012-04-16 17:21:11 +02009978 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009979
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009980 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009981 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009982 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02009983 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02009984 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +01009985 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009986 if (smp->data.u.str.len < temp->size - 1) {
9987 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
9988 temp->str[smp->data.u.str.len] = '\0';
9989 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009990 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +02009991 break;
9992 }
9993 }
9994 }
9995
Willy Tarreaud53e2422012-04-16 17:21:11 +02009996 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02009997 if (!(smp->flags & SMP_F_NOT_LAST))
9998 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02009999 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010000 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010001}
10002
Willy Tarreau737b0c12007-06-10 21:28:46 +020010003/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10004 * the first '/' after the possible hostname, and ends before the possible '?'.
10005 */
10006static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010007smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010008{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010009 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010010 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010011
Willy Tarreauc0239e02012-04-16 14:42:55 +020010012 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010013
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010014 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010015 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010016 ptr = http_get_path(txn);
10017 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010018 return 0;
10019
10020 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010021 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010022 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010023
10024 while (ptr < end && *ptr != '?')
10025 ptr++;
10026
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010027 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010028 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010029 return 1;
10030}
10031
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010032/* This produces a concatenation of the first occurrence of the Host header
10033 * followed by the path component if it begins with a slash ('/'). This means
10034 * that '*' will not be added, resulting in exactly the first Host entry.
10035 * If no Host header is found, then the path is returned as-is. The returned
10036 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010037 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010038 */
10039static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010040smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010041{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010042 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010043 char *ptr, *end, *beg;
10044 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010045 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010046
10047 CHECK_HTTP_MESSAGE_FIRST();
10048
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010049 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010050 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010051 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010052 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010053
10054 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010055 temp = get_trash_chunk();
10056 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010057 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010058 smp->data.u.str.str = temp->str;
10059 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010060
10061 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010062 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010063 beg = http_get_path(txn);
10064 if (!beg)
10065 beg = end;
10066
10067 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10068
10069 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010070 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10071 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010072 }
10073
10074 smp->flags = SMP_F_VOL_1ST;
10075 return 1;
10076}
10077
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010078/* This produces a 32-bit hash of the concatenation of the first occurrence of
10079 * the Host header followed by the path component if it begins with a slash ('/').
10080 * This means that '*' will not be added, resulting in exactly the first Host
10081 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010082 * is hashed using the path hash followed by a full avalanche hash and provides a
10083 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010084 * high-traffic sites without having to store whole paths.
10085 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010086int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010087smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010088{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010089 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010090 struct hdr_ctx ctx;
10091 unsigned int hash = 0;
10092 char *ptr, *beg, *end;
10093 int len;
10094
10095 CHECK_HTTP_MESSAGE_FIRST();
10096
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010097 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010098 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010099 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010100 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10101 ptr = ctx.line + ctx.val;
10102 len = ctx.vlen;
10103 while (len--)
10104 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10105 }
10106
10107 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010108 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010109 beg = http_get_path(txn);
10110 if (!beg)
10111 beg = end;
10112
10113 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10114
10115 if (beg < ptr && *beg == '/') {
10116 while (beg < ptr)
10117 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10118 }
10119 hash = full_hash(hash);
10120
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010121 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010122 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010123 smp->flags = SMP_F_VOL_1ST;
10124 return 1;
10125}
10126
Willy Tarreau4a550602012-12-09 14:53:32 +010010127/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010128 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10129 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10130 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010131 * that in environments where IPv6 is insignificant, truncating the output to
10132 * 8 bytes would still work.
10133 */
10134static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010135smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010136{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010137 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010138 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010139
10140 if (!cli_conn)
10141 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010142
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010143 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010144 return 0;
10145
Willy Tarreau47ca5452012-12-23 20:22:19 +010010146 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010147 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010148 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010149
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010150 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010151 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010152 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010153 temp->len += 4;
10154 break;
10155 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010156 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010157 temp->len += 16;
10158 break;
10159 default:
10160 return 0;
10161 }
10162
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010163 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010164 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010165 return 1;
10166}
10167
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010168/* Extracts the query string, which comes after the question mark '?'. If no
10169 * question mark is found, nothing is returned. Otherwise it returns a sample
10170 * of type string carrying the whole query string.
10171 */
10172static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010173smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010174{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010175 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010176 char *ptr, *end;
10177
10178 CHECK_HTTP_MESSAGE_FIRST();
10179
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010180 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010181 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10182 end = ptr + txn->req.sl.rq.u_l;
10183
10184 /* look up the '?' */
10185 do {
10186 if (ptr == end)
10187 return 0;
10188 } while (*ptr++ != '?');
10189
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010190 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010191 smp->data.u.str.str = ptr;
10192 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010193 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10194 return 1;
10195}
10196
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010197static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010198smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010199{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010200 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10201 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10202 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010203
Willy Tarreau24e32d82012-04-23 23:55:44 +020010204 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010205
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010206 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010207 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010208 return 1;
10209}
10210
Willy Tarreau7f18e522010-10-22 20:04:13 +020010211/* return a valid test if the current request is the first one on the connection */
10212static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010213smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010214{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010215 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010216 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010217 return 1;
10218}
10219
Willy Tarreau34db1082012-04-19 17:16:54 +020010220/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010221static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010222smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010223{
10224
Willy Tarreau24e32d82012-04-23 23:55:44 +020010225 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010226 return 0;
10227
Willy Tarreauc0239e02012-04-16 14:42:55 +020010228 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010229
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010230 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010231 return 0;
10232
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010233 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010234 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010235 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010236 return 1;
10237}
Willy Tarreau8797c062007-05-07 00:55:35 +020010238
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010239/* Accepts exactly 1 argument of type userlist */
10240static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010241smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010242{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010243 if (!args || args->type != ARGT_USR)
10244 return 0;
10245
10246 CHECK_HTTP_MESSAGE_FIRST();
10247
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010248 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010249 return 0;
10250
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010251 /* if the user does not belong to the userlist or has a wrong password,
10252 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010253 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010254 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010255 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10256 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010257 return 0;
10258
10259 /* pat_match_auth() will need the user list */
10260 smp->ctx.a[0] = args->data.usr;
10261
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010262 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010263 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010264 smp->data.u.str.str = smp->strm->txn->auth.user;
10265 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010266
10267 return 1;
10268}
10269
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010270/* Try to find the next occurrence of a cookie name in a cookie header value.
10271 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10272 * the cookie value is returned into *value and *value_l, and the function
10273 * returns a pointer to the next pointer to search from if the value was found.
10274 * Otherwise if the cookie was not found, NULL is returned and neither value
10275 * nor value_l are touched. The input <hdr> string should first point to the
10276 * header's value, and the <hdr_end> pointer must point to the first character
10277 * not part of the value. <list> must be non-zero if value may represent a list
10278 * of values (cookie headers). This makes it faster to abort parsing when no
10279 * list is expected.
10280 */
David Carlier4686f792015-09-25 14:10:50 +010010281char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010282extract_cookie_value(char *hdr, const char *hdr_end,
10283 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010284 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010285{
10286 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10287 char *next;
10288
10289 /* we search at least a cookie name followed by an equal, and more
10290 * generally something like this :
10291 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10292 */
10293 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10294 /* Iterate through all cookies on this line */
10295
Willy Tarreau2235b262016-11-05 15:50:20 +010010296 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010297 att_beg++;
10298
10299 /* find att_end : this is the first character after the last non
10300 * space before the equal. It may be equal to hdr_end.
10301 */
10302 equal = att_end = att_beg;
10303
10304 while (equal < hdr_end) {
10305 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10306 break;
Willy Tarreau2235b262016-11-05 15:50:20 +010010307 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010308 continue;
10309 att_end = equal;
10310 }
10311
10312 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10313 * is between <att_beg> and <equal>, both may be identical.
10314 */
10315
10316 /* look for end of cookie if there is an equal sign */
10317 if (equal < hdr_end && *equal == '=') {
10318 /* look for the beginning of the value */
10319 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +010010320 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010321 val_beg++;
10322
10323 /* find the end of the value, respecting quotes */
10324 next = find_cookie_value_end(val_beg, hdr_end);
10325
10326 /* make val_end point to the first white space or delimitor after the value */
10327 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +010010328 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010329 val_end--;
10330 } else {
10331 val_beg = val_end = next = equal;
10332 }
10333
10334 /* We have nothing to do with attributes beginning with '$'. However,
10335 * they will automatically be removed if a header before them is removed,
10336 * since they're supposed to be linked together.
10337 */
10338 if (*att_beg == '$')
10339 continue;
10340
10341 /* Ignore cookies with no equal sign */
10342 if (equal == next)
10343 continue;
10344
10345 /* Now we have the cookie name between att_beg and att_end, and
10346 * its value between val_beg and val_end.
10347 */
10348
10349 if (att_end - att_beg == cookie_name_l &&
10350 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10351 /* let's return this value and indicate where to go on from */
10352 *value = val_beg;
10353 *value_l = val_end - val_beg;
10354 return next + 1;
10355 }
10356
10357 /* Set-Cookie headers only have the name in the first attr=value part */
10358 if (!list)
10359 break;
10360 }
10361
10362 return NULL;
10363}
10364
William Lallemanda43ba4e2014-01-28 18:14:25 +010010365/* Fetch a captured HTTP request header. The index is the position of
10366 * the "capture" option in the configuration file
10367 */
10368static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010369smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010370{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010371 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010372 int idx;
10373
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010374 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010375 return 0;
10376
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010377 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010378
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010379 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 +010010380 return 0;
10381
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010382 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010383 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010384 smp->data.u.str.str = smp->strm->req_cap[idx];
10385 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010386
10387 return 1;
10388}
10389
10390/* Fetch a captured HTTP response header. The index is the position of
10391 * the "capture" option in the configuration file
10392 */
10393static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010394smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010395{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010396 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010397 int idx;
10398
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010399 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010400 return 0;
10401
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010402 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010403
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010404 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 +010010405 return 0;
10406
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010407 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010408 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010409 smp->data.u.str.str = smp->strm->res_cap[idx];
10410 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010411
10412 return 1;
10413}
10414
William Lallemand65ad6e12014-01-31 15:08:02 +010010415/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10416static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010417smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010418{
10419 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010420 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010010421 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010422
Willy Tarreau15e91e12015-04-04 00:52:09 +020010423 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010424 return 0;
10425
William Lallemand96a77852014-02-05 00:30:02 +010010426 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010427
William Lallemand96a77852014-02-05 00:30:02 +010010428 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10429 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010430
William Lallemand96a77852014-02-05 00:30:02 +010010431 temp = get_trash_chunk();
10432 temp->str = txn->uri;
10433 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010434 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010435 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010436 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010437
10438 return 1;
10439
10440}
10441
10442/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10443static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010444smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010445{
10446 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010447 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010010448 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010449
Willy Tarreau15e91e12015-04-04 00:52:09 +020010450 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010451 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010452
William Lallemand65ad6e12014-01-31 15:08:02 +010010453 ptr = txn->uri;
10454
10455 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10456 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010457
William Lallemand65ad6e12014-01-31 15:08:02 +010010458 if (!*ptr)
10459 return 0;
10460
10461 ptr++; /* skip the space */
10462
10463 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010464 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010465 if (!ptr)
10466 return 0;
10467 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10468 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010469
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010470 smp->data.u.str = *temp;
10471 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010472 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010473 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010474
10475 return 1;
10476}
10477
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010478/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10479 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10480 */
10481static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010482smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010483{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010484 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010485
Willy Tarreau15e91e12015-04-04 00:52:09 +020010486 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010487 return 0;
10488
10489 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010490 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010491 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010492 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010493
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010494 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010495 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010496 smp->flags = SMP_F_CONST;
10497 return 1;
10498
10499}
10500
10501/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10502 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10503 */
10504static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010505smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010506{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010507 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010508
Willy Tarreau15e91e12015-04-04 00:52:09 +020010509 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010510 return 0;
10511
10512 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010513 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010514 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010515 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010516
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010517 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010518 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010519 smp->flags = SMP_F_CONST;
10520 return 1;
10521
10522}
10523
William Lallemand65ad6e12014-01-31 15:08:02 +010010524
Willy Tarreaue333ec92012-04-16 16:26:40 +020010525/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010526 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010527 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010528 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010529 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010530 * Accepts exactly 1 argument of type string. If the input options indicate
10531 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010532 * The returned sample is of type CSTR. Can be used to parse cookies in other
10533 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010534 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010535int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010536{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010537 struct http_txn *txn;
10538 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010539 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010540 const struct http_msg *msg;
10541 const char *hdr_name;
10542 int hdr_name_len;
10543 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010544 int occ = 0;
10545 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010546
Willy Tarreau24e32d82012-04-23 23:55:44 +020010547 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010548 return 0;
10549
Willy Tarreaua890d072013-04-02 12:01:06 +020010550 if (!ctx) {
10551 /* first call */
10552 ctx = &static_hdr_ctx;
10553 ctx->idx = 0;
10554 smp->ctx.a[2] = ctx;
10555 }
10556
Willy Tarreaue333ec92012-04-16 16:26:40 +020010557 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010558
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010559 txn = smp->strm->txn;
10560 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010561
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010562 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010563 msg = &txn->req;
10564 hdr_name = "Cookie";
10565 hdr_name_len = 6;
10566 } else {
10567 msg = &txn->rsp;
10568 hdr_name = "Set-Cookie";
10569 hdr_name_len = 10;
10570 }
10571
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010572 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020010573 /* no explicit occurrence and single fetch => last cookie by default */
10574 occ = -1;
10575
10576 /* OK so basically here, either we want only one value and it's the
10577 * last one, or we want to iterate over all of them and we fetch the
10578 * next one.
10579 */
10580
Willy Tarreau9b28e032012-10-12 23:49:43 +020010581 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010582 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010583 /* search for the header from the beginning, we must first initialize
10584 * the search parameters.
10585 */
Willy Tarreau37406352012-04-23 16:16:37 +020010586 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010587 ctx->idx = 0;
10588 }
10589
Willy Tarreau28376d62012-04-26 21:26:10 +020010590 smp->flags |= SMP_F_VOL_HDR;
10591
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010592 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010593 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10594 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010595 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10596 goto out;
10597
Willy Tarreau24e32d82012-04-23 23:55:44 +020010598 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010599 continue;
10600
Willy Tarreau37406352012-04-23 16:16:37 +020010601 smp->ctx.a[0] = ctx->line + ctx->val;
10602 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010603 }
10604
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010605 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010606 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010607 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010608 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010609 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010610 &smp->data.u.str.str,
10611 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010612 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010613 found = 1;
10614 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010615 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020010616 smp->flags |= SMP_F_NOT_LAST;
10617 return 1;
10618 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010619 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010620 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010621 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010622 /* all cookie headers and values were scanned. If we're looking for the
10623 * last occurrence, we may return it now.
10624 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010625 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010626 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010627 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010628}
10629
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010630/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010631 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010632 * multiple cookies may be parsed on the same line. The returned sample is of
10633 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010634 */
10635static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010636smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010637{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010638 struct http_txn *txn;
10639 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010640 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010641 const struct http_msg *msg;
10642 const char *hdr_name;
10643 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010644 int cnt;
10645 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010646 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010647
Willy Tarreau24e32d82012-04-23 23:55:44 +020010648 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010649 return 0;
10650
Willy Tarreaue333ec92012-04-16 16:26:40 +020010651 CHECK_HTTP_MESSAGE_FIRST();
10652
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010653 txn = smp->strm->txn;
10654 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010655
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010656 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010657 msg = &txn->req;
10658 hdr_name = "Cookie";
10659 hdr_name_len = 6;
10660 } else {
10661 msg = &txn->rsp;
10662 hdr_name = "Set-Cookie";
10663 hdr_name_len = 10;
10664 }
10665
Willy Tarreau9b28e032012-10-12 23:49:43 +020010666 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010667 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010668 ctx.idx = 0;
10669 cnt = 0;
10670
10671 while (1) {
10672 /* Note: val_beg == NULL every time we need to fetch a new header */
10673 if (!val_beg) {
10674 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10675 break;
10676
Willy Tarreau24e32d82012-04-23 23:55:44 +020010677 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010678 continue;
10679
10680 val_beg = ctx.line + ctx.val;
10681 val_end = val_beg + ctx.vlen;
10682 }
10683
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010684 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010685 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010686 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010687 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010688 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010689 &smp->data.u.str.str,
10690 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010691 cnt++;
10692 }
10693 }
10694
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010695 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010696 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010697 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010698 return 1;
10699}
10700
Willy Tarreau51539362012-05-08 12:46:28 +020010701/* Fetch an cookie's integer value. The integer value is returned. It
10702 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10703 */
10704static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010705smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020010706{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010707 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020010708
10709 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010710 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010711 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020010712 }
10713
10714 return ret;
10715}
10716
Willy Tarreau8797c062007-05-07 00:55:35 +020010717/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010718/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010719/************************************************************************/
10720
David Cournapeau16023ee2010-12-23 20:55:41 +090010721/*
10722 * Given a path string and its length, find the position of beginning of the
10723 * query string. Returns NULL if no query string is found in the path.
10724 *
10725 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
10726 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010727 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090010728 */
bedis4c75cca2012-10-05 08:38:24 +020010729static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010730{
10731 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020010732
bedis4c75cca2012-10-05 08:38:24 +020010733 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090010734 return p ? p + 1 : NULL;
10735}
10736
bedis4c75cca2012-10-05 08:38:24 +020010737static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010738{
bedis4c75cca2012-10-05 08:38:24 +020010739 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090010740}
10741
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010742/* after increasing a pointer value, it can exceed the first buffer
10743 * size. This function transform the value of <ptr> according with
10744 * the expected position. <chunks> is an array of the one or two
10745 * avalaible chunks. The first value is the start of the first chunk,
10746 * the second value if the end+1 of the first chunks. The third value
10747 * is NULL or the start of the second chunk and the fourth value is
10748 * the end+1 of the second chunk. The function returns 1 if does a
10749 * wrap, else returns 0.
10750 */
10751static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
10752{
10753 if (*ptr < chunks[1])
10754 return 0;
10755 if (!chunks[2])
10756 return 0;
10757 *ptr = chunks[2] + ( *ptr - chunks[1] );
10758 return 1;
10759}
10760
David Cournapeau16023ee2010-12-23 20:55:41 +090010761/*
10762 * Given a url parameter, find the starting position of the first occurence,
10763 * or NULL if the parameter is not found.
10764 *
10765 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
10766 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010767 *
Willy Tarreauf6625822015-12-27 14:51:01 +010010768 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010769 * or the second chunk. The caller must be check the position before using the
10770 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090010771 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010772static const char *
10773find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010774 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020010775 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010776{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010777 const char *pos, *last, *equal;
10778 const char **bufs = chunks;
10779 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090010780
David Cournapeau16023ee2010-12-23 20:55:41 +090010781
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010782 pos = bufs[0];
10783 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010010784 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010785 /* Check the equal. */
10786 equal = pos + url_param_name_l;
10787 if (fix_pointer_if_wrap(chunks, &equal)) {
10788 if (equal >= chunks[3])
10789 return NULL;
10790 } else {
10791 if (equal >= chunks[1])
10792 return NULL;
10793 }
10794 if (*equal == '=') {
10795 if (pos + url_param_name_l > last) {
10796 /* process wrap case, we detect a wrap. In this case, the
10797 * comparison is performed in two parts.
10798 */
10799
10800 /* This is the end, we dont have any other chunk. */
10801 if (bufs != chunks || !bufs[2])
10802 return NULL;
10803
10804 /* Compute the length of each part of the comparison. */
10805 l1 = last - pos;
10806 l2 = url_param_name_l - l1;
10807
10808 /* The second buffer is too short to contain the compared string. */
10809 if (bufs[2] + l2 > bufs[3])
10810 return NULL;
10811
10812 if (memcmp(pos, url_param_name, l1) == 0 &&
10813 memcmp(bufs[2], url_param_name+l1, l2) == 0)
10814 return pos;
10815
10816 /* Perform wrapping and jump the string who fail the comparison. */
10817 bufs += 2;
10818 pos = bufs[0] + l2;
10819 last = bufs[1];
10820
10821 } else {
10822 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010010823 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
10824 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010825 pos += url_param_name_l + 1;
10826 if (fix_pointer_if_wrap(chunks, &pos))
10827 last = bufs[2];
10828 }
10829 }
10830
10831 while (1) {
10832 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010010833 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010834 pos++;
10835 if (pos < last)
10836 break;
10837 /* process buffer wrapping. */
10838 if (bufs != chunks || !bufs[2])
10839 return NULL;
10840 bufs += 2;
10841 pos = bufs[0];
10842 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090010843 }
David Cournapeau16023ee2010-12-23 20:55:41 +090010844 pos++;
10845 }
10846 return NULL;
10847}
10848
10849/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010010850 * Given a url parameter name and a query string, find the next value.
10851 * An empty url_param_name matches the first available parameter.
10852 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
10853 * respectively provide a pointer to the value and its end.
10854 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090010855 */
10856static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010857find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010858 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010859 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010860{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010861 const char *arg_start, *qs_end;
10862 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090010863
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010864 arg_start = chunks[0];
10865 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010866 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010867 /* Looks for an argument name. */
10868 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010869 url_param_name, url_param_name_l,
10870 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010871 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010010872 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010873 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010874 }
David Cournapeau16023ee2010-12-23 20:55:41 +090010875 if (!arg_start)
10876 return 0;
10877
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010878 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010879 while (1) {
10880 /* looks for the first argument. */
10881 value_start = memchr(arg_start, '=', qs_end - arg_start);
10882 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010883 /* Check for wrapping. */
10884 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010010885 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010886 chunks[2]) {
10887 arg_start = chunks[2];
10888 qs_end = chunks[3];
10889 continue;
10890 }
10891 return 0;
10892 }
10893 break;
10894 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010895 value_start++;
10896 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010897 else {
10898 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010899 value_start = arg_start + url_param_name_l + 1;
10900
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010901 /* Check for pointer wrapping. */
10902 if (fix_pointer_if_wrap(chunks, &value_start)) {
10903 /* Update the end pointer. */
10904 qs_end = chunks[3];
10905
10906 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010010907 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010908 return 0;
10909 }
10910 }
10911
David Cournapeau16023ee2010-12-23 20:55:41 +090010912 value_end = value_start;
10913
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010914 while (1) {
10915 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
10916 value_end++;
10917 if (value_end < qs_end)
10918 break;
10919 /* process buffer wrapping. */
10920 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010010921 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010922 chunks[2]) {
10923 value_end = chunks[2];
10924 qs_end = chunks[3];
10925 continue;
10926 }
10927 break;
10928 }
David Cournapeau16023ee2010-12-23 20:55:41 +090010929
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010930 *vstart = value_start;
10931 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010010932 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090010933}
10934
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010935/* This scans a URL-encoded query string. It takes an optionally wrapping
10936 * string whose first contigous chunk has its beginning in ctx->a[0] and end
10937 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
10938 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010939 */
David Cournapeau16023ee2010-12-23 20:55:41 +090010940static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010941smp_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 +090010942{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010943 const char *vstart, *vend;
10944 struct chunk *temp;
10945 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020010946
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010947 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020010948 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010949 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020010950 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010951 return 0;
10952
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010953 /* Create sample. If the value is contiguous, return the pointer as CONST,
10954 * if the value is wrapped, copy-it in a buffer.
10955 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010956 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010957 if (chunks[2] &&
10958 vstart >= chunks[0] && vstart <= chunks[1] &&
10959 vend >= chunks[2] && vend <= chunks[3]) {
10960 /* Wrapped case. */
10961 temp = get_trash_chunk();
10962 memcpy(temp->str, vstart, chunks[1] - vstart);
10963 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010964 smp->data.u.str.str = temp->str;
10965 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010966 } else {
10967 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010968 smp->data.u.str.str = (char *)vstart;
10969 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010970 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10971 }
10972
10973 /* Update context, check wrapping. */
10974 chunks[0] = vend;
10975 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
10976 chunks[1] = chunks[3];
10977 chunks[2] = NULL;
10978 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010979
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010980 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010981 smp->flags |= SMP_F_NOT_LAST;
10982
David Cournapeau16023ee2010-12-23 20:55:41 +090010983 return 1;
10984}
10985
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010986/* This function iterates over each parameter of the query string. It uses
10987 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010988 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
10989 * An optional parameter name is passed in args[0], otherwise any parameter is
10990 * considered. It supports an optional delimiter argument for the beginning of
10991 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010992 */
10993static int
10994smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
10995{
10996 struct http_msg *msg;
10997 char delim = '?';
10998 const char *name;
10999 int name_len;
11000
Dragan Dosen26f77e52015-05-25 10:02:11 +020011001 if (!args ||
11002 (args[0].type && args[0].type != ARGT_STR) ||
11003 (args[1].type && args[1].type != ARGT_STR))
11004 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011005
Dragan Dosen26f77e52015-05-25 10:02:11 +020011006 name = "";
11007 name_len = 0;
11008 if (args->type == ARGT_STR) {
11009 name = args->data.str.str;
11010 name_len = args->data.str.len;
11011 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011012
Dragan Dosen26f77e52015-05-25 10:02:11 +020011013 if (args[1].type)
11014 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011015
Dragan Dosen26f77e52015-05-25 10:02:11 +020011016 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011017 CHECK_HTTP_MESSAGE_FIRST();
11018
11019 msg = &smp->strm->txn->req;
11020
11021 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11022 msg->sl.rq.u_l, delim);
11023 if (!smp->ctx.a[0])
11024 return 0;
11025
11026 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011027
11028 /* Assume that the context is filled with NULL pointer
11029 * before the first call.
11030 * smp->ctx.a[2] = NULL;
11031 * smp->ctx.a[3] = NULL;
11032 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011033 }
11034
11035 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11036}
11037
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011038/* This function iterates over each parameter of the body. This requires
11039 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011040 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11041 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11042 * optional second part if the body wraps at the end of the buffer. An optional
11043 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011044 */
11045static int
11046smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11047{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011048 struct http_msg *msg;
11049 unsigned long len;
11050 unsigned long block1;
11051 char *body;
11052 const char *name;
11053 int name_len;
11054
11055 if (!args || (args[0].type && args[0].type != ARGT_STR))
11056 return 0;
11057
11058 name = "";
11059 name_len = 0;
11060 if (args[0].type == ARGT_STR) {
11061 name = args[0].data.str.str;
11062 name_len = args[0].data.str.len;
11063 }
11064
11065 if (!smp->ctx.a[0]) { // first call, find the query string
11066 CHECK_HTTP_MESSAGE_FIRST();
11067
11068 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010011069 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011070 else
Willy Tarreaube508f12016-03-10 11:47:01 +010011071 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011072
11073 len = http_body_bytes(msg);
11074 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11075
11076 block1 = len;
11077 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11078 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11079
11080 if (block1 == len) {
11081 /* buffer is not wrapped (or empty) */
11082 smp->ctx.a[0] = body;
11083 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011084
11085 /* Assume that the context is filled with NULL pointer
11086 * before the first call.
11087 * smp->ctx.a[2] = NULL;
11088 * smp->ctx.a[3] = NULL;
11089 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011090 }
11091 else {
11092 /* buffer is wrapped, we need to defragment it */
11093 smp->ctx.a[0] = body;
11094 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011095 smp->ctx.a[2] = msg->chn->buf->data;
11096 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011097 }
11098 }
11099 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11100}
11101
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011102/* Return the signed integer value for the specified url parameter (see url_param
11103 * above).
11104 */
11105static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011106smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011107{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011108 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011109
11110 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011111 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011112 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011113 }
11114
11115 return ret;
11116}
11117
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011118/* This produces a 32-bit hash of the concatenation of the first occurrence of
11119 * the Host header followed by the path component if it begins with a slash ('/').
11120 * This means that '*' will not be added, resulting in exactly the first Host
11121 * entry. If no Host header is found, then the path is used. The resulting value
11122 * is hashed using the url hash followed by a full avalanche hash and provides a
11123 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11124 * high-traffic sites without having to store whole paths.
11125 * this differs from the base32 functions in that it includes the url parameters
11126 * as well as the path
11127 */
11128static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011129smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011130{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011131 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011132 struct hdr_ctx ctx;
11133 unsigned int hash = 0;
11134 char *ptr, *beg, *end;
11135 int len;
11136
11137 CHECK_HTTP_MESSAGE_FIRST();
11138
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011139 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011140 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011141 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011142 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11143 ptr = ctx.line + ctx.val;
11144 len = ctx.vlen;
11145 while (len--)
11146 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11147 }
11148
11149 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011150 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 +000011151 beg = http_get_path(txn);
11152 if (!beg)
11153 beg = end;
11154
11155 for (ptr = beg; ptr < end ; ptr++);
11156
11157 if (beg < ptr && *beg == '/') {
11158 while (beg < ptr)
11159 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11160 }
11161 hash = full_hash(hash);
11162
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011163 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011164 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011165 smp->flags = SMP_F_VOL_1ST;
11166 return 1;
11167}
11168
11169/* This concatenates the source address with the 32-bit hash of the Host and
11170 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11171 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11172 * on the source address length. The URL hash is stored before the address so
11173 * that in environments where IPv6 is insignificant, truncating the output to
11174 * 8 bytes would still work.
11175 */
11176static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011177smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011178{
11179 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011180 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011181
Dragan Dosendb5af612016-06-16 11:23:01 +020011182 if (!cli_conn)
11183 return 0;
11184
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011185 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011186 return 0;
11187
11188 temp = get_trash_chunk();
Dragan Dosene5f41332016-06-16 11:08:08 +020011189 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
11190 temp->len += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011191
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011192 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011193 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011194 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011195 temp->len += 4;
11196 break;
11197 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011198 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011199 temp->len += 16;
11200 break;
11201 default:
11202 return 0;
11203 }
11204
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011205 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011206 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011207 return 1;
11208}
11209
Willy Tarreau185b5c42012-04-26 15:11:51 +020011210/* This function is used to validate the arguments passed to any "hdr" fetch
11211 * keyword. These keywords support an optional positive or negative occurrence
11212 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11213 * is assumed that the types are already the correct ones. Returns 0 on error,
11214 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11215 * error message in case of error, that the caller is responsible for freeing.
11216 * The initial location must either be freeable or NULL.
11217 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011218int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011219{
11220 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011221 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011222 return 0;
11223 }
11224 return 1;
11225}
11226
Willy Tarreau276fae92013-07-25 14:36:01 +020011227/* takes an UINT value on input supposed to represent the time since EPOCH,
11228 * adds an optional offset found in args[0] and emits a string representing
11229 * the date in RFC-1123/5322 format.
11230 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011231static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011232{
Cyril Bontéf78d8962016-01-22 19:40:28 +010011233 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020011234 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11235 struct chunk *temp;
11236 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011237 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011238 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020011239
11240 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011241 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020011242 curr_date += args[0].data.sint;
11243
11244 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020011245 if (!tm)
11246 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011247
11248 temp = get_trash_chunk();
11249 temp->len = snprintf(temp->str, temp->size - temp->len,
11250 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11251 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11252 tm->tm_hour, tm->tm_min, tm->tm_sec);
11253
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011254 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011255 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020011256 return 1;
11257}
11258
Thierry FOURNIERad903512014-04-11 17:51:01 +020011259/* Match language range with language tag. RFC2616 14.4:
11260 *
11261 * A language-range matches a language-tag if it exactly equals
11262 * the tag, or if it exactly equals a prefix of the tag such
11263 * that the first tag character following the prefix is "-".
11264 *
11265 * Return 1 if the strings match, else return 0.
11266 */
11267static inline int language_range_match(const char *range, int range_len,
11268 const char *tag, int tag_len)
11269{
11270 const char *end = range + range_len;
11271 const char *tend = tag + tag_len;
11272 while (range < end) {
11273 if (*range == '-' && tag == tend)
11274 return 1;
11275 if (*range != *tag || tag == tend)
11276 return 0;
11277 range++;
11278 tag++;
11279 }
11280 /* Return true only if the last char of the tag is matched. */
11281 return tag == tend;
11282}
11283
11284/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011285static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011286{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011287 const char *al = smp->data.u.str.str;
11288 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011289 const char *token;
11290 int toklen;
11291 int qvalue;
11292 const char *str;
11293 const char *w;
11294 int best_q = 0;
11295
11296 /* Set the constant to the sample, because the output of the
11297 * function will be peek in the constant configuration string.
11298 */
11299 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011300 smp->data.u.str.size = 0;
11301 smp->data.u.str.str = "";
11302 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011303
11304 /* Parse the accept language */
11305 while (1) {
11306
11307 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011308 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011309 al++;
11310 if (al >= end)
11311 break;
11312
11313 /* Start of the fisrt word. */
11314 token = al;
11315
11316 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011317 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011318 al++;
11319 if (al == token)
11320 goto expect_comma;
11321
11322 /* Length of the token. */
11323 toklen = al - token;
11324 qvalue = 1000;
11325
11326 /* Check if the token exists in the list. If the token not exists,
11327 * jump to the next token.
11328 */
11329 str = args[0].data.str.str;
11330 w = str;
11331 while (1) {
11332 if (*str == ';' || *str == '\0') {
11333 if (language_range_match(token, toklen, w, str-w))
11334 goto look_for_q;
11335 if (*str == '\0')
11336 goto expect_comma;
11337 w = str + 1;
11338 }
11339 str++;
11340 }
11341 goto expect_comma;
11342
11343look_for_q:
11344
11345 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011346 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011347 al++;
11348 if (al >= end)
11349 goto process_value;
11350
11351 /* If ',' is found, process the result */
11352 if (*al == ',')
11353 goto process_value;
11354
11355 /* If the character is different from ';', look
11356 * for the end of the header part in best effort.
11357 */
11358 if (*al != ';')
11359 goto expect_comma;
11360
11361 /* Assumes that the char is ';', now expect "q=". */
11362 al++;
11363
11364 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011365 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011366 al++;
11367 if (al >= end)
11368 goto process_value;
11369
11370 /* Expect 'q'. If no 'q', continue in best effort */
11371 if (*al != 'q')
11372 goto process_value;
11373 al++;
11374
11375 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011376 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011377 al++;
11378 if (al >= end)
11379 goto process_value;
11380
11381 /* Expect '='. If no '=', continue in best effort */
11382 if (*al != '=')
11383 goto process_value;
11384 al++;
11385
11386 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011387 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011388 al++;
11389 if (al >= end)
11390 goto process_value;
11391
11392 /* Parse the q value. */
11393 qvalue = parse_qvalue(al, &al);
11394
11395process_value:
11396
11397 /* If the new q value is the best q value, then store the associated
11398 * language in the response. If qvalue is the biggest value (1000),
11399 * break the process.
11400 */
11401 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011402 smp->data.u.str.str = (char *)w;
11403 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011404 if (qvalue >= 1000)
11405 break;
11406 best_q = qvalue;
11407 }
11408
11409expect_comma:
11410
11411 /* Expect comma or end. If the end is detected, quit the loop. */
11412 while (al < end && *al != ',')
11413 al++;
11414 if (al >= end)
11415 break;
11416
11417 /* Comma is found, jump it and restart the analyzer. */
11418 al++;
11419 }
11420
11421 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011422 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
11423 smp->data.u.str.str = args[1].data.str.str;
11424 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011425 }
11426
11427 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011428 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011429}
11430
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011431/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011432static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011433{
11434 /* If the constant flag is set or if not size is avalaible at
11435 * the end of the buffer, copy the string in other buffer
11436 * before decoding.
11437 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011438 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011439 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011440 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
11441 smp->data.u.str.str = str->str;
11442 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011443 smp->flags &= ~SMP_F_CONST;
11444 }
11445
11446 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011447 smp->data.u.str.str[smp->data.u.str.len] = '\0';
11448 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Christopher Fauleta2584792017-10-05 10:03:12 +020011449 return (smp->data.u.str.len >= 0);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011450}
11451
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011452static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
11453{
11454 struct proxy *fe = strm_fe(smp->strm);
11455 int idx, i;
11456 struct cap_hdr *hdr;
11457 int len;
11458
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011459 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011460 return 0;
11461
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011462 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011463
11464 /* Check the availibity of the capture id. */
11465 if (idx > fe->nb_req_cap - 1)
11466 return 0;
11467
11468 /* Look for the original configuration. */
11469 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
11470 hdr != NULL && i != idx ;
11471 i--, hdr = hdr->next);
11472 if (!hdr)
11473 return 0;
11474
11475 /* check for the memory allocation */
11476 if (smp->strm->req_cap[hdr->index] == NULL)
11477 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
11478 if (smp->strm->req_cap[hdr->index] == NULL)
11479 return 0;
11480
11481 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011482 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011483 if (len > hdr->len)
11484 len = hdr->len;
11485
11486 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011487 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011488 smp->strm->req_cap[idx][len] = '\0';
11489
11490 return 1;
11491}
11492
11493static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
11494{
11495 struct proxy *fe = strm_fe(smp->strm);
11496 int idx, i;
11497 struct cap_hdr *hdr;
11498 int len;
11499
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011500 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011501 return 0;
11502
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011503 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011504
11505 /* Check the availibity of the capture id. */
11506 if (idx > fe->nb_rsp_cap - 1)
11507 return 0;
11508
11509 /* Look for the original configuration. */
11510 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
11511 hdr != NULL && i != idx ;
11512 i--, hdr = hdr->next);
11513 if (!hdr)
11514 return 0;
11515
11516 /* check for the memory allocation */
11517 if (smp->strm->res_cap[hdr->index] == NULL)
11518 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
11519 if (smp->strm->res_cap[hdr->index] == NULL)
11520 return 0;
11521
11522 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011523 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011524 if (len > hdr->len)
11525 len = hdr->len;
11526
11527 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011528 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011529 smp->strm->res_cap[idx][len] = '\0';
11530
11531 return 1;
11532}
11533
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011534/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011535 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011536 * the relevant part of the request line accordingly. Then it updates various
11537 * pointers to the next elements which were moved, and the total buffer length.
11538 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011539 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
11540 * error, though this can be revisited when this code is finally exploited.
11541 *
11542 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
11543 * query string and 3 to replace uri.
11544 *
11545 * In query string case, the mark question '?' must be set at the start of the
11546 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011547 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011548int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011549 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011550{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011551 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011552 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011553 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011554 int delta;
11555
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011556 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011557 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011558 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011559 cur_end = cur_ptr + txn->req.sl.rq.m_l;
11560
11561 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011562 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011563 txn->req.sl.rq.m_l += delta;
11564 txn->req.sl.rq.u += delta;
11565 txn->req.sl.rq.v += delta;
11566 break;
11567
11568 case 1: // path
11569 cur_ptr = http_get_path(txn);
11570 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011571 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011572
11573 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011574 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 +010011575 cur_end++;
11576
11577 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011578 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011579 txn->req.sl.rq.u_l += delta;
11580 txn->req.sl.rq.v += delta;
11581 break;
11582
11583 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011584 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011585 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011586 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11587 while (cur_ptr < cur_end && *cur_ptr != '?')
11588 cur_ptr++;
11589
11590 /* skip the question mark or indicate that we must insert it
11591 * (but only if the format string is not empty then).
11592 */
11593 if (cur_ptr < cur_end)
11594 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011595 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011596 offset = 0;
11597
11598 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011599 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011600 txn->req.sl.rq.u_l += delta;
11601 txn->req.sl.rq.v += delta;
11602 break;
11603
11604 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011605 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011606 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11607
11608 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011609 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011610 txn->req.sl.rq.u_l += delta;
11611 txn->req.sl.rq.v += delta;
11612 break;
11613
11614 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011615 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011616 }
11617
11618 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011619 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020011620 txn->req.sl.rq.l += delta;
11621 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011622 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011623 return 0;
11624}
11625
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011626/* This function replace the HTTP status code and the associated message. The
11627 * variable <status> contains the new status code. This function never fails.
11628 */
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011629void http_set_status(unsigned int status, const char *reason, struct stream *s)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011630{
11631 struct http_txn *txn = s->txn;
11632 char *cur_ptr, *cur_end;
11633 int delta;
11634 char *res;
11635 int c_l;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011636 const char *msg = reason;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011637 int msg_len;
11638
11639 chunk_reset(&trash);
11640
11641 res = ultoa_o(status, trash.str, trash.size);
11642 c_l = res - trash.str;
11643
11644 trash.str[c_l] = ' ';
11645 trash.len = c_l + 1;
11646
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011647 /* Do we have a custom reason format string? */
11648 if (msg == NULL)
11649 msg = get_reason(status);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011650 msg_len = strlen(msg);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011651 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
11652 trash.len += msg_len;
11653
11654 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
11655 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
11656
11657 /* commit changes and adjust message */
11658 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
11659
11660 /* adjust res line offsets and lengths */
11661 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
11662 txn->rsp.sl.st.c_l = c_l;
11663 txn->rsp.sl.st.r_l = msg_len;
11664
11665 delta = trash.len - (cur_end - cur_ptr);
11666 txn->rsp.sl.st.l += delta;
11667 txn->hdr_idx.v[0].len += delta;
11668 http_msg_move_end(&txn->rsp, delta);
11669}
11670
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011671/* This function executes one of the set-{method,path,query,uri} actions. It
11672 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020011673 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011674 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020011675 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
11676 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011677 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011678enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011679 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011680{
11681 chunk_reset(&trash);
11682
11683 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011684 if (rule->arg.http.action == 2)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011685 trash.str[trash.len++] = '?';
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011686 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011687
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011688 http_replace_req_line(rule->arg.http.action, trash.str, trash.len, px, s);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011689 return ACT_RET_CONT;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011690}
11691
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011692/* This function is just a compliant action wrapper for "set-status". */
11693enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011694 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011695{
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011696 http_set_status(rule->arg.status.code, rule->arg.status.reason, s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011697 return ACT_RET_CONT;
11698}
11699
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011700/* parse an http-request action among :
11701 * set-method
11702 * set-path
11703 * set-query
11704 * set-uri
11705 *
11706 * All of them accept a single argument of type string representing a log-format.
11707 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
11708 * 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 +020011709 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011710 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011711enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
11712 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011713{
11714 int cur_arg = *orig_arg;
11715
Thierry FOURNIER42148732015-09-02 17:17:33 +020011716 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011717
11718 switch (args[0][4]) {
11719 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011720 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011721 rule->action_ptr = http_action_set_req_line;
11722 break;
11723 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011724 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011725 rule->action_ptr = http_action_set_req_line;
11726 break;
11727 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011728 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011729 rule->action_ptr = http_action_set_req_line;
11730 break;
11731 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011732 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011733 rule->action_ptr = http_action_set_req_line;
11734 break;
11735 default:
11736 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011737 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011738 }
11739
11740 if (!*args[cur_arg] ||
11741 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
11742 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011743 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011744 }
11745
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011746 LIST_INIT(&rule->arg.http.logfmt);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011747 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010011748 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +010011749 (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 +010011750 return ACT_RET_PRS_ERR;
11751 }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011752
11753 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011754 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011755}
11756
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011757/* parse set-status action:
11758 * This action accepts a single argument of type int representing
11759 * an http status code. It returns ACT_RET_PRS_OK on success,
11760 * ACT_RET_PRS_ERR on error.
11761 */
11762enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
11763 struct act_rule *rule, char **err)
11764{
11765 char *error;
11766
Thierry FOURNIER42148732015-09-02 17:17:33 +020011767 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011768 rule->action_ptr = action_http_set_status;
11769
11770 /* Check if an argument is available */
11771 if (!*args[*orig_arg]) {
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011772 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011773 return ACT_RET_PRS_ERR;
11774 }
11775
11776 /* convert status code as integer */
11777 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
11778 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
11779 memprintf(err, "expects an integer status code between 100 and 999");
11780 return ACT_RET_PRS_ERR;
11781 }
11782
11783 (*orig_arg)++;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011784
11785 /* set custom reason string */
11786 rule->arg.status.reason = NULL; // If null, we use the default reason for the status code.
11787 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
11788 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
11789 (*orig_arg)++;
11790 rule->arg.status.reason = strdup(args[*orig_arg]);
11791 (*orig_arg)++;
11792 }
11793
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011794 return ACT_RET_PRS_OK;
11795}
11796
Willy Tarreaua9083d02015-05-08 15:27:59 +020011797/* This function executes the "capture" action. It executes a fetch expression,
11798 * turns the result into a string and puts it in a capture slot. It always
11799 * returns 1. If an error occurs the action is cancelled, but the rule
11800 * processing continues.
11801 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011802enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011803 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020011804{
Willy Tarreaua9083d02015-05-08 15:27:59 +020011805 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020011806 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011807 char **cap = s->req_cap;
11808 int len;
11809
Thierry FOURNIER32b15002015-07-31 08:56:16 +020011810 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 +020011811 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011812 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011813
11814 if (cap[h->index] == NULL)
11815 cap[h->index] = pool_alloc2(h->pool);
11816
11817 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011818 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011819
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011820 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011821 if (len > h->len)
11822 len = h->len;
11823
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011824 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020011825 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011826 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011827}
11828
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011829/* This function executes the "capture" action and store the result in a
11830 * capture slot if exists. It executes a fetch expression, turns the result
11831 * into a string and puts it in a capture slot. It always returns 1. If an
11832 * error occurs the action is cancelled, but the rule processing continues.
11833 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011834enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011835 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011836{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011837 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011838 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011839 char **cap = s->req_cap;
11840 struct proxy *fe = strm_fe(s);
11841 int len;
11842 int i;
11843
11844 /* Look for the original configuration. */
11845 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020011846 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011847 i--, h = h->next);
11848 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011849 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011850
Thierry FOURNIERe2097972015-07-31 08:56:35 +020011851 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 +020011852 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011853 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011854
11855 if (cap[h->index] == NULL)
11856 cap[h->index] = pool_alloc2(h->pool);
11857
11858 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011859 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011860
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011861 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011862 if (len > h->len)
11863 len = h->len;
11864
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011865 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011866 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011867 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011868}
11869
Willy Tarreaua9083d02015-05-08 15:27:59 +020011870/* parse an "http-request capture" action. It takes a single argument which is
11871 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011872 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011873 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020011874 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011875enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
11876 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020011877{
11878 struct sample_expr *expr;
11879 struct cap_hdr *hdr;
11880 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020011881 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011882
11883 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
11884 if (strcmp(args[cur_arg], "if") == 0 ||
11885 strcmp(args[cur_arg], "unless") == 0)
11886 break;
11887
11888 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011889 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011890 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011891 }
11892
Willy Tarreaua9083d02015-05-08 15:27:59 +020011893 cur_arg = *orig_arg;
11894 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
11895 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011896 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011897
11898 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
11899 memprintf(err,
11900 "fetch method '%s' extracts information from '%s', none of which is available here",
11901 args[cur_arg-1], sample_src_names(expr->fetch->use));
11902 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011903 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011904 }
11905
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011906 if (!args[cur_arg] || !*args[cur_arg]) {
11907 memprintf(err, "expects 'len or 'id'");
11908 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011909 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011910 }
11911
Willy Tarreaua9083d02015-05-08 15:27:59 +020011912 if (strcmp(args[cur_arg], "len") == 0) {
11913 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011914
11915 if (!(px->cap & PR_CAP_FE)) {
11916 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011917 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011918 }
11919
11920 proxy->conf.args.ctx = ARGC_CAP;
11921
Willy Tarreaua9083d02015-05-08 15:27:59 +020011922 if (!args[cur_arg]) {
11923 memprintf(err, "missing length value");
11924 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011925 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011926 }
11927 /* we copy the table name for now, it will be resolved later */
11928 len = atoi(args[cur_arg]);
11929 if (len <= 0) {
11930 memprintf(err, "length must be > 0");
11931 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011932 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011933 }
11934 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011935
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011936 if (!len) {
11937 memprintf(err, "a positive 'len' argument is mandatory");
11938 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011939 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011940 }
11941
Vincent Bernat02779b62016-04-03 13:48:43 +020011942 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011943 hdr->next = px->req_cap;
11944 hdr->name = NULL; /* not a header capture */
11945 hdr->namelen = 0;
11946 hdr->len = len;
11947 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
11948 hdr->index = px->nb_req_cap++;
11949
11950 px->req_cap = hdr;
11951 px->to_log |= LW_REQHDR;
11952
Thierry FOURNIER42148732015-09-02 17:17:33 +020011953 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011954 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020011955 rule->arg.cap.expr = expr;
11956 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011957 }
11958
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011959 else if (strcmp(args[cur_arg], "id") == 0) {
11960 int id;
11961 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011962
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011963 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011964
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011965 if (!args[cur_arg]) {
11966 memprintf(err, "missing id value");
11967 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011968 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011969 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020011970
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011971 id = strtol(args[cur_arg], &error, 10);
11972 if (*error != '\0') {
11973 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
11974 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011975 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011976 }
11977 cur_arg++;
11978
11979 proxy->conf.args.ctx = ARGC_CAP;
11980
Thierry FOURNIER42148732015-09-02 17:17:33 +020011981 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011982 rule->action_ptr = http_action_req_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020011983 rule->arg.capid.expr = expr;
11984 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011985 }
11986
11987 else {
11988 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
11989 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011990 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011991 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020011992
11993 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011994 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011995}
11996
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011997/* This function executes the "capture" action and store the result in a
11998 * capture slot if exists. It executes a fetch expression, turns the result
11999 * into a string and puts it in a capture slot. It always returns 1. If an
12000 * error occurs the action is cancelled, but the rule processing continues.
12001 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012002enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012003 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012004{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012005 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012006 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012007 char **cap = s->res_cap;
12008 struct proxy *fe = strm_fe(s);
12009 int len;
12010 int i;
12011
12012 /* Look for the original configuration. */
12013 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012014 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012015 i--, h = h->next);
12016 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012017 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012018
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012019 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 +020012020 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012021 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012022
12023 if (cap[h->index] == NULL)
12024 cap[h->index] = pool_alloc2(h->pool);
12025
12026 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012027 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012028
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012029 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012030 if (len > h->len)
12031 len = h->len;
12032
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012033 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012034 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012035 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012036}
12037
12038/* parse an "http-response capture" action. It takes a single argument which is
12039 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12040 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012041 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012042 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012043enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12044 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012045{
12046 struct sample_expr *expr;
12047 int cur_arg;
12048 int id;
12049 char *error;
12050
12051 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12052 if (strcmp(args[cur_arg], "if") == 0 ||
12053 strcmp(args[cur_arg], "unless") == 0)
12054 break;
12055
12056 if (cur_arg < *orig_arg + 3) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012057 memprintf(err, "expects <expression> id <idx>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012058 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012059 }
12060
12061 cur_arg = *orig_arg;
12062 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12063 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012064 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012065
12066 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12067 memprintf(err,
12068 "fetch method '%s' extracts information from '%s', none of which is available here",
12069 args[cur_arg-1], sample_src_names(expr->fetch->use));
12070 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012071 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012072 }
12073
12074 if (!args[cur_arg] || !*args[cur_arg]) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012075 memprintf(err, "expects 'id'");
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012076 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012077 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012078 }
12079
12080 if (strcmp(args[cur_arg], "id") != 0) {
12081 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12082 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012083 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012084 }
12085
12086 cur_arg++;
12087
12088 if (!args[cur_arg]) {
12089 memprintf(err, "missing id value");
12090 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012091 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012092 }
12093
12094 id = strtol(args[cur_arg], &error, 10);
12095 if (*error != '\0') {
12096 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12097 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012098 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012099 }
12100 cur_arg++;
12101
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012102 proxy->conf.args.ctx = ARGC_CAP;
12103
Thierry FOURNIER42148732015-09-02 17:17:33 +020012104 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012105 rule->action_ptr = http_action_res_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012106 rule->arg.capid.expr = expr;
12107 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012108
12109 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012110 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012111}
12112
William Lallemand73025dd2014-04-24 14:38:37 +020012113/*
12114 * Return the struct http_req_action_kw associated to a keyword.
12115 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012116struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012117{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012118 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012119}
12120
12121/*
12122 * Return the struct http_res_action_kw associated to a keyword.
12123 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012124struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012125{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012126 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012127}
12128
Willy Tarreau12207b32016-11-22 19:48:51 +010012129
12130/* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop
12131 * now.
12132 */
12133static int cli_parse_show_errors(char **args, struct appctx *appctx, void *private)
12134{
12135 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
12136 return 1;
12137
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012138 if (*args[2]) {
12139 struct proxy *px;
12140
12141 px = proxy_find_by_name(args[2], 0, 0);
12142 if (px)
12143 appctx->ctx.errors.iid = px->uuid;
12144 else
12145 appctx->ctx.errors.iid = atoi(args[2]);
12146
12147 if (!appctx->ctx.errors.iid) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +020012148 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012149 appctx->ctx.cli.msg = "No such proxy.\n";
12150 appctx->st0 = CLI_ST_PRINT;
12151 return 1;
12152 }
12153 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012154 else
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012155 appctx->ctx.errors.iid = -1; // dump all proxies
12156
Willy Tarreau35069f82016-11-25 09:16:37 +010012157 appctx->ctx.errors.flag = 0;
12158 if (strcmp(args[3], "request") == 0)
12159 appctx->ctx.errors.flag |= 4; // ignore response
12160 else if (strcmp(args[3], "response") == 0)
12161 appctx->ctx.errors.flag |= 2; // ignore request
Willy Tarreau12207b32016-11-22 19:48:51 +010012162 appctx->ctx.errors.px = NULL;
Willy Tarreau12207b32016-11-22 19:48:51 +010012163 return 0;
12164}
12165
12166/* This function dumps all captured errors onto the stream interface's
12167 * read buffer. It returns 0 if the output buffer is full and it needs
12168 * to be called again, otherwise non-zero.
12169 */
12170static int cli_io_handler_show_errors(struct appctx *appctx)
12171{
12172 struct stream_interface *si = appctx->owner;
12173 extern const char *monthname[12];
12174
12175 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
12176 return 1;
12177
12178 chunk_reset(&trash);
12179
12180 if (!appctx->ctx.errors.px) {
12181 /* the function had not been called yet, let's prepare the
12182 * buffer for a response.
12183 */
12184 struct tm tm;
12185
12186 get_localtime(date.tv_sec, &tm);
12187 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
12188 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12189 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
12190 error_snapshot_id);
12191
Willy Tarreau06d80a92017-10-19 14:32:15 +020012192 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012193 /* Socket buffer full. Let's try again later from the same point */
12194 si_applet_cant_put(si);
12195 return 0;
12196 }
12197
12198 appctx->ctx.errors.px = proxy;
Willy Tarreau12207b32016-11-22 19:48:51 +010012199 appctx->ctx.errors.bol = 0;
12200 appctx->ctx.errors.ptr = -1;
12201 }
12202
12203 /* we have two inner loops here, one for the proxy, the other one for
12204 * the buffer.
12205 */
12206 while (appctx->ctx.errors.px) {
12207 struct error_snapshot *es;
12208
Willy Tarreau35069f82016-11-25 09:16:37 +010012209 if ((appctx->ctx.errors.flag & 1) == 0) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012210 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau35069f82016-11-25 09:16:37 +010012211 if (appctx->ctx.errors.flag & 2) // skip req
12212 goto next;
12213 }
12214 else {
Willy Tarreau12207b32016-11-22 19:48:51 +010012215 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau35069f82016-11-25 09:16:37 +010012216 if (appctx->ctx.errors.flag & 4) // skip resp
12217 goto next;
12218 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012219
12220 if (!es->when.tv_sec)
12221 goto next;
12222
12223 if (appctx->ctx.errors.iid >= 0 &&
12224 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
12225 es->oe->uuid != appctx->ctx.errors.iid)
12226 goto next;
12227
12228 if (appctx->ctx.errors.ptr < 0) {
12229 /* just print headers now */
12230
12231 char pn[INET6_ADDRSTRLEN];
12232 struct tm tm;
12233 int port;
12234
12235 get_localtime(es->when.tv_sec, &tm);
12236 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
12237 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12238 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
12239
12240 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
12241 case AF_INET:
12242 case AF_INET6:
12243 port = get_host_port(&es->src);
12244 break;
12245 default:
12246 port = 0;
12247 }
12248
Willy Tarreau35069f82016-11-25 09:16:37 +010012249 switch (appctx->ctx.errors.flag & 1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012250 case 0:
12251 chunk_appendf(&trash,
12252 " frontend %s (#%d): invalid request\n"
12253 " backend %s (#%d)",
12254 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
12255 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
12256 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
12257 break;
12258 case 1:
12259 chunk_appendf(&trash,
12260 " backend %s (#%d): invalid response\n"
12261 " frontend %s (#%d)",
12262 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
12263 es->oe->id, es->oe->uuid);
12264 break;
12265 }
12266
12267 chunk_appendf(&trash,
12268 ", server %s (#%d), event #%u\n"
12269 " src %s:%d, session #%d, session flags 0x%08x\n"
Willy Tarreau10e61cb2017-01-04 14:51:22 +010012270 " HTTP msg state %s(%d), msg flags 0x%08x, tx flags 0x%08x\n"
Willy Tarreau12207b32016-11-22 19:48:51 +010012271 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
12272 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
12273 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
12274 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
12275 es->ev_id,
12276 pn, port, es->sid, es->s_flags,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020012277 h1_msg_state_str(es->state), es->state, es->m_flags, es->t_flags,
Willy Tarreau12207b32016-11-22 19:48:51 +010012278 es->m_clen, es->m_blen,
12279 es->b_flags, es->b_out, es->b_tot,
12280 es->len, es->b_wrap, es->pos);
12281
Willy Tarreau06d80a92017-10-19 14:32:15 +020012282 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012283 /* Socket buffer full. Let's try again later from the same point */
12284 si_applet_cant_put(si);
12285 return 0;
12286 }
12287 appctx->ctx.errors.ptr = 0;
12288 appctx->ctx.errors.sid = es->sid;
12289 }
12290
12291 if (appctx->ctx.errors.sid != es->sid) {
12292 /* the snapshot changed while we were dumping it */
12293 chunk_appendf(&trash,
12294 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +020012295 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012296 si_applet_cant_put(si);
12297 return 0;
12298 }
12299 goto next;
12300 }
12301
12302 /* OK, ptr >= 0, so we have to dump the current line */
12303 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
12304 int newptr;
12305 int newline;
12306
12307 newline = appctx->ctx.errors.bol;
12308 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
12309 if (newptr == appctx->ctx.errors.ptr)
12310 return 0;
12311
Willy Tarreau06d80a92017-10-19 14:32:15 +020012312 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012313 /* Socket buffer full. Let's try again later from the same point */
12314 si_applet_cant_put(si);
12315 return 0;
12316 }
12317 appctx->ctx.errors.ptr = newptr;
12318 appctx->ctx.errors.bol = newline;
12319 };
12320 next:
12321 appctx->ctx.errors.bol = 0;
12322 appctx->ctx.errors.ptr = -1;
Willy Tarreau35069f82016-11-25 09:16:37 +010012323 appctx->ctx.errors.flag ^= 1;
12324 if (!(appctx->ctx.errors.flag & 1))
Willy Tarreau12207b32016-11-22 19:48:51 +010012325 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau12207b32016-11-22 19:48:51 +010012326 }
12327
12328 /* dump complete */
12329 return 1;
12330}
12331
12332/* register cli keywords */
12333static struct cli_kw_list cli_kws = {{ },{
12334 { { "show", "errors", NULL },
12335 "show errors : report last request and response errors for each proxy",
12336 cli_parse_show_errors, cli_io_handler_show_errors, NULL,
12337 },
12338 {{},}
12339}};
12340
Willy Tarreau4a568972010-05-12 08:08:50 +020012341/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012342/* All supported ACL keywords must be declared here. */
12343/************************************************************************/
12344
12345/* Note: must not be declared <const> as its list will be overwritten.
12346 * Please take care of keeping this list alphabetically sorted.
12347 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012348static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012349 { "base", "base", PAT_MATCH_STR },
12350 { "base_beg", "base", PAT_MATCH_BEG },
12351 { "base_dir", "base", PAT_MATCH_DIR },
12352 { "base_dom", "base", PAT_MATCH_DOM },
12353 { "base_end", "base", PAT_MATCH_END },
12354 { "base_len", "base", PAT_MATCH_LEN },
12355 { "base_reg", "base", PAT_MATCH_REG },
12356 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012357
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012358 { "cook", "req.cook", PAT_MATCH_STR },
12359 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12360 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12361 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12362 { "cook_end", "req.cook", PAT_MATCH_END },
12363 { "cook_len", "req.cook", PAT_MATCH_LEN },
12364 { "cook_reg", "req.cook", PAT_MATCH_REG },
12365 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012366
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012367 { "hdr", "req.hdr", PAT_MATCH_STR },
12368 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12369 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12370 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12371 { "hdr_end", "req.hdr", PAT_MATCH_END },
12372 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12373 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12374 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012375
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012376 /* these two declarations uses strings with list storage (in place
12377 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12378 * and delete functions are relative to the list management. The parse
12379 * and match method are related to the corresponding fetch methods. This
12380 * is very particular ACL declaration mode.
12381 */
12382 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12383 { "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 +020012384
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012385 { "path", "path", PAT_MATCH_STR },
12386 { "path_beg", "path", PAT_MATCH_BEG },
12387 { "path_dir", "path", PAT_MATCH_DIR },
12388 { "path_dom", "path", PAT_MATCH_DOM },
12389 { "path_end", "path", PAT_MATCH_END },
12390 { "path_len", "path", PAT_MATCH_LEN },
12391 { "path_reg", "path", PAT_MATCH_REG },
12392 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012393
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012394 { "req_ver", "req.ver", PAT_MATCH_STR },
12395 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012396
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012397 { "scook", "res.cook", PAT_MATCH_STR },
12398 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12399 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12400 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12401 { "scook_end", "res.cook", PAT_MATCH_END },
12402 { "scook_len", "res.cook", PAT_MATCH_LEN },
12403 { "scook_reg", "res.cook", PAT_MATCH_REG },
12404 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012405
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012406 { "shdr", "res.hdr", PAT_MATCH_STR },
12407 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12408 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12409 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12410 { "shdr_end", "res.hdr", PAT_MATCH_END },
12411 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12412 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12413 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012414
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012415 { "url", "url", PAT_MATCH_STR },
12416 { "url_beg", "url", PAT_MATCH_BEG },
12417 { "url_dir", "url", PAT_MATCH_DIR },
12418 { "url_dom", "url", PAT_MATCH_DOM },
12419 { "url_end", "url", PAT_MATCH_END },
12420 { "url_len", "url", PAT_MATCH_LEN },
12421 { "url_reg", "url", PAT_MATCH_REG },
12422 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012423
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012424 { "urlp", "urlp", PAT_MATCH_STR },
12425 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12426 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12427 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12428 { "urlp_end", "urlp", PAT_MATCH_END },
12429 { "urlp_len", "urlp", PAT_MATCH_LEN },
12430 { "urlp_reg", "urlp", PAT_MATCH_REG },
12431 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012432
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012433 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012434}};
12435
12436/************************************************************************/
12437/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012438/************************************************************************/
12439/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012440static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012441 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012442 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012443 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12444
Willy Tarreau87b09662015-04-03 00:22:06 +020012445 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012446 { "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 +020012447
12448 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012449 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12450 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12451 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012452
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012453 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
12454 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012455
Willy Tarreau409bcde2013-01-08 00:31:00 +010012456 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12457 * are only here to match the ACL's name, are request-only and are used
12458 * for ACL compatibility only.
12459 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012460 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12461 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012462 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12463 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012464
12465 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12466 * only here to match the ACL's name, are request-only and are used for
12467 * ACL compatibility only.
12468 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012469 { "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 +020012470 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012471 { "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 +020012472 { "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 +010012473
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012474 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012475 { "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 +010012476 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012477 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012478 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012479 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012480
12481 /* HTTP protocol on the request path */
12482 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012483 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012484
12485 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012486 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12487 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012488
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012489 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012490 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
12491 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012492 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012493
Thierry FOURNIERd7d88812017-04-19 15:15:14 +020012494 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER5617dce2017-04-09 05:38:19 +020012495 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12496
Willy Tarreau18ed2562013-01-14 15:56:36 +010012497 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012498 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12499 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012500
Willy Tarreau18ed2562013-01-14 15:56:36 +010012501 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012502 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012503 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12504 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012505
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012506 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012507 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012508 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012509 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012510 { "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 +010012511 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012512 { "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 +010012513
12514 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012515 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012516 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12517 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012518
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012519 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012520 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012521 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012522 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012523 { "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 +010012524 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012525 { "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 +010012526
Willy Tarreau409bcde2013-01-08 00:31:00 +010012527 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012528 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012529 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12530 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012531 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010012532
12533 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012534 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012535 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012536 { "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 +020012537 { "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 +010012538
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012539 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020012540 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012541 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012542 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012543 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012544 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012545 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020012546 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12547 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012548 { "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 +010012549 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020012550}};
12551
Willy Tarreau8797c062007-05-07 00:55:35 +020012552
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012553/************************************************************************/
12554/* All supported converter keywords must be declared here. */
12555/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020012556/* Note: must not be declared <const> as its list will be overwritten */
12557static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012558 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020012559 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012560 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
12561 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012562 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020012563 { NULL, NULL, 0, 0, 0 },
12564}};
12565
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012566
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012567/************************************************************************/
12568/* All supported http-request action keywords must be declared here. */
12569/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012570struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012571 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020012572 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012573 { "set-method", parse_set_req_line },
12574 { "set-path", parse_set_req_line },
12575 { "set-query", parse_set_req_line },
12576 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020012577 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012578 }
12579};
12580
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012581struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012582 .kw = {
12583 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012584 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012585 { NULL, NULL }
12586 }
12587};
12588
Willy Tarreau8797c062007-05-07 00:55:35 +020012589__attribute__((constructor))
12590static void __http_protocol_init(void)
12591{
12592 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020012593 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020012594 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012595 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012596 http_res_keywords_register(&http_res_actions);
Willy Tarreau12207b32016-11-22 19:48:51 +010012597 cli_register_kw(&cli_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020012598}
12599
12600
Willy Tarreau58f10d72006-12-04 02:26:12 +010012601/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020012602 * Local variables:
12603 * c-indent-level: 8
12604 * c-basic-offset: 8
12605 * End:
12606 */