blob: c0532d7722195d63953ff67079440fc1fdebc767 [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 Tarreau4baf44b2012-03-09 14:10:20 +01003975 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003976
Willy Tarreau115acb92009-12-26 13:56:06 +01003977 if (!ret)
3978 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003979 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02003980 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003981 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003982 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01003983 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02003984 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01003985 }
3986
Willy Tarreaud98cf932009-12-27 22:54:55 +01003987 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02003988 * We have the first data byte is in msg->sov + msg->sol. We're waiting
3989 * for at least a whole chunk or the whole content length bytes after
3990 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01003991 */
Willy Tarreau890988f2014-04-10 11:59:33 +02003992 if (msg->msg_state == HTTP_MSG_TRAILERS)
3993 goto http_end;
3994
Willy Tarreaue115b492015-05-01 23:05:14 +02003995 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003996 goto http_end;
3997
3998 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02003999 /* we get here if we need to wait for more data. If the buffer is full,
4000 * we have the maximum we can expect.
4001 */
4002 if (buffer_full(req->buf, global.tune.maxrewrite))
4003 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004004
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004005 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004006 txn->status = 408;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004007 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004008
Willy Tarreaue7dff022015-04-03 01:14:29 +02004009 if (!(s->flags & SF_ERR_MASK))
4010 s->flags |= SF_ERR_CLITO;
4011 if (!(s->flags & SF_FINST_MASK))
4012 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004013 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004014 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004015
4016 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004017 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004018 /* Not enough data. We'll re-use the http-request
4019 * timeout here. Ideally, we should set the timeout
4020 * relative to the accept() date. We just set the
4021 * request timeout once at the beginning of the
4022 * request.
4023 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004024 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004025 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004026 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004027 return 0;
4028 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004029
4030 http_end:
4031 /* The situation will not evolve, so let's give up on the analysis. */
4032 s->logs.tv_request = now; /* update the request timer to reflect full request */
4033 req->analysers &= ~an_bit;
4034 req->analyse_exp = TICK_ETERNITY;
4035 return 1;
4036
4037 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004038 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004039 txn->req.msg_state = HTTP_MSG_ERROR;
4040 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004041 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004042
Willy Tarreaue7dff022015-04-03 01:14:29 +02004043 if (!(s->flags & SF_ERR_MASK))
4044 s->flags |= SF_ERR_PRXCOND;
4045 if (!(s->flags & SF_FINST_MASK))
4046 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004047
Willy Tarreau522d6c02009-12-06 18:49:18 +01004048 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01004049 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004050 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004051 if (sess->listener->counters)
4052 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004053 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004054}
4055
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004056/* send a server's name with an outgoing request over an established connection.
4057 * Note: this function is designed to be called once the request has been scheduled
4058 * for being forwarded. This is the reason why it rewinds the buffer before
4059 * proceeding.
4060 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004061int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004062
4063 struct hdr_ctx ctx;
4064
Mark Lamourinec2247f02012-01-04 13:02:01 -05004065 char *hdr_name = be->server_id_hdr_name;
4066 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004067 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004068 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004069 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004070
William Lallemandd9e90662012-01-30 17:27:17 +01004071 ctx.idx = 0;
4072
Willy Tarreau211cdec2014-04-17 20:18:08 +02004073 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004074 if (old_o) {
4075 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004076 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004077 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004078 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004079 }
4080
Willy Tarreau9b28e032012-10-12 23:49:43 +02004081 old_i = chn->buf->i;
4082 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 -05004083 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004084 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004085 }
4086
4087 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004088 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004089 memcpy(hdr_val, hdr_name, hdr_name_len);
4090 hdr_val += hdr_name_len;
4091 *hdr_val++ = ':';
4092 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004093 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4094 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004095
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004096 if (old_o) {
4097 /* If this was a forwarded request, we must readjust the amount of
4098 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004099 * variations. Note that the current state is >= HTTP_MSG_BODY,
4100 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004101 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004102 old_o += chn->buf->i - old_i;
4103 b_adv(chn->buf, old_o);
4104 txn->req.next -= old_o;
4105 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004106 }
4107
Mark Lamourinec2247f02012-01-04 13:02:01 -05004108 return 0;
4109}
4110
Willy Tarreau610ecce2010-01-04 21:15:02 +01004111/* Terminate current transaction and prepare a new one. This is very tricky
4112 * right now but it works.
4113 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004114void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004115{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004116 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02004117 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01004118 struct proxy *be = s->be;
Willy Tarreau323a2d92015-08-04 19:00:17 +02004119 struct connection *srv_conn;
4120 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02004121 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01004122
Willy Tarreau610ecce2010-01-04 21:15:02 +01004123 /* FIXME: We need a more portable way of releasing a backend's and a
4124 * server's connections. We need a safer way to reinitialize buffer
4125 * flags. We also need a more accurate method for computing per-request
4126 * data.
4127 */
Willy Tarreau323a2d92015-08-04 19:00:17 +02004128 srv_conn = objt_conn(s->si[1].end);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004129
Willy Tarreau4213a112013-12-15 10:25:42 +01004130 /* unless we're doing keep-alive, we want to quickly close the connection
4131 * to the server.
4132 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004133 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004134 !si_conn_ready(&s->si[1])) {
4135 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4136 si_shutr(&s->si[1]);
4137 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01004138 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004139
Willy Tarreaue7dff022015-04-03 01:14:29 +02004140 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau858b1032015-12-07 17:04:59 +01004141 be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004142 if (unlikely(s->srv_conn))
4143 sess_change_server(s, NULL);
4144 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004145
4146 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02004147 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004148
Willy Tarreaueee5b512015-04-03 23:46:31 +02004149 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004150 int n;
4151
Willy Tarreaueee5b512015-04-03 23:46:31 +02004152 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004153 if (n < 1 || n > 5)
4154 n = 0;
4155
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004156 if (fe->mode == PR_MODE_HTTP) {
4157 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004158 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02004159 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01004160 (be->mode == PR_MODE_HTTP)) {
4161 be->be_counters.p.http.rsp[n]++;
4162 be->be_counters.p.http.cum_req++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004163 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004164 }
4165
4166 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004167 s->logs.bytes_in -= s->req.buf->i;
4168 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004169
4170 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004171 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02004172 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004173 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004174 s->do_log(s);
4175 }
4176
Willy Tarreaud713bcc2014-06-25 15:36:04 +02004177 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02004178 stream_stop_content_counters(s);
4179 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004180
Willy Tarreau610ecce2010-01-04 21:15:02 +01004181 s->logs.accept_date = date; /* user-visible date for logging */
4182 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02004183 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
4184 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004185 tv_zero(&s->logs.tv_request);
4186 s->logs.t_queue = -1;
4187 s->logs.t_connect = -1;
4188 s->logs.t_data = -1;
4189 s->logs.t_close = 0;
4190 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4191 s->logs.srv_queue_size = 0; /* we will get this number soon */
4192
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004193 s->logs.bytes_in = s->req.total = s->req.buf->i;
4194 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004195
4196 if (s->pend_pos)
4197 pendconn_free(s->pend_pos);
4198
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004199 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004200 if (s->flags & SF_CURR_SESS) {
4201 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004202 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004203 }
Willy Tarreau858b1032015-12-07 17:04:59 +01004204 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004205 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004206 }
4207
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004208 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004209
Willy Tarreau4213a112013-12-15 10:25:42 +01004210 /* only release our endpoint if we don't intend to reuse the
4211 * connection.
4212 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004213 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01004214 !si_conn_ready(&s->si[1])) {
4215 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02004216 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004217 }
4218
Willy Tarreau350f4872014-11-28 14:42:25 +01004219 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
4220 s->si[1].err_type = SI_ET_NONE;
4221 s->si[1].conn_retries = 0; /* used for logging too */
4222 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02004223 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 +01004224 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);
4225 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 +02004226 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
4227 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
4228 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01004229
Willy Tarreaueee5b512015-04-03 23:46:31 +02004230 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004231 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02004232 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004233
4234 if (prev_status == 401 || prev_status == 407) {
4235 /* In HTTP keep-alive mode, if we receive a 401, we still have
4236 * a chance of being able to send the visitor again to the same
4237 * server over the same connection. This is required by some
4238 * broken protocols such as NTLM, and anyway whenever there is
4239 * an opportunity for sending the challenge to the proper place,
4240 * it's better to do it (at least it helps with debugging).
4241 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02004242 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02004243 if (srv_conn)
4244 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01004245 }
4246
Willy Tarreau53f96852016-02-02 18:50:47 +01004247 /* Never ever allow to reuse a connection from a non-reuse backend */
4248 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
4249 srv_conn->flags |= CO_FL_PRIVATE;
4250
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004251 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01004252 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004253
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004254 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004255 s->req.flags |= CF_NEVER_WAIT;
4256 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004257 }
4258
Willy Tarreau714ea782015-11-25 20:11:11 +01004259 /* we're removing the analysers, we MUST re-enable events detection.
4260 * We don't enable close on the response channel since it's either
4261 * already closed, or in keep-alive with an idle connection handler.
4262 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004263 channel_auto_read(&s->req);
4264 channel_auto_close(&s->req);
4265 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004266
Willy Tarreau1c59bd52015-11-02 20:20:11 +01004267 /* we're in keep-alive with an idle connection, monitor it if not already done */
4268 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02004269 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02004270 if (!srv)
4271 si_idle_conn(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01004272 else if (srv_conn->flags & CO_FL_PRIVATE)
Willy Tarreau8dff9982015-08-04 20:45:52 +02004273 si_idle_conn(&s->si[1], &srv->priv_conns);
Willy Tarreau449d74a2015-08-05 17:16:33 +02004274 else if (prev_flags & TX_NOT_FIRST)
4275 /* note: we check the request, not the connection, but
4276 * this is valid for strategies SAFE and AGGR, and in
4277 * case of ALWS, we don't care anyway.
4278 */
4279 si_idle_conn(&s->si[1], &srv->safe_conns);
Willy Tarreau8dff9982015-08-04 20:45:52 +02004280 else
4281 si_idle_conn(&s->si[1], &srv->idle_conns);
Willy Tarreau4320eaa2015-08-05 11:08:30 +02004282 }
Christopher Faulete6006242017-03-10 11:52:44 +01004283 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
4284 s->res.analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004285}
4286
4287
4288/* This function updates the request state machine according to the response
4289 * state machine and buffer flags. It returns 1 if it changes anything (flag
4290 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4291 * it is only used to find when a request/response couple is complete. Both
4292 * this function and its equivalent should loop until both return zero. It
4293 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4294 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004295int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004296{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004297 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004298 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004299 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004300 unsigned int old_state = txn->req.msg_state;
4301
Christopher Faulet4be98032017-07-18 10:48:24 +02004302 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004303 return 0;
4304
4305 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004306 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004307 * We can shut the read side unless we want to abort_on_close,
4308 * or we have a POST request. The issue with POST requests is
4309 * that some browsers still send a CRLF after the request, and
4310 * this CRLF must be read so that it does not remain in the kernel
4311 * buffers, otherwise a close could cause an RST on some systems
4312 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004313 * Note that if we're using keep-alive on the client side, we'd
4314 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02004315 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01004316 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004317 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004318 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4319 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4320 !(s->be->options & PR_O_ABRT_CLOSE) &&
4321 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004322 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004323
Willy Tarreau40f151a2012-12-20 12:10:09 +01004324 /* if the server closes the connection, we want to immediately react
4325 * and close the socket to save packets and syscalls.
4326 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004327 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01004328
Willy Tarreau7f876a12015-11-18 11:59:55 +01004329 /* In any case we've finished parsing the request so we must
4330 * disable Nagle when sending data because 1) we're not going
4331 * to shut this side, and 2) the server is waiting for us to
4332 * send pending data.
4333 */
4334 chn->flags |= CF_NEVER_WAIT;
4335
Willy Tarreau610ecce2010-01-04 21:15:02 +01004336 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4337 goto wait_other_side;
4338
4339 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4340 /* The server has not finished to respond, so we
4341 * don't want to move in order not to upset it.
4342 */
4343 goto wait_other_side;
4344 }
4345
Willy Tarreau610ecce2010-01-04 21:15:02 +01004346 /* When we get here, it means that both the request and the
4347 * response have finished receiving. Depending on the connection
4348 * mode, we'll have to wait for the last bytes to leave in either
4349 * direction, and sometimes for a close to be effective.
4350 */
4351
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004352 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4353 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004354 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4355 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004356 }
4357 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4358 /* Option forceclose is set, or either side wants to close,
4359 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004360 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004361 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004362 *
4363 * However, there is an exception if the response
4364 * length is undefined. In this case, we need to wait
4365 * the close from the server. The response will be
4366 * switched in TUNNEL mode until the end.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004367 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004368 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4369 txn->rsp.msg_state != HTTP_MSG_CLOSED)
4370 goto check_channel_flags;
4371
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004372 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4373 channel_shutr_now(chn);
4374 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004375 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004376 }
4377 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004378 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4379 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004380 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004381 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4382 channel_auto_read(chn);
4383 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01004384 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004385 }
4386
Christopher Faulet4be98032017-07-18 10:48:24 +02004387 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004388 }
4389
4390 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4391 http_msg_closing:
4392 /* nothing else to forward, just waiting for the output buffer
4393 * to be empty and for the shutw_now to take effect.
4394 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004395 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004396 txn->req.msg_state = HTTP_MSG_CLOSED;
4397 goto http_msg_closed;
4398 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004399 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004400 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004401 txn->req.msg_state = HTTP_MSG_ERROR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004402 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004403 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004404 }
4405
4406 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4407 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01004408 /* see above in MSG_DONE why we only do this in these states */
4409 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4410 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4411 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004412 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004413 goto wait_other_side;
4414 }
4415
Christopher Faulet4be98032017-07-18 10:48:24 +02004416 check_channel_flags:
4417 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4418 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4419 /* if we've just closed an output, let's switch */
4420 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4421 txn->req.msg_state = HTTP_MSG_CLOSING;
4422 goto http_msg_closing;
4423 }
4424
4425
Willy Tarreau610ecce2010-01-04 21:15:02 +01004426 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004427 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004428}
4429
4430
4431/* This function updates the response state machine according to the request
4432 * state machine and buffer flags. It returns 1 if it changes anything (flag
4433 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4434 * it is only used to find when a request/response couple is complete. Both
4435 * this function and its equivalent should loop until both return zero. It
4436 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4437 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004438int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004439{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004440 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004441 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004442 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004443 unsigned int old_state = txn->rsp.msg_state;
4444
Christopher Faulet4be98032017-07-18 10:48:24 +02004445 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004446 return 0;
4447
4448 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4449 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004450 * still monitor the server connection for a possible close
4451 * while the request is being uploaded, so we don't disable
4452 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004453 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004454 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004455
4456 if (txn->req.msg_state == HTTP_MSG_ERROR)
4457 goto wait_other_side;
4458
4459 if (txn->req.msg_state < HTTP_MSG_DONE) {
4460 /* The client seems to still be sending data, probably
4461 * because we got an error response during an upload.
4462 * We have the choice of either breaking the connection
4463 * or letting it pass through. Let's do the later.
4464 */
4465 goto wait_other_side;
4466 }
4467
Willy Tarreau610ecce2010-01-04 21:15:02 +01004468 /* When we get here, it means that both the request and the
4469 * response have finished receiving. Depending on the connection
4470 * mode, we'll have to wait for the last bytes to leave in either
4471 * direction, and sometimes for a close to be effective.
4472 */
4473
4474 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4475 /* Server-close mode : shut read and wait for the request
4476 * side to close its output buffer. The caller will detect
4477 * when we're in DONE and the other is in CLOSED and will
4478 * catch that for the final cleanup.
4479 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004480 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4481 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004482 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004483 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4484 /* Option forceclose is set, or either side wants to close,
4485 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02004486 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004487 * once both states are CLOSED.
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004488 *
4489 * However, there is an exception if the response length
4490 * is undefined. In this case, we switch in TUNNEL mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004491 */
Christopher Faulet1486b0a2017-07-18 11:42:08 +02004492 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN)) {
4493 channel_auto_read(chn);
4494 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4495 chn->flags |= CF_NEVER_WAIT;
4496 }
4497 else if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004498 channel_shutr_now(chn);
4499 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004500 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004501 }
4502 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004503 /* The last possible modes are keep-alive and tunnel. Tunnel will
4504 * need to forward remaining data. Keep-alive will need to monitor
4505 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004506 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004507 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004508 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004509 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4510 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004511 }
4512
Christopher Faulet4be98032017-07-18 10:48:24 +02004513 goto check_channel_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004514 }
4515
4516 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4517 http_msg_closing:
4518 /* nothing else to forward, just waiting for the output buffer
4519 * to be empty and for the shutw_now to take effect.
4520 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004521 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004522 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4523 goto http_msg_closed;
4524 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004525 else if (chn->flags & CF_SHUTW) {
Christopher Fauleta3992e02017-07-18 10:35:55 +02004526 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004527 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004528 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004529 if (objt_server(s->target))
4530 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004531 }
Christopher Faulet56d26092017-07-20 11:05:10 +02004532 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004533 }
4534
4535 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4536 http_msg_closed:
4537 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01004538 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004539 channel_auto_close(chn);
4540 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004541 goto wait_other_side;
4542 }
4543
Christopher Faulet4be98032017-07-18 10:48:24 +02004544 check_channel_flags:
4545 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4546 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4547 /* if we've just closed an output, let's switch */
4548 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4549 goto http_msg_closing;
4550 }
4551
Willy Tarreau610ecce2010-01-04 21:15:02 +01004552 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004553 /* We force the response to leave immediately if we're waiting for the
4554 * other side, since there is no pending shutdown to push it out.
4555 */
4556 if (!channel_is_empty(chn))
4557 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004558 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004559}
4560
4561
Christopher Faulet894da4c2017-07-18 11:29:07 +02004562/* Resync the request and response state machines. */
4563void http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004564{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004565 struct http_txn *txn = s->txn;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004566#ifdef DEBUG_FULL
Willy Tarreau610ecce2010-01-04 21:15:02 +01004567 int old_req_state = txn->req.msg_state;
4568 int old_res_state = txn->rsp.msg_state;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004569#endif
Willy Tarreau610ecce2010-01-04 21:15:02 +01004570
Willy Tarreau610ecce2010-01-04 21:15:02 +01004571 http_sync_req_state(s);
4572 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004573 if (!http_sync_res_state(s))
4574 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004575 if (!http_sync_req_state(s))
4576 break;
4577 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004578
Christopher Faulet894da4c2017-07-18 11:29:07 +02004579 DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s "
4580 "req->analysers=0x%08x res->analysers=0x%08x\n",
4581 now_ms, __FUNCTION__, s,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02004582 h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state),
4583 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
Christopher Faulet894da4c2017-07-18 11:29:07 +02004584 s->req.analysers, s->res.analysers);
Christopher Faulet814d2702017-03-30 11:33:44 +02004585
4586
Willy Tarreau610ecce2010-01-04 21:15:02 +01004587 /* OK, both state machines agree on a compatible state.
4588 * There are a few cases we're interested in :
Willy Tarreau610ecce2010-01-04 21:15:02 +01004589 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4590 * directions, so let's simply disable both analysers.
Christopher Fauletf77bb532017-07-18 11:18:46 +02004591 * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either
4592 * means we must abort the request.
4593 * - HTTP_MSG_TUNNEL on either means we have to disable analyser on
4594 * corresponding channel.
4595 * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE
4596 * on the response with server-close mode means we've completed one
4597 * request and we must re-initialize the server connection.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004598 */
Christopher Fauletf77bb532017-07-18 11:18:46 +02004599 if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4600 txn->rsp.msg_state == HTTP_MSG_CLOSED) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004601 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004602 channel_auto_close(&s->req);
4603 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004604 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004605 channel_auto_close(&s->res);
4606 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004607 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004608 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4609 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004610 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004611 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004612 channel_auto_close(&s->res);
4613 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004614 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004615 channel_abort(&s->req);
4616 channel_auto_close(&s->req);
4617 channel_auto_read(&s->req);
4618 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004619 }
Christopher Fauletf77bb532017-07-18 11:18:46 +02004620 else if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4621 txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4622 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4623 s->req.analysers &= AN_REQ_FLT_END;
4624 if (HAS_REQ_DATA_FILTERS(s))
4625 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
4626 }
4627 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4628 s->res.analysers &= AN_RES_FLT_END;
4629 if (HAS_RSP_DATA_FILTERS(s))
4630 s->res.analysers |= AN_RES_FLT_XFER_DATA;
4631 }
4632 channel_auto_close(&s->req);
4633 channel_auto_read(&s->req);
4634 channel_auto_close(&s->res);
4635 channel_auto_read(&s->res);
4636 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004637 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4638 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004639 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004640 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4641 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4642 /* server-close/keep-alive: terminate this transaction,
4643 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004644 * a fresh-new transaction, but only once we're sure there's
4645 * enough room in the request and response buffer to process
Christopher Fauletc0c672a2017-03-28 11:51:33 +02004646 * another request. They must not hold any pending output data
4647 * and the response buffer must realigned
4648 * (realign is done is http_end_txn_clean_session).
Willy Tarreau610ecce2010-01-04 21:15:02 +01004649 */
Christopher Faulet894da4c2017-07-18 11:29:07 +02004650 if (s->req.buf->o)
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004651 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet894da4c2017-07-18 11:29:07 +02004652 else if (s->res.buf->o)
Willy Tarreau3de5bd62016-05-02 16:07:07 +02004653 s->res.flags |= CF_WAKE_WRITE;
Christopher Fauleta81ff602017-07-18 22:01:05 +02004654 else {
4655 s->req.analysers = AN_REQ_FLT_END;
4656 s->res.analysers = AN_RES_FLT_END;
4657 txn->flags |= TX_WAIT_CLEANUP;
4658 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004659 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004660}
4661
Willy Tarreaud98cf932009-12-27 22:54:55 +01004662/* This function is an analyser which forwards request body (including chunk
4663 * sizes if any). It is called as soon as we must forward, even if we forward
4664 * zero byte. The only situation where it must not be called is when we're in
4665 * tunnel mode and we want to forward till the close. It's used both to forward
4666 * remaining data and to resync after end of body. It expects the msg_state to
4667 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02004668 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01004669 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004670 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004671 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004672int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004673{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004674 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004675 struct http_txn *txn = s->txn;
4676 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01004677 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004678
Christopher Faulet814d2702017-03-30 11:33:44 +02004679 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
4680 now_ms, __FUNCTION__,
4681 s,
4682 req,
4683 req->rex, req->wex,
4684 req->flags,
4685 req->buf->i,
4686 req->analysers);
4687
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004688 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4689 return 0;
4690
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004691 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004692 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004693 /* Output closed while we were sending data. We must abort and
4694 * wake the other side up.
4695 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004696 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02004697 msg->msg_state = HTTP_MSG_ERROR;
4698 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004699 return 1;
4700 }
4701
Willy Tarreaud98cf932009-12-27 22:54:55 +01004702 /* Note that we don't have to send 100-continue back because we don't
4703 * need the data to complete our job, and it's up to the server to
4704 * decide whether to return 100, 417 or anything else in return of
4705 * an "Expect: 100-continue" header.
4706 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02004707 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004708 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
4709 ? HTTP_MSG_CHUNK_SIZE
4710 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004711
4712 /* TODO/filters: when http-buffer-request option is set or if a
4713 * rule on url_param exists, the first chunk size could be
4714 * already parsed. In that case, msg->next is after the chunk
4715 * size (including the CRLF after the size). So this case should
4716 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004717 }
4718
Willy Tarreau7ba23542014-04-17 21:50:00 +02004719 /* Some post-connect processing might want us to refrain from starting to
4720 * forward data. Currently, the only reason for this is "balance url_param"
4721 * whichs need to parse/process the request after we've enabled forwarding.
4722 */
4723 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004724 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02004725 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02004726 req->flags |= CF_WAKE_CONNECT;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004727 goto missing_data_or_waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02004728 }
4729 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
4730 }
4731
Willy Tarreau80a92c02014-03-12 10:41:13 +01004732 /* in most states, we should abort in case of early close */
4733 channel_auto_close(req);
4734
Willy Tarreauefdf0942014-04-24 20:08:57 +02004735 if (req->to_forward) {
4736 /* We can't process the buffer's contents yet */
4737 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004738 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02004739 }
4740
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004741 if (msg->msg_state < HTTP_MSG_DONE) {
4742 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
4743 ? http_msg_forward_chunked_body(s, msg)
4744 : http_msg_forward_body(s, msg));
4745 if (!ret)
4746 goto missing_data_or_waiting;
4747 if (ret < 0)
4748 goto return_bad_req;
4749 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02004750
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004751 /* other states, DONE...TUNNEL */
4752 /* we don't want to forward closes on DONE except in tunnel mode. */
4753 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4754 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004755
Christopher Faulet894da4c2017-07-18 11:29:07 +02004756 http_resync_states(s);
4757 if (!(req->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004758 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4759 if (req->flags & CF_SHUTW) {
4760 /* request errors are most likely due to the
4761 * server aborting the transfer. */
4762 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004763 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004764 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004765 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004766 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004767 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004768 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004769 }
4770
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004771 /* If "option abortonclose" is set on the backend, we want to monitor
4772 * the client's connection and forward any shutdown notification to the
4773 * server, which will decide whether to close or to go on processing the
4774 * request. We only do that in tunnel mode, and not in other modes since
4775 * it can be abused to exhaust source ports. */
4776 if (s->be->options & PR_O_ABRT_CLOSE) {
4777 channel_auto_read(req);
4778 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
4779 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
4780 s->si[1].flags |= SI_FL_NOLINGER;
4781 channel_auto_close(req);
4782 }
4783 else if (s->txn->meth == HTTP_METH_POST) {
4784 /* POST requests may require to read extra CRLF sent by broken
4785 * browsers and which could cause an RST to be sent upon close
4786 * on some systems (eg: Linux). */
4787 channel_auto_read(req);
4788 }
4789 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004790
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01004791 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004792 /* stop waiting for data if the input is closed before the end */
Christopher Fauleta33510b2017-03-31 15:37:29 +02004793 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02004794 if (!(s->flags & SF_ERR_MASK))
4795 s->flags |= SF_ERR_CLICL;
4796 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004797 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004798 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004799 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004800 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004801 }
4802
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004803 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004804 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004805 if (objt_server(s->target))
4806 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004807
4808 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004809 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004810
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004811 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004812 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004813 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004814
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004815 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004816 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004817 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004818 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004819 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004820
Willy Tarreau5c620922011-05-11 19:56:11 +02004821 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004822 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004823 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004824 * modes are already handled by the stream sock layer. We must not do
4825 * this in content-length mode because it could present the MSG_MORE
4826 * flag with the last block of forwarded data, which would cause an
4827 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004828 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004829 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004830 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004831
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004832 return 0;
4833
Willy Tarreaud98cf932009-12-27 22:54:55 +01004834 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004835 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004836 if (sess->listener->counters)
4837 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02004838
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004839 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004840 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004841 txn->req.msg_state = HTTP_MSG_ERROR;
4842 if (txn->status) {
4843 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004844 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004845 } else {
4846 txn->status = 400;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004847 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004848 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004849 req->analysers &= AN_REQ_FLT_END;
4850 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 +01004851
Willy Tarreaue7dff022015-04-03 01:14:29 +02004852 if (!(s->flags & SF_ERR_MASK))
4853 s->flags |= SF_ERR_PRXCOND;
4854 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004855 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004856 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004857 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004858 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004859 }
4860 return 0;
4861
4862 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004863 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004864 txn->req.msg_state = HTTP_MSG_ERROR;
4865 if (txn->status) {
4866 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004867 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004868 } else {
4869 txn->status = 502;
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02004870 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004871 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01004872 req->analysers &= AN_REQ_FLT_END;
4873 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 +01004874
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004875 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004876 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004877 if (objt_server(s->target))
4878 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004879
Willy Tarreaue7dff022015-04-03 01:14:29 +02004880 if (!(s->flags & SF_ERR_MASK))
4881 s->flags |= SF_ERR_SRVCL;
4882 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004883 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004884 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004885 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02004886 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004887 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004888 return 0;
4889}
4890
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004891/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4892 * processing can continue on next analysers, or zero if it either needs more
4893 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004894 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004895 * when it has nothing left to do, and may remove any analyser when it wants to
4896 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004897 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004898int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004899{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004900 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004901 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004902 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004903 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004904 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004905 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004906 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004907
Willy Tarreau87b09662015-04-03 00:22:06 +02004908 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 +02004909 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004910 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004911 rep,
4912 rep->rex, rep->wex,
4913 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004914 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004915 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004916
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004917 /*
4918 * Now parse the partial (or complete) lines.
4919 * We will check the response syntax, and also join multi-line
4920 * headers. An index of all the lines will be elaborated while
4921 * parsing.
4922 *
4923 * For the parsing, we use a 28 states FSM.
4924 *
4925 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02004926 * rep->buf->p = beginning of response
4927 * rep->buf->p + msg->eoh = end of processed headers / start of current one
4928 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02004929 * msg->eol = end of current header or line (LF or CRLF)
4930 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004931 */
4932
Willy Tarreau628c40c2014-04-24 19:11:26 +02004933 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01004934 /* There's a protected area at the end of the buffer for rewriting
4935 * purposes. We don't want to start to parse the request if the
4936 * protected area is affected, because we may have to move processed
4937 * data later, which is much more complicated.
4938 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004939 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01004940 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02004941 /* some data has still not left the buffer, wake us once that's done */
4942 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
4943 goto abort_response;
4944 channel_dont_close(rep);
4945 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004946 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02004947 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01004948 }
4949
Willy Tarreau379357a2013-06-08 12:55:46 +02004950 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
4951 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
4952 buffer_slow_realign(rep->buf);
4953
Willy Tarreau9b28e032012-10-12 23:49:43 +02004954 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01004955 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004956 }
4957
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004958 /* 1: we might have to print this header in debug mode */
4959 if (unlikely((global.mode & MODE_DEBUG) &&
4960 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02004961 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004962 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004963
Willy Tarreau9b28e032012-10-12 23:49:43 +02004964 sol = rep->buf->p;
4965 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004966 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004967
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004968 sol += hdr_idx_first_pos(&txn->hdr_idx);
4969 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004970
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004971 while (cur_idx) {
4972 eol = sol + txn->hdr_idx.v[cur_idx].len;
4973 debug_hdr("srvhdr", s, sol, eol);
4974 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4975 cur_idx = txn->hdr_idx.v[cur_idx].next;
4976 }
4977 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004978
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004979 /*
4980 * Now we quickly check if we have found a full valid response.
4981 * If not so, we check the FD and buffer states before leaving.
4982 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004983 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004984 * responses are checked first.
4985 *
4986 * Depending on whether the client is still there or not, we
4987 * may send an error response back or not. Note that normally
4988 * we should only check for HTTP status there, and check I/O
4989 * errors somewhere else.
4990 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004991
Willy Tarreau655dce92009-11-08 13:10:58 +01004992 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004993 /* Invalid response */
4994 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4995 /* we detected a parsing error. We want to archive this response
4996 * in the dedicated proxy area for later troubleshooting.
4997 */
4998 hdr_response_bad:
4999 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005000 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005001
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005002 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005003 if (objt_server(s->target)) {
5004 objt_server(s->target)->counters.failed_resp++;
5005 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005006 }
Willy Tarreau64648412010-03-05 10:41:54 +01005007 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005008 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005009 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005010 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005011 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005012 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005013 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005014
Willy Tarreaue7dff022015-04-03 01:14:29 +02005015 if (!(s->flags & SF_ERR_MASK))
5016 s->flags |= SF_ERR_PRXCOND;
5017 if (!(s->flags & SF_FINST_MASK))
5018 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005019
5020 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005021 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005022
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005023 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005024 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005025 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005026 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005027 goto hdr_response_bad;
5028 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005029
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005030 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005031 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005032 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005033 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005034 else if (txn->flags & TX_NOT_FIRST)
5035 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005036
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005037 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005038 if (objt_server(s->target)) {
5039 objt_server(s->target)->counters.failed_resp++;
5040 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005041 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005042
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005043 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005044 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005045 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005046 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005047 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005048 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau816b9792009-09-15 21:25:21 +02005049
Willy Tarreaue7dff022015-04-03 01:14:29 +02005050 if (!(s->flags & SF_ERR_MASK))
5051 s->flags |= SF_ERR_SRVCL;
5052 if (!(s->flags & SF_FINST_MASK))
5053 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005054 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005055 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005056
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005057 /* read timeout : return a 504 to the client. */
5058 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005059 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005060 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005061
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005062 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005063 if (objt_server(s->target)) {
5064 objt_server(s->target)->counters.failed_resp++;
5065 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005066 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005067
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005068 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005069 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005070 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01005071 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005072 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005073 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau4076a152009-04-02 15:18:36 +02005074
Willy Tarreaue7dff022015-04-03 01:14:29 +02005075 if (!(s->flags & SF_ERR_MASK))
5076 s->flags |= SF_ERR_SRVTO;
5077 if (!(s->flags & SF_FINST_MASK))
5078 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005079 return 0;
5080 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005081
Willy Tarreauf003d372012-11-26 13:35:37 +01005082 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005083 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005084 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01005085 s->be->be_counters.cli_aborts++;
5086 if (objt_server(s->target))
5087 objt_server(s->target)->counters.cli_aborts++;
5088
Christopher Faulet0184ea72017-01-05 14:06:34 +01005089 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01005090 channel_auto_close(rep);
5091
5092 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01005093 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005094 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreauf003d372012-11-26 13:35:37 +01005095
Willy Tarreaue7dff022015-04-03 01:14:29 +02005096 if (!(s->flags & SF_ERR_MASK))
5097 s->flags |= SF_ERR_CLICL;
5098 if (!(s->flags & SF_FINST_MASK))
5099 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01005100
Willy Tarreau87b09662015-04-03 00:22:06 +02005101 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01005102 return 0;
5103 }
5104
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005105 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005106 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005107 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005108 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005109 else if (txn->flags & TX_NOT_FIRST)
5110 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005111
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005112 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005113 if (objt_server(s->target)) {
5114 objt_server(s->target)->counters.failed_resp++;
5115 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005116 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005117
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005118 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005119 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005120 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005121 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005122 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005123 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005124
Willy Tarreaue7dff022015-04-03 01:14:29 +02005125 if (!(s->flags & SF_ERR_MASK))
5126 s->flags |= SF_ERR_SRVCL;
5127 if (!(s->flags & SF_FINST_MASK))
5128 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005129 return 0;
5130 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005131
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005132 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005133 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005134 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005135 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005136 else if (txn->flags & TX_NOT_FIRST)
5137 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005138
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005139 s->be->be_counters.failed_resp++;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005140 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005141 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005142
Willy Tarreaue7dff022015-04-03 01:14:29 +02005143 if (!(s->flags & SF_ERR_MASK))
5144 s->flags |= SF_ERR_CLICL;
5145 if (!(s->flags & SF_FINST_MASK))
5146 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005147
Willy Tarreau87b09662015-04-03 00:22:06 +02005148 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005149 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005150 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005151
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005152 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005153 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005154 return 0;
5155 }
5156
5157 /* More interesting part now : we know that we have a complete
5158 * response which at least looks like HTTP. We have an indicator
5159 * of each header's length, so we can parse them quickly.
5160 */
5161
5162 if (unlikely(msg->err_pos >= 0))
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005163 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005164
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005165 /*
5166 * 1: get the status code
5167 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005168 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005169 if (n < 1 || n > 5)
5170 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005171 /* when the client triggers a 4xx from the server, it's most often due
5172 * to a missing object or permission. These events should be tracked
5173 * because if they happen often, it may indicate a brute force or a
5174 * vulnerability scan.
5175 */
5176 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02005177 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02005178
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005179 if (objt_server(s->target))
5180 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005181
Willy Tarreau91852eb2015-05-01 13:26:00 +02005182 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
5183 * exactly one digit "." one digit. This check may be disabled using
5184 * option accept-invalid-http-response.
5185 */
5186 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
5187 if (msg->sl.st.v_l != 8) {
5188 msg->err_pos = 0;
5189 goto hdr_response_bad;
5190 }
5191
5192 if (rep->buf->p[4] != '/' ||
5193 !isdigit((unsigned char)rep->buf->p[5]) ||
5194 rep->buf->p[6] != '.' ||
5195 !isdigit((unsigned char)rep->buf->p[7])) {
5196 msg->err_pos = 4;
5197 goto hdr_response_bad;
5198 }
5199 }
5200
Willy Tarreau5b154472009-12-21 20:11:07 +01005201 /* check if the response is HTTP/1.1 or above */
5202 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005203 ((rep->buf->p[5] > '1') ||
5204 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005205 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005206
5207 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005208 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 +01005209
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005210 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005211 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005212
Willy Tarreau9b28e032012-10-12 23:49:43 +02005213 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005214
Willy Tarreau39650402010-03-15 19:44:39 +01005215 /* Adjust server's health based on status code. Note: status codes 501
5216 * and 505 are triggered on demand by client request, so we must not
5217 * count them as server failures.
5218 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005219 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005220 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005221 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005222 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005223 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005224 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005225
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005226 /*
Willy Tarreaua14ad722017-07-07 11:36:32 +02005227 * We may be facing a 100-continue response, or any other informational
5228 * 1xx response which is non-final, in which case this is not the right
5229 * response, and we're waiting for the next one. Let's allow this response
5230 * to go to the client and wait for the next one. There's an exception for
5231 * 101 which is used later in the code to switch protocols.
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005232 */
Willy Tarreaua14ad722017-07-07 11:36:32 +02005233 if (txn->status < 200 &&
5234 (txn->status == 100 || txn->status >= 102)) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02005235 hdr_idx_init(&txn->hdr_idx);
5236 msg->next -= channel_forward(rep, msg->next);
5237 msg->msg_state = HTTP_MSG_RPBEFORE;
5238 txn->status = 0;
5239 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01005240 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02005241 goto next_one;
Willy Tarreaua14ad722017-07-07 11:36:32 +02005242 }
Willy Tarreau628c40c2014-04-24 19:11:26 +02005243
Willy Tarreaua14ad722017-07-07 11:36:32 +02005244 /*
5245 * 2: check for cacheability.
5246 */
5247
5248 switch (txn->status) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005249 case 200:
5250 case 203:
5251 case 206:
5252 case 300:
5253 case 301:
5254 case 410:
5255 /* RFC2616 @13.4:
5256 * "A response received with a status code of
5257 * 200, 203, 206, 300, 301 or 410 MAY be stored
5258 * by a cache (...) unless a cache-control
5259 * directive prohibits caching."
5260 *
5261 * RFC2616 @9.5: POST method :
5262 * "Responses to this method are not cacheable,
5263 * unless the response includes appropriate
5264 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005265 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005266 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005267 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005268 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5269 break;
5270 default:
5271 break;
5272 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005273
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005274 /*
5275 * 3: we may need to capture headers
5276 */
5277 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005278 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005279 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02005280 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005281
Willy Tarreau557f1992015-05-01 10:05:17 +02005282 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
5283 * by RFC7230#3.3.3 :
5284 *
5285 * The length of a message body is determined by one of the following
5286 * (in order of precedence):
5287 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005288 * 1. Any 2xx (Successful) response to a CONNECT request implies that
5289 * the connection will become a tunnel immediately after the empty
5290 * line that concludes the header fields. A client MUST ignore
5291 * any Content-Length or Transfer-Encoding header fields received
5292 * in such a message. Any 101 response (Switching Protocols) is
5293 * managed in the same manner.
5294 *
5295 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02005296 * (Informational), 204 (No Content), or 304 (Not Modified) status
5297 * code is always terminated by the first empty line after the
5298 * header fields, regardless of the header fields present in the
5299 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005300 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005301 * 3. If a Transfer-Encoding header field is present and the chunked
5302 * transfer coding (Section 4.1) is the final encoding, the message
5303 * body length is determined by reading and decoding the chunked
5304 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005305 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005306 * If a Transfer-Encoding header field is present in a response and
5307 * the chunked transfer coding is not the final encoding, the
5308 * message body length is determined by reading the connection until
5309 * it is closed by the server. If a Transfer-Encoding header field
5310 * is present in a request and the chunked transfer coding is not
5311 * the final encoding, the message body length cannot be determined
5312 * reliably; the server MUST respond with the 400 (Bad Request)
5313 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005314 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005315 * If a message is received with both a Transfer-Encoding and a
5316 * Content-Length header field, the Transfer-Encoding overrides the
5317 * Content-Length. Such a message might indicate an attempt to
5318 * perform request smuggling (Section 9.5) or response splitting
5319 * (Section 9.4) and ought to be handled as an error. A sender MUST
5320 * remove the received Content-Length field prior to forwarding such
5321 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005322 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005323 * 4. If a message is received without Transfer-Encoding and with
5324 * either multiple Content-Length header fields having differing
5325 * field-values or a single Content-Length header field having an
5326 * invalid value, then the message framing is invalid and the
5327 * recipient MUST treat it as an unrecoverable error. If this is a
5328 * request message, the server MUST respond with a 400 (Bad Request)
5329 * status code and then close the connection. If this is a response
5330 * message received by a proxy, the proxy MUST close the connection
5331 * to the server, discard the received response, and send a 502 (Bad
5332 * Gateway) response to the client. If this is a response message
5333 * received by a user agent, the user agent MUST close the
5334 * connection to the server and discard the received response.
5335 *
5336 * 5. If a valid Content-Length header field is present without
5337 * Transfer-Encoding, its decimal value defines the expected message
5338 * body length in octets. If the sender closes the connection or
5339 * the recipient times out before the indicated number of octets are
5340 * received, the recipient MUST consider the message to be
5341 * incomplete and close the connection.
5342 *
5343 * 6. If this is a request message and none of the above are true, then
5344 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005345 *
Willy Tarreau557f1992015-05-01 10:05:17 +02005346 * 7. Otherwise, this is a response message without a declared message
5347 * body length, so the message body length is determined by the
5348 * number of octets received prior to the server closing the
5349 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005350 */
5351
5352 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005353 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005354 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005355 * FIXME: should we parse anyway and return an error on chunked encoding ?
5356 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005357 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5358 txn->status == 101)) {
5359 /* Either we've established an explicit tunnel, or we're
5360 * switching the protocol. In both cases, we're very unlikely
5361 * to understand the next protocols. We have to switch to tunnel
5362 * mode, so that we transfer the request and responses then let
5363 * this protocol pass unmodified. When we later implement specific
5364 * parsers for such protocols, we'll want to check the Upgrade
5365 * header which contains information about that protocol for
5366 * responses with status 101 (eg: see RFC2817 about TLS).
5367 */
5368 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5369 msg->flags |= HTTP_MSGF_XFER_LEN;
5370 goto end;
5371 }
5372
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005373 if (txn->meth == HTTP_METH_HEAD ||
5374 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005375 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005376 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005377 goto skip_content_length;
5378 }
5379
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005380 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005381 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02005382 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005383 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005384 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5385 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005386 /* bad transfer-encoding (chunked followed by something else) */
5387 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005388 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005389 break;
5390 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005391 }
5392
Willy Tarreau1c913912015-04-30 10:57:51 +02005393 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005394 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005395 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02005396 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
5397 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5398 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02005399 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005400 signed long long cl;
5401
Willy Tarreauad14f752011-09-02 20:33:27 +02005402 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005403 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005404 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005405 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005406
Willy Tarreauad14f752011-09-02 20:33:27 +02005407 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005408 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005409 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005410 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005411
Willy Tarreauad14f752011-09-02 20:33:27 +02005412 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005413 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005414 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005415 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005416
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005417 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005418 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005419 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005420 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005421
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005422 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005423 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005424 }
5425
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005426 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01005427 /* Now we have to check if we need to modify the Connection header.
5428 * This is more difficult on the response than it is on the request,
5429 * because we can have two different HTTP versions and we don't know
5430 * how the client will interprete a response. For instance, let's say
5431 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5432 * HTTP/1.1 response without any header. Maybe it will bound itself to
5433 * HTTP/1.0 because it only knows about it, and will consider the lack
5434 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5435 * the lack of header as a keep-alive. Thus we will use two flags
5436 * indicating how a request MAY be understood by the client. In case
5437 * of multiple possibilities, we'll fix the header to be explicit. If
5438 * ambiguous cases such as both close and keepalive are seen, then we
5439 * will fall back to explicit close. Note that we won't take risks with
5440 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005441 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005442 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005443 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5444 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5445 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5446 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005447 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005448
Willy Tarreau70dffda2014-01-30 03:07:23 +01005449 /* this situation happens when combining pretend-keepalive with httpclose. */
5450 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005451 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005452 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01005453 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
5454
Willy Tarreau60466522010-01-18 19:08:45 +01005455 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005456 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005457 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5458 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005459
Willy Tarreau60466522010-01-18 19:08:45 +01005460 /* now adjust header transformations depending on current state */
5461 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5462 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5463 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005464 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005465 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005466 }
Willy Tarreau60466522010-01-18 19:08:45 +01005467 else { /* SCL / KAL */
5468 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005469 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005470 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005471 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005472
Willy Tarreau60466522010-01-18 19:08:45 +01005473 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005474 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005475
Willy Tarreau60466522010-01-18 19:08:45 +01005476 /* Some keep-alive responses are converted to Server-close if
5477 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005478 */
Willy Tarreau60466522010-01-18 19:08:45 +01005479 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5480 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005481 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005482 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005483 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005484 }
5485
Christopher Fauletd1cd2092016-11-28 10:14:03 +01005486 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02005487 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005488 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005489
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005490 /* end of job, return OK */
5491 rep->analysers &= ~an_bit;
5492 rep->analyse_exp = TICK_ETERNITY;
5493 channel_auto_close(rep);
5494 return 1;
5495
5496 abort_keep_alive:
5497 /* A keep-alive request to the server failed on a network error.
5498 * The client is required to retry. We need to close without returning
5499 * any other information so that the client retries.
5500 */
5501 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01005502 rep->analysers &= AN_RES_FLT_END;
5503 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005504 channel_auto_close(rep);
5505 s->logs.logwait = 0;
5506 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005507 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01005508 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005509 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005510 return 0;
5511}
5512
5513/* This function performs all the processing enabled for the current response.
5514 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005515 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005516 * other functions. It works like process_request (see indications above).
5517 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005518int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005519{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005520 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005521 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005522 struct http_msg *msg = &txn->rsp;
5523 struct proxy *cur_proxy;
5524 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01005525 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005526
Willy Tarreau87b09662015-04-03 00:22:06 +02005527 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 +02005528 now_ms, __FUNCTION__,
5529 s,
5530 rep,
5531 rep->rex, rep->wex,
5532 rep->flags,
5533 rep->buf->i,
5534 rep->analysers);
5535
5536 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
5537 return 0;
5538
Willy Tarreau70730dd2014-04-24 18:06:27 +02005539 /* The stats applet needs to adjust the Connection header but we don't
5540 * apply any filter there.
5541 */
Willy Tarreau612adb82015-03-10 15:25:54 +01005542 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
5543 rep->analysers &= ~an_bit;
5544 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02005545 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01005546 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02005547
Willy Tarreau58975672014-04-24 21:13:57 +02005548 /*
5549 * We will have to evaluate the filters.
5550 * As opposed to version 1.2, now they will be evaluated in the
5551 * filters order and not in the header order. This means that
5552 * each filter has to be validated among all headers.
5553 *
5554 * Filters are tried with ->be first, then with ->fe if it is
5555 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005556 *
5557 * Maybe we are in resume condiion. In this case I choose the
5558 * "struct proxy" which contains the rule list matching the resume
5559 * pointer. If none of theses "struct proxy" match, I initialise
5560 * the process with the first one.
5561 *
5562 * In fact, I check only correspondance betwwen the current list
5563 * pointer and the ->fe rule list. If it doesn't match, I initialize
5564 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02005565 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005566 if (s->current_rule_list == &sess->fe->http_res_rules)
5567 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005568 else
5569 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02005570 while (1) {
5571 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005572
Willy Tarreau58975672014-04-24 21:13:57 +02005573 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02005574 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02005575 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005576
Willy Tarreau51d861a2015-05-22 17:30:48 +02005577 if (ret == HTTP_RULE_RES_BADREQ)
5578 goto return_srv_prx_502;
5579
5580 if (ret == HTTP_RULE_RES_DONE) {
5581 rep->analysers &= ~an_bit;
5582 rep->analyse_exp = TICK_ETERNITY;
5583 return 1;
5584 }
5585 }
5586
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005587 /* we need to be called again. */
5588 if (ret == HTTP_RULE_RES_YIELD) {
5589 channel_dont_close(rep);
5590 return 0;
5591 }
5592
Willy Tarreau58975672014-04-24 21:13:57 +02005593 /* try headers filters */
5594 if (rule_set->rsp_exp != NULL) {
5595 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5596 return_bad_resp:
5597 if (objt_server(s->target)) {
5598 objt_server(s->target)->counters.failed_resp++;
5599 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005600 }
Willy Tarreau58975672014-04-24 21:13:57 +02005601 s->be->be_counters.failed_resp++;
5602 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005603 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02005604 txn->status = 502;
5605 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01005606 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005607 channel_truncate(rep);
Jarno Huuskonen9e6906b2017-03-06 14:21:49 +02005608 http_reply_and_close(s, txn->status, http_error_message(s));
Willy Tarreaue7dff022015-04-03 01:14:29 +02005609 if (!(s->flags & SF_ERR_MASK))
5610 s->flags |= SF_ERR_PRXCOND;
5611 if (!(s->flags & SF_FINST_MASK))
5612 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02005613 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005614 }
Willy Tarreau58975672014-04-24 21:13:57 +02005615 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005616
Willy Tarreau58975672014-04-24 21:13:57 +02005617 /* has the response been denied ? */
5618 if (txn->flags & TX_SVDENY) {
5619 if (objt_server(s->target))
5620 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005621
Willy Tarreau58975672014-04-24 21:13:57 +02005622 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005623 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005624 if (sess->listener->counters)
5625 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005626
Willy Tarreau58975672014-04-24 21:13:57 +02005627 goto return_srv_prx_502;
5628 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005629
Willy Tarreau58975672014-04-24 21:13:57 +02005630 /* add response headers from the rule sets in the same order */
5631 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02005632 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005633 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005634 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02005635 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02005636 ret = acl_pass(ret);
5637 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5638 ret = !ret;
5639 if (!ret)
5640 continue;
5641 }
5642 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
5643 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005644 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005645
Willy Tarreau58975672014-04-24 21:13:57 +02005646 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005647 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02005648 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005649 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02005650 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005651
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01005652 /* After this point, this anayzer can't return yield, so we can
5653 * remove the bit corresponding to this analyzer from the list.
5654 *
5655 * Note that the intermediate returns and goto found previously
5656 * reset the analyzers.
5657 */
5658 rep->analysers &= ~an_bit;
5659 rep->analyse_exp = TICK_ETERNITY;
5660
Willy Tarreau58975672014-04-24 21:13:57 +02005661 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02005662 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02005663 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005664
Willy Tarreau58975672014-04-24 21:13:57 +02005665 /*
5666 * Now check for a server cookie.
5667 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02005668 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02005669 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005670
Willy Tarreau58975672014-04-24 21:13:57 +02005671 /*
5672 * Check for cache-control or pragma headers if required.
5673 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005674 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 +02005675 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005676
Willy Tarreau58975672014-04-24 21:13:57 +02005677 /*
5678 * Add server cookie in the response if needed
5679 */
5680 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5681 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005682 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02005683 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5684 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5685 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5686 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5687 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005688 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005689 /* the server is known, it's not the one the client requested, or the
5690 * cookie's last seen date needs to be refreshed. We have to
5691 * insert a set-cookie here, except if we want to insert only on POST
5692 * requests and this one isn't. Note that servers which don't have cookies
5693 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005694 */
Willy Tarreau58975672014-04-24 21:13:57 +02005695 if (!objt_server(s->target)->cookie) {
5696 chunk_printf(&trash,
5697 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5698 s->be->cookie_name);
5699 }
5700 else {
5701 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005702
Willy Tarreau58975672014-04-24 21:13:57 +02005703 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5704 /* emit last_date, which is mandatory */
5705 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5706 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5707 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005708
Willy Tarreau58975672014-04-24 21:13:57 +02005709 if (s->be->cookie_maxlife) {
5710 /* emit first_date, which is either the original one or
5711 * the current date.
5712 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005713 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005714 s30tob64(txn->cookie_first_date ?
5715 txn->cookie_first_date >> 2 :
5716 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005717 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005718 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02005719 }
Willy Tarreau58975672014-04-24 21:13:57 +02005720 chunk_appendf(&trash, "; path=/");
5721 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02005722
Willy Tarreau58975672014-04-24 21:13:57 +02005723 if (s->be->cookie_domain)
5724 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005725
Willy Tarreau58975672014-04-24 21:13:57 +02005726 if (s->be->ck_opts & PR_CK_HTTPONLY)
5727 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005728
Willy Tarreau58975672014-04-24 21:13:57 +02005729 if (s->be->ck_opts & PR_CK_SECURE)
5730 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02005731
Willy Tarreau58975672014-04-24 21:13:57 +02005732 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
5733 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005734
Willy Tarreau58975672014-04-24 21:13:57 +02005735 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02005736 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02005737 /* the server did not change, only the date was updated */
5738 txn->flags |= TX_SCK_UPDATED;
5739 else
5740 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005741
Willy Tarreau58975672014-04-24 21:13:57 +02005742 /* Here, we will tell an eventual cache on the client side that we don't
5743 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5744 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5745 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005746 */
Willy Tarreau58975672014-04-24 21:13:57 +02005747 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005748
Willy Tarreau58975672014-04-24 21:13:57 +02005749 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005750
Willy Tarreau58975672014-04-24 21:13:57 +02005751 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
5752 "Cache-control: private", 22) < 0))
5753 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005754 }
Willy Tarreau58975672014-04-24 21:13:57 +02005755 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005756
Willy Tarreau58975672014-04-24 21:13:57 +02005757 /*
5758 * Check if result will be cacheable with a cookie.
5759 * We'll block the response if security checks have caught
5760 * nasty things such as a cacheable cookie.
5761 */
5762 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5763 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
5764 (s->be->options & PR_O_CHK_CACHE)) {
5765 /* we're in presence of a cacheable response containing
5766 * a set-cookie header. We'll block it as requested by
5767 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005768 */
Willy Tarreau58975672014-04-24 21:13:57 +02005769 if (objt_server(s->target))
5770 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01005771
Willy Tarreau58975672014-04-24 21:13:57 +02005772 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005773 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005774 if (sess->listener->counters)
5775 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005776
Willy Tarreau58975672014-04-24 21:13:57 +02005777 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
5778 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5779 send_log(s->be, LOG_ALERT,
5780 "Blocking cacheable cookie in response from instance %s, server %s.\n",
5781 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
5782 goto return_srv_prx_502;
5783 }
Willy Tarreau03945942009-12-22 16:50:27 +01005784
Willy Tarreau70730dd2014-04-24 18:06:27 +02005785 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02005786 /*
5787 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
5788 * If an "Upgrade" token is found, the header is left untouched in order
5789 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02005790 * if anything but "Upgrade" is present in the Connection header. We don't
5791 * want to touch any 101 response either since it's switching to another
5792 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02005793 */
Willy Tarreauce730de2014-09-16 10:40:38 +02005794 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02005795 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005796 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02005797 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
5798 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005799
Willy Tarreau58975672014-04-24 21:13:57 +02005800 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5801 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5802 /* we want a keep-alive response here. Keep-alive header
5803 * required if either side is not 1.1.
5804 */
5805 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
5806 want_flags |= TX_CON_KAL_SET;
5807 }
5808 else {
5809 /* we want a close response here. Close header required if
5810 * the server is 1.1, regardless of the client.
5811 */
5812 if (msg->flags & HTTP_MSGF_VER_11)
5813 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005814 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005815
Willy Tarreau58975672014-04-24 21:13:57 +02005816 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5817 http_change_connection_header(txn, msg, want_flags);
5818 }
5819
5820 skip_header_mangling:
Christopher Faulet69744d92017-03-30 10:54:35 +02005821 /* Always enter in the body analyzer */
5822 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
5823 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005824
Willy Tarreau58975672014-04-24 21:13:57 +02005825 /* if the user wants to log as soon as possible, without counting
5826 * bytes from the server, then this is the right moment. We have
5827 * to temporarily assign bytes_out to log what we currently have.
5828 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005829 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02005830 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
5831 s->logs.bytes_out = txn->rsp.eoh;
5832 s->do_log(s);
5833 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005834 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005835 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005836}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005837
Willy Tarreaud98cf932009-12-27 22:54:55 +01005838/* This function is an analyser which forwards response body (including chunk
5839 * sizes if any). It is called as soon as we must forward, even if we forward
5840 * zero byte. The only situation where it must not be called is when we're in
5841 * tunnel mode and we want to forward till the close. It's used both to forward
5842 * remaining data and to resync after end of body. It expects the msg_state to
5843 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005844 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02005845 *
5846 * It is capable of compressing response data both in content-length mode and
5847 * in chunked mode. The state machines follows different flows depending on
5848 * whether content-length and chunked modes are used, since there are no
5849 * trailers in content-length :
5850 *
5851 * chk-mode cl-mode
5852 * ,----- BODY -----.
5853 * / \
5854 * V size > 0 V chk-mode
5855 * .--> SIZE -------------> DATA -------------> CRLF
5856 * | | size == 0 | last byte |
5857 * | v final crlf v inspected |
5858 * | TRAILERS -----------> DONE |
5859 * | |
5860 * `----------------------------------------------'
5861 *
5862 * Compression only happens in the DATA state, and must be flushed in final
5863 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
5864 * is performed at once on final states for all bytes parsed, or when leaving
5865 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005866 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005867int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005868{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005869 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005870 struct http_txn *txn = s->txn;
5871 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01005872 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005873
Christopher Faulet814d2702017-03-30 11:33:44 +02005874 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
5875 now_ms, __FUNCTION__,
5876 s,
5877 res,
5878 res->rex, res->wex,
5879 res->flags,
5880 res->buf->i,
5881 res->analysers);
5882
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005883 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5884 return 0;
5885
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005886 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005887 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02005888 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005889 /* Output closed while we were sending data. We must abort and
5890 * wake the other side up.
5891 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005892 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005893 msg->msg_state = HTTP_MSG_ERROR;
5894 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005895 return 1;
5896 }
5897
Willy Tarreau4fe41902010-06-07 22:27:41 +02005898 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005899 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005900
Christopher Fauletd7c91962015-04-30 11:48:27 +02005901 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005902 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5903 ? HTTP_MSG_CHUNK_SIZE
5904 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005905 }
5906
Willy Tarreauefdf0942014-04-24 20:08:57 +02005907 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005908 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02005909 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005910 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005911 }
5912
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005913 if (msg->msg_state < HTTP_MSG_DONE) {
5914 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5915 ? http_msg_forward_chunked_body(s, msg)
5916 : http_msg_forward_body(s, msg));
5917 if (!ret)
5918 goto missing_data_or_waiting;
5919 if (ret < 0)
5920 goto return_bad_res;
5921 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005922
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005923 /* other states, DONE...TUNNEL */
5924 /* for keep-alive we don't want to forward closes on DONE */
5925 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5926 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5927 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005928
Christopher Faulet894da4c2017-07-18 11:29:07 +02005929 http_resync_states(s);
5930 if (!(res->analysers & an_bit)) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005931 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5932 if (res->flags & CF_SHUTW) {
5933 /* response errors are most likely due to the
5934 * client aborting the transfer. */
5935 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01005936 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005937 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005938 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005939 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005940 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005941 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005942 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02005943 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005944
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005945 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01005946 if (res->flags & CF_SHUTW)
5947 goto aborted_xfer;
5948
5949 /* stop waiting for data if the input is closed before the end. If the
5950 * client side was already closed, it means that the client has aborted,
5951 * so we don't want to count this as a server abort. Otherwise it's a
5952 * server abort.
5953 */
Christopher Fauleta33510b2017-03-31 15:37:29 +02005954 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005955 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01005956 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02005957 /* If we have some pending data, we continue the processing */
5958 if (!buffer_pending(res->buf)) {
5959 if (!(s->flags & SF_ERR_MASK))
5960 s->flags |= SF_ERR_SRVCL;
5961 s->be->be_counters.srv_aborts++;
5962 if (objt_server(s->target))
5963 objt_server(s->target)->counters.srv_aborts++;
5964 goto return_bad_res_stats_ok;
5965 }
Willy Tarreau40dba092010-03-04 18:14:51 +01005966 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005967
Willy Tarreau40dba092010-03-04 18:14:51 +01005968 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005969 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005970 goto return_bad_res;
5971
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005972 /* When TE: chunked is used, we need to get there again to parse
5973 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet69744d92017-03-30 10:54:35 +02005974 * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side
5975 * or if there are filters registered on the stream, we don't want to
5976 * forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005977 */
Christopher Faulet69744d92017-03-30 10:54:35 +02005978 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01005979 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005980 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5981 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005982 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005983
Willy Tarreau5c620922011-05-11 19:56:11 +02005984 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005985 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005986 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005987 * modes are already handled by the stream sock layer. We must not do
5988 * this in content-length mode because it could present the MSG_MORE
5989 * flag with the last block of forwarded data, which would cause an
5990 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005991 */
Christopher Faulet92d36382015-11-05 13:35:03 +01005992 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005993 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005994
Willy Tarreau87b09662015-04-03 00:22:06 +02005995 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005996 return 0;
5997
Willy Tarreau40dba092010-03-04 18:14:51 +01005998 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005999 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006000 if (objt_server(s->target))
6001 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006002
6003 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006004 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006005 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006006 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006007 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006008 res->analysers &= AN_RES_FLT_END;
6009 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 +01006010 if (objt_server(s->target))
6011 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006012
Willy Tarreaue7dff022015-04-03 01:14:29 +02006013 if (!(s->flags & SF_ERR_MASK))
6014 s->flags |= SF_ERR_PRXCOND;
6015 if (!(s->flags & SF_FINST_MASK))
6016 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006017 return 0;
6018
6019 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006020 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006021 txn->rsp.msg_state = HTTP_MSG_ERROR;
6022 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006023 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006024 res->analysers &= AN_RES_FLT_END;
6025 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 +01006026
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006027 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006028 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006029 if (objt_server(s->target))
6030 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006031
Willy Tarreaue7dff022015-04-03 01:14:29 +02006032 if (!(s->flags & SF_ERR_MASK))
6033 s->flags |= SF_ERR_CLICL;
6034 if (!(s->flags & SF_FINST_MASK))
6035 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006036 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006037}
6038
6039
6040static inline int
6041http_msg_forward_body(struct stream *s, struct http_msg *msg)
6042{
6043 struct channel *chn = msg->chn;
6044 int ret;
6045
6046 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
6047
6048 if (msg->msg_state == HTTP_MSG_ENDING)
6049 goto ending;
6050
6051 /* Neither content-length, nor transfer-encoding was found, so we must
6052 * read the body until the server connection is closed. In that case, we
6053 * eat data as they come. Of course, this happens for response only. */
6054 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6055 unsigned long long len = (chn->buf->i - msg->next);
6056 msg->chunk_len += len;
6057 msg->body_len += len;
6058 }
Christopher Fauletda02e172015-12-04 09:25:05 +01006059 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
6060 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6061 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006062 msg->next += ret;
6063 msg->chunk_len -= ret;
6064 if (msg->chunk_len) {
6065 /* input empty or output full */
6066 if (chn->buf->i > msg->next)
6067 chn->flags |= CF_WAKE_WRITE;
6068 goto missing_data_or_waiting;
6069 }
6070
Christopher Faulet1486b0a2017-07-18 11:42:08 +02006071 /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is
6072 * always set for a request. */
6073 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
6074 /* The server still sending data that should be filtered */
6075 if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn))
6076 goto missing_data_or_waiting;
6077 }
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006078
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006079 msg->msg_state = HTTP_MSG_ENDING;
6080
6081 ending:
6082 /* we may have some pending data starting at res->buf->p such as a last
6083 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006084 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6085 /* default_ret */ msg->next,
6086 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006087 b_adv(chn->buf, ret);
6088 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006089 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6090 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006091 if (msg->next)
6092 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006093
Christopher Fauletda02e172015-12-04 09:25:05 +01006094 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
6095 /* default_ret */ 1,
6096 /* on_error */ goto error,
6097 /* on_wait */ goto waiting);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006098 msg->msg_state = HTTP_MSG_DONE;
6099 return 1;
6100
6101 missing_data_or_waiting:
6102 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006103 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
6104 /* default_ret */ msg->next,
6105 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006106 b_adv(chn->buf, ret);
6107 msg->next -= ret;
6108 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6109 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006110 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006111 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006112 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006113 return 0;
6114 error:
6115 return -1;
6116}
6117
6118static inline int
6119http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
6120{
6121 struct channel *chn = msg->chn;
6122 int ret;
6123
6124 /* Here we have the guarantee to be in one of the following state:
6125 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
6126 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
6127
6128 switch_states:
6129 switch (msg->msg_state) {
6130 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01006131 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
6132 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
6133 /* on_error */ goto error);
6134 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006135 msg->chunk_len -= ret;
6136 if (msg->chunk_len) {
6137 /* input empty or output full */
6138 if (chn->buf->i > msg->next)
6139 chn->flags |= CF_WAKE_WRITE;
6140 goto missing_data_or_waiting;
6141 }
6142
6143 /* nothing left to forward for this chunk*/
6144 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
6145 /* fall through for HTTP_MSG_CHUNK_CRLF */
6146
6147 case HTTP_MSG_CHUNK_CRLF:
6148 /* we want the CRLF after the data */
6149 ret = http_skip_chunk_crlf(msg);
6150 if (ret == 0)
6151 goto missing_data_or_waiting;
6152 if (ret < 0)
6153 goto chunk_parsing_error;
Christopher Faulet113f7de2015-12-14 14:52:13 +01006154 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006155 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6156 /* fall through for HTTP_MSG_CHUNK_SIZE */
6157
6158 case HTTP_MSG_CHUNK_SIZE:
6159 /* read the chunk size and assign it to ->chunk_len,
6160 * then set ->next to point to the body and switch to
6161 * DATA or TRAILERS state.
6162 */
6163 ret = http_parse_chunk_size(msg);
6164 if (ret == 0)
6165 goto missing_data_or_waiting;
6166 if (ret < 0)
6167 goto chunk_parsing_error;
Christopher Faulet113f7de2015-12-14 14:52:13 +01006168 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006169 if (msg->chunk_len) {
6170 msg->msg_state = HTTP_MSG_DATA;
6171 goto switch_states;
6172 }
6173 msg->msg_state = HTTP_MSG_TRAILERS;
6174 /* fall through for HTTP_MSG_TRAILERS */
6175
6176 case HTTP_MSG_TRAILERS:
6177 ret = http_forward_trailers(msg);
6178 if (ret < 0)
6179 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01006180 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
6181 /* default_ret */ 1,
6182 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006183 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006184 if (!ret)
6185 goto missing_data_or_waiting;
6186 break;
6187
6188 case HTTP_MSG_ENDING:
6189 goto ending;
6190
6191 default:
6192 /* This should no happen in this function */
6193 goto error;
6194 }
6195
6196 msg->msg_state = HTTP_MSG_ENDING;
6197 ending:
6198 /* we may have some pending data starting at res->buf->p such as a last
6199 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01006200 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006201 /* default_ret */ msg->next,
6202 /* on_error */ goto error);
6203 b_adv(chn->buf, ret);
6204 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006205 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
6206 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02006207 if (msg->next)
6208 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02006209
Christopher Fauletda02e172015-12-04 09:25:05 +01006210 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006211 /* default_ret */ 1,
6212 /* on_error */ goto error,
6213 /* on_wait */ goto waiting);
6214 msg->msg_state = HTTP_MSG_DONE;
6215 return 1;
6216
6217 missing_data_or_waiting:
6218 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01006219 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006220 /* default_ret */ msg->next,
6221 /* on_error */ goto error);
6222 b_adv(chn->buf, ret);
6223 msg->next -= ret;
6224 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
6225 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01006226 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006227 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02006228 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006229 return 0;
6230
6231 chunk_parsing_error:
6232 if (msg->err_pos >= 0) {
6233 if (chn->flags & CF_ISRESP)
6234 http_capture_bad_message(&s->be->invalid_rep, s, msg,
6235 msg->msg_state, strm_fe(s));
6236 else
6237 http_capture_bad_message(&strm_fe(s)->invalid_req, s,
6238 msg, msg->msg_state, s->be);
6239 }
6240 error:
6241 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006242}
6243
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006244
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006245/* Iterate the same filter through all request headers.
6246 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006247 * Since it can manage the switch to another backend, it updates the per-proxy
6248 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006249 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006250int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006251{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006252 char *cur_ptr, *cur_end, *cur_next;
6253 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006254 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006255 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006256 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006257
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006258 last_hdr = 0;
6259
Willy Tarreau9b28e032012-10-12 23:49:43 +02006260 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006261 old_idx = 0;
6262
6263 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006264 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006265 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006266 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006267 (exp->action == ACT_ALLOW ||
6268 exp->action == ACT_DENY ||
6269 exp->action == ACT_TARPIT))
6270 return 0;
6271
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006272 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006273 if (!cur_idx)
6274 break;
6275
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006276 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006277 cur_ptr = cur_next;
6278 cur_end = cur_ptr + cur_hdr->len;
6279 cur_next = cur_end + cur_hdr->cr + 1;
6280
6281 /* Now we have one header between cur_ptr and cur_end,
6282 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006283 */
6284
Willy Tarreau15a53a42015-01-21 13:39:42 +01006285 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006286 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006287 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006288 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006289 last_hdr = 1;
6290 break;
6291
6292 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006293 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006294 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006295 break;
6296
6297 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006298 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006299 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006300 break;
6301
6302 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006303 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6304 if (trash.len < 0)
6305 return -1;
6306
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006307 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006308 /* FIXME: if the user adds a newline in the replacement, the
6309 * index will not be recalculated for now, and the new line
6310 * will not be counted as a new header.
6311 */
6312
6313 cur_end += delta;
6314 cur_next += delta;
6315 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006316 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006317 break;
6318
6319 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006320 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006321 cur_next += delta;
6322
Willy Tarreaufa355d42009-11-29 18:12:29 +01006323 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006324 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6325 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006326 cur_hdr->len = 0;
6327 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006328 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006329 break;
6330
6331 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006332 }
6333
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006334 /* keep the link from this header to next one in case of later
6335 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006336 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006337 old_idx = cur_idx;
6338 }
6339 return 0;
6340}
6341
6342
6343/* Apply the filter to the request line.
6344 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6345 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006346 * Since it can manage the switch to another backend, it updates the per-proxy
6347 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006348 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006349int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006350{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006351 char *cur_ptr, *cur_end;
6352 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006353 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006354 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006355
Willy Tarreau3d300592007-03-18 18:34:41 +01006356 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006357 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006358 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006359 (exp->action == ACT_ALLOW ||
6360 exp->action == ACT_DENY ||
6361 exp->action == ACT_TARPIT))
6362 return 0;
6363 else if (exp->action == ACT_REMOVE)
6364 return 0;
6365
6366 done = 0;
6367
Willy Tarreau9b28e032012-10-12 23:49:43 +02006368 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006369 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006370
6371 /* Now we have the request line between cur_ptr and cur_end */
6372
Willy Tarreau15a53a42015-01-21 13:39:42 +01006373 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006374 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006375 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006376 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006377 done = 1;
6378 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006379
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006380 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006381 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006382 done = 1;
6383 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006384
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006385 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006386 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006387 done = 1;
6388 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006389
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006390 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006391 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6392 if (trash.len < 0)
6393 return -1;
6394
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006395 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006396 /* FIXME: if the user adds a newline in the replacement, the
6397 * index will not be recalculated for now, and the new line
6398 * will not be counted as a new header.
6399 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006400
Willy Tarreaufa355d42009-11-29 18:12:29 +01006401 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006402 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006403 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006404 HTTP_MSG_RQMETH,
6405 cur_ptr, cur_end + 1,
6406 NULL, NULL);
6407 if (unlikely(!cur_end))
6408 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006409
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006410 /* we have a full request and we know that we have either a CR
6411 * or an LF at <ptr>.
6412 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006413 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6414 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006415 /* there is no point trying this regex on headers */
6416 return 1;
6417 }
6418 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006419 return done;
6420}
Willy Tarreau97de6242006-12-27 17:18:38 +01006421
Willy Tarreau58f10d72006-12-04 02:26:12 +01006422
Willy Tarreau58f10d72006-12-04 02:26:12 +01006423
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006424/*
Willy Tarreau87b09662015-04-03 00:22:06 +02006425 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006426 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006427 * unparsable request. Since it can manage the switch to another backend, it
6428 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006429 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006430int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006431{
Willy Tarreau192252e2015-04-04 01:47:55 +02006432 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006433 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006434 struct hdr_exp *exp;
6435
6436 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006437 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006438
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006439 /*
6440 * The interleaving of transformations and verdicts
6441 * makes it difficult to decide to continue or stop
6442 * the evaluation.
6443 */
6444
Willy Tarreau6c123b12010-01-28 20:22:06 +01006445 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6446 break;
6447
Willy Tarreau3d300592007-03-18 18:34:41 +01006448 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006449 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006450 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006451 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006452
6453 /* if this filter had a condition, evaluate it now and skip to
6454 * next filter if the condition does not match.
6455 */
6456 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006457 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006458 ret = acl_pass(ret);
6459 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6460 ret = !ret;
6461
6462 if (!ret)
6463 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006464 }
6465
6466 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006467 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006468 if (unlikely(ret < 0))
6469 return -1;
6470
6471 if (likely(ret == 0)) {
6472 /* The filter did not match the request, it can be
6473 * iterated through all headers.
6474 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01006475 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
6476 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006477 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006478 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006479 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006480}
6481
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006482
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006483/* Find the end of a cookie value contained between <s> and <e>. It works the
6484 * same way as with headers above except that the semi-colon also ends a token.
6485 * See RFC2965 for more information. Note that it requires a valid header to
6486 * return a valid result.
6487 */
6488char *find_cookie_value_end(char *s, const char *e)
6489{
6490 int quoted, qdpair;
6491
6492 quoted = qdpair = 0;
6493 for (; s < e; s++) {
6494 if (qdpair) qdpair = 0;
6495 else if (quoted) {
6496 if (*s == '\\') qdpair = 1;
6497 else if (*s == '"') quoted = 0;
6498 }
6499 else if (*s == '"') quoted = 1;
6500 else if (*s == ',' || *s == ';') return s;
6501 }
6502 return s;
6503}
6504
6505/* Delete a value in a header between delimiters <from> and <next> in buffer
6506 * <buf>. The number of characters displaced is returned, and the pointer to
6507 * the first delimiter is updated if required. The function tries as much as
6508 * possible to respect the following principles :
6509 * - replace <from> delimiter by the <next> one unless <from> points to a
6510 * colon, in which case <next> is simply removed
6511 * - set exactly one space character after the new first delimiter, unless
6512 * there are not enough characters in the block being moved to do so.
6513 * - remove unneeded spaces before the previous delimiter and after the new
6514 * one.
6515 *
6516 * It is the caller's responsibility to ensure that :
6517 * - <from> points to a valid delimiter or the colon ;
6518 * - <next> points to a valid delimiter or the final CR/LF ;
6519 * - there are non-space chars before <from> ;
6520 * - there is a CR/LF at or after <next>.
6521 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006522int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006523{
6524 char *prev = *from;
6525
6526 if (*prev == ':') {
6527 /* We're removing the first value, preserve the colon and add a
6528 * space if possible.
6529 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006530 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006531 next++;
6532 prev++;
6533 if (prev < next)
6534 *prev++ = ' ';
6535
Willy Tarreau2235b262016-11-05 15:50:20 +01006536 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006537 next++;
6538 } else {
6539 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01006540 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006541 prev--;
6542 *from = prev;
6543
6544 /* copy the delimiter and if possible a space if we're
6545 * not at the end of the line.
6546 */
Willy Tarreau2235b262016-11-05 15:50:20 +01006547 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006548 *prev++ = *next++;
6549 if (prev + 1 < next)
6550 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01006551 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006552 next++;
6553 }
6554 }
6555 return buffer_replace2(buf, prev, next, NULL, 0);
6556}
6557
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006558/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006559 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006560 * desirable to call it only when needed. This code is quite complex because
6561 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6562 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006563 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006564void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006565{
Willy Tarreaueee5b512015-04-03 23:46:31 +02006566 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006567 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006568 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006569 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006570 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6571 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006572
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006573 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006574 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006575 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006576
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006577 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006578 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006579 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006580
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006581 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006582 hdr_beg = hdr_next;
6583 hdr_end = hdr_beg + cur_hdr->len;
6584 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006585
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006586 /* We have one full header between hdr_beg and hdr_end, and the
6587 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006588 * "Cookie:" headers.
6589 */
6590
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006591 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006592 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006593 old_idx = cur_idx;
6594 continue;
6595 }
6596
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006597 del_from = NULL; /* nothing to be deleted */
6598 preserve_hdr = 0; /* assume we may kill the whole header */
6599
Willy Tarreau58f10d72006-12-04 02:26:12 +01006600 /* Now look for cookies. Conforming to RFC2109, we have to support
6601 * attributes whose name begin with a '$', and associate them with
6602 * the right cookie, if we want to delete this cookie.
6603 * So there are 3 cases for each cookie read :
6604 * 1) it's a special attribute, beginning with a '$' : ignore it.
6605 * 2) it's a server id cookie that we *MAY* want to delete : save
6606 * some pointers on it (last semi-colon, beginning of cookie...)
6607 * 3) it's an application cookie : we *MAY* have to delete a previous
6608 * "special" cookie.
6609 * At the end of loop, if a "special" cookie remains, we may have to
6610 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006611 * *MUST* delete it.
6612 *
6613 * Note: RFC2965 is unclear about the processing of spaces around
6614 * the equal sign in the ATTR=VALUE form. A careful inspection of
6615 * the RFC explicitly allows spaces before it, and not within the
6616 * tokens (attrs or values). An inspection of RFC2109 allows that
6617 * too but section 10.1.3 lets one think that spaces may be allowed
6618 * after the equal sign too, resulting in some (rare) buggy
6619 * implementations trying to do that. So let's do what servers do.
6620 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6621 * allowed quoted strings in values, with any possible character
6622 * after a backslash, including control chars and delimitors, which
6623 * causes parsing to become ambiguous. Browsers also allow spaces
6624 * within values even without quotes.
6625 *
6626 * We have to keep multiple pointers in order to support cookie
6627 * removal at the beginning, middle or end of header without
6628 * corrupting the header. All of these headers are valid :
6629 *
6630 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6631 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6632 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6633 * | | | | | | | | |
6634 * | | | | | | | | hdr_end <--+
6635 * | | | | | | | +--> next
6636 * | | | | | | +----> val_end
6637 * | | | | | +-----------> val_beg
6638 * | | | | +--------------> equal
6639 * | | | +----------------> att_end
6640 * | | +---------------------> att_beg
6641 * | +--------------------------> prev
6642 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006643 */
6644
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006645 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6646 /* Iterate through all cookies on this line */
6647
6648 /* find att_beg */
6649 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006650 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006651 att_beg++;
6652
6653 /* find att_end : this is the first character after the last non
6654 * space before the equal. It may be equal to hdr_end.
6655 */
6656 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006657
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006658 while (equal < hdr_end) {
6659 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006660 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01006661 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006662 continue;
6663 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006664 }
6665
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006666 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6667 * is between <att_beg> and <equal>, both may be identical.
6668 */
6669
6670 /* look for end of cookie if there is an equal sign */
6671 if (equal < hdr_end && *equal == '=') {
6672 /* look for the beginning of the value */
6673 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01006674 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006675 val_beg++;
6676
6677 /* find the end of the value, respecting quotes */
6678 next = find_cookie_value_end(val_beg, hdr_end);
6679
6680 /* make val_end point to the first white space or delimitor after the value */
6681 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01006682 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006683 val_end--;
6684 } else {
6685 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006686 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006687
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006688 /* We have nothing to do with attributes beginning with '$'. However,
6689 * they will automatically be removed if a header before them is removed,
6690 * since they're supposed to be linked together.
6691 */
6692 if (*att_beg == '$')
6693 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006694
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006695 /* Ignore cookies with no equal sign */
6696 if (equal == next) {
6697 /* This is not our cookie, so we must preserve it. But if we already
6698 * scheduled another cookie for removal, we cannot remove the
6699 * complete header, but we can remove the previous block itself.
6700 */
6701 preserve_hdr = 1;
6702 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006703 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006704 val_end += delta;
6705 next += delta;
6706 hdr_end += delta;
6707 hdr_next += delta;
6708 cur_hdr->len += delta;
6709 http_msg_move_end(&txn->req, delta);
6710 prev = del_from;
6711 del_from = NULL;
6712 }
6713 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006714 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006715
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006716 /* if there are spaces around the equal sign, we need to
6717 * strip them otherwise we'll get trouble for cookie captures,
6718 * or even for rewrites. Since this happens extremely rarely,
6719 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006720 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006721 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6722 int stripped_before = 0;
6723 int stripped_after = 0;
6724
6725 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006726 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006727 equal += stripped_before;
6728 val_beg += stripped_before;
6729 }
6730
6731 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006732 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006733 val_beg += stripped_after;
6734 stripped_before += stripped_after;
6735 }
6736
6737 val_end += stripped_before;
6738 next += stripped_before;
6739 hdr_end += stripped_before;
6740 hdr_next += stripped_before;
6741 cur_hdr->len += stripped_before;
6742 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006743 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006744 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006745
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006746 /* First, let's see if we want to capture this cookie. We check
6747 * that we don't already have a client side cookie, because we
6748 * can only capture one. Also as an optimisation, we ignore
6749 * cookies shorter than the declared name.
6750 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006751 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6752 (val_end - att_beg >= sess->fe->capture_namelen) &&
6753 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006754 int log_len = val_end - att_beg;
6755
6756 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6757 Alert("HTTP logging : out of memory.\n");
6758 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006759 if (log_len > sess->fe->capture_len)
6760 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006761 memcpy(txn->cli_cookie, att_beg, log_len);
6762 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006763 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006764 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006765
Willy Tarreaubca99692010-10-06 19:25:55 +02006766 /* Persistence cookies in passive, rewrite or insert mode have the
6767 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006768 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006769 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006770 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006771 * For cookies in prefix mode, the form is :
6772 *
6773 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006774 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006775 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
6776 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
6777 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006778 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006779
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006780 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6781 * have the server ID between val_beg and delim, and the original cookie between
6782 * delim+1 and val_end. Otherwise, delim==val_end :
6783 *
6784 * Cookie: NAME=SRV; # in all but prefix modes
6785 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6786 * | || || | |+-> next
6787 * | || || | +--> val_end
6788 * | || || +---------> delim
6789 * | || |+------------> val_beg
6790 * | || +-------------> att_end = equal
6791 * | |+-----------------> att_beg
6792 * | +------------------> prev
6793 * +-------------------------> hdr_beg
6794 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006795
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006796 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006797 for (delim = val_beg; delim < val_end; delim++)
6798 if (*delim == COOKIE_DELIM)
6799 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006800 } else {
6801 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006802 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006803 /* Now check if the cookie contains a date field, which would
6804 * appear after a vertical bar ('|') just after the server name
6805 * and before the delimiter.
6806 */
6807 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6808 if (vbar1) {
6809 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006810 * right is the last seen date. It is a base64 encoded
6811 * 30-bit value representing the UNIX date since the
6812 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006813 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006814 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006815 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006816 if (val_end - vbar1 >= 5) {
6817 val = b64tos30(vbar1);
6818 if (val > 0)
6819 txn->cookie_last_date = val << 2;
6820 }
6821 /* look for a second vertical bar */
6822 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6823 if (vbar1 && (val_end - vbar1 > 5)) {
6824 val = b64tos30(vbar1 + 1);
6825 if (val > 0)
6826 txn->cookie_first_date = val << 2;
6827 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006828 }
6829 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006830
Willy Tarreauf64d1412010-10-07 20:06:11 +02006831 /* if the cookie has an expiration date and the proxy wants to check
6832 * it, then we do that now. We first check if the cookie is too old,
6833 * then only if it has expired. We detect strict overflow because the
6834 * time resolution here is not great (4 seconds). Cookies with dates
6835 * in the future are ignored if their offset is beyond one day. This
6836 * allows an admin to fix timezone issues without expiring everyone
6837 * and at the same time avoids keeping unwanted side effects for too
6838 * long.
6839 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006840 if (txn->cookie_first_date && s->be->cookie_maxlife &&
6841 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006842 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006843 txn->flags &= ~TX_CK_MASK;
6844 txn->flags |= TX_CK_OLD;
6845 delim = val_beg; // let's pretend we have not found the cookie
6846 txn->cookie_first_date = 0;
6847 txn->cookie_last_date = 0;
6848 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006849 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
6850 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02006851 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006852 txn->flags &= ~TX_CK_MASK;
6853 txn->flags |= TX_CK_EXPIRED;
6854 delim = val_beg; // let's pretend we have not found the cookie
6855 txn->cookie_first_date = 0;
6856 txn->cookie_last_date = 0;
6857 }
6858
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006859 /* Here, we'll look for the first running server which supports the cookie.
6860 * This allows to share a same cookie between several servers, for example
6861 * to dedicate backup servers to specific servers only.
6862 * However, to prevent clients from sticking to cookie-less backup server
6863 * when they have incidentely learned an empty cookie, we simply ignore
6864 * empty cookies and mark them as invalid.
6865 * The same behaviour is applied when persistence must be ignored.
6866 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02006867 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006868 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006869
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006870 while (srv) {
6871 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6872 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02006873 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006874 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02006875 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006876 /* we found the server and we can use it */
6877 txn->flags &= ~TX_CK_MASK;
Emeric Brun52a91d32017-08-31 14:41:55 +02006878 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006879 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006880 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006881 break;
6882 } else {
6883 /* we found a server, but it's down,
6884 * mark it as such and go on in case
6885 * another one is available.
6886 */
6887 txn->flags &= ~TX_CK_MASK;
6888 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006889 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006890 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006891 srv = srv->next;
6892 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006893
Willy Tarreauf64d1412010-10-07 20:06:11 +02006894 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006895 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006896 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006897 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006898 txn->flags |= TX_CK_UNUSED;
6899 else
6900 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006901 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006902
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006903 /* depending on the cookie mode, we may have to either :
6904 * - delete the complete cookie if we're in insert+indirect mode, so that
6905 * the server never sees it ;
6906 * - remove the server id from the cookie value, and tag the cookie as an
6907 * application cookie so that it does not get accidentely removed later,
6908 * if we're in cookie prefix mode
6909 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006910 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006911 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006912
Willy Tarreau9b28e032012-10-12 23:49:43 +02006913 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006914 val_end += delta;
6915 next += delta;
6916 hdr_end += delta;
6917 hdr_next += delta;
6918 cur_hdr->len += delta;
6919 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006920
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006921 del_from = NULL;
6922 preserve_hdr = 1; /* we want to keep this cookie */
6923 }
6924 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006925 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006926 del_from = prev;
6927 }
6928 } else {
6929 /* This is not our cookie, so we must preserve it. But if we already
6930 * scheduled another cookie for removal, we cannot remove the
6931 * complete header, but we can remove the previous block itself.
6932 */
6933 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006934
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006935 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006936 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006937 if (att_beg >= del_from)
6938 att_beg += delta;
6939 if (att_end >= del_from)
6940 att_end += delta;
6941 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006942 val_end += delta;
6943 next += delta;
6944 hdr_end += delta;
6945 hdr_next += delta;
6946 cur_hdr->len += delta;
6947 http_msg_move_end(&txn->req, delta);
6948 prev = del_from;
6949 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006950 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006951 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006952
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006953 /* continue with next cookie on this header line */
6954 att_beg = next;
6955 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006956
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006957 /* There are no more cookies on this line.
6958 * We may still have one (or several) marked for deletion at the
6959 * end of the line. We must do this now in two ways :
6960 * - if some cookies must be preserved, we only delete from the
6961 * mark to the end of line ;
6962 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006963 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006964 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006965 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006966 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006967 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006968 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006969 cur_hdr->len += delta;
6970 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006971 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006972
6973 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006974 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6975 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006976 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006977 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006978 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006979 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006980 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006981 }
6982
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006983 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006984 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006985 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006986}
6987
6988
Willy Tarreaua15645d2007-03-18 16:22:39 +01006989/* Iterate the same filter through all response headers contained in <rtr>.
6990 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6991 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006992int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006993{
Willy Tarreaua15645d2007-03-18 16:22:39 +01006994 char *cur_ptr, *cur_end, *cur_next;
6995 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006996 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006997 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006998 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006999
7000 last_hdr = 0;
7001
Willy Tarreau9b28e032012-10-12 23:49:43 +02007002 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007003 old_idx = 0;
7004
7005 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007006 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007007 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007008 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007009 (exp->action == ACT_ALLOW ||
7010 exp->action == ACT_DENY))
7011 return 0;
7012
7013 cur_idx = txn->hdr_idx.v[old_idx].next;
7014 if (!cur_idx)
7015 break;
7016
7017 cur_hdr = &txn->hdr_idx.v[cur_idx];
7018 cur_ptr = cur_next;
7019 cur_end = cur_ptr + cur_hdr->len;
7020 cur_next = cur_end + cur_hdr->cr + 1;
7021
7022 /* Now we have one header between cur_ptr and cur_end,
7023 * and the next header starts at cur_next.
7024 */
7025
Willy Tarreau15a53a42015-01-21 13:39:42 +01007026 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007027 switch (exp->action) {
7028 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007029 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007030 last_hdr = 1;
7031 break;
7032
7033 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007034 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007035 last_hdr = 1;
7036 break;
7037
7038 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007039 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7040 if (trash.len < 0)
7041 return -1;
7042
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007043 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007044 /* FIXME: if the user adds a newline in the replacement, the
7045 * index will not be recalculated for now, and the new line
7046 * will not be counted as a new header.
7047 */
7048
7049 cur_end += delta;
7050 cur_next += delta;
7051 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007052 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007053 break;
7054
7055 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007056 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007057 cur_next += delta;
7058
Willy Tarreaufa355d42009-11-29 18:12:29 +01007059 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007060 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7061 txn->hdr_idx.used--;
7062 cur_hdr->len = 0;
7063 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007064 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007065 break;
7066
7067 }
7068 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007069
7070 /* keep the link from this header to next one in case of later
7071 * removal of next header.
7072 */
7073 old_idx = cur_idx;
7074 }
7075 return 0;
7076}
7077
7078
7079/* Apply the filter to the status line in the response buffer <rtr>.
7080 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7081 * or -1 if a replacement resulted in an invalid status line.
7082 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007083int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007084{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007085 char *cur_ptr, *cur_end;
7086 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007087 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007088 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007089
7090
Willy Tarreau3d300592007-03-18 18:34:41 +01007091 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007092 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007093 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007094 (exp->action == ACT_ALLOW ||
7095 exp->action == ACT_DENY))
7096 return 0;
7097 else if (exp->action == ACT_REMOVE)
7098 return 0;
7099
7100 done = 0;
7101
Willy Tarreau9b28e032012-10-12 23:49:43 +02007102 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007103 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007104
7105 /* Now we have the status line between cur_ptr and cur_end */
7106
Willy Tarreau15a53a42015-01-21 13:39:42 +01007107 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007108 switch (exp->action) {
7109 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007110 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007111 done = 1;
7112 break;
7113
7114 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007115 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007116 done = 1;
7117 break;
7118
7119 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007120 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7121 if (trash.len < 0)
7122 return -1;
7123
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007124 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007125 /* FIXME: if the user adds a newline in the replacement, the
7126 * index will not be recalculated for now, and the new line
7127 * will not be counted as a new header.
7128 */
7129
Willy Tarreaufa355d42009-11-29 18:12:29 +01007130 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007131 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007132 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007133 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007134 cur_ptr, cur_end + 1,
7135 NULL, NULL);
7136 if (unlikely(!cur_end))
7137 return -1;
7138
7139 /* we have a full respnse and we know that we have either a CR
7140 * or an LF at <ptr>.
7141 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007142 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007143 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007144 /* there is no point trying this regex on headers */
7145 return 1;
7146 }
7147 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007148 return done;
7149}
7150
7151
7152
7153/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007154 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007155 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7156 * unparsable response.
7157 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007158int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007159{
Willy Tarreau192252e2015-04-04 01:47:55 +02007160 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007161 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007162 struct hdr_exp *exp;
7163
7164 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007165 int ret;
7166
7167 /*
7168 * The interleaving of transformations and verdicts
7169 * makes it difficult to decide to continue or stop
7170 * the evaluation.
7171 */
7172
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007173 if (txn->flags & TX_SVDENY)
7174 break;
7175
Willy Tarreau3d300592007-03-18 18:34:41 +01007176 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007177 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7178 exp->action == ACT_PASS)) {
7179 exp = exp->next;
7180 continue;
7181 }
7182
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007183 /* if this filter had a condition, evaluate it now and skip to
7184 * next filter if the condition does not match.
7185 */
7186 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007187 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007188 ret = acl_pass(ret);
7189 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7190 ret = !ret;
7191 if (!ret)
7192 continue;
7193 }
7194
Willy Tarreaua15645d2007-03-18 16:22:39 +01007195 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007196 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007197 if (unlikely(ret < 0))
7198 return -1;
7199
7200 if (likely(ret == 0)) {
7201 /* The filter did not match the response, it can be
7202 * iterated through all headers.
7203 */
Sasha Pachevc6002042014-05-26 12:33:48 -06007204 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
7205 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007206 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007207 }
7208 return 0;
7209}
7210
7211
Willy Tarreaua15645d2007-03-18 16:22:39 +01007212/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007213 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007214 * desirable to call it only when needed. This function is also used when we
7215 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007216 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007217void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007218{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007219 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007220 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01007221 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007222 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007223 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007224 char *hdr_beg, *hdr_end, *hdr_next;
7225 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007226
Willy Tarreaua15645d2007-03-18 16:22:39 +01007227 /* Iterate through the headers.
7228 * we start with the start line.
7229 */
7230 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007231 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007232
7233 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7234 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007235 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007236
7237 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007238 hdr_beg = hdr_next;
7239 hdr_end = hdr_beg + cur_hdr->len;
7240 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007241
Willy Tarreau24581ba2010-08-31 22:39:35 +02007242 /* We have one full header between hdr_beg and hdr_end, and the
7243 * next header starts at hdr_next. We're only interested in
7244 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007245 */
7246
Willy Tarreau24581ba2010-08-31 22:39:35 +02007247 is_cookie2 = 0;
7248 prev = hdr_beg + 10;
7249 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007250 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007251 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7252 if (!val) {
7253 old_idx = cur_idx;
7254 continue;
7255 }
7256 is_cookie2 = 1;
7257 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007258 }
7259
Willy Tarreau24581ba2010-08-31 22:39:35 +02007260 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7261 * <prev> points to the colon.
7262 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007263 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007264
Willy Tarreau24581ba2010-08-31 22:39:35 +02007265 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7266 * check-cache is enabled) and we are not interested in checking
7267 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007268 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02007269 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007270 return;
7271
Willy Tarreau24581ba2010-08-31 22:39:35 +02007272 /* OK so now we know we have to process this response cookie.
7273 * The format of the Set-Cookie header is slightly different
7274 * from the format of the Cookie header in that it does not
7275 * support the comma as a cookie delimiter (thus the header
7276 * cannot be folded) because the Expires attribute described in
7277 * the original Netscape's spec may contain an unquoted date
7278 * with a comma inside. We have to live with this because
7279 * many browsers don't support Max-Age and some browsers don't
7280 * support quoted strings. However the Set-Cookie2 header is
7281 * clean.
7282 *
7283 * We have to keep multiple pointers in order to support cookie
7284 * removal at the beginning, middle or end of header without
7285 * corrupting the header (in case of set-cookie2). A special
7286 * pointer, <scav> points to the beginning of the set-cookie-av
7287 * fields after the first semi-colon. The <next> pointer points
7288 * either to the end of line (set-cookie) or next unquoted comma
7289 * (set-cookie2). All of these headers are valid :
7290 *
7291 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7292 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7293 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7294 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7295 * | | | | | | | | | |
7296 * | | | | | | | | +-> next hdr_end <--+
7297 * | | | | | | | +------------> scav
7298 * | | | | | | +--------------> val_end
7299 * | | | | | +--------------------> val_beg
7300 * | | | | +----------------------> equal
7301 * | | | +------------------------> att_end
7302 * | | +----------------------------> att_beg
7303 * | +------------------------------> prev
7304 * +-----------------------------------------> hdr_beg
7305 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007306
Willy Tarreau24581ba2010-08-31 22:39:35 +02007307 for (; prev < hdr_end; prev = next) {
7308 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007309
Willy Tarreau24581ba2010-08-31 22:39:35 +02007310 /* find att_beg */
7311 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007312 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007313 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007314
Willy Tarreau24581ba2010-08-31 22:39:35 +02007315 /* find att_end : this is the first character after the last non
7316 * space before the equal. It may be equal to hdr_end.
7317 */
7318 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007319
Willy Tarreau24581ba2010-08-31 22:39:35 +02007320 while (equal < hdr_end) {
7321 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7322 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007323 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007324 continue;
7325 att_end = equal;
7326 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007327
Willy Tarreau24581ba2010-08-31 22:39:35 +02007328 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7329 * is between <att_beg> and <equal>, both may be identical.
7330 */
7331
7332 /* look for end of cookie if there is an equal sign */
7333 if (equal < hdr_end && *equal == '=') {
7334 /* look for the beginning of the value */
7335 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007336 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007337 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007338
Willy Tarreau24581ba2010-08-31 22:39:35 +02007339 /* find the end of the value, respecting quotes */
7340 next = find_cookie_value_end(val_beg, hdr_end);
7341
7342 /* make val_end point to the first white space or delimitor after the value */
7343 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007344 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02007345 val_end--;
7346 } else {
7347 /* <equal> points to next comma, semi-colon or EOL */
7348 val_beg = val_end = next = equal;
7349 }
7350
7351 if (next < hdr_end) {
7352 /* Set-Cookie2 supports multiple cookies, and <next> points to
7353 * a colon or semi-colon before the end. So skip all attr-value
7354 * pairs and look for the next comma. For Set-Cookie, since
7355 * commas are permitted in values, skip to the end.
7356 */
7357 if (is_cookie2)
7358 next = find_hdr_value_end(next, hdr_end);
7359 else
7360 next = hdr_end;
7361 }
7362
7363 /* Now everything is as on the diagram above */
7364
7365 /* Ignore cookies with no equal sign */
7366 if (equal == val_end)
7367 continue;
7368
7369 /* If there are spaces around the equal sign, we need to
7370 * strip them otherwise we'll get trouble for cookie captures,
7371 * or even for rewrites. Since this happens extremely rarely,
7372 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007373 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007374 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7375 int stripped_before = 0;
7376 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007377
Willy Tarreau24581ba2010-08-31 22:39:35 +02007378 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007379 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007380 equal += stripped_before;
7381 val_beg += stripped_before;
7382 }
7383
7384 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007385 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007386 val_beg += stripped_after;
7387 stripped_before += stripped_after;
7388 }
7389
7390 val_end += stripped_before;
7391 next += stripped_before;
7392 hdr_end += stripped_before;
7393 hdr_next += stripped_before;
7394 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007395 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007396 }
7397
7398 /* First, let's see if we want to capture this cookie. We check
7399 * that we don't already have a server side cookie, because we
7400 * can only capture one. Also as an optimisation, we ignore
7401 * cookies shorter than the declared name.
7402 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007403 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007404 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007405 (val_end - att_beg >= sess->fe->capture_namelen) &&
7406 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007407 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007408 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007409 Alert("HTTP logging : out of memory.\n");
7410 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007411 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007412 if (log_len > sess->fe->capture_len)
7413 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01007414 memcpy(txn->srv_cookie, att_beg, log_len);
7415 txn->srv_cookie[log_len] = 0;
7416 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007417 }
7418
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007419 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007420 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007421 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007422 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7423 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007424 /* assume passive cookie by default */
7425 txn->flags &= ~TX_SCK_MASK;
7426 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007427
7428 /* If the cookie is in insert mode on a known server, we'll delete
7429 * this occurrence because we'll insert another one later.
7430 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007431 * a direct access.
7432 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007433 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007434 /* The "preserve" flag was set, we don't want to touch the
7435 * server's cookie.
7436 */
7437 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007438 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007439 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007440 /* this cookie must be deleted */
7441 if (*prev == ':' && next == hdr_end) {
7442 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007443 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007444 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7445 txn->hdr_idx.used--;
7446 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007447 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007448 hdr_next += delta;
7449 http_msg_move_end(&txn->rsp, delta);
7450 /* note: while both invalid now, <next> and <hdr_end>
7451 * are still equal, so the for() will stop as expected.
7452 */
7453 } else {
7454 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007455 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007456 next = prev;
7457 hdr_end += delta;
7458 hdr_next += delta;
7459 cur_hdr->len += delta;
7460 http_msg_move_end(&txn->rsp, delta);
7461 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007462 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007463 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007464 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007465 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007466 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007467 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007468 * with this server since we know it.
7469 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007470 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007471 next += delta;
7472 hdr_end += delta;
7473 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007474 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007475 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007476
Willy Tarreauf1348312010-10-07 15:54:11 +02007477 txn->flags &= ~TX_SCK_MASK;
7478 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007479 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007480 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007481 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007482 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007483 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007484 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007485 next += delta;
7486 hdr_end += delta;
7487 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007488 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007489 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007490
Willy Tarreau827aee92011-03-10 16:55:02 +01007491 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007492 txn->flags &= ~TX_SCK_MASK;
7493 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007494 }
7495 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007496 /* that's done for this cookie, check the next one on the same
7497 * line when next != hdr_end (only if is_cookie2).
7498 */
7499 }
7500 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007501 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007502 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007503}
7504
7505
Willy Tarreaua15645d2007-03-18 16:22:39 +01007506/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007507 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007508 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007509void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007510{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007511 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007512 char *p1, *p2;
7513
7514 char *cur_ptr, *cur_end, *cur_next;
7515 int cur_idx;
7516
Willy Tarreau5df51872007-11-25 16:20:08 +01007517 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007518 return;
7519
7520 /* Iterate through the headers.
7521 * we start with the start line.
7522 */
7523 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007524 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007525
7526 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7527 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007528 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007529
7530 cur_hdr = &txn->hdr_idx.v[cur_idx];
7531 cur_ptr = cur_next;
7532 cur_end = cur_ptr + cur_hdr->len;
7533 cur_next = cur_end + cur_hdr->cr + 1;
7534
7535 /* We have one full header between cur_ptr and cur_end, and the
7536 * next header starts at cur_next. We're only interested in
7537 * "Cookie:" headers.
7538 */
7539
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007540 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7541 if (val) {
7542 if ((cur_end - (cur_ptr + val) >= 8) &&
7543 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7544 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7545 return;
7546 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007547 }
7548
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007549 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7550 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007551 continue;
7552
7553 /* OK, right now we know we have a cache-control header at cur_ptr */
7554
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007555 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007556
7557 if (p1 >= cur_end) /* no more info */
7558 continue;
7559
7560 /* p1 is at the beginning of the value */
7561 p2 = p1;
7562
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007563 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007564 p2++;
7565
7566 /* we have a complete value between p1 and p2 */
7567 if (p2 < cur_end && *p2 == '=') {
7568 /* we have something of the form no-cache="set-cookie" */
7569 if ((cur_end - p1 >= 21) &&
7570 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7571 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007572 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007573 continue;
7574 }
7575
7576 /* OK, so we know that either p2 points to the end of string or to a comma */
7577 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02007578 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01007579 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7580 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7581 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007582 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007583 return;
7584 }
7585
7586 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007587 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007588 continue;
7589 }
7590 }
7591}
7592
Willy Tarreau58f10d72006-12-04 02:26:12 +01007593
Willy Tarreaub2513902006-12-17 14:52:38 +01007594/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007595 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007596 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007597 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007598 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007599 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007600 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007601 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007602 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007603int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007604{
7605 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007606 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007607 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01007608
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007609 if (!uri_auth)
7610 return 0;
7611
Cyril Bonté70be45d2010-10-12 00:14:35 +02007612 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007613 return 0;
7614
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007615 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007616 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007617 return 0;
7618
Willy Tarreau414e9bb2013-11-23 00:30:38 +01007619 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007620 return 0;
7621
Willy Tarreaub2513902006-12-17 14:52:38 +01007622 return 1;
7623}
7624
Willy Tarreau4076a152009-04-02 15:18:36 +02007625/*
7626 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007627 * By default it tries to report the error position as msg->err_pos. However if
7628 * this one is not set, it will then report msg->next, which is the last known
7629 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007630 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007631 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007632void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007633 struct http_msg *msg,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02007634 enum h1_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007635{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007636 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007637 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007638 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007639
Willy Tarreauf3764b72016-03-31 13:45:10 +02007640 es->len = MIN(chn->buf->i, global.tune.bufsize);
Willy Tarreau9b28e032012-10-12 23:49:43 +02007641 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007642 len1 = MIN(len1, es->len);
7643 len2 = es->len - len1; /* remaining data if buffer wraps */
7644
Willy Tarreauf3764b72016-03-31 13:45:10 +02007645 if (!es->buf)
7646 es->buf = malloc(global.tune.bufsize);
7647
7648 if (es->buf) {
7649 memcpy(es->buf, chn->buf->p, len1);
7650 if (len2)
7651 memcpy(es->buf + len1, chn->buf->data, len2);
7652 }
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007653
Willy Tarreau4076a152009-04-02 15:18:36 +02007654 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007655 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007656 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007657 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007658
Willy Tarreau4076a152009-04-02 15:18:36 +02007659 es->when = date; // user-visible date
7660 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007661 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007662 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007663 if (objt_conn(sess->origin))
7664 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007665 else
7666 memset(&es->src, 0, sizeof(es->src));
7667
Willy Tarreau078272e2010-12-12 12:46:33 +01007668 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007669 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007670 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007671 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007672 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007673 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007674 es->b_out = chn->buf->o;
7675 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007676 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007677 es->m_clen = msg->chunk_len;
7678 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02007679}
Willy Tarreaub2513902006-12-17 14:52:38 +01007680
Willy Tarreau294c4732011-12-16 21:35:50 +01007681/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7682 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7683 * performed over the whole headers. Otherwise it must contain a valid header
7684 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7685 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7686 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7687 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007688 * -1. The value fetch stops at commas, so this function is suited for use with
7689 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01007690 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007691 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007692unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007693 struct hdr_idx *idx, int occ,
7694 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007695{
Willy Tarreau294c4732011-12-16 21:35:50 +01007696 struct hdr_ctx local_ctx;
7697 char *ptr_hist[MAX_HDR_HISTORY];
7698 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007699 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007700 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007701
Willy Tarreau294c4732011-12-16 21:35:50 +01007702 if (!ctx) {
7703 local_ctx.idx = 0;
7704 ctx = &local_ctx;
7705 }
7706
Willy Tarreaubce70882009-09-07 11:51:47 +02007707 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007708 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007709 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007710 occ--;
7711 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007712 *vptr = ctx->line + ctx->val;
7713 *vlen = ctx->vlen;
7714 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007715 }
7716 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007717 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007718 }
7719
7720 /* negative occurrence, we scan all the list then walk back */
7721 if (-occ > MAX_HDR_HISTORY)
7722 return 0;
7723
Willy Tarreau294c4732011-12-16 21:35:50 +01007724 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007725 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007726 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7727 len_hist[hist_ptr] = ctx->vlen;
7728 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007729 hist_ptr = 0;
7730 found++;
7731 }
7732 if (-occ > found)
7733 return 0;
7734 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02007735 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7736 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7737 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02007738 */
Willy Tarreau67dad272013-06-12 22:27:44 +02007739 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02007740 if (hist_ptr >= MAX_HDR_HISTORY)
7741 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007742 *vptr = ptr_hist[hist_ptr];
7743 *vlen = len_hist[hist_ptr];
7744 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007745}
7746
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007747/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7748 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7749 * performed over the whole headers. Otherwise it must contain a valid header
7750 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7751 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7752 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7753 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7754 * -1. This function differs from http_get_hdr() in that it only returns full
7755 * line header values and does not stop at commas.
7756 * The return value is 0 if nothing was found, or non-zero otherwise.
7757 */
7758unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
7759 struct hdr_idx *idx, int occ,
7760 struct hdr_ctx *ctx, char **vptr, int *vlen)
7761{
7762 struct hdr_ctx local_ctx;
7763 char *ptr_hist[MAX_HDR_HISTORY];
7764 int len_hist[MAX_HDR_HISTORY];
7765 unsigned int hist_ptr;
7766 int found;
7767
7768 if (!ctx) {
7769 local_ctx.idx = 0;
7770 ctx = &local_ctx;
7771 }
7772
7773 if (occ >= 0) {
7774 /* search from the beginning */
7775 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
7776 occ--;
7777 if (occ <= 0) {
7778 *vptr = ctx->line + ctx->val;
7779 *vlen = ctx->vlen;
7780 return 1;
7781 }
7782 }
7783 return 0;
7784 }
7785
7786 /* negative occurrence, we scan all the list then walk back */
7787 if (-occ > MAX_HDR_HISTORY)
7788 return 0;
7789
7790 found = hist_ptr = 0;
7791 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
7792 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7793 len_hist[hist_ptr] = ctx->vlen;
7794 if (++hist_ptr >= MAX_HDR_HISTORY)
7795 hist_ptr = 0;
7796 found++;
7797 }
7798 if (-occ > found)
7799 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007800
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007801 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007802 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
7803 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
7804 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007805 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02007806 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02007807 if (hist_ptr >= MAX_HDR_HISTORY)
7808 hist_ptr -= MAX_HDR_HISTORY;
7809 *vptr = ptr_hist[hist_ptr];
7810 *vlen = len_hist[hist_ptr];
7811 return 1;
7812}
7813
Willy Tarreaubaaee002006-06-26 02:48:02 +02007814/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007815 * Print a debug line with a header. Always stop at the first CR or LF char,
7816 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7817 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007818 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007819void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007820{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007821 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007822 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02007823
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007824 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007825 dir,
Willy Tarreau585744b2017-08-24 14:31:19 +02007826 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
7827 objt_conn(s->si[1].end) ? (unsigned short)objt_conn(s->si[1].end)->handle.fd : -1);
Willy Tarreaue92693a2012-09-24 21:13:39 +02007828
7829 for (max = 0; start + max < end; max++)
7830 if (start[max] == '\r' || start[max] == '\n')
7831 break;
7832
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007833 UBOUND(max, trash.size - trash.len - 3);
7834 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
7835 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01007836 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007837}
7838
Willy Tarreaueee5b512015-04-03 23:46:31 +02007839
7840/* Allocate a new HTTP transaction for stream <s> unless there is one already.
7841 * The hdr_idx is allocated as well. In case of allocation failure, everything
7842 * allocated is freed and NULL is returned. Otherwise the new transaction is
7843 * assigned to the stream and returned.
7844 */
7845struct http_txn *http_alloc_txn(struct stream *s)
7846{
7847 struct http_txn *txn = s->txn;
7848
7849 if (txn)
7850 return txn;
7851
7852 txn = pool_alloc2(pool2_http_txn);
7853 if (!txn)
7854 return txn;
7855
7856 txn->hdr_idx.size = global.tune.max_http_hdr;
7857 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
7858 if (!txn->hdr_idx.v) {
7859 pool_free2(pool2_http_txn, txn);
7860 return NULL;
7861 }
7862
7863 s->txn = txn;
7864 return txn;
7865}
7866
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007867void http_txn_reset_req(struct http_txn *txn)
7868{
7869 txn->req.flags = 0;
7870 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
7871 txn->req.next = 0;
7872 txn->req.chunk_len = 0LL;
7873 txn->req.body_len = 0LL;
7874 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7875}
7876
7877void http_txn_reset_res(struct http_txn *txn)
7878{
7879 txn->rsp.flags = 0;
7880 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
7881 txn->rsp.next = 0;
7882 txn->rsp.chunk_len = 0LL;
7883 txn->rsp.body_len = 0LL;
7884 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
7885}
7886
Willy Tarreau0937bc42009-12-22 15:03:09 +01007887/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007888 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01007889 * the required fields are properly allocated and that we only need to (re)init
7890 * them. This should be used before processing any new request.
7891 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007892void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007893{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007894 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007895 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007896
7897 txn->flags = 0;
7898 txn->status = -1;
7899
Willy Tarreauf64d1412010-10-07 20:06:11 +02007900 txn->cookie_first_date = 0;
7901 txn->cookie_last_date = 0;
7902
Willy Tarreaueee5b512015-04-03 23:46:31 +02007903 txn->srv_cookie = NULL;
7904 txn->cli_cookie = NULL;
7905 txn->uri = NULL;
7906
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02007907 http_txn_reset_req(txn);
7908 http_txn_reset_res(txn);
7909
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007910 txn->req.chn = &s->req;
7911 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007912
7913 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007914
7915 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7916 if (fe->options2 & PR_O2_REQBUG_OK)
7917 txn->req.err_pos = -1; /* let buggy requests pass */
7918
Willy Tarreau0937bc42009-12-22 15:03:09 +01007919 if (txn->hdr_idx.v)
7920 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02007921
7922 vars_init(&s->vars_txn, SCOPE_TXN);
7923 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007924}
7925
7926/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02007927void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007928{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007929 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007930 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007931
7932 /* these ones will have been dynamically allocated */
7933 pool_free2(pool2_requri, txn->uri);
7934 pool_free2(pool2_capture, txn->cli_cookie);
7935 pool_free2(pool2_capture, txn->srv_cookie);
William Lallemanda73203e2012-03-12 12:48:57 +01007936 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007937
William Lallemanda73203e2012-03-12 12:48:57 +01007938 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007939 txn->uri = NULL;
7940 txn->srv_cookie = NULL;
7941 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007942
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007943 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007944 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007945 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007946 pool_free2(h->pool, s->req_cap[h->index]);
7947 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007948 }
7949
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007950 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01007951 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007952 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02007953 pool_free2(h->pool, s->res_cap[h->index]);
7954 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01007955 }
7956
Willy Tarreau6204cd92016-03-10 16:33:04 +01007957 vars_prune(&s->vars_txn, s->sess, s);
7958 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01007959}
7960
7961/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02007962void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007963{
7964 http_end_txn(s);
7965 http_init_txn(s);
7966
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01007967 /* reinitialise the current rule list pointer to NULL. We are sure that
7968 * any rulelist match the NULL pointer.
7969 */
7970 s->current_rule_list = NULL;
7971
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007972 s->be = strm_fe(s);
7973 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02007974 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02007975 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007976 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01007977 /* re-init store persistence */
7978 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01007979 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01007980
Willy Tarreau0937bc42009-12-22 15:03:09 +01007981 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007982
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007983 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01007984
Willy Tarreau739cfba2010-01-25 23:11:14 +01007985 /* We must trim any excess data from the response buffer, because we
7986 * may have blocked an invalid response from a server that we don't
7987 * want to accidentely forward once we disable the analysers, nor do
7988 * we want those data to come along with next response. A typical
7989 * example of such data would be from a buggy server responding to
7990 * a HEAD with some data, or sending more than the advertised
7991 * content-length.
7992 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007993 if (unlikely(s->res.buf->i))
7994 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01007995
Christopher Fauletc0c672a2017-03-28 11:51:33 +02007996 /* Now we can realign the response buffer */
7997 buffer_realign(s->res.buf);
7998
Willy Tarreaud0d8da92015-04-04 02:10:38 +02007999 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008000 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008001
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008002 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008003 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008004
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008005 s->req.rex = TICK_ETERNITY;
8006 s->req.wex = TICK_ETERNITY;
8007 s->req.analyse_exp = TICK_ETERNITY;
8008 s->res.rex = TICK_ETERNITY;
8009 s->res.wex = TICK_ETERNITY;
8010 s->res.analyse_exp = TICK_ETERNITY;
Hongbo Longe39683c2017-03-10 18:41:51 +01008011 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008012}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008013
Sasha Pachev218f0642014-06-16 12:05:59 -06008014void free_http_res_rules(struct list *r)
8015{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008016 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06008017
8018 list_for_each_entry_safe(pr, tr, r, list) {
8019 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008020 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008021 free(pr);
8022 }
8023}
8024
8025void free_http_req_rules(struct list *r)
8026{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008027 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01008028
8029 list_for_each_entry_safe(pr, tr, r, list) {
8030 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008031 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008032 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008033
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008034 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008035 free(pr);
8036 }
8037}
8038
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008039/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008040struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01008041{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008042 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008043 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008044 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008045 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008046
Vincent Bernat02779b62016-04-03 13:48:43 +02008047 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01008048 if (!rule) {
8049 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008050 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008051 }
8052
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008053 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008054 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01008055 cur_arg = 1;
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008056 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block") || !strcmp(args[0], "tarpit")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04008057 int code;
8058 int hc;
8059
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008060 if (!strcmp(args[0], "tarpit")) {
8061 rule->action = ACT_HTTP_REQ_TARPIT;
8062 rule->deny_status = HTTP_ERR_500;
8063 }
8064 else {
8065 rule->action = ACT_ACTION_DENY;
8066 rule->deny_status = HTTP_ERR_403;
8067 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008068 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04008069 if (strcmp(args[cur_arg], "deny_status") == 0) {
8070 cur_arg++;
8071 if (!args[cur_arg]) {
8072 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
8073 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
8074 goto out_err;
8075 }
8076
8077 code = atol(args[cur_arg]);
8078 cur_arg++;
8079 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
8080 if (http_err_codes[hc] == code) {
8081 rule->deny_status = hc;
8082 break;
8083 }
8084 }
8085
8086 if (hc >= HTTP_ERR_SIZE) {
Jarno Huuskonen800d1762017-03-06 14:56:36 +02008087 Warning("parsing [%s:%d] : status code %d not handled, using default code %d.\n",
8088 file, linenum, code, http_err_codes[rule->deny_status]);
CJ Ess108b1dd2015-04-07 12:03:37 -04008089 }
8090 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008091 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008092 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008093 cur_arg = 1;
8094
8095 while(*args[cur_arg]) {
8096 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008097 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008098 cur_arg+=2;
8099 continue;
8100 } else
8101 break;
8102 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008103 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008104 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008105 cur_arg = 1;
8106
8107 if (!*args[cur_arg] ||
8108 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8109 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8110 file, linenum, args[0]);
8111 goto out_err;
8112 }
8113 rule->arg.nice = atoi(args[cur_arg]);
8114 if (rule->arg.nice < -1024)
8115 rule->arg.nice = -1024;
8116 else if (rule->arg.nice > 1024)
8117 rule->arg.nice = 1024;
8118 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008119 } else if (!strcmp(args[0], "set-tos")) {
8120#ifdef IP_TOS
8121 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008122 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008123 cur_arg = 1;
8124
8125 if (!*args[cur_arg] ||
8126 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8127 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8128 file, linenum, args[0]);
8129 goto out_err;
8130 }
8131
8132 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8133 if (err && *err != '\0') {
8134 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8135 file, linenum, err, args[0]);
8136 goto out_err;
8137 }
8138 cur_arg++;
8139#else
8140 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8141 goto out_err;
8142#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008143 } else if (!strcmp(args[0], "set-mark")) {
8144#ifdef SO_MARK
8145 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008146 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008147 cur_arg = 1;
8148
8149 if (!*args[cur_arg] ||
8150 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8151 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8152 file, linenum, args[0]);
8153 goto out_err;
8154 }
8155
8156 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8157 if (err && *err != '\0') {
8158 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8159 file, linenum, err, args[0]);
8160 goto out_err;
8161 }
8162 cur_arg++;
8163 global.last_checks |= LSTCHK_NETADM;
8164#else
8165 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8166 goto out_err;
8167#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008168 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008169 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +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 bad_log_level:
8175 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8176 file, linenum, args[0]);
8177 goto out_err;
8178 }
8179 if (strcmp(args[cur_arg], "silent") == 0)
8180 rule->arg.loglevel = -1;
8181 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8182 goto bad_log_level;
8183 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008184 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008185 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008186 cur_arg = 1;
8187
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008188 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8189 (*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 +01008190 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8191 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008192 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008193 }
8194
8195 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8196 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8197 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008198
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008199 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008200 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008201 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 +01008202 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8203 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8204 file, linenum, args[0], error);
8205 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008206 goto out_err;
8207 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008208 free(proxy->conf.lfs_file);
8209 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8210 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008211 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02008212 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008213 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008214 cur_arg = 1;
8215
8216 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02008217 (*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 -06008218 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
8219 file, linenum, args[0]);
8220 goto out_err;
8221 }
8222
8223 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8224 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8225 LIST_INIT(&rule->arg.hdr_add.fmt);
8226
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008227 error = NULL;
8228 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
8229 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8230 args[cur_arg + 1], error);
8231 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008232 goto out_err;
8233 }
8234
8235 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008236 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008237 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 +01008238 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8239 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8240 file, linenum, args[0], error);
8241 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008242 goto out_err;
8243 }
Sasha Pachev218f0642014-06-16 12:05:59 -06008244
8245 free(proxy->conf.lfs_file);
8246 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8247 proxy->conf.lfs_line = proxy->conf.args.line;
8248 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008249 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008250 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008251 cur_arg = 1;
8252
8253 if (!*args[cur_arg] ||
8254 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8255 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8256 file, linenum, args[0]);
8257 goto out_err;
8258 }
8259
8260 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8261 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8262
8263 proxy->conf.args.ctx = ARGC_HRQ;
8264 free(proxy->conf.lfs_file);
8265 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8266 proxy->conf.lfs_line = proxy->conf.args.line;
8267 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02008268 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
8269 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02008270 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02008271 struct sample_expr *expr;
8272 unsigned int where;
8273 char *err = NULL;
8274
8275 cur_arg = 1;
8276 proxy->conf.args.ctx = ARGC_TRK;
8277
8278 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
8279 if (!expr) {
8280 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8281 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
8282 free(err);
8283 goto out_err;
8284 }
8285
8286 where = 0;
8287 if (proxy->cap & PR_CAP_FE)
8288 where |= SMP_VAL_FE_HRQ_HDR;
8289 if (proxy->cap & PR_CAP_BE)
8290 where |= SMP_VAL_BE_HRQ_HDR;
8291
8292 if (!(expr->fetch->val & where)) {
8293 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
8294 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
8295 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
8296 args[cur_arg-1], sample_src_names(expr->fetch->use));
8297 free(expr);
8298 goto out_err;
8299 }
8300
8301 if (strcmp(args[cur_arg], "table") == 0) {
8302 cur_arg++;
8303 if (!args[cur_arg]) {
8304 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
8305 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
8306 free(expr);
8307 goto out_err;
8308 }
8309 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008310 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02008311 cur_arg++;
8312 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02008313 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008314 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01008315 } else if (strcmp(args[0], "redirect") == 0) {
8316 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008317 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008318
Willy Tarreaube4653b2015-05-28 15:26:58 +02008319 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01008320 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8321 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8322 goto out_err;
8323 }
8324
8325 /* this redirect rule might already contain a parsed condition which
8326 * we'll pass to the http-request rule.
8327 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008328 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008329 rule->arg.redir = redir;
8330 rule->cond = redir->cond;
8331 redir->cond = NULL;
8332 cur_arg = 2;
8333 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008334 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8335 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008336 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008337 /*
8338 * '+ 8' for 'add-acl('
8339 * '- 9' for 'add-acl(' + trailing ')'
8340 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008341 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008342
8343 cur_arg = 1;
8344
8345 if (!*args[cur_arg] ||
8346 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8347 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8348 file, linenum, args[0]);
8349 goto out_err;
8350 }
8351
8352 LIST_INIT(&rule->arg.map.key);
8353 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008354 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008355 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008356 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8357 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8358 file, linenum, args[0], error);
8359 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008360 goto out_err;
8361 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008362 free(proxy->conf.lfs_file);
8363 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8364 proxy->conf.lfs_line = proxy->conf.args.line;
8365 cur_arg += 1;
8366 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8367 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008368 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008369 /*
8370 * '+ 8' for 'del-acl('
8371 * '- 9' for 'del-acl(' + trailing ')'
8372 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008373 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008374
8375 cur_arg = 1;
8376
8377 if (!*args[cur_arg] ||
8378 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8379 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8380 file, linenum, args[0]);
8381 goto out_err;
8382 }
8383
8384 LIST_INIT(&rule->arg.map.key);
8385 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008386 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008387 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008388 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8389 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8390 file, linenum, args[0], error);
8391 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008392 goto out_err;
8393 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008394 free(proxy->conf.lfs_file);
8395 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8396 proxy->conf.lfs_line = proxy->conf.args.line;
8397 cur_arg += 1;
8398 } else if (strncmp(args[0], "del-map", 7) == 0) {
8399 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008400 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008401 /*
8402 * '+ 8' for 'del-map('
8403 * '- 9' for 'del-map(' + trailing ')'
8404 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008405 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008406
8407 cur_arg = 1;
8408
8409 if (!*args[cur_arg] ||
8410 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8411 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8412 file, linenum, args[0]);
8413 goto out_err;
8414 }
8415
8416 LIST_INIT(&rule->arg.map.key);
8417 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008418 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008419 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008420 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8421 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
8422 file, linenum, args[0], error);
8423 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008424 goto out_err;
8425 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008426 free(proxy->conf.lfs_file);
8427 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8428 proxy->conf.lfs_line = proxy->conf.args.line;
8429 cur_arg += 1;
8430 } else if (strncmp(args[0], "set-map", 7) == 0) {
8431 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008432 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008433 /*
8434 * '+ 8' for 'set-map('
8435 * '- 9' for 'set-map(' + trailing ')'
8436 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008437 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008438
8439 cur_arg = 1;
8440
8441 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8442 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8443 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8444 file, linenum, args[0]);
8445 goto out_err;
8446 }
8447
8448 LIST_INIT(&rule->arg.map.key);
8449 LIST_INIT(&rule->arg.map.value);
8450 proxy->conf.args.ctx = ARGC_HRQ;
8451
8452 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008453 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008454 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008455 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8456 Alert("parsing [%s:%d]: 'http-request %s' key: %s.\n",
8457 file, linenum, args[0], error);
8458 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008459 goto out_err;
8460 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008461
8462 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008463 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008464 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 +01008465 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
8466 Alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n",
8467 file, linenum, args[0], error);
8468 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008469 goto out_err;
8470 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008471 free(proxy->conf.lfs_file);
8472 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8473 proxy->conf.lfs_line = proxy->conf.args.line;
8474
8475 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02008476 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
8477 char *errmsg = NULL;
8478 cur_arg = 1;
8479 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02008480 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02008481 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02008482 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02008483 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8484 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8485 free(errmsg);
8486 goto out_err;
8487 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008488 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02008489 action_build_list(&http_req_keywords.list, &trash);
8490 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
8491 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08008492 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02008493 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02008494 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008495 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008496 }
8497
8498 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8499 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008500 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008501
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008502 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8503 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8504 file, linenum, args[0], errmsg);
8505 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008506 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008507 }
8508 rule->cond = cond;
8509 }
8510 else if (*args[cur_arg]) {
8511 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8512 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8513 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008514 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008515 }
8516
8517 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008518 out_err:
8519 free(rule);
8520 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008521}
8522
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008523/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008524struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008525{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008526 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008527 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008528 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008529 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008530
8531 rule = calloc(1, sizeof(*rule));
8532 if (!rule) {
8533 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
8534 goto out_err;
8535 }
8536
8537 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008538 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008539 cur_arg = 1;
8540 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008541 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008542 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008543 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008544 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008545 cur_arg = 1;
8546
8547 if (!*args[cur_arg] ||
8548 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8549 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8550 file, linenum, args[0]);
8551 goto out_err;
8552 }
8553 rule->arg.nice = atoi(args[cur_arg]);
8554 if (rule->arg.nice < -1024)
8555 rule->arg.nice = -1024;
8556 else if (rule->arg.nice > 1024)
8557 rule->arg.nice = 1024;
8558 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008559 } else if (!strcmp(args[0], "set-tos")) {
8560#ifdef IP_TOS
8561 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008562 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008563 cur_arg = 1;
8564
8565 if (!*args[cur_arg] ||
8566 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8567 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8568 file, linenum, args[0]);
8569 goto out_err;
8570 }
8571
8572 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8573 if (err && *err != '\0') {
8574 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8575 file, linenum, err, args[0]);
8576 goto out_err;
8577 }
8578 cur_arg++;
8579#else
8580 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8581 goto out_err;
8582#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008583 } else if (!strcmp(args[0], "set-mark")) {
8584#ifdef SO_MARK
8585 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008586 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02008587 cur_arg = 1;
8588
8589 if (!*args[cur_arg] ||
8590 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8591 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8592 file, linenum, args[0]);
8593 goto out_err;
8594 }
8595
8596 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8597 if (err && *err != '\0') {
8598 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8599 file, linenum, err, args[0]);
8600 goto out_err;
8601 }
8602 cur_arg++;
8603 global.last_checks |= LSTCHK_NETADM;
8604#else
8605 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8606 goto out_err;
8607#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008608 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008609 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +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 bad_log_level:
8615 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8616 file, linenum, args[0]);
8617 goto out_err;
8618 }
8619 if (strcmp(args[cur_arg], "silent") == 0)
8620 rule->arg.loglevel = -1;
Ruoshan Huangdd016782016-06-15 22:16:03 +08008621 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008622 goto bad_log_level;
8623 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008624 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008625 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008626 cur_arg = 1;
8627
8628 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8629 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8630 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8631 file, linenum, args[0]);
8632 goto out_err;
8633 }
8634
8635 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8636 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8637 LIST_INIT(&rule->arg.hdr_add.fmt);
8638
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008639 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008640 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008641 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 +01008642 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8643 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8644 file, linenum, args[0], error);
8645 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008646 goto out_err;
8647 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008648 free(proxy->conf.lfs_file);
8649 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8650 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008651 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06008652 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008653 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008654 cur_arg = 1;
8655
8656 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02008657 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
8658 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06008659 file, linenum, args[0]);
8660 goto out_err;
8661 }
8662
8663 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8664 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8665 LIST_INIT(&rule->arg.hdr_add.fmt);
8666
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008667 error = NULL;
8668 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
8669 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8670 args[cur_arg + 1], error);
8671 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008672 goto out_err;
8673 }
8674
8675 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008676 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008677 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 +01008678 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8679 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8680 file, linenum, args[0], error);
8681 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008682 goto out_err;
8683 }
Sasha Pachev218f0642014-06-16 12:05:59 -06008684
8685 free(proxy->conf.lfs_file);
8686 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8687 proxy->conf.lfs_line = proxy->conf.args.line;
8688 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008689 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008690 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008691 cur_arg = 1;
8692
8693 if (!*args[cur_arg] ||
8694 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8695 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8696 file, linenum, args[0]);
8697 goto out_err;
8698 }
8699
8700 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8701 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8702
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008703 proxy->conf.args.ctx = ARGC_HRS;
8704 free(proxy->conf.lfs_file);
8705 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8706 proxy->conf.lfs_line = proxy->conf.args.line;
8707 cur_arg += 1;
8708 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8709 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008710 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008711 /*
8712 * '+ 8' for 'add-acl('
8713 * '- 9' for 'add-acl(' + trailing ')'
8714 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008715 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008716
8717 cur_arg = 1;
8718
8719 if (!*args[cur_arg] ||
8720 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8721 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8722 file, linenum, args[0]);
8723 goto out_err;
8724 }
8725
8726 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008727 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008728 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008729 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008730 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8731 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8732 file, linenum, args[0], error);
8733 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008734 goto out_err;
8735 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008736 free(proxy->conf.lfs_file);
8737 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8738 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008739
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008740 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008741 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8742 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008743 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008744 /*
8745 * '+ 8' for 'del-acl('
8746 * '- 9' for 'del-acl(' + trailing ')'
8747 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008748 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008749
8750 cur_arg = 1;
8751
8752 if (!*args[cur_arg] ||
8753 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8754 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8755 file, linenum, args[0]);
8756 goto out_err;
8757 }
8758
8759 LIST_INIT(&rule->arg.map.key);
8760 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008761 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008762 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008763 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8764 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
8765 file, linenum, args[0], error);
8766 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008767 goto out_err;
8768 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008769 free(proxy->conf.lfs_file);
8770 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8771 proxy->conf.lfs_line = proxy->conf.args.line;
8772 cur_arg += 1;
8773 } else if (strncmp(args[0], "del-map", 7) == 0) {
8774 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008775 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008776 /*
8777 * '+ 8' for 'del-map('
8778 * '- 9' for 'del-map(' + trailing ')'
8779 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008780 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008781
8782 cur_arg = 1;
8783
8784 if (!*args[cur_arg] ||
8785 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8786 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8787 file, linenum, args[0]);
8788 goto out_err;
8789 }
8790
8791 LIST_INIT(&rule->arg.map.key);
8792 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008793 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008794 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008795 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8796 Alert("parsing [%s:%d]: 'http-response %s' %s.\n",
8797 file, linenum, args[0], error);
8798 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008799 goto out_err;
8800 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008801 free(proxy->conf.lfs_file);
8802 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8803 proxy->conf.lfs_line = proxy->conf.args.line;
8804 cur_arg += 1;
8805 } else if (strncmp(args[0], "set-map", 7) == 0) {
8806 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008807 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008808 /*
8809 * '+ 8' for 'set-map('
8810 * '- 9' for 'set-map(' + trailing ')'
8811 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008812 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008813
8814 cur_arg = 1;
8815
8816 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8817 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8818 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8819 file, linenum, args[0]);
8820 goto out_err;
8821 }
8822
8823 LIST_INIT(&rule->arg.map.key);
8824 LIST_INIT(&rule->arg.map.value);
8825
8826 proxy->conf.args.ctx = ARGC_HRS;
8827
8828 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008829 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008830 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008831 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8832 Alert("parsing [%s:%d]: 'http-response %s' name: %s.\n",
8833 file, linenum, args[0], error);
8834 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008835 goto out_err;
8836 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008837
8838 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01008839 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008840 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 +01008841 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
8842 Alert("parsing [%s:%d]: 'http-response %s' value: %s.\n",
8843 file, linenum, args[0], error);
8844 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01008845 goto out_err;
8846 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008847
8848 free(proxy->conf.lfs_file);
8849 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8850 proxy->conf.lfs_line = proxy->conf.args.line;
8851
8852 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02008853 } else if (strcmp(args[0], "redirect") == 0) {
8854 struct redirect_rule *redir;
8855 char *errmsg = NULL;
8856
8857 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
8858 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
8859 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8860 goto out_err;
8861 }
8862
8863 /* this redirect rule might already contain a parsed condition which
8864 * we'll pass to the http-request rule.
8865 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008866 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02008867 rule->arg.redir = redir;
8868 rule->cond = redir->cond;
8869 redir->cond = NULL;
8870 cur_arg = 2;
8871 return rule;
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08008872 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
8873 args[0][9] == '\0' && args[0][8] >= '0' &&
8874 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
8875 struct sample_expr *expr;
8876 unsigned int where;
8877 char *err = NULL;
8878
8879 cur_arg = 1;
8880 proxy->conf.args.ctx = ARGC_TRK;
8881
8882 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
8883 if (!expr) {
8884 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
8885 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
8886 free(err);
8887 goto out_err;
8888 }
8889
8890 where = 0;
8891 if (proxy->cap & PR_CAP_FE)
8892 where |= SMP_VAL_FE_HRS_HDR;
8893 if (proxy->cap & PR_CAP_BE)
8894 where |= SMP_VAL_BE_HRS_HDR;
8895
8896 if (!(expr->fetch->val & where)) {
8897 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :"
8898 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
8899 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
8900 args[cur_arg-1], sample_src_names(expr->fetch->use));
8901 free(expr);
8902 goto out_err;
8903 }
8904
8905 if (strcmp(args[cur_arg], "table") == 0) {
8906 cur_arg++;
8907 if (!args[cur_arg]) {
8908 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n",
8909 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
8910 free(expr);
8911 goto out_err;
8912 }
8913 /* we copy the table name for now, it will be resolved later */
8914 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
8915 cur_arg++;
8916 }
8917 rule->arg.trk_ctr.expr = expr;
8918 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
William Lallemand73025dd2014-04-24 14:38:37 +02008919 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
8920 char *errmsg = NULL;
8921 cur_arg = 1;
8922 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02008923 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02008924 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02008925 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02008926 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
8927 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8928 free(errmsg);
8929 goto out_err;
8930 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008931 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02008932 action_build_list(&http_res_keywords.list, &trash);
8933 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
8934 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08008935 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02008936 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02008937 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008938 goto out_err;
8939 }
8940
8941 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8942 struct acl_cond *cond;
8943 char *errmsg = NULL;
8944
8945 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8946 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
8947 file, linenum, args[0], errmsg);
8948 free(errmsg);
8949 goto out_err;
8950 }
8951 rule->cond = cond;
8952 }
8953 else if (*args[cur_arg]) {
8954 Alert("parsing [%s:%d]: 'http-response %s' expects"
8955 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8956 file, linenum, args[0], args[cur_arg]);
8957 goto out_err;
8958 }
8959
8960 return rule;
8961 out_err:
8962 free(rule);
8963 return NULL;
8964}
8965
Willy Tarreau4baae242012-12-27 12:00:31 +01008966/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008967 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02008968 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
8969 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01008970 */
8971struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02008972 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01008973{
8974 struct redirect_rule *rule;
8975 int cur_arg;
8976 int type = REDIRECT_TYPE_NONE;
8977 int code = 302;
8978 const char *destination = NULL;
8979 const char *cookie = NULL;
8980 int cookie_set = 0;
8981 unsigned int flags = REDIRECT_FLAG_NONE;
8982 struct acl_cond *cond = NULL;
8983
8984 cur_arg = 0;
8985 while (*(args[cur_arg])) {
8986 if (strcmp(args[cur_arg], "location") == 0) {
8987 if (!*args[cur_arg + 1])
8988 goto missing_arg;
8989
8990 type = REDIRECT_TYPE_LOCATION;
8991 cur_arg++;
8992 destination = args[cur_arg];
8993 }
8994 else if (strcmp(args[cur_arg], "prefix") == 0) {
8995 if (!*args[cur_arg + 1])
8996 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01008997 type = REDIRECT_TYPE_PREFIX;
8998 cur_arg++;
8999 destination = args[cur_arg];
9000 }
9001 else if (strcmp(args[cur_arg], "scheme") == 0) {
9002 if (!*args[cur_arg + 1])
9003 goto missing_arg;
9004
9005 type = REDIRECT_TYPE_SCHEME;
9006 cur_arg++;
9007 destination = args[cur_arg];
9008 }
9009 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9010 if (!*args[cur_arg + 1])
9011 goto missing_arg;
9012
9013 cur_arg++;
9014 cookie = args[cur_arg];
9015 cookie_set = 1;
9016 }
9017 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9018 if (!*args[cur_arg + 1])
9019 goto missing_arg;
9020
9021 cur_arg++;
9022 cookie = args[cur_arg];
9023 cookie_set = 0;
9024 }
9025 else if (strcmp(args[cur_arg], "code") == 0) {
9026 if (!*args[cur_arg + 1])
9027 goto missing_arg;
9028
9029 cur_arg++;
9030 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009031 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009032 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009033 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009034 args[cur_arg - 1], args[cur_arg]);
9035 return NULL;
9036 }
9037 }
9038 else if (!strcmp(args[cur_arg],"drop-query")) {
9039 flags |= REDIRECT_FLAG_DROP_QS;
9040 }
9041 else if (!strcmp(args[cur_arg],"append-slash")) {
9042 flags |= REDIRECT_FLAG_APPEND_SLASH;
9043 }
9044 else if (strcmp(args[cur_arg], "if") == 0 ||
9045 strcmp(args[cur_arg], "unless") == 0) {
9046 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9047 if (!cond) {
9048 memprintf(errmsg, "error in condition: %s", *errmsg);
9049 return NULL;
9050 }
9051 break;
9052 }
9053 else {
9054 memprintf(errmsg,
9055 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9056 args[cur_arg]);
9057 return NULL;
9058 }
9059 cur_arg++;
9060 }
9061
9062 if (type == REDIRECT_TYPE_NONE) {
9063 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9064 return NULL;
9065 }
9066
Willy Tarreaube4653b2015-05-28 15:26:58 +02009067 if (dir && type != REDIRECT_TYPE_LOCATION) {
9068 memprintf(errmsg, "response only supports redirect type 'location'");
9069 return NULL;
9070 }
9071
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02009072 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +01009073 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009074 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009075
9076 if (!use_fmt) {
9077 /* old-style static redirect rule */
9078 rule->rdr_str = strdup(destination);
9079 rule->rdr_len = strlen(destination);
9080 }
9081 else {
9082 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009083
9084 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9085 * if prefix == "/", we don't want to add anything, otherwise it
9086 * makes it hard for the user to configure a self-redirection.
9087 */
Godbachd9722032014-12-18 15:44:58 +08009088 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009089 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009090 if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
9091 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 +01009092 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9093 errmsg)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009094 return NULL;
9095 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009096 free(curproxy->conf.lfs_file);
9097 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9098 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009099 }
9100 }
9101
Willy Tarreau4baae242012-12-27 12:00:31 +01009102 if (cookie) {
9103 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9104 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9105 */
9106 rule->cookie_len = strlen(cookie);
9107 if (cookie_set) {
9108 rule->cookie_str = malloc(rule->cookie_len + 10);
9109 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9110 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9111 rule->cookie_len += 9;
9112 } else {
9113 rule->cookie_str = malloc(rule->cookie_len + 21);
9114 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9115 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9116 rule->cookie_len += 20;
9117 }
9118 }
9119 rule->type = type;
9120 rule->code = code;
9121 rule->flags = flags;
9122 LIST_INIT(&rule->list);
9123 return rule;
9124
9125 missing_arg:
9126 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9127 return NULL;
9128}
9129
Willy Tarreau8797c062007-05-07 00:55:35 +02009130/************************************************************************/
9131/* The code below is dedicated to ACL parsing and matching */
9132/************************************************************************/
9133
9134
Willy Tarreau14174bc2012-04-16 14:34:04 +02009135/* This function ensures that the prerequisites for an L7 fetch are ready,
9136 * which means that a request or response is ready. If some data is missing,
9137 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009138 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9139 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009140 *
9141 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009142 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9143 * decide whether or not an HTTP message is present ;
9144 * 0 if the requested data cannot be fetched or if it is certain that
9145 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009146 * 1 if an HTTP message is ready
9147 */
James Rosewell91a41cb2015-09-18 17:11:16 +01009148int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009149 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009150{
Willy Tarreau192252e2015-04-04 01:47:55 +02009151 struct http_txn *txn;
9152 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009153
Willy Tarreaube508f12016-03-10 11:47:01 +01009154 /* Note: it is possible that <s> is NULL when called before stream
9155 * initialization (eg: tcp-request connection), so this function is the
9156 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009157 */
Willy Tarreau192252e2015-04-04 01:47:55 +02009158 if (!s)
9159 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +01009160
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +02009161 if (!s->txn) {
9162 if (unlikely(!http_alloc_txn(s)))
9163 return 0; /* not enough memory */
9164 http_init_txn(s);
9165 }
Willy Tarreau192252e2015-04-04 01:47:55 +02009166 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +02009167 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009168
9169 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009170 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009171
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009172 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +01009173 /* If the buffer does not leave enough free space at the end,
9174 * we must first realign it.
9175 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009176 if (s->req.buf->p > s->req.buf->data &&
9177 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
9178 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +01009179
Willy Tarreau14174bc2012-04-16 14:34:04 +02009180 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009181 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009182 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009183
9184 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009185 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009186 http_msg_analyzer(msg, &txn->hdr_idx);
9187
9188 /* Still no valid request ? */
9189 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009190 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009191 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009192 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009193 }
9194 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009195 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009196 return 0;
9197 }
9198
9199 /* OK we just got a valid HTTP request. We have some minor
9200 * preparation to perform so that further checks can rely
9201 * on HTTP tests.
9202 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009203
9204 /* If the request was parsed but was too large, we must absolutely
9205 * return an error so that it is not processed. At the moment this
9206 * cannot happen, but if the parsers are to change in the future,
9207 * we want this check to be maintained.
9208 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009209 if (unlikely(s->req.buf->i + s->req.buf->p >
9210 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01009211 msg->err_state = msg->msg_state;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009212 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009213 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009214 return 1;
9215 }
9216
Willy Tarreau9b28e032012-10-12 23:49:43 +02009217 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009218 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02009219 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009220
Willy Tarreau506d0502013-07-06 13:29:24 +02009221 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9222 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009223 }
9224
Willy Tarreau506d0502013-07-06 13:29:24 +02009225 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009226 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009227 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009228
9229 /* otherwise everything's ready for the request */
9230 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009231 else {
9232 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009233 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9234 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009235 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009236 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009237 }
9238
9239 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009240 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009241 return 1;
9242}
Willy Tarreau8797c062007-05-07 00:55:35 +02009243
Willy Tarreau8797c062007-05-07 00:55:35 +02009244/* 1. Check on METHOD
9245 * We use the pre-parsed method if it is known, and store its number as an
9246 * integer. If it is unknown, we use the pointer and the length.
9247 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009248static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009249{
9250 int len, meth;
9251
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009252 len = strlen(text);
9253 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009254
9255 pattern->val.i = meth;
9256 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +02009257 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009258 pattern->len = len;
9259 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009260 else {
9261 pattern->ptr.str = NULL;
9262 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009263 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009264 return 1;
9265}
9266
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009267/* This function fetches the method of current HTTP request and stores
9268 * it in the global pattern struct as a chunk. There are two possibilities :
9269 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9270 * in <len> and <ptr> is NULL ;
9271 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9272 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009273 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009274 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009275static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009276smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009277{
9278 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +01009279 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009280
Willy Tarreau24e32d82012-04-23 23:55:44 +02009281 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009282
Willy Tarreaube508f12016-03-10 11:47:01 +01009283 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009284 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009285 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009286 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009287 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009288 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9289 /* ensure the indexes are not affected */
9290 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009291 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009292 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
9293 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009294 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009295 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009296 return 1;
9297}
9298
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009299/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009300static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009301{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009302 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009303 struct pattern_list *lst;
9304 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009305
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009306 list_for_each_entry(lst, &expr->patterns, list) {
9307 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009308
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009309 /* well-known method */
9310 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009311 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009312 return pattern;
9313 else
9314 continue;
9315 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009316
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009317 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009318 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009319 continue;
9320
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009321 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009322 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
9323 (!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 +01009324 return pattern;
9325 }
9326 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009327}
9328
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009329static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009330smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009331{
Willy Tarreaube508f12016-03-10 11:47:01 +01009332 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009333 char *ptr;
9334 int len;
9335
Willy Tarreauc0239e02012-04-16 14:42:55 +02009336 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009337
Willy Tarreaube508f12016-03-10 11:47:01 +01009338 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009339 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009340 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009341
9342 while ((len-- > 0) && (*ptr++ != '/'));
9343 if (len <= 0)
9344 return 0;
9345
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009346 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009347 smp->data.u.str.str = ptr;
9348 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009349
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009350 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009351 return 1;
9352}
9353
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009354static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009355smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009356{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009357 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009358 char *ptr;
9359 int len;
9360
Willy Tarreauc0239e02012-04-16 14:42:55 +02009361 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009362
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009363 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009364 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9365 return 0;
9366
Willy Tarreau8797c062007-05-07 00:55:35 +02009367 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009368 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009369
9370 while ((len-- > 0) && (*ptr++ != '/'));
9371 if (len <= 0)
9372 return 0;
9373
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009374 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009375 smp->data.u.str.str = ptr;
9376 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009377
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009378 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009379 return 1;
9380}
9381
9382/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009383static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009384smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009385{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009386 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009387 char *ptr;
9388 int len;
9389
Willy Tarreauc0239e02012-04-16 14:42:55 +02009390 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009391
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009392 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +01009393 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9394 return 0;
9395
Willy Tarreau8797c062007-05-07 00:55:35 +02009396 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009397 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009398
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009399 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009400 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009401 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009402 return 1;
9403}
9404
Thierry Fournierf4011dd2016-03-29 17:23:51 +02009405static int
9406smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
9407{
9408 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
9409 return 0;
9410
9411 if (!smp->strm->unique_id) {
9412 if ((smp->strm->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
9413 return 0;
9414 smp->strm->unique_id[0] = '\0';
9415 }
9416 smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id,
9417 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
9418
9419 smp->data.type = SMP_T_STR;
9420 smp->data.u.str.str = smp->strm->unique_id;
9421 smp->flags = SMP_F_CONST;
9422 return 1;
9423}
9424
Thierry FOURNIERd7d88812017-04-19 15:15:14 +02009425/* Returns a string block containing all headers including the
9426 * empty line wich separes headers from the body. This is useful
9427 * form some headers analysis.
9428 */
9429static int
9430smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
9431{
9432 struct http_msg *msg;
9433 struct hdr_idx *idx;
9434 struct http_txn *txn;
9435
9436 CHECK_HTTP_MESSAGE_FIRST();
9437
9438 txn = smp->strm->txn;
9439 idx = &txn->hdr_idx;
9440 msg = &txn->req;
9441
9442 smp->data.type = SMP_T_STR;
9443 smp->data.u.str.str = msg->chn->buf->p + hdr_idx_first_pos(idx);
9444 smp->data.u.str.len = msg->eoh - hdr_idx_first_pos(idx) + 1 +
9445 (msg->chn->buf->p[msg->eoh] == '\r');
9446
9447 return 1;
9448}
9449
Thierry FOURNIER5617dce2017-04-09 05:38:19 +02009450/* Returns the header request in a length/value encoded format.
9451 * This is useful for exchanges with the SPOE.
9452 *
9453 * A "length value" is a multibyte code encoding numbers. It uses the
9454 * SPOE format. The encoding is the following:
9455 *
9456 * Each couple "header name" / "header value" is composed
9457 * like this:
9458 * "length value" "header name bytes"
9459 * "length value" "header value bytes"
9460 * When the last header is reached, the header name and the header
9461 * value are empty. Their length are 0
9462 */
9463static int
9464smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
9465{
9466 struct http_msg *msg;
9467 struct chunk *temp;
9468 struct hdr_idx *idx;
9469 const char *cur_ptr, *cur_next, *p;
9470 int old_idx, cur_idx;
9471 struct hdr_idx_elem *cur_hdr;
9472 const char *hn, *hv;
9473 int hnl, hvl;
9474 int ret;
9475 struct http_txn *txn;
9476 char *buf;
9477 char *end;
9478
9479 CHECK_HTTP_MESSAGE_FIRST();
9480
9481 temp = get_trash_chunk();
9482 buf = temp->str;
9483 end = temp->str + temp->size;
9484
9485 txn = smp->strm->txn;
9486 idx = &txn->hdr_idx;
9487 msg = &txn->req;
9488
9489 /* Build array of headers. */
9490 old_idx = 0;
9491 cur_next = msg->chn->buf->p + hdr_idx_first_pos(idx);
9492 while (1) {
9493 cur_idx = idx->v[old_idx].next;
9494 if (!cur_idx)
9495 break;
9496 old_idx = cur_idx;
9497
9498 cur_hdr = &idx->v[cur_idx];
9499 cur_ptr = cur_next;
9500 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
9501
9502 /* Now we have one full header at cur_ptr of len cur_hdr->len,
9503 * and the next header starts at cur_next. We'll check
9504 * this header in the list as well as against the default
9505 * rule.
9506 */
9507
9508 /* look for ': *'. */
9509 hn = cur_ptr;
9510 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
9511 if (p >= cur_ptr+cur_hdr->len)
9512 continue;
9513 hnl = p - hn;
9514 p++;
9515 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
9516 p++;
9517 if (p >= cur_ptr + cur_hdr->len)
9518 continue;
9519 hv = p;
9520 hvl = cur_ptr + cur_hdr->len-p;
9521
9522 /* encode the header name. */
9523 ret = encode_varint(hnl, &buf, end);
9524 if (ret == -1)
9525 return 0;
9526 if (buf + hnl > end)
9527 return 0;
9528 memcpy(buf, hn, hnl);
9529 buf += hnl;
9530
9531 /* encode and copy the value. */
9532 ret = encode_varint(hvl, &buf, end);
9533 if (ret == -1)
9534 return 0;
9535 if (buf + hvl > end)
9536 return 0;
9537 memcpy(buf, hv, hvl);
9538 buf += hvl;
9539 }
9540
9541 /* encode the end of the header list with empty
9542 * header name and header value.
9543 */
9544 ret = encode_varint(0, &buf, end);
9545 if (ret == -1)
9546 return 0;
9547 ret = encode_varint(0, &buf, end);
9548 if (ret == -1)
9549 return 0;
9550
9551 /* Initialise sample data which will be filled. */
9552 smp->data.type = SMP_T_BIN;
9553 smp->data.u.str.str = temp->str;
9554 smp->data.u.str.len = buf - temp->str;
9555 smp->data.u.str.size = temp->size;
9556
9557 return 1;
9558}
9559
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009560/* returns the longest available part of the body. This requires that the body
9561 * has been waited for using http-buffer-request.
9562 */
9563static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009564smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009565{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009566 struct http_msg *msg;
9567 unsigned long len;
9568 unsigned long block1;
9569 char *body;
9570 struct chunk *temp;
9571
9572 CHECK_HTTP_MESSAGE_FIRST();
9573
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009574 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009575 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009576 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009577 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009578
9579 len = http_body_bytes(msg);
9580 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
9581
9582 block1 = len;
9583 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
9584 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
9585
9586 if (block1 == len) {
9587 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009588 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009589 smp->data.u.str.str = body;
9590 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009591 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
9592 }
9593 else {
9594 /* buffer is wrapped, we need to defragment it */
9595 temp = get_trash_chunk();
9596 memcpy(temp->str, body, block1);
9597 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009598 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009599 smp->data.u.str.str = temp->str;
9600 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009601 smp->flags = SMP_F_VOL_TEST;
9602 }
9603 return 1;
9604}
9605
9606
9607/* returns the available length of the body. This requires that the body
9608 * has been waited for using http-buffer-request.
9609 */
9610static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009611smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009612{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009613 struct http_msg *msg;
9614
9615 CHECK_HTTP_MESSAGE_FIRST();
9616
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009617 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009618 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009619 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009620 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009621
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009622 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009623 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009624
9625 smp->flags = SMP_F_VOL_TEST;
9626 return 1;
9627}
9628
9629
9630/* returns the advertised length of the body, or the advertised size of the
9631 * chunks available in the buffer. This requires that the body has been waited
9632 * for using http-buffer-request.
9633 */
9634static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009635smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009636{
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009637 struct http_msg *msg;
9638
9639 CHECK_HTTP_MESSAGE_FIRST();
9640
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009641 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +01009642 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009643 else
Willy Tarreaube508f12016-03-10 11:47:01 +01009644 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009645
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009646 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009647 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +02009648
9649 smp->flags = SMP_F_VOL_TEST;
9650 return 1;
9651}
9652
9653
Willy Tarreau8797c062007-05-07 00:55:35 +02009654/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009655static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009656smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +02009657{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009658 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +02009659
Willy Tarreauc0239e02012-04-16 14:42:55 +02009660 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009661
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009662 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009663 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009664 smp->data.u.str.len = txn->req.sl.rq.u_l;
9665 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009666 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009667 return 1;
9668}
9669
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009670static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009671smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009672{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009673 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009674 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009675
Willy Tarreauc0239e02012-04-16 14:42:55 +02009676 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009677
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009678 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009679 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 +02009680 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009681 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009682
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009683 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009684 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009685 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009686 return 1;
9687}
9688
9689static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009690smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009691{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009692 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009693 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009694
Willy Tarreauc0239e02012-04-16 14:42:55 +02009695 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009696
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009697 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009698 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 +02009699 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
9700 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009701
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009702 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009703 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009704 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009705 return 1;
9706}
9707
Willy Tarreau185b5c42012-04-26 15:11:51 +02009708/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9709 * Accepts an optional argument of type string containing the header field name,
9710 * and an optional argument of type signed or unsigned integer to request an
9711 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009712 * headers are considered from the first one. It does not stop on commas and
9713 * returns full lines instead (useful for User-Agent or Date for example).
9714 */
9715static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009716smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009717{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009718 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009719 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +02009720 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009721 int occ = 0;
9722 const char *name_str = NULL;
9723 int name_len = 0;
9724
9725 if (!ctx) {
9726 /* first call */
9727 ctx = &static_hdr_ctx;
9728 ctx->idx = 0;
9729 smp->ctx.a[0] = ctx;
9730 }
9731
9732 if (args) {
9733 if (args[0].type != ARGT_STR)
9734 return 0;
9735 name_str = args[0].data.str.str;
9736 name_len = args[0].data.str.len;
9737
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02009738 if (args[1].type == ARGT_SINT)
9739 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009740 }
9741
9742 CHECK_HTTP_MESSAGE_FIRST();
9743
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009744 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009745 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 +02009746
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009747 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9748 /* search for header from the beginning */
9749 ctx->idx = 0;
9750
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009751 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009752 /* no explicit occurrence and single fetch => last header by default */
9753 occ = -1;
9754
9755 if (!occ)
9756 /* prepare to report multiple occurrences for ACL fetches */
9757 smp->flags |= SMP_F_NOT_LAST;
9758
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009759 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009760 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009761 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 +02009762 return 1;
9763
9764 smp->flags &= ~SMP_F_NOT_LAST;
9765 return 0;
9766}
9767
9768/* 6. Check on HTTP header count. The number of occurrences is returned.
9769 * Accepts exactly 1 argument of type string. It does not stop on commas and
9770 * returns full lines instead (useful for User-Agent or Date for example).
9771 */
9772static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009773smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009774{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009775 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009776 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +02009777 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009778 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +02009779 const char *name = NULL;
9780 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009781
Willy Tarreau601a4d12015-04-01 19:16:09 +02009782 if (args && args->type == ARGT_STR) {
9783 name = args->data.str.str;
9784 len = args->data.str.len;
9785 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009786
9787 CHECK_HTTP_MESSAGE_FIRST();
9788
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009789 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009790 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 +02009791
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009792 ctx.idx = 0;
9793 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +02009794 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009795 cnt++;
9796
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009797 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009798 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009799 smp->flags = SMP_F_VOL_HDR;
9800 return 1;
9801}
9802
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009803static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009804smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009805{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009806 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009807 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +02009808 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009809 struct chunk *temp;
9810 char del = ',';
9811
9812 if (args && args->type == ARGT_STR)
9813 del = *args[0].data.str.str;
9814
9815 CHECK_HTTP_MESSAGE_FIRST();
9816
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009817 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009818 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 +02009819
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009820 temp = get_trash_chunk();
9821
9822 ctx.idx = 0;
9823 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
9824 if (temp->len)
9825 temp->str[temp->len++] = del;
9826 memcpy(temp->str + temp->len, ctx.line, ctx.del);
9827 temp->len += ctx.del;
9828 }
9829
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009830 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009831 smp->data.u.str.str = temp->str;
9832 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +01009833 smp->flags = SMP_F_VOL_HDR;
9834 return 1;
9835}
9836
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009837/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9838 * Accepts an optional argument of type string containing the header field name,
9839 * and an optional argument of type signed or unsigned integer to request an
9840 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009841 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009842 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009843static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009844smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009845{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009846 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009847 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +02009848 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009849 int occ = 0;
9850 const char *name_str = NULL;
9851 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009852
Willy Tarreaua890d072013-04-02 12:01:06 +02009853 if (!ctx) {
9854 /* first call */
9855 ctx = &static_hdr_ctx;
9856 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02009857 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009858 }
9859
Willy Tarreau185b5c42012-04-26 15:11:51 +02009860 if (args) {
9861 if (args[0].type != ARGT_STR)
9862 return 0;
9863 name_str = args[0].data.str.str;
9864 name_len = args[0].data.str.len;
9865
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02009866 if (args[1].type == ARGT_SINT)
9867 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009868 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009869
Willy Tarreaue333ec92012-04-16 16:26:40 +02009870 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02009871
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009872 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009873 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 +02009874
Willy Tarreau185b5c42012-04-26 15:11:51 +02009875 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009876 /* search for header from the beginning */
9877 ctx->idx = 0;
9878
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009879 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +02009880 /* no explicit occurrence and single fetch => last header by default */
9881 occ = -1;
9882
9883 if (!occ)
9884 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02009885 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01009886
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009887 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009888 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009889 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 +02009890 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009891
Willy Tarreau37406352012-04-23 16:16:37 +02009892 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009893 return 0;
9894}
9895
Willy Tarreauc11416f2007-06-17 16:58:38 +02009896/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02009897 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009898 */
9899static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009900smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009901{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009902 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009903 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +02009904 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009905 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +02009906 const char *name = NULL;
9907 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +02009908
Willy Tarreau601a4d12015-04-01 19:16:09 +02009909 if (args && args->type == ARGT_STR) {
9910 name = args->data.str.str;
9911 len = args->data.str.len;
9912 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009913
Willy Tarreaue333ec92012-04-16 16:26:40 +02009914 CHECK_HTTP_MESSAGE_FIRST();
9915
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009916 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009917 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 +02009918
Willy Tarreau33a7e692007-06-10 19:45:56 +02009919 ctx.idx = 0;
9920 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +02009921 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009922 cnt++;
9923
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009924 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009925 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009926 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009927 return 1;
9928}
9929
Willy Tarreau185b5c42012-04-26 15:11:51 +02009930/* Fetch an HTTP header's integer value. The integer value is returned. It
9931 * takes a mandatory argument of type string and an optional one of type int
9932 * to designate a specific occurrence. It returns an unsigned integer, which
9933 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02009934 */
9935static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009936smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009937{
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009938 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +02009939
Willy Tarreauf853c462012-04-23 18:53:56 +02009940 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009941 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009942 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +02009943 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02009944
Willy Tarreaud53e2422012-04-16 17:21:11 +02009945 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009946}
9947
Cyril Bonté69fa9922012-10-25 00:01:06 +02009948/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
9949 * and an optional one of type int to designate a specific occurrence.
9950 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02009951 */
9952static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009953smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +02009954{
Willy Tarreaud53e2422012-04-16 17:21:11 +02009955 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009956
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009957 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009958 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009959 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02009960 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02009961 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +01009962 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009963 if (smp->data.u.str.len < temp->size - 1) {
9964 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
9965 temp->str[smp->data.u.str.len] = '\0';
9966 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009967 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +02009968 break;
9969 }
9970 }
9971 }
9972
Willy Tarreaud53e2422012-04-16 17:21:11 +02009973 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02009974 if (!(smp->flags & SMP_F_NOT_LAST))
9975 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02009976 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02009977 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02009978}
9979
Willy Tarreau737b0c12007-06-10 21:28:46 +02009980/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
9981 * the first '/' after the possible hostname, and ends before the possible '?'.
9982 */
9983static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009984smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009985{
Willy Tarreau15e91e12015-04-04 00:52:09 +02009986 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009987 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009988
Willy Tarreauc0239e02012-04-16 14:42:55 +02009989 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009990
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009991 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009992 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01009993 ptr = http_get_path(txn);
9994 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009995 return 0;
9996
9997 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009998 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02009999 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010000
10001 while (ptr < end && *ptr != '?')
10002 ptr++;
10003
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010004 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010005 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010006 return 1;
10007}
10008
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010009/* This produces a concatenation of the first occurrence of the Host header
10010 * followed by the path component if it begins with a slash ('/'). This means
10011 * that '*' will not be added, resulting in exactly the first Host entry.
10012 * If no Host header is found, then the path is returned as-is. The returned
10013 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010014 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010015 */
10016static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010017smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010018{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010019 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010020 char *ptr, *end, *beg;
10021 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010022 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010023
10024 CHECK_HTTP_MESSAGE_FIRST();
10025
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010026 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010027 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010028 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010029 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010030
10031 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010032 temp = get_trash_chunk();
10033 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010034 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010035 smp->data.u.str.str = temp->str;
10036 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010037
10038 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010039 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010040 beg = http_get_path(txn);
10041 if (!beg)
10042 beg = end;
10043
10044 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10045
10046 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010047 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10048 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010049 }
10050
10051 smp->flags = SMP_F_VOL_1ST;
10052 return 1;
10053}
10054
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010055/* This produces a 32-bit hash of the concatenation of the first occurrence of
10056 * the Host header followed by the path component if it begins with a slash ('/').
10057 * This means that '*' will not be added, resulting in exactly the first Host
10058 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010059 * is hashed using the path hash followed by a full avalanche hash and provides a
10060 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010061 * high-traffic sites without having to store whole paths.
10062 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010063int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010064smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010065{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010066 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010067 struct hdr_ctx ctx;
10068 unsigned int hash = 0;
10069 char *ptr, *beg, *end;
10070 int len;
10071
10072 CHECK_HTTP_MESSAGE_FIRST();
10073
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010074 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010075 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010076 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010077 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10078 ptr = ctx.line + ctx.val;
10079 len = ctx.vlen;
10080 while (len--)
10081 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10082 }
10083
10084 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010085 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010086 beg = http_get_path(txn);
10087 if (!beg)
10088 beg = end;
10089
10090 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10091
10092 if (beg < ptr && *beg == '/') {
10093 while (beg < ptr)
10094 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10095 }
10096 hash = full_hash(hash);
10097
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010098 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010099 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010100 smp->flags = SMP_F_VOL_1ST;
10101 return 1;
10102}
10103
Willy Tarreau4a550602012-12-09 14:53:32 +010010104/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010105 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10106 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10107 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010108 * that in environments where IPv6 is insignificant, truncating the output to
10109 * 8 bytes would still work.
10110 */
10111static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010112smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010113{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010114 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010115 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010116
10117 if (!cli_conn)
10118 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010119
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010120 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010121 return 0;
10122
Willy Tarreau47ca5452012-12-23 20:22:19 +010010123 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010124 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010125 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010126
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010127 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010128 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010129 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010130 temp->len += 4;
10131 break;
10132 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010133 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010134 temp->len += 16;
10135 break;
10136 default:
10137 return 0;
10138 }
10139
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010140 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010141 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010142 return 1;
10143}
10144
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010145/* Extracts the query string, which comes after the question mark '?'. If no
10146 * question mark is found, nothing is returned. Otherwise it returns a sample
10147 * of type string carrying the whole query string.
10148 */
10149static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010150smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010151{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010152 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010153 char *ptr, *end;
10154
10155 CHECK_HTTP_MESSAGE_FIRST();
10156
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010157 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010158 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10159 end = ptr + txn->req.sl.rq.u_l;
10160
10161 /* look up the '?' */
10162 do {
10163 if (ptr == end)
10164 return 0;
10165 } while (*ptr++ != '?');
10166
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010167 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010168 smp->data.u.str.str = ptr;
10169 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010170 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10171 return 1;
10172}
10173
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010174static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010175smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010176{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010177 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10178 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10179 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010180
Willy Tarreau24e32d82012-04-23 23:55:44 +020010181 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010182
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010183 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010184 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010185 return 1;
10186}
10187
Willy Tarreau7f18e522010-10-22 20:04:13 +020010188/* return a valid test if the current request is the first one on the connection */
10189static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010190smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010191{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010192 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010193 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010194 return 1;
10195}
10196
Willy Tarreau34db1082012-04-19 17:16:54 +020010197/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010198static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010199smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010200{
10201
Willy Tarreau24e32d82012-04-23 23:55:44 +020010202 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010203 return 0;
10204
Willy Tarreauc0239e02012-04-16 14:42:55 +020010205 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010206
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010207 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010208 return 0;
10209
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010210 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010211 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010212 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010213 return 1;
10214}
Willy Tarreau8797c062007-05-07 00:55:35 +020010215
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010216/* Accepts exactly 1 argument of type userlist */
10217static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010218smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010219{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010220 if (!args || args->type != ARGT_USR)
10221 return 0;
10222
10223 CHECK_HTTP_MESSAGE_FIRST();
10224
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010225 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010226 return 0;
10227
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010228 /* if the user does not belong to the userlist or has a wrong password,
10229 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010230 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010231 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010232 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
10233 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010234 return 0;
10235
10236 /* pat_match_auth() will need the user list */
10237 smp->ctx.a[0] = args->data.usr;
10238
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010239 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010240 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010241 smp->data.u.str.str = smp->strm->txn->auth.user;
10242 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010243
10244 return 1;
10245}
10246
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010247/* Try to find the next occurrence of a cookie name in a cookie header value.
10248 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10249 * the cookie value is returned into *value and *value_l, and the function
10250 * returns a pointer to the next pointer to search from if the value was found.
10251 * Otherwise if the cookie was not found, NULL is returned and neither value
10252 * nor value_l are touched. The input <hdr> string should first point to the
10253 * header's value, and the <hdr_end> pointer must point to the first character
10254 * not part of the value. <list> must be non-zero if value may represent a list
10255 * of values (cookie headers). This makes it faster to abort parsing when no
10256 * list is expected.
10257 */
David Carlier4686f792015-09-25 14:10:50 +010010258char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010259extract_cookie_value(char *hdr, const char *hdr_end,
10260 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010261 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010262{
10263 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10264 char *next;
10265
10266 /* we search at least a cookie name followed by an equal, and more
10267 * generally something like this :
10268 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10269 */
10270 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10271 /* Iterate through all cookies on this line */
10272
Willy Tarreau2235b262016-11-05 15:50:20 +010010273 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010274 att_beg++;
10275
10276 /* find att_end : this is the first character after the last non
10277 * space before the equal. It may be equal to hdr_end.
10278 */
10279 equal = att_end = att_beg;
10280
10281 while (equal < hdr_end) {
10282 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10283 break;
Willy Tarreau2235b262016-11-05 15:50:20 +010010284 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010285 continue;
10286 att_end = equal;
10287 }
10288
10289 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10290 * is between <att_beg> and <equal>, both may be identical.
10291 */
10292
10293 /* look for end of cookie if there is an equal sign */
10294 if (equal < hdr_end && *equal == '=') {
10295 /* look for the beginning of the value */
10296 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +010010297 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010298 val_beg++;
10299
10300 /* find the end of the value, respecting quotes */
10301 next = find_cookie_value_end(val_beg, hdr_end);
10302
10303 /* make val_end point to the first white space or delimitor after the value */
10304 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +010010305 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010306 val_end--;
10307 } else {
10308 val_beg = val_end = next = equal;
10309 }
10310
10311 /* We have nothing to do with attributes beginning with '$'. However,
10312 * they will automatically be removed if a header before them is removed,
10313 * since they're supposed to be linked together.
10314 */
10315 if (*att_beg == '$')
10316 continue;
10317
10318 /* Ignore cookies with no equal sign */
10319 if (equal == next)
10320 continue;
10321
10322 /* Now we have the cookie name between att_beg and att_end, and
10323 * its value between val_beg and val_end.
10324 */
10325
10326 if (att_end - att_beg == cookie_name_l &&
10327 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10328 /* let's return this value and indicate where to go on from */
10329 *value = val_beg;
10330 *value_l = val_end - val_beg;
10331 return next + 1;
10332 }
10333
10334 /* Set-Cookie headers only have the name in the first attr=value part */
10335 if (!list)
10336 break;
10337 }
10338
10339 return NULL;
10340}
10341
William Lallemanda43ba4e2014-01-28 18:14:25 +010010342/* Fetch a captured HTTP request header. The index is the position of
10343 * the "capture" option in the configuration file
10344 */
10345static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010346smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010347{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010348 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010349 int idx;
10350
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010351 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010352 return 0;
10353
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010354 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010355
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010356 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 +010010357 return 0;
10358
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010359 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010360 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010361 smp->data.u.str.str = smp->strm->req_cap[idx];
10362 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010363
10364 return 1;
10365}
10366
10367/* Fetch a captured HTTP response header. The index is the position of
10368 * the "capture" option in the configuration file
10369 */
10370static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010371smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010372{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010373 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010374 int idx;
10375
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010376 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010010377 return 0;
10378
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010379 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010380
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010381 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 +010010382 return 0;
10383
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010384 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010385 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010386 smp->data.u.str.str = smp->strm->res_cap[idx];
10387 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010010388
10389 return 1;
10390}
10391
William Lallemand65ad6e12014-01-31 15:08:02 +010010392/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10393static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010394smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010395{
10396 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010397 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010010398 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010399
Willy Tarreau15e91e12015-04-04 00:52:09 +020010400 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010401 return 0;
10402
William Lallemand96a77852014-02-05 00:30:02 +010010403 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010404
William Lallemand96a77852014-02-05 00:30:02 +010010405 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10406 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010407
William Lallemand96a77852014-02-05 00:30:02 +010010408 temp = get_trash_chunk();
10409 temp->str = txn->uri;
10410 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010411 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010412 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010413 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010414
10415 return 1;
10416
10417}
10418
10419/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10420static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010421smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010010422{
10423 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010424 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010010425 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010426
Willy Tarreau15e91e12015-04-04 00:52:09 +020010427 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010010428 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010429
William Lallemand65ad6e12014-01-31 15:08:02 +010010430 ptr = txn->uri;
10431
10432 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10433 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010434
William Lallemand65ad6e12014-01-31 15:08:02 +010010435 if (!*ptr)
10436 return 0;
10437
10438 ptr++; /* skip the space */
10439
10440 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010441 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010442 if (!ptr)
10443 return 0;
10444 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10445 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010446
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010447 smp->data.u.str = *temp;
10448 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010449 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010450 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010451
10452 return 1;
10453}
10454
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010455/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10456 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10457 */
10458static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010459smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010460{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010461 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010462
Willy Tarreau15e91e12015-04-04 00:52:09 +020010463 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010464 return 0;
10465
10466 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010467 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010468 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010469 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010470
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010471 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010472 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010473 smp->flags = SMP_F_CONST;
10474 return 1;
10475
10476}
10477
10478/* Retrieves the HTTP version from the response (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_res_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->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010487 return 0;
10488
10489 if (txn->rsp.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
William Lallemand65ad6e12014-01-31 15:08:02 +010010501
Willy Tarreaue333ec92012-04-16 16:26:40 +020010502/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010503 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010504 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010505 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010506 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010507 * Accepts exactly 1 argument of type string. If the input options indicate
10508 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010509 * The returned sample is of type CSTR. Can be used to parse cookies in other
10510 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010511 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010512int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010513{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010514 struct http_txn *txn;
10515 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010516 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010517 const struct http_msg *msg;
10518 const char *hdr_name;
10519 int hdr_name_len;
10520 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010521 int occ = 0;
10522 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010523
Willy Tarreau24e32d82012-04-23 23:55:44 +020010524 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010525 return 0;
10526
Willy Tarreaua890d072013-04-02 12:01:06 +020010527 if (!ctx) {
10528 /* first call */
10529 ctx = &static_hdr_ctx;
10530 ctx->idx = 0;
10531 smp->ctx.a[2] = ctx;
10532 }
10533
Willy Tarreaue333ec92012-04-16 16:26:40 +020010534 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010535
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010536 txn = smp->strm->txn;
10537 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010538
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010539 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010540 msg = &txn->req;
10541 hdr_name = "Cookie";
10542 hdr_name_len = 6;
10543 } else {
10544 msg = &txn->rsp;
10545 hdr_name = "Set-Cookie";
10546 hdr_name_len = 10;
10547 }
10548
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010549 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020010550 /* no explicit occurrence and single fetch => last cookie by default */
10551 occ = -1;
10552
10553 /* OK so basically here, either we want only one value and it's the
10554 * last one, or we want to iterate over all of them and we fetch the
10555 * next one.
10556 */
10557
Willy Tarreau9b28e032012-10-12 23:49:43 +020010558 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010559 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010560 /* search for the header from the beginning, we must first initialize
10561 * the search parameters.
10562 */
Willy Tarreau37406352012-04-23 16:16:37 +020010563 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010564 ctx->idx = 0;
10565 }
10566
Willy Tarreau28376d62012-04-26 21:26:10 +020010567 smp->flags |= SMP_F_VOL_HDR;
10568
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010569 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010570 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10571 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010572 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10573 goto out;
10574
Willy Tarreau24e32d82012-04-23 23:55:44 +020010575 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010576 continue;
10577
Willy Tarreau37406352012-04-23 16:16:37 +020010578 smp->ctx.a[0] = ctx->line + ctx->val;
10579 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010580 }
10581
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010582 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010583 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010584 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010585 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010586 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010587 &smp->data.u.str.str,
10588 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010589 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010590 found = 1;
10591 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010592 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020010593 smp->flags |= SMP_F_NOT_LAST;
10594 return 1;
10595 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010596 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010597 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010598 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010599 /* all cookie headers and values were scanned. If we're looking for the
10600 * last occurrence, we may return it now.
10601 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010602 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010603 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010604 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010605}
10606
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010607/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010608 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010609 * multiple cookies may be parsed on the same line. The returned sample is of
10610 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010611 */
10612static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010613smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010614{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010615 struct http_txn *txn;
10616 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010617 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010618 const struct http_msg *msg;
10619 const char *hdr_name;
10620 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010621 int cnt;
10622 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010623 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010624
Willy Tarreau24e32d82012-04-23 23:55:44 +020010625 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010626 return 0;
10627
Willy Tarreaue333ec92012-04-16 16:26:40 +020010628 CHECK_HTTP_MESSAGE_FIRST();
10629
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010630 txn = smp->strm->txn;
10631 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010632
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010633 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010634 msg = &txn->req;
10635 hdr_name = "Cookie";
10636 hdr_name_len = 6;
10637 } else {
10638 msg = &txn->rsp;
10639 hdr_name = "Set-Cookie";
10640 hdr_name_len = 10;
10641 }
10642
Willy Tarreau9b28e032012-10-12 23:49:43 +020010643 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010644 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010645 ctx.idx = 0;
10646 cnt = 0;
10647
10648 while (1) {
10649 /* Note: val_beg == NULL every time we need to fetch a new header */
10650 if (!val_beg) {
10651 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10652 break;
10653
Willy Tarreau24e32d82012-04-23 23:55:44 +020010654 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010655 continue;
10656
10657 val_beg = ctx.line + ctx.val;
10658 val_end = val_beg + ctx.vlen;
10659 }
10660
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010661 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010662 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010663 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010664 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010665 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010666 &smp->data.u.str.str,
10667 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010668 cnt++;
10669 }
10670 }
10671
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010672 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010673 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010674 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010675 return 1;
10676}
10677
Willy Tarreau51539362012-05-08 12:46:28 +020010678/* Fetch an cookie's integer value. The integer value is returned. It
10679 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10680 */
10681static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010682smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020010683{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010684 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020010685
10686 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010687 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010688 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020010689 }
10690
10691 return ret;
10692}
10693
Willy Tarreau8797c062007-05-07 00:55:35 +020010694/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010695/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010696/************************************************************************/
10697
David Cournapeau16023ee2010-12-23 20:55:41 +090010698/*
10699 * Given a path string and its length, find the position of beginning of the
10700 * query string. Returns NULL if no query string is found in the path.
10701 *
10702 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
10703 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010704 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090010705 */
bedis4c75cca2012-10-05 08:38:24 +020010706static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010707{
10708 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020010709
bedis4c75cca2012-10-05 08:38:24 +020010710 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090010711 return p ? p + 1 : NULL;
10712}
10713
bedis4c75cca2012-10-05 08:38:24 +020010714static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010715{
bedis4c75cca2012-10-05 08:38:24 +020010716 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090010717}
10718
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010719/* after increasing a pointer value, it can exceed the first buffer
10720 * size. This function transform the value of <ptr> according with
10721 * the expected position. <chunks> is an array of the one or two
10722 * avalaible chunks. The first value is the start of the first chunk,
10723 * the second value if the end+1 of the first chunks. The third value
10724 * is NULL or the start of the second chunk and the fourth value is
10725 * the end+1 of the second chunk. The function returns 1 if does a
10726 * wrap, else returns 0.
10727 */
10728static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
10729{
10730 if (*ptr < chunks[1])
10731 return 0;
10732 if (!chunks[2])
10733 return 0;
10734 *ptr = chunks[2] + ( *ptr - chunks[1] );
10735 return 1;
10736}
10737
David Cournapeau16023ee2010-12-23 20:55:41 +090010738/*
10739 * Given a url parameter, find the starting position of the first occurence,
10740 * or NULL if the parameter is not found.
10741 *
10742 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
10743 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010744 *
Willy Tarreauf6625822015-12-27 14:51:01 +010010745 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010746 * or the second chunk. The caller must be check the position before using the
10747 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090010748 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010749static const char *
10750find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010751 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020010752 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010753{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010754 const char *pos, *last, *equal;
10755 const char **bufs = chunks;
10756 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090010757
David Cournapeau16023ee2010-12-23 20:55:41 +090010758
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010759 pos = bufs[0];
10760 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010010761 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010762 /* Check the equal. */
10763 equal = pos + url_param_name_l;
10764 if (fix_pointer_if_wrap(chunks, &equal)) {
10765 if (equal >= chunks[3])
10766 return NULL;
10767 } else {
10768 if (equal >= chunks[1])
10769 return NULL;
10770 }
10771 if (*equal == '=') {
10772 if (pos + url_param_name_l > last) {
10773 /* process wrap case, we detect a wrap. In this case, the
10774 * comparison is performed in two parts.
10775 */
10776
10777 /* This is the end, we dont have any other chunk. */
10778 if (bufs != chunks || !bufs[2])
10779 return NULL;
10780
10781 /* Compute the length of each part of the comparison. */
10782 l1 = last - pos;
10783 l2 = url_param_name_l - l1;
10784
10785 /* The second buffer is too short to contain the compared string. */
10786 if (bufs[2] + l2 > bufs[3])
10787 return NULL;
10788
10789 if (memcmp(pos, url_param_name, l1) == 0 &&
10790 memcmp(bufs[2], url_param_name+l1, l2) == 0)
10791 return pos;
10792
10793 /* Perform wrapping and jump the string who fail the comparison. */
10794 bufs += 2;
10795 pos = bufs[0] + l2;
10796 last = bufs[1];
10797
10798 } else {
10799 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010010800 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
10801 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010802 pos += url_param_name_l + 1;
10803 if (fix_pointer_if_wrap(chunks, &pos))
10804 last = bufs[2];
10805 }
10806 }
10807
10808 while (1) {
10809 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010010810 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010811 pos++;
10812 if (pos < last)
10813 break;
10814 /* process buffer wrapping. */
10815 if (bufs != chunks || !bufs[2])
10816 return NULL;
10817 bufs += 2;
10818 pos = bufs[0];
10819 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090010820 }
David Cournapeau16023ee2010-12-23 20:55:41 +090010821 pos++;
10822 }
10823 return NULL;
10824}
10825
10826/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010010827 * Given a url parameter name and a query string, find the next value.
10828 * An empty url_param_name matches the first available parameter.
10829 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
10830 * respectively provide a pointer to the value and its end.
10831 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090010832 */
10833static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010834find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010835 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010836 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010837{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010838 const char *arg_start, *qs_end;
10839 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090010840
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010841 arg_start = chunks[0];
10842 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010843 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010844 /* Looks for an argument name. */
10845 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010846 url_param_name, url_param_name_l,
10847 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010848 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010010849 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010850 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010851 }
David Cournapeau16023ee2010-12-23 20:55:41 +090010852 if (!arg_start)
10853 return 0;
10854
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010855 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010856 while (1) {
10857 /* looks for the first argument. */
10858 value_start = memchr(arg_start, '=', qs_end - arg_start);
10859 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010860 /* Check for wrapping. */
10861 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010010862 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010863 chunks[2]) {
10864 arg_start = chunks[2];
10865 qs_end = chunks[3];
10866 continue;
10867 }
10868 return 0;
10869 }
10870 break;
10871 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010872 value_start++;
10873 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010874 else {
10875 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010876 value_start = arg_start + url_param_name_l + 1;
10877
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010878 /* Check for pointer wrapping. */
10879 if (fix_pointer_if_wrap(chunks, &value_start)) {
10880 /* Update the end pointer. */
10881 qs_end = chunks[3];
10882
10883 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010010884 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010885 return 0;
10886 }
10887 }
10888
David Cournapeau16023ee2010-12-23 20:55:41 +090010889 value_end = value_start;
10890
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010891 while (1) {
10892 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
10893 value_end++;
10894 if (value_end < qs_end)
10895 break;
10896 /* process buffer wrapping. */
10897 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010010898 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010899 chunks[2]) {
10900 value_end = chunks[2];
10901 qs_end = chunks[3];
10902 continue;
10903 }
10904 break;
10905 }
David Cournapeau16023ee2010-12-23 20:55:41 +090010906
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010907 *vstart = value_start;
10908 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010010909 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090010910}
10911
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010912/* This scans a URL-encoded query string. It takes an optionally wrapping
10913 * string whose first contigous chunk has its beginning in ctx->a[0] and end
10914 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
10915 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010916 */
David Cournapeau16023ee2010-12-23 20:55:41 +090010917static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010918smp_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 +090010919{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010920 const char *vstart, *vend;
10921 struct chunk *temp;
10922 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020010923
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010924 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020010925 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010926 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020010927 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010928 return 0;
10929
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010930 /* Create sample. If the value is contiguous, return the pointer as CONST,
10931 * if the value is wrapped, copy-it in a buffer.
10932 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010933 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010934 if (chunks[2] &&
10935 vstart >= chunks[0] && vstart <= chunks[1] &&
10936 vend >= chunks[2] && vend <= chunks[3]) {
10937 /* Wrapped case. */
10938 temp = get_trash_chunk();
10939 memcpy(temp->str, vstart, chunks[1] - vstart);
10940 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010941 smp->data.u.str.str = temp->str;
10942 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010943 } else {
10944 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010945 smp->data.u.str.str = (char *)vstart;
10946 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010947 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10948 }
10949
10950 /* Update context, check wrapping. */
10951 chunks[0] = vend;
10952 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
10953 chunks[1] = chunks[3];
10954 chunks[2] = NULL;
10955 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010956
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010957 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020010958 smp->flags |= SMP_F_NOT_LAST;
10959
David Cournapeau16023ee2010-12-23 20:55:41 +090010960 return 1;
10961}
10962
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010963/* This function iterates over each parameter of the query string. It uses
10964 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020010965 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
10966 * An optional parameter name is passed in args[0], otherwise any parameter is
10967 * considered. It supports an optional delimiter argument for the beginning of
10968 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010969 */
10970static int
10971smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
10972{
10973 struct http_msg *msg;
10974 char delim = '?';
10975 const char *name;
10976 int name_len;
10977
Dragan Dosen26f77e52015-05-25 10:02:11 +020010978 if (!args ||
10979 (args[0].type && args[0].type != ARGT_STR) ||
10980 (args[1].type && args[1].type != ARGT_STR))
10981 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010982
Dragan Dosen26f77e52015-05-25 10:02:11 +020010983 name = "";
10984 name_len = 0;
10985 if (args->type == ARGT_STR) {
10986 name = args->data.str.str;
10987 name_len = args->data.str.len;
10988 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010989
Dragan Dosen26f77e52015-05-25 10:02:11 +020010990 if (args[1].type)
10991 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010992
Dragan Dosen26f77e52015-05-25 10:02:11 +020010993 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020010994 CHECK_HTTP_MESSAGE_FIRST();
10995
10996 msg = &smp->strm->txn->req;
10997
10998 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
10999 msg->sl.rq.u_l, delim);
11000 if (!smp->ctx.a[0])
11001 return 0;
11002
11003 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011004
11005 /* Assume that the context is filled with NULL pointer
11006 * before the first call.
11007 * smp->ctx.a[2] = NULL;
11008 * smp->ctx.a[3] = NULL;
11009 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011010 }
11011
11012 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11013}
11014
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011015/* This function iterates over each parameter of the body. This requires
11016 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011017 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11018 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11019 * optional second part if the body wraps at the end of the buffer. An optional
11020 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011021 */
11022static int
11023smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11024{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011025 struct http_msg *msg;
11026 unsigned long len;
11027 unsigned long block1;
11028 char *body;
11029 const char *name;
11030 int name_len;
11031
11032 if (!args || (args[0].type && args[0].type != ARGT_STR))
11033 return 0;
11034
11035 name = "";
11036 name_len = 0;
11037 if (args[0].type == ARGT_STR) {
11038 name = args[0].data.str.str;
11039 name_len = args[0].data.str.len;
11040 }
11041
11042 if (!smp->ctx.a[0]) { // first call, find the query string
11043 CHECK_HTTP_MESSAGE_FIRST();
11044
11045 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010011046 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011047 else
Willy Tarreaube508f12016-03-10 11:47:01 +010011048 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011049
11050 len = http_body_bytes(msg);
11051 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11052
11053 block1 = len;
11054 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11055 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11056
11057 if (block1 == len) {
11058 /* buffer is not wrapped (or empty) */
11059 smp->ctx.a[0] = body;
11060 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011061
11062 /* Assume that the context is filled with NULL pointer
11063 * before the first call.
11064 * smp->ctx.a[2] = NULL;
11065 * smp->ctx.a[3] = NULL;
11066 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011067 }
11068 else {
11069 /* buffer is wrapped, we need to defragment it */
11070 smp->ctx.a[0] = body;
11071 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011072 smp->ctx.a[2] = msg->chn->buf->data;
11073 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011074 }
11075 }
11076 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11077}
11078
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011079/* Return the signed integer value for the specified url parameter (see url_param
11080 * above).
11081 */
11082static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011083smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011084{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011085 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011086
11087 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011088 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011089 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011090 }
11091
11092 return ret;
11093}
11094
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011095/* This produces a 32-bit hash of the concatenation of the first occurrence of
11096 * the Host header followed by the path component if it begins with a slash ('/').
11097 * This means that '*' will not be added, resulting in exactly the first Host
11098 * entry. If no Host header is found, then the path is used. The resulting value
11099 * is hashed using the url hash followed by a full avalanche hash and provides a
11100 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11101 * high-traffic sites without having to store whole paths.
11102 * this differs from the base32 functions in that it includes the url parameters
11103 * as well as the path
11104 */
11105static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011106smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011107{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011108 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011109 struct hdr_ctx ctx;
11110 unsigned int hash = 0;
11111 char *ptr, *beg, *end;
11112 int len;
11113
11114 CHECK_HTTP_MESSAGE_FIRST();
11115
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011116 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011117 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011118 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011119 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11120 ptr = ctx.line + ctx.val;
11121 len = ctx.vlen;
11122 while (len--)
11123 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11124 }
11125
11126 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011127 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 +000011128 beg = http_get_path(txn);
11129 if (!beg)
11130 beg = end;
11131
11132 for (ptr = beg; ptr < end ; ptr++);
11133
11134 if (beg < ptr && *beg == '/') {
11135 while (beg < ptr)
11136 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11137 }
11138 hash = full_hash(hash);
11139
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011140 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011141 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011142 smp->flags = SMP_F_VOL_1ST;
11143 return 1;
11144}
11145
11146/* This concatenates the source address with the 32-bit hash of the Host and
11147 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11148 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11149 * on the source address length. The URL hash is stored before the address so
11150 * that in environments where IPv6 is insignificant, truncating the output to
11151 * 8 bytes would still work.
11152 */
11153static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011154smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011155{
11156 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011157 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011158
Dragan Dosendb5af612016-06-16 11:23:01 +020011159 if (!cli_conn)
11160 return 0;
11161
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011162 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011163 return 0;
11164
11165 temp = get_trash_chunk();
Dragan Dosene5f41332016-06-16 11:08:08 +020011166 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
11167 temp->len += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011168
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011169 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011170 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011171 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011172 temp->len += 4;
11173 break;
11174 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011175 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011176 temp->len += 16;
11177 break;
11178 default:
11179 return 0;
11180 }
11181
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011182 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011183 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011184 return 1;
11185}
11186
Willy Tarreau185b5c42012-04-26 15:11:51 +020011187/* This function is used to validate the arguments passed to any "hdr" fetch
11188 * keyword. These keywords support an optional positive or negative occurrence
11189 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11190 * is assumed that the types are already the correct ones. Returns 0 on error,
11191 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11192 * error message in case of error, that the caller is responsible for freeing.
11193 * The initial location must either be freeable or NULL.
11194 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010011195int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020011196{
11197 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011198 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011199 return 0;
11200 }
11201 return 1;
11202}
11203
Willy Tarreau276fae92013-07-25 14:36:01 +020011204/* takes an UINT value on input supposed to represent the time since EPOCH,
11205 * adds an optional offset found in args[0] and emits a string representing
11206 * the date in RFC-1123/5322 format.
11207 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011208static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020011209{
Cyril Bontéf78d8962016-01-22 19:40:28 +010011210 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020011211 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11212 struct chunk *temp;
11213 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011214 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011215 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020011216
11217 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011218 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020011219 curr_date += args[0].data.sint;
11220
11221 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020011222 if (!tm)
11223 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011224
11225 temp = get_trash_chunk();
11226 temp->len = snprintf(temp->str, temp->size - temp->len,
11227 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11228 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11229 tm->tm_hour, tm->tm_min, tm->tm_sec);
11230
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011231 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011232 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020011233 return 1;
11234}
11235
Thierry FOURNIERad903512014-04-11 17:51:01 +020011236/* Match language range with language tag. RFC2616 14.4:
11237 *
11238 * A language-range matches a language-tag if it exactly equals
11239 * the tag, or if it exactly equals a prefix of the tag such
11240 * that the first tag character following the prefix is "-".
11241 *
11242 * Return 1 if the strings match, else return 0.
11243 */
11244static inline int language_range_match(const char *range, int range_len,
11245 const char *tag, int tag_len)
11246{
11247 const char *end = range + range_len;
11248 const char *tend = tag + tag_len;
11249 while (range < end) {
11250 if (*range == '-' && tag == tend)
11251 return 1;
11252 if (*range != *tag || tag == tend)
11253 return 0;
11254 range++;
11255 tag++;
11256 }
11257 /* Return true only if the last char of the tag is matched. */
11258 return tag == tend;
11259}
11260
11261/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011262static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020011263{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011264 const char *al = smp->data.u.str.str;
11265 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011266 const char *token;
11267 int toklen;
11268 int qvalue;
11269 const char *str;
11270 const char *w;
11271 int best_q = 0;
11272
11273 /* Set the constant to the sample, because the output of the
11274 * function will be peek in the constant configuration string.
11275 */
11276 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011277 smp->data.u.str.size = 0;
11278 smp->data.u.str.str = "";
11279 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011280
11281 /* Parse the accept language */
11282 while (1) {
11283
11284 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011285 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011286 al++;
11287 if (al >= end)
11288 break;
11289
11290 /* Start of the fisrt word. */
11291 token = al;
11292
11293 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011294 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011295 al++;
11296 if (al == token)
11297 goto expect_comma;
11298
11299 /* Length of the token. */
11300 toklen = al - token;
11301 qvalue = 1000;
11302
11303 /* Check if the token exists in the list. If the token not exists,
11304 * jump to the next token.
11305 */
11306 str = args[0].data.str.str;
11307 w = str;
11308 while (1) {
11309 if (*str == ';' || *str == '\0') {
11310 if (language_range_match(token, toklen, w, str-w))
11311 goto look_for_q;
11312 if (*str == '\0')
11313 goto expect_comma;
11314 w = str + 1;
11315 }
11316 str++;
11317 }
11318 goto expect_comma;
11319
11320look_for_q:
11321
11322 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011323 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011324 al++;
11325 if (al >= end)
11326 goto process_value;
11327
11328 /* If ',' is found, process the result */
11329 if (*al == ',')
11330 goto process_value;
11331
11332 /* If the character is different from ';', look
11333 * for the end of the header part in best effort.
11334 */
11335 if (*al != ';')
11336 goto expect_comma;
11337
11338 /* Assumes that the char is ';', now expect "q=". */
11339 al++;
11340
11341 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011342 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011343 al++;
11344 if (al >= end)
11345 goto process_value;
11346
11347 /* Expect 'q'. If no 'q', continue in best effort */
11348 if (*al != 'q')
11349 goto process_value;
11350 al++;
11351
11352 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011353 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011354 al++;
11355 if (al >= end)
11356 goto process_value;
11357
11358 /* Expect '='. If no '=', continue in best effort */
11359 if (*al != '=')
11360 goto process_value;
11361 al++;
11362
11363 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011364 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011365 al++;
11366 if (al >= end)
11367 goto process_value;
11368
11369 /* Parse the q value. */
11370 qvalue = parse_qvalue(al, &al);
11371
11372process_value:
11373
11374 /* If the new q value is the best q value, then store the associated
11375 * language in the response. If qvalue is the biggest value (1000),
11376 * break the process.
11377 */
11378 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011379 smp->data.u.str.str = (char *)w;
11380 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011381 if (qvalue >= 1000)
11382 break;
11383 best_q = qvalue;
11384 }
11385
11386expect_comma:
11387
11388 /* Expect comma or end. If the end is detected, quit the loop. */
11389 while (al < end && *al != ',')
11390 al++;
11391 if (al >= end)
11392 break;
11393
11394 /* Comma is found, jump it and restart the analyzer. */
11395 al++;
11396 }
11397
11398 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011399 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
11400 smp->data.u.str.str = args[1].data.str.str;
11401 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011402 }
11403
11404 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011405 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020011406}
11407
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011408/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011409static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011410{
11411 /* If the constant flag is set or if not size is avalaible at
11412 * the end of the buffer, copy the string in other buffer
11413 * before decoding.
11414 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011415 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011416 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011417 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
11418 smp->data.u.str.str = str->str;
11419 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011420 smp->flags &= ~SMP_F_CONST;
11421 }
11422
11423 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011424 smp->data.u.str.str[smp->data.u.str.len] = '\0';
11425 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Christopher Fauleta2584792017-10-05 10:03:12 +020011426 return (smp->data.u.str.len >= 0);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020011427}
11428
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011429static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
11430{
11431 struct proxy *fe = strm_fe(smp->strm);
11432 int idx, i;
11433 struct cap_hdr *hdr;
11434 int len;
11435
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011436 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011437 return 0;
11438
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011439 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011440
11441 /* Check the availibity of the capture id. */
11442 if (idx > fe->nb_req_cap - 1)
11443 return 0;
11444
11445 /* Look for the original configuration. */
11446 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
11447 hdr != NULL && i != idx ;
11448 i--, hdr = hdr->next);
11449 if (!hdr)
11450 return 0;
11451
11452 /* check for the memory allocation */
11453 if (smp->strm->req_cap[hdr->index] == NULL)
11454 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
11455 if (smp->strm->req_cap[hdr->index] == NULL)
11456 return 0;
11457
11458 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011459 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011460 if (len > hdr->len)
11461 len = hdr->len;
11462
11463 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011464 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011465 smp->strm->req_cap[idx][len] = '\0';
11466
11467 return 1;
11468}
11469
11470static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
11471{
11472 struct proxy *fe = strm_fe(smp->strm);
11473 int idx, i;
11474 struct cap_hdr *hdr;
11475 int len;
11476
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011477 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011478 return 0;
11479
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011480 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011481
11482 /* Check the availibity of the capture id. */
11483 if (idx > fe->nb_rsp_cap - 1)
11484 return 0;
11485
11486 /* Look for the original configuration. */
11487 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
11488 hdr != NULL && i != idx ;
11489 i--, hdr = hdr->next);
11490 if (!hdr)
11491 return 0;
11492
11493 /* check for the memory allocation */
11494 if (smp->strm->res_cap[hdr->index] == NULL)
11495 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
11496 if (smp->strm->res_cap[hdr->index] == NULL)
11497 return 0;
11498
11499 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011500 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011501 if (len > hdr->len)
11502 len = hdr->len;
11503
11504 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011505 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020011506 smp->strm->res_cap[idx][len] = '\0';
11507
11508 return 1;
11509}
11510
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011511/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011512 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011513 * the relevant part of the request line accordingly. Then it updates various
11514 * pointers to the next elements which were moved, and the total buffer length.
11515 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011516 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
11517 * error, though this can be revisited when this code is finally exploited.
11518 *
11519 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
11520 * query string and 3 to replace uri.
11521 *
11522 * In query string case, the mark question '?' must be set at the start of the
11523 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011524 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011525int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011526 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011527{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020011528 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011529 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011530 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011531 int delta;
11532
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011533 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011534 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011535 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011536 cur_end = cur_ptr + txn->req.sl.rq.m_l;
11537
11538 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011539 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011540 txn->req.sl.rq.m_l += delta;
11541 txn->req.sl.rq.u += delta;
11542 txn->req.sl.rq.v += delta;
11543 break;
11544
11545 case 1: // path
11546 cur_ptr = http_get_path(txn);
11547 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011548 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011549
11550 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011551 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 +010011552 cur_end++;
11553
11554 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011555 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011556 txn->req.sl.rq.u_l += delta;
11557 txn->req.sl.rq.v += delta;
11558 break;
11559
11560 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011561 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011562 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011563 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11564 while (cur_ptr < cur_end && *cur_ptr != '?')
11565 cur_ptr++;
11566
11567 /* skip the question mark or indicate that we must insert it
11568 * (but only if the format string is not empty then).
11569 */
11570 if (cur_ptr < cur_end)
11571 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011572 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011573 offset = 0;
11574
11575 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011576 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011577 txn->req.sl.rq.u_l += delta;
11578 txn->req.sl.rq.v += delta;
11579 break;
11580
11581 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010011582 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011583 cur_end = cur_ptr + txn->req.sl.rq.u_l;
11584
11585 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011586 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011587 txn->req.sl.rq.u_l += delta;
11588 txn->req.sl.rq.v += delta;
11589 break;
11590
11591 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011592 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011593 }
11594
11595 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011596 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020011597 txn->req.sl.rq.l += delta;
11598 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011599 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011600 return 0;
11601}
11602
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011603/* This function replace the HTTP status code and the associated message. The
11604 * variable <status> contains the new status code. This function never fails.
11605 */
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011606void http_set_status(unsigned int status, const char *reason, struct stream *s)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011607{
11608 struct http_txn *txn = s->txn;
11609 char *cur_ptr, *cur_end;
11610 int delta;
11611 char *res;
11612 int c_l;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011613 const char *msg = reason;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011614 int msg_len;
11615
11616 chunk_reset(&trash);
11617
11618 res = ultoa_o(status, trash.str, trash.size);
11619 c_l = res - trash.str;
11620
11621 trash.str[c_l] = ' ';
11622 trash.len = c_l + 1;
11623
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011624 /* Do we have a custom reason format string? */
11625 if (msg == NULL)
11626 msg = get_reason(status);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011627 msg_len = strlen(msg);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011628 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
11629 trash.len += msg_len;
11630
11631 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
11632 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
11633
11634 /* commit changes and adjust message */
11635 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
11636
11637 /* adjust res line offsets and lengths */
11638 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
11639 txn->rsp.sl.st.c_l = c_l;
11640 txn->rsp.sl.st.r_l = msg_len;
11641
11642 delta = trash.len - (cur_end - cur_ptr);
11643 txn->rsp.sl.st.l += delta;
11644 txn->hdr_idx.v[0].len += delta;
11645 http_msg_move_end(&txn->rsp, delta);
11646}
11647
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011648/* This function executes one of the set-{method,path,query,uri} actions. It
11649 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020011650 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011651 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020011652 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
11653 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011654 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011655enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011656 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011657{
11658 chunk_reset(&trash);
11659
11660 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011661 if (rule->arg.http.action == 2)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011662 trash.str[trash.len++] = '?';
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011663 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010011664
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011665 http_replace_req_line(rule->arg.http.action, trash.str, trash.len, px, s);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011666 return ACT_RET_CONT;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011667}
11668
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011669/* This function is just a compliant action wrapper for "set-status". */
11670enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011671 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011672{
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011673 http_set_status(rule->arg.status.code, rule->arg.status.reason, s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011674 return ACT_RET_CONT;
11675}
11676
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011677/* parse an http-request action among :
11678 * set-method
11679 * set-path
11680 * set-query
11681 * set-uri
11682 *
11683 * All of them accept a single argument of type string representing a log-format.
11684 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
11685 * 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 +020011686 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011687 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011688enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
11689 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011690{
11691 int cur_arg = *orig_arg;
11692
Thierry FOURNIER42148732015-09-02 17:17:33 +020011693 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011694
11695 switch (args[0][4]) {
11696 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011697 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011698 rule->action_ptr = http_action_set_req_line;
11699 break;
11700 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011701 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011702 rule->action_ptr = http_action_set_req_line;
11703 break;
11704 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011705 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011706 rule->action_ptr = http_action_set_req_line;
11707 break;
11708 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011709 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011710 rule->action_ptr = http_action_set_req_line;
11711 break;
11712 default:
11713 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011714 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011715 }
11716
11717 if (!*args[cur_arg] ||
11718 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
11719 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011720 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011721 }
11722
Thierry FOURNIER8855a922015-07-31 08:54:25 +020011723 LIST_INIT(&rule->arg.http.logfmt);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011724 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010011725 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +010011726 (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 +010011727 return ACT_RET_PRS_ERR;
11728 }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011729
11730 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011731 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010011732}
11733
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011734/* parse set-status action:
11735 * This action accepts a single argument of type int representing
11736 * an http status code. It returns ACT_RET_PRS_OK on success,
11737 * ACT_RET_PRS_ERR on error.
11738 */
11739enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
11740 struct act_rule *rule, char **err)
11741{
11742 char *error;
11743
Thierry FOURNIER42148732015-09-02 17:17:33 +020011744 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011745 rule->action_ptr = action_http_set_status;
11746
11747 /* Check if an argument is available */
11748 if (!*args[*orig_arg]) {
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011749 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011750 return ACT_RET_PRS_ERR;
11751 }
11752
11753 /* convert status code as integer */
11754 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
11755 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
11756 memprintf(err, "expects an integer status code between 100 and 999");
11757 return ACT_RET_PRS_ERR;
11758 }
11759
11760 (*orig_arg)++;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080011761
11762 /* set custom reason string */
11763 rule->arg.status.reason = NULL; // If null, we use the default reason for the status code.
11764 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
11765 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
11766 (*orig_arg)++;
11767 rule->arg.status.reason = strdup(args[*orig_arg]);
11768 (*orig_arg)++;
11769 }
11770
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020011771 return ACT_RET_PRS_OK;
11772}
11773
Willy Tarreaua9083d02015-05-08 15:27:59 +020011774/* This function executes the "capture" action. It executes a fetch expression,
11775 * turns the result into a string and puts it in a capture slot. It always
11776 * returns 1. If an error occurs the action is cancelled, but the rule
11777 * processing continues.
11778 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011779enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011780 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020011781{
Willy Tarreaua9083d02015-05-08 15:27:59 +020011782 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020011783 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011784 char **cap = s->req_cap;
11785 int len;
11786
Thierry FOURNIER32b15002015-07-31 08:56:16 +020011787 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 +020011788 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011789 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011790
11791 if (cap[h->index] == NULL)
11792 cap[h->index] = pool_alloc2(h->pool);
11793
11794 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011795 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011796
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011797 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011798 if (len > h->len)
11799 len = h->len;
11800
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011801 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020011802 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011803 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011804}
11805
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011806/* This function executes the "capture" action and store the result in a
11807 * capture slot if exists. It executes a fetch expression, turns the result
11808 * into a string and puts it in a capture slot. It always returns 1. If an
11809 * error occurs the action is cancelled, but the rule processing continues.
11810 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011811enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011812 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011813{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011814 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011815 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011816 char **cap = s->req_cap;
11817 struct proxy *fe = strm_fe(s);
11818 int len;
11819 int i;
11820
11821 /* Look for the original configuration. */
11822 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020011823 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011824 i--, h = h->next);
11825 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011826 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011827
Thierry FOURNIERe2097972015-07-31 08:56:35 +020011828 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 +020011829 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011830 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011831
11832 if (cap[h->index] == NULL)
11833 cap[h->index] = pool_alloc2(h->pool);
11834
11835 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011836 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011837
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011838 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011839 if (len > h->len)
11840 len = h->len;
11841
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011842 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011843 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011844 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011845}
11846
Willy Tarreaua9083d02015-05-08 15:27:59 +020011847/* parse an "http-request capture" action. It takes a single argument which is
11848 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011849 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011850 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020011851 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011852enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
11853 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020011854{
11855 struct sample_expr *expr;
11856 struct cap_hdr *hdr;
11857 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020011858 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011859
11860 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
11861 if (strcmp(args[cur_arg], "if") == 0 ||
11862 strcmp(args[cur_arg], "unless") == 0)
11863 break;
11864
11865 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011866 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011867 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011868 }
11869
Willy Tarreaua9083d02015-05-08 15:27:59 +020011870 cur_arg = *orig_arg;
11871 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
11872 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011873 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011874
11875 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
11876 memprintf(err,
11877 "fetch method '%s' extracts information from '%s', none of which is available here",
11878 args[cur_arg-1], sample_src_names(expr->fetch->use));
11879 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011880 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011881 }
11882
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011883 if (!args[cur_arg] || !*args[cur_arg]) {
11884 memprintf(err, "expects 'len or 'id'");
11885 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011886 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011887 }
11888
Willy Tarreaua9083d02015-05-08 15:27:59 +020011889 if (strcmp(args[cur_arg], "len") == 0) {
11890 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011891
11892 if (!(px->cap & PR_CAP_FE)) {
11893 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011894 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011895 }
11896
11897 proxy->conf.args.ctx = ARGC_CAP;
11898
Willy Tarreaua9083d02015-05-08 15:27:59 +020011899 if (!args[cur_arg]) {
11900 memprintf(err, "missing length value");
11901 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011902 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011903 }
11904 /* we copy the table name for now, it will be resolved later */
11905 len = atoi(args[cur_arg]);
11906 if (len <= 0) {
11907 memprintf(err, "length must be > 0");
11908 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011909 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011910 }
11911 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011912
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011913 if (!len) {
11914 memprintf(err, "a positive 'len' argument is mandatory");
11915 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011916 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011917 }
11918
Vincent Bernat02779b62016-04-03 13:48:43 +020011919 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011920 hdr->next = px->req_cap;
11921 hdr->name = NULL; /* not a header capture */
11922 hdr->namelen = 0;
11923 hdr->len = len;
11924 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
11925 hdr->index = px->nb_req_cap++;
11926
11927 px->req_cap = hdr;
11928 px->to_log |= LW_REQHDR;
11929
Thierry FOURNIER42148732015-09-02 17:17:33 +020011930 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011931 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020011932 rule->arg.cap.expr = expr;
11933 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011934 }
11935
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011936 else if (strcmp(args[cur_arg], "id") == 0) {
11937 int id;
11938 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011939
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011940 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011941
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011942 if (!args[cur_arg]) {
11943 memprintf(err, "missing id value");
11944 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011945 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011946 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020011947
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011948 id = strtol(args[cur_arg], &error, 10);
11949 if (*error != '\0') {
11950 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
11951 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011952 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011953 }
11954 cur_arg++;
11955
11956 proxy->conf.args.ctx = ARGC_CAP;
11957
Thierry FOURNIER42148732015-09-02 17:17:33 +020011958 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011959 rule->action_ptr = http_action_req_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020011960 rule->arg.capid.expr = expr;
11961 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011962 }
11963
11964 else {
11965 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
11966 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011967 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020011968 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020011969
11970 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020011971 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020011972}
11973
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011974/* This function executes the "capture" action and store the result in a
11975 * capture slot if exists. It executes a fetch expression, turns the result
11976 * into a string and puts it in a capture slot. It always returns 1. If an
11977 * error occurs the action is cancelled, but the rule processing continues.
11978 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011979enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020011980 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011981{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011982 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011983 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011984 char **cap = s->res_cap;
11985 struct proxy *fe = strm_fe(s);
11986 int len;
11987 int i;
11988
11989 /* Look for the original configuration. */
11990 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020011991 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011992 i--, h = h->next);
11993 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011994 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011995
Thierry FOURNIERe2097972015-07-31 08:56:35 +020011996 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 +020011997 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020011998 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020011999
12000 if (cap[h->index] == NULL)
12001 cap[h->index] = pool_alloc2(h->pool);
12002
12003 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012004 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012005
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012006 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012007 if (len > h->len)
12008 len = h->len;
12009
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012010 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012011 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012012 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012013}
12014
12015/* parse an "http-response capture" action. It takes a single argument which is
12016 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12017 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012018 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012019 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012020enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12021 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012022{
12023 struct sample_expr *expr;
12024 int cur_arg;
12025 int id;
12026 char *error;
12027
12028 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12029 if (strcmp(args[cur_arg], "if") == 0 ||
12030 strcmp(args[cur_arg], "unless") == 0)
12031 break;
12032
12033 if (cur_arg < *orig_arg + 3) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012034 memprintf(err, "expects <expression> id <idx>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012035 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012036 }
12037
12038 cur_arg = *orig_arg;
12039 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12040 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012041 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012042
12043 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12044 memprintf(err,
12045 "fetch method '%s' extracts information from '%s', none of which is available here",
12046 args[cur_arg-1], sample_src_names(expr->fetch->use));
12047 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012048 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012049 }
12050
12051 if (!args[cur_arg] || !*args[cur_arg]) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012052 memprintf(err, "expects 'id'");
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012053 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012054 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012055 }
12056
12057 if (strcmp(args[cur_arg], "id") != 0) {
12058 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12059 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012060 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012061 }
12062
12063 cur_arg++;
12064
12065 if (!args[cur_arg]) {
12066 memprintf(err, "missing id value");
12067 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012068 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012069 }
12070
12071 id = strtol(args[cur_arg], &error, 10);
12072 if (*error != '\0') {
12073 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12074 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012075 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012076 }
12077 cur_arg++;
12078
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012079 proxy->conf.args.ctx = ARGC_CAP;
12080
Thierry FOURNIER42148732015-09-02 17:17:33 +020012081 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012082 rule->action_ptr = http_action_res_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012083 rule->arg.capid.expr = expr;
12084 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012085
12086 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012087 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012088}
12089
William Lallemand73025dd2014-04-24 14:38:37 +020012090/*
12091 * Return the struct http_req_action_kw associated to a keyword.
12092 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012093struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012094{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012095 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012096}
12097
12098/*
12099 * Return the struct http_res_action_kw associated to a keyword.
12100 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012101struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012102{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012103 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012104}
12105
Willy Tarreau12207b32016-11-22 19:48:51 +010012106
12107/* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop
12108 * now.
12109 */
12110static int cli_parse_show_errors(char **args, struct appctx *appctx, void *private)
12111{
12112 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
12113 return 1;
12114
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012115 if (*args[2]) {
12116 struct proxy *px;
12117
12118 px = proxy_find_by_name(args[2], 0, 0);
12119 if (px)
12120 appctx->ctx.errors.iid = px->uuid;
12121 else
12122 appctx->ctx.errors.iid = atoi(args[2]);
12123
12124 if (!appctx->ctx.errors.iid) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +020012125 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012126 appctx->ctx.cli.msg = "No such proxy.\n";
12127 appctx->st0 = CLI_ST_PRINT;
12128 return 1;
12129 }
12130 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012131 else
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012132 appctx->ctx.errors.iid = -1; // dump all proxies
12133
Willy Tarreau35069f82016-11-25 09:16:37 +010012134 appctx->ctx.errors.flag = 0;
12135 if (strcmp(args[3], "request") == 0)
12136 appctx->ctx.errors.flag |= 4; // ignore response
12137 else if (strcmp(args[3], "response") == 0)
12138 appctx->ctx.errors.flag |= 2; // ignore request
Willy Tarreau12207b32016-11-22 19:48:51 +010012139 appctx->ctx.errors.px = NULL;
Willy Tarreau12207b32016-11-22 19:48:51 +010012140 return 0;
12141}
12142
12143/* This function dumps all captured errors onto the stream interface's
12144 * read buffer. It returns 0 if the output buffer is full and it needs
12145 * to be called again, otherwise non-zero.
12146 */
12147static int cli_io_handler_show_errors(struct appctx *appctx)
12148{
12149 struct stream_interface *si = appctx->owner;
12150 extern const char *monthname[12];
12151
12152 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
12153 return 1;
12154
12155 chunk_reset(&trash);
12156
12157 if (!appctx->ctx.errors.px) {
12158 /* the function had not been called yet, let's prepare the
12159 * buffer for a response.
12160 */
12161 struct tm tm;
12162
12163 get_localtime(date.tv_sec, &tm);
12164 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
12165 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12166 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
12167 error_snapshot_id);
12168
Willy Tarreau06d80a92017-10-19 14:32:15 +020012169 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012170 /* Socket buffer full. Let's try again later from the same point */
12171 si_applet_cant_put(si);
12172 return 0;
12173 }
12174
12175 appctx->ctx.errors.px = proxy;
Willy Tarreau12207b32016-11-22 19:48:51 +010012176 appctx->ctx.errors.bol = 0;
12177 appctx->ctx.errors.ptr = -1;
12178 }
12179
12180 /* we have two inner loops here, one for the proxy, the other one for
12181 * the buffer.
12182 */
12183 while (appctx->ctx.errors.px) {
12184 struct error_snapshot *es;
12185
Willy Tarreau35069f82016-11-25 09:16:37 +010012186 if ((appctx->ctx.errors.flag & 1) == 0) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012187 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau35069f82016-11-25 09:16:37 +010012188 if (appctx->ctx.errors.flag & 2) // skip req
12189 goto next;
12190 }
12191 else {
Willy Tarreau12207b32016-11-22 19:48:51 +010012192 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau35069f82016-11-25 09:16:37 +010012193 if (appctx->ctx.errors.flag & 4) // skip resp
12194 goto next;
12195 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012196
12197 if (!es->when.tv_sec)
12198 goto next;
12199
12200 if (appctx->ctx.errors.iid >= 0 &&
12201 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
12202 es->oe->uuid != appctx->ctx.errors.iid)
12203 goto next;
12204
12205 if (appctx->ctx.errors.ptr < 0) {
12206 /* just print headers now */
12207
12208 char pn[INET6_ADDRSTRLEN];
12209 struct tm tm;
12210 int port;
12211
12212 get_localtime(es->when.tv_sec, &tm);
12213 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
12214 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12215 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
12216
12217 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
12218 case AF_INET:
12219 case AF_INET6:
12220 port = get_host_port(&es->src);
12221 break;
12222 default:
12223 port = 0;
12224 }
12225
Willy Tarreau35069f82016-11-25 09:16:37 +010012226 switch (appctx->ctx.errors.flag & 1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012227 case 0:
12228 chunk_appendf(&trash,
12229 " frontend %s (#%d): invalid request\n"
12230 " backend %s (#%d)",
12231 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
12232 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
12233 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
12234 break;
12235 case 1:
12236 chunk_appendf(&trash,
12237 " backend %s (#%d): invalid response\n"
12238 " frontend %s (#%d)",
12239 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
12240 es->oe->id, es->oe->uuid);
12241 break;
12242 }
12243
12244 chunk_appendf(&trash,
12245 ", server %s (#%d), event #%u\n"
12246 " src %s:%d, session #%d, session flags 0x%08x\n"
Willy Tarreau10e61cb2017-01-04 14:51:22 +010012247 " HTTP msg state %s(%d), msg flags 0x%08x, tx flags 0x%08x\n"
Willy Tarreau12207b32016-11-22 19:48:51 +010012248 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
12249 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
12250 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
12251 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
12252 es->ev_id,
12253 pn, port, es->sid, es->s_flags,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +020012254 h1_msg_state_str(es->state), es->state, es->m_flags, es->t_flags,
Willy Tarreau12207b32016-11-22 19:48:51 +010012255 es->m_clen, es->m_blen,
12256 es->b_flags, es->b_out, es->b_tot,
12257 es->len, es->b_wrap, es->pos);
12258
Willy Tarreau06d80a92017-10-19 14:32:15 +020012259 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012260 /* Socket buffer full. Let's try again later from the same point */
12261 si_applet_cant_put(si);
12262 return 0;
12263 }
12264 appctx->ctx.errors.ptr = 0;
12265 appctx->ctx.errors.sid = es->sid;
12266 }
12267
12268 if (appctx->ctx.errors.sid != es->sid) {
12269 /* the snapshot changed while we were dumping it */
12270 chunk_appendf(&trash,
12271 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +020012272 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012273 si_applet_cant_put(si);
12274 return 0;
12275 }
12276 goto next;
12277 }
12278
12279 /* OK, ptr >= 0, so we have to dump the current line */
12280 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
12281 int newptr;
12282 int newline;
12283
12284 newline = appctx->ctx.errors.bol;
12285 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
12286 if (newptr == appctx->ctx.errors.ptr)
12287 return 0;
12288
Willy Tarreau06d80a92017-10-19 14:32:15 +020012289 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012290 /* Socket buffer full. Let's try again later from the same point */
12291 si_applet_cant_put(si);
12292 return 0;
12293 }
12294 appctx->ctx.errors.ptr = newptr;
12295 appctx->ctx.errors.bol = newline;
12296 };
12297 next:
12298 appctx->ctx.errors.bol = 0;
12299 appctx->ctx.errors.ptr = -1;
Willy Tarreau35069f82016-11-25 09:16:37 +010012300 appctx->ctx.errors.flag ^= 1;
12301 if (!(appctx->ctx.errors.flag & 1))
Willy Tarreau12207b32016-11-22 19:48:51 +010012302 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau12207b32016-11-22 19:48:51 +010012303 }
12304
12305 /* dump complete */
12306 return 1;
12307}
12308
12309/* register cli keywords */
12310static struct cli_kw_list cli_kws = {{ },{
12311 { { "show", "errors", NULL },
12312 "show errors : report last request and response errors for each proxy",
12313 cli_parse_show_errors, cli_io_handler_show_errors, NULL,
12314 },
12315 {{},}
12316}};
12317
Willy Tarreau4a568972010-05-12 08:08:50 +020012318/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012319/* All supported ACL keywords must be declared here. */
12320/************************************************************************/
12321
12322/* Note: must not be declared <const> as its list will be overwritten.
12323 * Please take care of keeping this list alphabetically sorted.
12324 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012325static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012326 { "base", "base", PAT_MATCH_STR },
12327 { "base_beg", "base", PAT_MATCH_BEG },
12328 { "base_dir", "base", PAT_MATCH_DIR },
12329 { "base_dom", "base", PAT_MATCH_DOM },
12330 { "base_end", "base", PAT_MATCH_END },
12331 { "base_len", "base", PAT_MATCH_LEN },
12332 { "base_reg", "base", PAT_MATCH_REG },
12333 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020012334
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012335 { "cook", "req.cook", PAT_MATCH_STR },
12336 { "cook_beg", "req.cook", PAT_MATCH_BEG },
12337 { "cook_dir", "req.cook", PAT_MATCH_DIR },
12338 { "cook_dom", "req.cook", PAT_MATCH_DOM },
12339 { "cook_end", "req.cook", PAT_MATCH_END },
12340 { "cook_len", "req.cook", PAT_MATCH_LEN },
12341 { "cook_reg", "req.cook", PAT_MATCH_REG },
12342 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012343
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012344 { "hdr", "req.hdr", PAT_MATCH_STR },
12345 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
12346 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
12347 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
12348 { "hdr_end", "req.hdr", PAT_MATCH_END },
12349 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
12350 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
12351 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012352
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012353 /* these two declarations uses strings with list storage (in place
12354 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
12355 * and delete functions are relative to the list management. The parse
12356 * and match method are related to the corresponding fetch methods. This
12357 * is very particular ACL declaration mode.
12358 */
12359 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
12360 { "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 +020012361
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012362 { "path", "path", PAT_MATCH_STR },
12363 { "path_beg", "path", PAT_MATCH_BEG },
12364 { "path_dir", "path", PAT_MATCH_DIR },
12365 { "path_dom", "path", PAT_MATCH_DOM },
12366 { "path_end", "path", PAT_MATCH_END },
12367 { "path_len", "path", PAT_MATCH_LEN },
12368 { "path_reg", "path", PAT_MATCH_REG },
12369 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012370
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012371 { "req_ver", "req.ver", PAT_MATCH_STR },
12372 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012373
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012374 { "scook", "res.cook", PAT_MATCH_STR },
12375 { "scook_beg", "res.cook", PAT_MATCH_BEG },
12376 { "scook_dir", "res.cook", PAT_MATCH_DIR },
12377 { "scook_dom", "res.cook", PAT_MATCH_DOM },
12378 { "scook_end", "res.cook", PAT_MATCH_END },
12379 { "scook_len", "res.cook", PAT_MATCH_LEN },
12380 { "scook_reg", "res.cook", PAT_MATCH_REG },
12381 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012382
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012383 { "shdr", "res.hdr", PAT_MATCH_STR },
12384 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
12385 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
12386 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
12387 { "shdr_end", "res.hdr", PAT_MATCH_END },
12388 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
12389 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
12390 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012391
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012392 { "url", "url", PAT_MATCH_STR },
12393 { "url_beg", "url", PAT_MATCH_BEG },
12394 { "url_dir", "url", PAT_MATCH_DIR },
12395 { "url_dom", "url", PAT_MATCH_DOM },
12396 { "url_end", "url", PAT_MATCH_END },
12397 { "url_len", "url", PAT_MATCH_LEN },
12398 { "url_reg", "url", PAT_MATCH_REG },
12399 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012400
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012401 { "urlp", "urlp", PAT_MATCH_STR },
12402 { "urlp_beg", "urlp", PAT_MATCH_BEG },
12403 { "urlp_dir", "urlp", PAT_MATCH_DIR },
12404 { "urlp_dom", "urlp", PAT_MATCH_DOM },
12405 { "urlp_end", "urlp", PAT_MATCH_END },
12406 { "urlp_len", "urlp", PAT_MATCH_LEN },
12407 { "urlp_reg", "urlp", PAT_MATCH_REG },
12408 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012409
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012410 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020012411}};
12412
12413/************************************************************************/
12414/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020012415/************************************************************************/
12416/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012417static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012418 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012419 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012420 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12421
Willy Tarreau87b09662015-04-03 00:22:06 +020012422 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012423 { "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 +020012424
12425 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012426 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12427 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
12428 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020012429
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012430 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
12431 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010012432
Willy Tarreau409bcde2013-01-08 00:31:00 +010012433 /* cookie is valid in both directions (eg: for "stick ...") but cook*
12434 * are only here to match the ACL's name, are request-only and are used
12435 * for ACL compatibility only.
12436 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012437 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12438 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012439 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12440 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012441
12442 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
12443 * only here to match the ACL's name, are request-only and are used for
12444 * ACL compatibility only.
12445 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012446 { "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 +020012447 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012448 { "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 +020012449 { "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 +010012450
Willy Tarreau0a0daec2013-04-02 22:44:58 +020012451 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012452 { "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 +010012453 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010012454 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012455 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010012456 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012457
12458 /* HTTP protocol on the request path */
12459 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012460 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012461
12462 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012463 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
12464 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012465
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012466 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012467 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
12468 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020012469 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020012470
Thierry FOURNIERd7d88812017-04-19 15:15:14 +020012471 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER5617dce2017-04-09 05:38:19 +020012472 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
12473
Willy Tarreau18ed2562013-01-14 15:56:36 +010012474 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012475 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
12476 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012477
Willy Tarreau18ed2562013-01-14 15:56:36 +010012478 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012479 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012480 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
12481 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012482
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012483 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012484 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012485 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012486 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012487 { "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 +010012488 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012489 { "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 +010012490
12491 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012492 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012493 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12494 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012495
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012496 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012497 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012498 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012499 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010012500 { "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 +010012501 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012502 { "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 +010012503
Willy Tarreau409bcde2013-01-08 00:31:00 +010012504 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012505 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012506 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
12507 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012508 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010012509
12510 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012511 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012512 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012513 { "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 +020012514 { "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 +010012515
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012516 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020012517 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012518 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012519 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012520 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010012521 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012522 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020012523 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
12524 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012525 { "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 +010012526 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020012527}};
12528
Willy Tarreau8797c062007-05-07 00:55:35 +020012529
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012530/************************************************************************/
12531/* All supported converter keywords must be declared here. */
12532/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020012533/* Note: must not be declared <const> as its list will be overwritten */
12534static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012535 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020012536 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012537 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
12538 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012539 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020012540 { NULL, NULL, 0, 0, 0 },
12541}};
12542
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012543
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012544/************************************************************************/
12545/* All supported http-request action keywords must be declared here. */
12546/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012547struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012548 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020012549 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012550 { "set-method", parse_set_req_line },
12551 { "set-path", parse_set_req_line },
12552 { "set-query", parse_set_req_line },
12553 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020012554 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012555 }
12556};
12557
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012558struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012559 .kw = {
12560 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012561 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012562 { NULL, NULL }
12563 }
12564};
12565
Willy Tarreau8797c062007-05-07 00:55:35 +020012566__attribute__((constructor))
12567static void __http_protocol_init(void)
12568{
12569 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020012570 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020012571 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012572 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012573 http_res_keywords_register(&http_res_actions);
Willy Tarreau12207b32016-11-22 19:48:51 +010012574 cli_register_kw(&cli_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020012575}
12576
12577
Willy Tarreau58f10d72006-12-04 02:26:12 +010012578/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020012579 * Local variables:
12580 * c-indent-level: 8
12581 * c-basic-offset: 8
12582 * End:
12583 */