blob: 80ba5660b4833e5ce6d6e0cba44ac44a4daab6c7 [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 Tarreaubaaee002006-06-26 02:48:02 +020060#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010061#include <proto/hdr_idx.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010062#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020063#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020064#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010065#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020066#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020067#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010068#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020069#include <proto/stream.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010070#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020071#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020072#include <proto/pattern.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020073#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020074
Willy Tarreau522d6c02009-12-06 18:49:18 +010075const char HTTP_100[] =
76 "HTTP/1.1 100 Continue\r\n\r\n";
77
78const struct chunk http_100_chunk = {
79 .str = (char *)&HTTP_100,
80 .len = sizeof(HTTP_100)-1
81};
82
Willy Tarreaua9679ac2010-01-03 17:32:57 +010083/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020084const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010085 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010086 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020087 "Location: "; /* not terminated since it will be concatenated with the URL */
88
Willy Tarreau0f772532006-12-23 20:51:41 +010089const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010090 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010091 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010092 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010093 "Location: "; /* not terminated since it will be concatenated with the URL */
94
95/* same as 302 except that the browser MUST retry with the GET method */
96const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010097 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010098 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010099 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100100 "Location: "; /* not terminated since it will be concatenated with the URL */
101
Yves Lafon3e8d1ae2013-03-11 11:06:05 -0400102
103/* same as 302 except that the browser MUST retry with the same method */
104const char *HTTP_307 =
105 "HTTP/1.1 307 Temporary Redirect\r\n"
106 "Cache-Control: no-cache\r\n"
107 "Content-length: 0\r\n"
108 "Location: "; /* not terminated since it will be concatenated with the URL */
109
110/* same as 301 except that the browser MUST retry with the same method */
111const char *HTTP_308 =
112 "HTTP/1.1 308 Permanent Redirect\r\n"
113 "Content-length: 0\r\n"
114 "Location: "; /* not terminated since it will be concatenated with the URL */
115
Willy Tarreaubaaee002006-06-26 02:48:02 +0200116/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
117const char *HTTP_401_fmt =
118 "HTTP/1.0 401 Unauthorized\r\n"
119 "Cache-Control: no-cache\r\n"
120 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200121 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200122 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
123 "\r\n"
124 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
125
Willy Tarreau844a7e72010-01-31 21:46:18 +0100126const char *HTTP_407_fmt =
127 "HTTP/1.0 407 Unauthorized\r\n"
128 "Cache-Control: no-cache\r\n"
129 "Connection: close\r\n"
130 "Content-Type: text/html\r\n"
131 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
132 "\r\n"
Godbach1f1fae62014-12-17 16:32:05 +0800133 "<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 +0100134
Willy Tarreau0f772532006-12-23 20:51:41 +0100135
136const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200137 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100138 [HTTP_ERR_400] = 400,
139 [HTTP_ERR_403] = 403,
CJ Ess108b1dd2015-04-07 12:03:37 -0400140 [HTTP_ERR_405] = 405,
Willy Tarreau0f772532006-12-23 20:51:41 +0100141 [HTTP_ERR_408] = 408,
CJ Ess108b1dd2015-04-07 12:03:37 -0400142 [HTTP_ERR_429] = 429,
Willy Tarreau0f772532006-12-23 20:51:41 +0100143 [HTTP_ERR_500] = 500,
144 [HTTP_ERR_502] = 502,
145 [HTTP_ERR_503] = 503,
146 [HTTP_ERR_504] = 504,
147};
148
Willy Tarreau80587432006-12-24 17:47:20 +0100149static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200150 [HTTP_ERR_200] =
151 "HTTP/1.0 200 OK\r\n"
152 "Cache-Control: no-cache\r\n"
153 "Connection: close\r\n"
154 "Content-Type: text/html\r\n"
155 "\r\n"
156 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
157
Willy Tarreau0f772532006-12-23 20:51:41 +0100158 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100159 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100160 "Cache-Control: no-cache\r\n"
161 "Connection: close\r\n"
162 "Content-Type: text/html\r\n"
163 "\r\n"
164 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
165
166 [HTTP_ERR_403] =
167 "HTTP/1.0 403 Forbidden\r\n"
168 "Cache-Control: no-cache\r\n"
169 "Connection: close\r\n"
170 "Content-Type: text/html\r\n"
171 "\r\n"
172 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
173
CJ Ess108b1dd2015-04-07 12:03:37 -0400174 [HTTP_ERR_405] =
175 "HTTP/1.0 405 Method Not Allowed\r\n"
176 "Cache-Control: no-cache\r\n"
177 "Connection: close\r\n"
178 "Content-Type: text/html\r\n"
179 "\r\n"
180 "<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",
181
Willy Tarreau0f772532006-12-23 20:51:41 +0100182 [HTTP_ERR_408] =
183 "HTTP/1.0 408 Request Time-out\r\n"
184 "Cache-Control: no-cache\r\n"
185 "Connection: close\r\n"
186 "Content-Type: text/html\r\n"
187 "\r\n"
188 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
189
CJ Ess108b1dd2015-04-07 12:03:37 -0400190 [HTTP_ERR_429] =
191 "HTTP/1.0 429 Too Many Requests\r\n"
192 "Cache-Control: no-cache\r\n"
193 "Connection: close\r\n"
194 "Content-Type: text/html\r\n"
195 "\r\n"
196 "<html><body><h1>429 Too Many Requests</h1>\nYou have sent too many requests in a given amount of time.\n</body></html>\n",
197
Willy Tarreau0f772532006-12-23 20:51:41 +0100198 [HTTP_ERR_500] =
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200199 "HTTP/1.0 500 Internal Server Error\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100200 "Cache-Control: no-cache\r\n"
201 "Connection: close\r\n"
202 "Content-Type: text/html\r\n"
203 "\r\n"
Jarno Huuskonen16ad94a2017-01-09 14:17:10 +0200204 "<html><body><h1>500 Internal Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
Willy Tarreau0f772532006-12-23 20:51:41 +0100205
206 [HTTP_ERR_502] =
207 "HTTP/1.0 502 Bad Gateway\r\n"
208 "Cache-Control: no-cache\r\n"
209 "Connection: close\r\n"
210 "Content-Type: text/html\r\n"
211 "\r\n"
212 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
213
214 [HTTP_ERR_503] =
215 "HTTP/1.0 503 Service Unavailable\r\n"
216 "Cache-Control: no-cache\r\n"
217 "Connection: close\r\n"
218 "Content-Type: text/html\r\n"
219 "\r\n"
220 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
221
222 [HTTP_ERR_504] =
223 "HTTP/1.0 504 Gateway Time-out\r\n"
224 "Cache-Control: no-cache\r\n"
225 "Connection: close\r\n"
226 "Content-Type: text/html\r\n"
227 "\r\n"
228 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
229
230};
231
Cyril Bonté19979e12012-04-04 12:57:21 +0200232/* status codes available for the stats admin page (strictly 4 chars length) */
233const char *stat_status_codes[STAT_STATUS_SIZE] = {
234 [STAT_STATUS_DENY] = "DENY",
235 [STAT_STATUS_DONE] = "DONE",
236 [STAT_STATUS_ERRP] = "ERRP",
237 [STAT_STATUS_EXCD] = "EXCD",
238 [STAT_STATUS_NONE] = "NONE",
239 [STAT_STATUS_PART] = "PART",
240 [STAT_STATUS_UNKN] = "UNKN",
241};
242
243
William Lallemand73025dd2014-04-24 14:38:37 +0200244/* List head of all known action keywords for "http-request" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200245struct action_kw_list http_req_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200246 .list = LIST_HEAD_INIT(http_req_keywords.list)
247};
248
249/* List head of all known action keywords for "http-response" */
Thierry FOURNIER36481b82015-08-19 09:01:53 +0200250struct action_kw_list http_res_keywords = {
William Lallemand73025dd2014-04-24 14:38:37 +0200251 .list = LIST_HEAD_INIT(http_res_keywords.list)
252};
253
Willy Tarreau80587432006-12-24 17:47:20 +0100254/* We must put the messages here since GCC cannot initialize consts depending
255 * on strlen().
256 */
257struct chunk http_err_chunks[HTTP_ERR_SIZE];
258
Willy Tarreaua890d072013-04-02 12:01:06 +0200259/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
260static struct hdr_ctx static_hdr_ctx;
261
Willy Tarreau42250582007-04-01 01:30:43 +0200262#define FD_SETS_ARE_BITFIELDS
263#ifdef FD_SETS_ARE_BITFIELDS
264/*
265 * This map is used with all the FD_* macros to check whether a particular bit
266 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
267 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
268 * byte should be encoded. Be careful to always pass bytes from 0 to 255
269 * exclusively to the macros.
270 */
271fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
272fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100273fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200274
275#else
276#error "Check if your OS uses bitfields for fd_sets"
277#endif
278
Willy Tarreau87b09662015-04-03 00:22:06 +0200279static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn);
Willy Tarreau0b748332014-04-29 00:13:29 +0200280
David Carlier7365f7d2016-04-04 11:54:42 +0100281static inline int http_msg_forward_body(struct stream *s, struct http_msg *msg);
282static inline int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +0100283
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200284/* This function returns a reason associated with the HTTP status.
285 * This function never fails, a message is always returned.
286 */
287const char *get_reason(unsigned int status)
288{
289 switch (status) {
290 case 100: return "Continue";
291 case 101: return "Switching Protocols";
292 case 102: return "Processing";
293 case 200: return "OK";
294 case 201: return "Created";
295 case 202: return "Accepted";
296 case 203: return "Non-Authoritative Information";
297 case 204: return "No Content";
298 case 205: return "Reset Content";
299 case 206: return "Partial Content";
300 case 207: return "Multi-Status";
301 case 210: return "Content Different";
302 case 226: return "IM Used";
303 case 300: return "Multiple Choices";
304 case 301: return "Moved Permanently";
305 case 302: return "Moved Temporarily";
306 case 303: return "See Other";
307 case 304: return "Not Modified";
308 case 305: return "Use Proxy";
309 case 307: return "Temporary Redirect";
310 case 308: return "Permanent Redirect";
311 case 310: return "Too many Redirects";
312 case 400: return "Bad Request";
313 case 401: return "Unauthorized";
314 case 402: return "Payment Required";
315 case 403: return "Forbidden";
316 case 404: return "Not Found";
317 case 405: return "Method Not Allowed";
318 case 406: return "Not Acceptable";
319 case 407: return "Proxy Authentication Required";
320 case 408: return "Request Time-out";
321 case 409: return "Conflict";
322 case 410: return "Gone";
323 case 411: return "Length Required";
324 case 412: return "Precondition Failed";
325 case 413: return "Request Entity Too Large";
326 case 414: return "Request-URI Too Long";
327 case 415: return "Unsupported Media Type";
328 case 416: return "Requested range unsatisfiable";
329 case 417: return "Expectation failed";
330 case 418: return "I'm a teapot";
331 case 422: return "Unprocessable entity";
332 case 423: return "Locked";
333 case 424: return "Method failure";
334 case 425: return "Unordered Collection";
335 case 426: return "Upgrade Required";
336 case 428: return "Precondition Required";
337 case 429: return "Too Many Requests";
338 case 431: return "Request Header Fields Too Large";
339 case 449: return "Retry With";
340 case 450: return "Blocked by Windows Parental Controls";
341 case 451: return "Unavailable For Legal Reasons";
342 case 456: return "Unrecoverable Error";
343 case 499: return "client has closed connection";
344 case 500: return "Internal Server Error";
345 case 501: return "Not Implemented";
Jarno Huuskonen59af2df2016-12-28 10:49:01 +0200346 case 502: return "Bad Gateway or Proxy Error";
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +0200347 case 503: return "Service Unavailable";
348 case 504: return "Gateway Time-out";
349 case 505: return "HTTP Version not supported";
350 case 506: return "Variant also negociate";
351 case 507: return "Insufficient storage";
352 case 508: return "Loop detected";
353 case 509: return "Bandwidth Limit Exceeded";
354 case 510: return "Not extended";
355 case 511: return "Network authentication required";
356 case 520: return "Web server is returning an unknown error";
357 default:
358 switch (status) {
359 case 100 ... 199: return "Informational";
360 case 200 ... 299: return "Success";
361 case 300 ... 399: return "Redirection";
362 case 400 ... 499: return "Client Error";
363 case 500 ... 599: return "Server Error";
364 default: return "Other";
365 }
366 }
367}
368
Willy Tarreau80587432006-12-24 17:47:20 +0100369void init_proto_http()
370{
Willy Tarreau42250582007-04-01 01:30:43 +0200371 int i;
372 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100373 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200374
Willy Tarreau80587432006-12-24 17:47:20 +0100375 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
376 if (!http_err_msgs[msg]) {
377 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
378 abort();
379 }
380
381 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
382 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
383 }
Willy Tarreau42250582007-04-01 01:30:43 +0200384
385 /* initialize the log header encoding map : '{|}"#' should be encoded with
386 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
387 * URL encoding only requires '"', '#' to be encoded as well as non-
388 * printable characters above.
389 */
390 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
391 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100392 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200393 for (i = 0; i < 32; i++) {
394 FD_SET(i, hdr_encode_map);
395 FD_SET(i, url_encode_map);
396 }
397 for (i = 127; i < 256; i++) {
398 FD_SET(i, hdr_encode_map);
399 FD_SET(i, url_encode_map);
400 }
401
402 tmp = "\"#{|}";
403 while (*tmp) {
404 FD_SET(*tmp, hdr_encode_map);
405 tmp++;
406 }
407
408 tmp = "\"#";
409 while (*tmp) {
410 FD_SET(*tmp, url_encode_map);
411 tmp++;
412 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200413
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100414 /* initialize the http header encoding map. The draft httpbis define the
415 * header content as:
416 *
417 * HTTP-message = start-line
418 * *( header-field CRLF )
419 * CRLF
420 * [ message-body ]
421 * header-field = field-name ":" OWS field-value OWS
422 * field-value = *( field-content / obs-fold )
423 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
424 * obs-fold = CRLF 1*( SP / HTAB )
425 * field-vchar = VCHAR / obs-text
426 * VCHAR = %x21-7E
427 * obs-text = %x80-FF
428 *
429 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
430 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
431 * "obs-fold" is volontary forgotten because haproxy remove this.
432 */
433 memset(http_encode_map, 0, sizeof(http_encode_map));
434 for (i = 0x00; i <= 0x08; i++)
435 FD_SET(i, http_encode_map);
436 for (i = 0x0a; i <= 0x1f; i++)
437 FD_SET(i, http_encode_map);
438 FD_SET(0x7f, http_encode_map);
439
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200440 /* memory allocations */
Willy Tarreau63986c72015-04-03 22:55:33 +0200441 pool2_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED);
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200442 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100443 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100444}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200445
Willy Tarreau53b6c742006-12-17 13:37:46 +0100446/*
447 * We have 26 list of methods (1 per first letter), each of which can have
448 * up to 3 entries (2 valid, 1 null).
449 */
450struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100451 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100452 int len;
453 const char text[8];
454};
455
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100456const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100457 ['C' - 'A'] = {
458 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
459 },
460 ['D' - 'A'] = {
461 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
462 },
463 ['G' - 'A'] = {
464 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
465 },
466 ['H' - 'A'] = {
467 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
468 },
Christopher Fauletd57ad642015-07-31 14:26:57 +0200469 ['O' - 'A'] = {
470 [0] = { .meth = HTTP_METH_OPTIONS , .len=7, .text="OPTIONS" },
471 },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100472 ['P' - 'A'] = {
473 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
474 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
475 },
476 ['T' - 'A'] = {
477 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
478 },
479 /* rest is empty like this :
Willy Tarreaub7ce4242015-09-03 17:15:21 +0200480 * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100481 */
482};
483
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100484const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100485 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
486 [HTTP_METH_GET] = { "GET", 3 },
487 [HTTP_METH_HEAD] = { "HEAD", 4 },
488 [HTTP_METH_POST] = { "POST", 4 },
489 [HTTP_METH_PUT] = { "PUT", 3 },
490 [HTTP_METH_DELETE] = { "DELETE", 6 },
491 [HTTP_METH_TRACE] = { "TRACE", 5 },
492 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
493};
494
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100495/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200496 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau2235b262016-11-05 15:50:20 +0100497 * RFC2616/RFC5234/RFC7230 for those chars. A token is any ASCII char that is
498 * neither a separator nor a CTL char. An http ver_token is any ASCII which can
499 * be found in an HTTP version, which includes 'H', 'T', 'P', '/', '.' and any
500 * digit. Note: please do not overwrite values in assignment since gcc-2.95
501 * will not handle them correctly. It's worth noting that chars 128..255 are
502 * nothing, not even control chars.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100503 */
Willy Tarreau2235b262016-11-05 15:50:20 +0100504const unsigned char http_char_classes[256] = {
505 [ 0] = HTTP_FLG_CTL,
506 [ 1] = HTTP_FLG_CTL,
507 [ 2] = HTTP_FLG_CTL,
508 [ 3] = HTTP_FLG_CTL,
509 [ 4] = HTTP_FLG_CTL,
510 [ 5] = HTTP_FLG_CTL,
511 [ 6] = HTTP_FLG_CTL,
512 [ 7] = HTTP_FLG_CTL,
513 [ 8] = HTTP_FLG_CTL,
514 [ 9] = HTTP_FLG_SPHT | HTTP_FLG_LWS | HTTP_FLG_SEP | HTTP_FLG_CTL,
515 [ 10] = HTTP_FLG_CRLF | HTTP_FLG_LWS | HTTP_FLG_CTL,
516 [ 11] = HTTP_FLG_CTL,
517 [ 12] = HTTP_FLG_CTL,
518 [ 13] = HTTP_FLG_CRLF | HTTP_FLG_LWS | HTTP_FLG_CTL,
519 [ 14] = HTTP_FLG_CTL,
520 [ 15] = HTTP_FLG_CTL,
521 [ 16] = HTTP_FLG_CTL,
522 [ 17] = HTTP_FLG_CTL,
523 [ 18] = HTTP_FLG_CTL,
524 [ 19] = HTTP_FLG_CTL,
525 [ 20] = HTTP_FLG_CTL,
526 [ 21] = HTTP_FLG_CTL,
527 [ 22] = HTTP_FLG_CTL,
528 [ 23] = HTTP_FLG_CTL,
529 [ 24] = HTTP_FLG_CTL,
530 [ 25] = HTTP_FLG_CTL,
531 [ 26] = HTTP_FLG_CTL,
532 [ 27] = HTTP_FLG_CTL,
533 [ 28] = HTTP_FLG_CTL,
534 [ 29] = HTTP_FLG_CTL,
535 [ 30] = HTTP_FLG_CTL,
536 [ 31] = HTTP_FLG_CTL,
537 [' '] = HTTP_FLG_SPHT | HTTP_FLG_LWS | HTTP_FLG_SEP,
538 ['!'] = HTTP_FLG_TOK,
539 ['"'] = HTTP_FLG_SEP,
540 ['#'] = HTTP_FLG_TOK,
541 ['$'] = HTTP_FLG_TOK,
542 ['%'] = HTTP_FLG_TOK,
543 ['&'] = HTTP_FLG_TOK,
544 [ 39] = HTTP_FLG_TOK,
545 ['('] = HTTP_FLG_SEP,
546 [')'] = HTTP_FLG_SEP,
547 ['*'] = HTTP_FLG_TOK,
548 ['+'] = HTTP_FLG_TOK,
549 [','] = HTTP_FLG_SEP,
550 ['-'] = HTTP_FLG_TOK,
551 ['.'] = HTTP_FLG_TOK | HTTP_FLG_VER,
552 ['/'] = HTTP_FLG_SEP | HTTP_FLG_VER,
553 ['0'] = HTTP_FLG_TOK | HTTP_FLG_VER,
554 ['1'] = HTTP_FLG_TOK | HTTP_FLG_VER,
555 ['2'] = HTTP_FLG_TOK | HTTP_FLG_VER,
556 ['3'] = HTTP_FLG_TOK | HTTP_FLG_VER,
557 ['4'] = HTTP_FLG_TOK | HTTP_FLG_VER,
558 ['5'] = HTTP_FLG_TOK | HTTP_FLG_VER,
559 ['6'] = HTTP_FLG_TOK | HTTP_FLG_VER,
560 ['7'] = HTTP_FLG_TOK | HTTP_FLG_VER,
561 ['8'] = HTTP_FLG_TOK | HTTP_FLG_VER,
562 ['9'] = HTTP_FLG_TOK | HTTP_FLG_VER,
563 [':'] = HTTP_FLG_SEP,
564 [';'] = HTTP_FLG_SEP,
565 ['<'] = HTTP_FLG_SEP,
566 ['='] = HTTP_FLG_SEP,
567 ['>'] = HTTP_FLG_SEP,
568 ['?'] = HTTP_FLG_SEP,
569 ['@'] = HTTP_FLG_SEP,
570 ['A'] = HTTP_FLG_TOK,
571 ['B'] = HTTP_FLG_TOK,
572 ['C'] = HTTP_FLG_TOK,
573 ['D'] = HTTP_FLG_TOK,
574 ['E'] = HTTP_FLG_TOK,
575 ['F'] = HTTP_FLG_TOK,
576 ['G'] = HTTP_FLG_TOK,
577 ['H'] = HTTP_FLG_TOK | HTTP_FLG_VER,
578 ['I'] = HTTP_FLG_TOK,
579 ['J'] = HTTP_FLG_TOK,
580 ['K'] = HTTP_FLG_TOK,
581 ['L'] = HTTP_FLG_TOK,
582 ['M'] = HTTP_FLG_TOK,
583 ['N'] = HTTP_FLG_TOK,
584 ['O'] = HTTP_FLG_TOK,
585 ['P'] = HTTP_FLG_TOK | HTTP_FLG_VER,
586 ['Q'] = HTTP_FLG_TOK,
587 ['R'] = HTTP_FLG_TOK | HTTP_FLG_VER,
588 ['S'] = HTTP_FLG_TOK | HTTP_FLG_VER,
589 ['T'] = HTTP_FLG_TOK | HTTP_FLG_VER,
590 ['U'] = HTTP_FLG_TOK,
591 ['V'] = HTTP_FLG_TOK,
592 ['W'] = HTTP_FLG_TOK,
593 ['X'] = HTTP_FLG_TOK,
594 ['Y'] = HTTP_FLG_TOK,
595 ['Z'] = HTTP_FLG_TOK,
596 ['['] = HTTP_FLG_SEP,
597 [ 92] = HTTP_FLG_SEP,
598 [']'] = HTTP_FLG_SEP,
599 ['^'] = HTTP_FLG_TOK,
600 ['_'] = HTTP_FLG_TOK,
601 ['`'] = HTTP_FLG_TOK,
602 ['a'] = HTTP_FLG_TOK,
603 ['b'] = HTTP_FLG_TOK,
604 ['c'] = HTTP_FLG_TOK,
605 ['d'] = HTTP_FLG_TOK,
606 ['e'] = HTTP_FLG_TOK,
607 ['f'] = HTTP_FLG_TOK,
608 ['g'] = HTTP_FLG_TOK,
609 ['h'] = HTTP_FLG_TOK,
610 ['i'] = HTTP_FLG_TOK,
611 ['j'] = HTTP_FLG_TOK,
612 ['k'] = HTTP_FLG_TOK,
613 ['l'] = HTTP_FLG_TOK,
614 ['m'] = HTTP_FLG_TOK,
615 ['n'] = HTTP_FLG_TOK,
616 ['o'] = HTTP_FLG_TOK,
617 ['p'] = HTTP_FLG_TOK,
618 ['q'] = HTTP_FLG_TOK,
619 ['r'] = HTTP_FLG_TOK,
620 ['s'] = HTTP_FLG_TOK,
621 ['t'] = HTTP_FLG_TOK,
622 ['u'] = HTTP_FLG_TOK,
623 ['v'] = HTTP_FLG_TOK,
624 ['w'] = HTTP_FLG_TOK,
625 ['x'] = HTTP_FLG_TOK,
626 ['y'] = HTTP_FLG_TOK,
627 ['z'] = HTTP_FLG_TOK,
628 ['{'] = HTTP_FLG_SEP,
629 ['|'] = HTTP_FLG_TOK,
630 ['}'] = HTTP_FLG_SEP,
631 ['~'] = HTTP_FLG_TOK,
632 [127] = HTTP_FLG_CTL,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100633};
634
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100635/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100636 * Adds a header and its CRLF at the tail of the message's buffer, just before
637 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100638 * The header is also automatically added to the index <hdr_idx>, and the end
639 * of headers is automatically adjusted. The number of bytes added is returned
640 * on success, otherwise <0 is returned indicating an error.
641 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100642int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100643{
644 int bytes, len;
645
646 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200647 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100648 if (!bytes)
649 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100650 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100651 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
652}
653
654/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100655 * Adds a header and its CRLF at the tail of the message's buffer, just before
656 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100657 * the buffer is only opened and the space reserved, but nothing is copied.
658 * The header is also automatically added to the index <hdr_idx>, and the end
659 * of headers is automatically adjusted. The number of bytes added is returned
660 * on success, otherwise <0 is returned indicating an error.
661 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100662int http_header_add_tail2(struct http_msg *msg,
663 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100664{
665 int bytes;
666
Willy Tarreau9b28e032012-10-12 23:49:43 +0200667 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100668 if (!bytes)
669 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100670 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100671 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
672}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200673
674/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100675 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
676 * If so, returns the position of the first non-space character relative to
677 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
678 * to return a pointer to the place after the first space. Returns 0 if the
679 * header name does not match. Checks are case-insensitive.
680 */
681int http_header_match2(const char *hdr, const char *end,
682 const char *name, int len)
683{
684 const char *val;
685
686 if (hdr + len >= end)
687 return 0;
688 if (hdr[len] != ':')
689 return 0;
690 if (strncasecmp(hdr, name, len) != 0)
691 return 0;
692 val = hdr + len + 1;
693 while (val < end && HTTP_IS_SPHT(*val))
694 val++;
695 if ((val >= end) && (len + 2 <= end - hdr))
696 return len + 2; /* we may replace starting from second space */
697 return val - hdr;
698}
699
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200700/* Find the first or next occurrence of header <name> in message buffer <sol>
701 * using headers index <idx>, and return it in the <ctx> structure. This
702 * structure holds everything necessary to use the header and find next
703 * occurrence. If its <idx> member is 0, the header is searched from the
704 * beginning. Otherwise, the next occurrence is returned. The function returns
705 * 1 when it finds a value, and 0 when there is no more. It is very similar to
706 * http_find_header2() except that it is designed to work with full-line headers
707 * whose comma is not a delimiter but is part of the syntax. As a special case,
708 * if ctx->val is NULL when searching for a new values of a header, the current
709 * header is rescanned. This allows rescanning after a header deletion.
710 */
711int http_find_full_header2(const char *name, int len,
712 char *sol, struct hdr_idx *idx,
713 struct hdr_ctx *ctx)
714{
715 char *eol, *sov;
716 int cur_idx, old_idx;
717
718 cur_idx = ctx->idx;
719 if (cur_idx) {
720 /* We have previously returned a header, let's search another one */
721 sol = ctx->line;
722 eol = sol + idx->v[cur_idx].len;
723 goto next_hdr;
724 }
725
726 /* first request for this header */
727 sol += hdr_idx_first_pos(idx);
728 old_idx = 0;
729 cur_idx = hdr_idx_first_idx(idx);
730 while (cur_idx) {
731 eol = sol + idx->v[cur_idx].len;
732
733 if (len == 0) {
734 /* No argument was passed, we want any header.
735 * To achieve this, we simply build a fake request. */
736 while (sol + len < eol && sol[len] != ':')
737 len++;
738 name = sol;
739 }
740
741 if ((len < eol - sol) &&
742 (sol[len] == ':') &&
743 (strncasecmp(sol, name, len) == 0)) {
744 ctx->del = len;
745 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100746 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200747 sov++;
748
749 ctx->line = sol;
750 ctx->prev = old_idx;
751 ctx->idx = cur_idx;
752 ctx->val = sov - sol;
753 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100754 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200755 eol--;
756 ctx->tws++;
757 }
758 ctx->vlen = eol - sov;
759 return 1;
760 }
761 next_hdr:
762 sol = eol + idx->v[cur_idx].cr + 1;
763 old_idx = cur_idx;
764 cur_idx = idx->v[cur_idx].next;
765 }
766 return 0;
767}
768
Willy Tarreauc90dc232015-02-20 13:51:36 +0100769/* Find the first or next header field in message buffer <sol> using headers
770 * index <idx>, and return it in the <ctx> structure. This structure holds
771 * everything necessary to use the header and find next occurrence. If its
772 * <idx> member is 0, the first header is retrieved. Otherwise, the next
773 * occurrence is returned. The function returns 1 when it finds a value, and
774 * 0 when there is no more. It is equivalent to http_find_full_header2() with
775 * no header name.
776 */
777int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx)
778{
779 char *eol, *sov;
780 int cur_idx, old_idx;
781 int len;
782
783 cur_idx = ctx->idx;
784 if (cur_idx) {
785 /* We have previously returned a header, let's search another one */
786 sol = ctx->line;
787 eol = sol + idx->v[cur_idx].len;
788 goto next_hdr;
789 }
790
791 /* first request for this header */
792 sol += hdr_idx_first_pos(idx);
793 old_idx = 0;
794 cur_idx = hdr_idx_first_idx(idx);
795 while (cur_idx) {
796 eol = sol + idx->v[cur_idx].len;
797
798 len = 0;
799 while (1) {
800 if (len >= eol - sol)
801 goto next_hdr;
802 if (sol[len] == ':')
803 break;
804 len++;
805 }
806
807 ctx->del = len;
808 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100809 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreauc90dc232015-02-20 13:51:36 +0100810 sov++;
811
812 ctx->line = sol;
813 ctx->prev = old_idx;
814 ctx->idx = cur_idx;
815 ctx->val = sov - sol;
816 ctx->tws = 0;
817
Willy Tarreau2235b262016-11-05 15:50:20 +0100818 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreauc90dc232015-02-20 13:51:36 +0100819 eol--;
820 ctx->tws++;
821 }
822 ctx->vlen = eol - sov;
823 return 1;
824
825 next_hdr:
826 sol = eol + idx->v[cur_idx].cr + 1;
827 old_idx = cur_idx;
828 cur_idx = idx->v[cur_idx].next;
829 }
830 return 0;
831}
832
Willy Tarreau68085d82010-01-18 14:54:04 +0100833/* Find the end of the header value contained between <s> and <e>. See RFC2616,
834 * par 2.2 for more information. Note that it requires a valid header to return
835 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200836 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100837char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200838{
839 int quoted, qdpair;
840
841 quoted = qdpair = 0;
Willy Tarreaue6d9c212016-11-05 18:23:38 +0100842
843#if defined(__x86_64__) || \
844 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
845 defined(__ARM_ARCH_7A__)
846 /* speedup: skip everything not a comma nor a double quote */
847 for (; s <= e - sizeof(int); s += sizeof(int)) {
848 unsigned int c = *(int *)s; // comma
849 unsigned int q = c; // quote
850
851 c ^= 0x2c2c2c2c; // contains one zero on a comma
852 q ^= 0x22222222; // contains one zero on a quote
853
854 c = (c - 0x01010101) & ~c; // contains 0x80 below a comma
855 q = (q - 0x01010101) & ~q; // contains 0x80 below a quote
856
857 if ((c | q) & 0x80808080)
858 break; // found a comma or a quote
859 }
860#endif
Willy Tarreau33a7e692007-06-10 19:45:56 +0200861 for (; s < e; s++) {
862 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200863 else if (quoted) {
864 if (*s == '\\') qdpair = 1;
865 else if (*s == '"') quoted = 0;
866 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200867 else if (*s == '"') quoted = 1;
868 else if (*s == ',') return s;
869 }
870 return s;
871}
872
873/* Find the first or next occurrence of header <name> in message buffer <sol>
874 * using headers index <idx>, and return it in the <ctx> structure. This
875 * structure holds everything necessary to use the header and find next
876 * occurrence. If its <idx> member is 0, the header is searched from the
877 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100878 * 1 when it finds a value, and 0 when there is no more. It is designed to work
879 * with headers defined as comma-separated lists. As a special case, if ctx->val
880 * is NULL when searching for a new values of a header, the current header is
881 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200882 */
883int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100884 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200885 struct hdr_ctx *ctx)
886{
Willy Tarreau68085d82010-01-18 14:54:04 +0100887 char *eol, *sov;
888 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200889
Willy Tarreau68085d82010-01-18 14:54:04 +0100890 cur_idx = ctx->idx;
891 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200892 /* We have previously returned a value, let's search
893 * another one on the same line.
894 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200895 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200896 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100897 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200898 eol = sol + idx->v[cur_idx].len;
899
900 if (sov >= eol)
901 /* no more values in this header */
902 goto next_hdr;
903
Willy Tarreau68085d82010-01-18 14:54:04 +0100904 /* values remaining for this header, skip the comma but save it
905 * for later use (eg: for header deletion).
906 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200907 sov++;
Willy Tarreau2235b262016-11-05 15:50:20 +0100908 while (sov < eol && HTTP_IS_LWS((*sov)))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200909 sov++;
910
911 goto return_hdr;
912 }
913
914 /* first request for this header */
915 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100916 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200917 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200918 while (cur_idx) {
919 eol = sol + idx->v[cur_idx].len;
920
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200921 if (len == 0) {
922 /* No argument was passed, we want any header.
923 * To achieve this, we simply build a fake request. */
924 while (sol + len < eol && sol[len] != ':')
925 len++;
926 name = sol;
927 }
928
Willy Tarreau33a7e692007-06-10 19:45:56 +0200929 if ((len < eol - sol) &&
930 (sol[len] == ':') &&
931 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100932 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200933 sov = sol + len + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +0100934 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau33a7e692007-06-10 19:45:56 +0200935 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100936
Willy Tarreau33a7e692007-06-10 19:45:56 +0200937 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100938 ctx->prev = old_idx;
939 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200940 ctx->idx = cur_idx;
941 ctx->val = sov - sol;
942
943 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200944 ctx->tws = 0;
Willy Tarreau2235b262016-11-05 15:50:20 +0100945 while (eol > sov && HTTP_IS_LWS(*(eol - 1))) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200946 eol--;
947 ctx->tws++;
948 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200949 ctx->vlen = eol - sov;
950 return 1;
951 }
952 next_hdr:
953 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100954 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200955 cur_idx = idx->v[cur_idx].next;
956 }
957 return 0;
958}
959
960int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100961 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200962 struct hdr_ctx *ctx)
963{
964 return http_find_header2(name, strlen(name), sol, idx, ctx);
965}
966
Willy Tarreau68085d82010-01-18 14:54:04 +0100967/* Remove one value of a header. This only works on a <ctx> returned by one of
968 * the http_find_header functions. The value is removed, as well as surrounding
969 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100970 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100971 * message <msg>. The new index is returned. If it is zero, it means there is
972 * no more header, so any processing may stop. The ctx is always left in a form
973 * that can be handled by http_find_header2() to find next occurrence.
974 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100975int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100976{
977 int cur_idx = ctx->idx;
978 char *sol = ctx->line;
979 struct hdr_idx_elem *hdr;
980 int delta, skip_comma;
981
982 if (!cur_idx)
983 return 0;
984
985 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200986 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100987 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200988 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100989 http_msg_move_end(msg, delta);
990 idx->used--;
991 hdr->len = 0; /* unused entry */
992 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100993 if (idx->tail == ctx->idx)
994 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100995 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau7c1c2172015-01-07 17:23:50 +0100996 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100997 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200998 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100999 return ctx->idx;
1000 }
1001
1002 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001003 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
1004 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +01001005 */
1006
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001007 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001008 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001009 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +01001010 NULL, 0);
1011 hdr->len += delta;
1012 http_msg_move_end(msg, delta);
1013 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +02001014 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +01001015 return ctx->idx;
1016}
1017
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001018/* This function handles a server error at the stream interface level. The
1019 * stream interface is assumed to be already in a closed state. An optional
1020 * message is copied into the input buffer, and an HTTP status code stored.
1021 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +01001022 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001023 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001024static void http_server_error(struct stream *s, struct stream_interface *si,
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001025 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001026{
Christopher Faulet3e344292015-11-24 16:24:13 +01001027 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001028 channel_auto_read(si_oc(si));
1029 channel_abort(si_oc(si));
1030 channel_auto_close(si_oc(si));
1031 channel_erase(si_oc(si));
1032 channel_auto_close(si_ic(si));
1033 channel_auto_read(si_ic(si));
Willy Tarreau0f772532006-12-23 20:51:41 +01001034 if (status > 0 && msg) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02001035 s->txn->status = status;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01001036 bo_inject(si_ic(si), msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001037 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02001038 if (!(s->flags & SF_ERR_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001039 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001040 if (!(s->flags & SF_FINST_MASK))
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02001041 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001042}
1043
Willy Tarreau87b09662015-04-03 00:22:06 +02001044/* This function returns the appropriate error location for the given stream
Willy Tarreau80587432006-12-24 17:47:20 +01001045 * and message.
1046 */
1047
Willy Tarreau87b09662015-04-03 00:22:06 +02001048struct chunk *http_error_message(struct stream *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +01001049{
Willy Tarreaue2e27a52007-04-01 00:01:37 +02001050 if (s->be->errmsg[msgnum].str)
1051 return &s->be->errmsg[msgnum];
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001052 else if (strm_fe(s)->errmsg[msgnum].str)
1053 return &strm_fe(s)->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +01001054 else
1055 return &http_err_chunks[msgnum];
1056}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001057
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001058void
1059http_reply_and_close(struct stream *s, short status, struct chunk *msg)
1060{
Christopher Fauletd7c91962015-04-30 11:48:27 +02001061 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Faulet3e344292015-11-24 16:24:13 +01001062 FLT_STRM_CB(s, flt_http_reply(s, status, msg));
Christopher Fauleta94e5a52015-12-09 15:55:06 +01001063 stream_int_retnclose(&s->si[0], msg);
1064}
1065
Willy Tarreau53b6c742006-12-17 13:37:46 +01001066/*
Willy Tarreaub7ce4242015-09-03 17:15:21 +02001067 * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown
1068 * ones.
Willy Tarreau53b6c742006-12-17 13:37:46 +01001069 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +01001070enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +01001071{
1072 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001073 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001074
1075 m = ((unsigned)*str - 'A');
1076
1077 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001078 for (h = http_methods[m]; h->len > 0; h++) {
1079 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +01001080 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001081 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +01001082 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001083 };
Willy Tarreau53b6c742006-12-17 13:37:46 +01001084 }
Willy Tarreaub7ce4242015-09-03 17:15:21 +02001085 return HTTP_METH_OTHER;
Willy Tarreau53b6c742006-12-17 13:37:46 +01001086}
1087
Willy Tarreau21d2af32008-02-14 20:25:24 +01001088/* Parse the URI from the given transaction (which is assumed to be in request
1089 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
1090 * It is returned otherwise.
1091 */
Thierry FOURNIER3c331782015-09-17 19:33:35 +02001092char *http_get_path(struct http_txn *txn)
Willy Tarreau21d2af32008-02-14 20:25:24 +01001093{
1094 char *ptr, *end;
1095
Willy Tarreau9b28e032012-10-12 23:49:43 +02001096 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +01001097 end = ptr + txn->req.sl.rq.u_l;
1098
1099 if (ptr >= end)
1100 return NULL;
1101
1102 /* RFC2616, par. 5.1.2 :
1103 * Request-URI = "*" | absuri | abspath | authority
1104 */
1105
1106 if (*ptr == '*')
1107 return NULL;
1108
1109 if (isalpha((unsigned char)*ptr)) {
1110 /* this is a scheme as described by RFC3986, par. 3.1 */
1111 ptr++;
1112 while (ptr < end &&
1113 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
1114 ptr++;
1115 /* skip '://' */
1116 if (ptr == end || *ptr++ != ':')
1117 return NULL;
1118 if (ptr == end || *ptr++ != '/')
1119 return NULL;
1120 if (ptr == end || *ptr++ != '/')
1121 return NULL;
1122 }
1123 /* skip [user[:passwd]@]host[:[port]] */
1124
1125 while (ptr < end && *ptr != '/')
1126 ptr++;
1127
1128 if (ptr == end)
1129 return NULL;
1130
1131 /* OK, we got the '/' ! */
1132 return ptr;
1133}
1134
William Lallemand65ad6e12014-01-31 15:08:02 +01001135/* Parse the URI from the given string and look for the "/" beginning the PATH.
1136 * If not found, return NULL. It is returned otherwise.
1137 */
1138static char *
1139http_get_path_from_string(char *str)
1140{
1141 char *ptr = str;
1142
1143 /* RFC2616, par. 5.1.2 :
1144 * Request-URI = "*" | absuri | abspath | authority
1145 */
1146
1147 if (*ptr == '*')
1148 return NULL;
1149
1150 if (isalpha((unsigned char)*ptr)) {
1151 /* this is a scheme as described by RFC3986, par. 3.1 */
1152 ptr++;
1153 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
1154 ptr++;
1155 /* skip '://' */
1156 if (*ptr == '\0' || *ptr++ != ':')
1157 return NULL;
1158 if (*ptr == '\0' || *ptr++ != '/')
1159 return NULL;
1160 if (*ptr == '\0' || *ptr++ != '/')
1161 return NULL;
1162 }
1163 /* skip [user[:passwd]@]host[:[port]] */
1164
1165 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
1166 ptr++;
1167
1168 if (*ptr == '\0' || *ptr == ' ')
1169 return NULL;
1170
1171 /* OK, we got the '/' ! */
1172 return ptr;
1173}
1174
Willy Tarreau71241ab2012-12-27 11:30:54 +01001175/* Returns a 302 for a redirectable request that reaches a server working in
1176 * in redirect mode. This may only be called just after the stream interface
1177 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
1178 * follow normal proxy processing. NOTE: this function is designed to support
1179 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001180 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001181void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001182{
1183 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01001184 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001185 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001186 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001187
1188 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001189 trash.len = strlen(HTTP_302);
1190 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001191
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001192 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001193
Willy Tarreauefb453c2008-10-26 20:49:47 +01001194 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001195 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001196 return;
1197
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001198 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +01001199 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001200 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
1201 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +01001202 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001203
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001204 /* 3: add the request URI. Since it was already forwarded, we need
1205 * to temporarily rewind the buffer.
1206 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001207 txn = s->txn;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001208 b_rew(s->req.buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001209
Willy Tarreauefb453c2008-10-26 20:49:47 +01001210 path = http_get_path(txn);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001211 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 +02001212
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001213 b_adv(s->req.buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +02001214
Willy Tarreauefb453c2008-10-26 20:49:47 +01001215 if (!path)
1216 return;
1217
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001218 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +01001219 return;
1220
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001221 memcpy(trash.str + trash.len, path, len);
1222 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001223
1224 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001225 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
1226 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001227 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001228 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
1229 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001230 }
Willy Tarreauefb453c2008-10-26 20:49:47 +01001231
1232 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001233 si_shutr(si);
1234 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001235 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001236 si->state = SI_ST_CLO;
1237
1238 /* send the message */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001239 http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001240
1241 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +01001242 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001243 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +01001244}
1245
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001246/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +01001247 * that the server side is closed. Note that err_type is actually a
1248 * bitmask, where almost only aborts may be cumulated with other
1249 * values. We consider that aborted operations are more important
1250 * than timeouts or errors due to the fact that nobody else in the
1251 * logs might explain incomplete retries. All others should avoid
1252 * being cumulated. It should normally not be possible to have multiple
1253 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001254 * Note that connection errors appearing on the second request of a keep-alive
1255 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +01001256 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001257void http_return_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +01001258{
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001259 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +01001260
1261 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001262 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001263 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001264 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001265 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001266 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001267 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001268 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001269 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001270 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001271 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001272 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001273 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001274 else if (err_type & SI_ET_CONN_TO)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001275 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001276 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001277 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001278 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001279 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
1280 503, (s->flags & SF_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001281 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001282 else if (err_type & SI_ET_CONN_RES)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001283 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
Willy Tarreaueee5b512015-04-03 23:46:31 +02001284 503, (s->txn->flags & TX_NOT_FIRST) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001285 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001286 else /* SI_ET_CONN_OTHER and others */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001287 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001288 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001289}
1290
Willy Tarreau42250582007-04-01 01:30:43 +02001291extern const char sess_term_cond[8];
1292extern const char sess_fin_state[8];
1293extern const char *monthname[12];
Willy Tarreau63986c72015-04-03 22:55:33 +02001294struct pool_head *pool2_http_txn;
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001295struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001296struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001297struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001298
Willy Tarreau117f59e2007-03-04 18:17:17 +01001299/*
1300 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001301 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001302 */
1303void capture_headers(char *som, struct hdr_idx *idx,
1304 char **cap, struct cap_hdr *cap_hdr)
1305{
1306 char *eol, *sol, *col, *sov;
1307 int cur_idx;
1308 struct cap_hdr *h;
1309 int len;
1310
1311 sol = som + hdr_idx_first_pos(idx);
1312 cur_idx = hdr_idx_first_idx(idx);
1313
1314 while (cur_idx) {
1315 eol = sol + idx->v[cur_idx].len;
1316
1317 col = sol;
1318 while (col < eol && *col != ':')
1319 col++;
1320
1321 sov = col + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01001322 while (sov < eol && HTTP_IS_LWS(*sov))
Willy Tarreau117f59e2007-03-04 18:17:17 +01001323 sov++;
1324
1325 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001326 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001327 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1328 if (cap[h->index] == NULL)
1329 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001330 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001331
1332 if (cap[h->index] == NULL) {
1333 Alert("HTTP capture : out of memory.\n");
1334 continue;
1335 }
1336
1337 len = eol - sov;
1338 if (len > h->len)
1339 len = h->len;
1340
1341 memcpy(cap[h->index], sov, len);
1342 cap[h->index][len]=0;
1343 }
1344 }
1345 sol = eol + idx->v[cur_idx].cr + 1;
1346 cur_idx = idx->v[cur_idx].next;
1347 }
1348}
1349
1350
Willy Tarreau42250582007-04-01 01:30:43 +02001351/* either we find an LF at <ptr> or we jump to <bad>.
1352 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001353#define EXPECT_LF_HERE(ptr, bad, st) do { if (unlikely(*(ptr) != '\n')) { state = st; goto bad;}; } while (0)
Willy Tarreau42250582007-04-01 01:30:43 +02001354
1355/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1356 * otherwise to <http_msg_ood> with <state> set to <st>.
1357 */
1358#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1359 ptr++; \
1360 if (likely(ptr < end)) \
1361 goto good; \
1362 else { \
1363 state = (st); \
1364 goto http_msg_ood; \
1365 } \
1366 } while (0)
1367
1368
Willy Tarreaubaaee002006-06-26 02:48:02 +02001369/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001370 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001371 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1372 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1373 * will give undefined results.
1374 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1375 * and that msg->sol points to the beginning of the response.
1376 * If a complete line is found (which implies that at least one CR or LF is
1377 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1378 * returned indicating an incomplete line (which does not mean that parts have
1379 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1380 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1381 * upon next call.
1382 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001383 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001384 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1385 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001386 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001387 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001388const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001389 enum ht_state state, const char *ptr, const char *end,
1390 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001391{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001392 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001393
Willy Tarreau8973c702007-01-21 23:58:29 +01001394 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001395 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001396 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001397 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001398 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1399
1400 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001401 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001402 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1403 }
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001404 msg->err_state = HTTP_MSG_RPVER;
Willy Tarreau7552c032009-03-01 11:10:40 +01001405 state = HTTP_MSG_ERROR;
1406 break;
1407
Willy Tarreau8973c702007-01-21 23:58:29 +01001408 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001409 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001410 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001411 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001412 goto http_msg_rpcode;
1413 }
1414 if (likely(HTTP_IS_SPHT(*ptr)))
1415 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1416 /* so it's a CR/LF, this is invalid */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001417 msg->err_state = HTTP_MSG_RPVER_SP;
Willy Tarreau7552c032009-03-01 11:10:40 +01001418 state = HTTP_MSG_ERROR;
1419 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001420
Willy Tarreau8973c702007-01-21 23:58:29 +01001421 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001422 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001423 if (likely(!HTTP_IS_LWS(*ptr)))
1424 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1425
1426 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001427 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001428 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1429 }
1430
1431 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001432 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001433 http_msg_rsp_reason:
1434 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001435 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001436 msg->sl.st.r_l = 0;
1437 goto http_msg_rpline_eol;
1438
Willy Tarreau8973c702007-01-21 23:58:29 +01001439 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001440 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001441 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001442 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001443 goto http_msg_rpreason;
1444 }
1445 if (likely(HTTP_IS_SPHT(*ptr)))
1446 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1447 /* so it's a CR/LF, so there is no reason phrase */
1448 goto http_msg_rsp_reason;
1449
Willy Tarreau8973c702007-01-21 23:58:29 +01001450 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001451 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001452 if (likely(!HTTP_IS_CRLF(*ptr)))
1453 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001454 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001455 http_msg_rpline_eol:
1456 /* We have seen the end of line. Note that we do not
1457 * necessarily have the \n yet, but at least we know that we
1458 * have EITHER \r OR \n, otherwise the response would not be
1459 * complete. We can then record the response length and return
1460 * to the caller which will be able to register it.
1461 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001462 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001463 return ptr;
1464
Willy Tarreau8973c702007-01-21 23:58:29 +01001465 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001466#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001467 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1468 exit(1);
1469#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001470 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001471 }
1472
1473 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001474 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001475 if (ret_state)
1476 *ret_state = state;
1477 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001478 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001479 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001480}
1481
Willy Tarreau8973c702007-01-21 23:58:29 +01001482/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001483 * This function parses a request line between <ptr> and <end>, starting with
1484 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1485 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1486 * will give undefined results.
1487 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1488 * and that msg->sol points to the beginning of the request.
1489 * If a complete line is found (which implies that at least one CR or LF is
1490 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1491 * returned indicating an incomplete line (which does not mean that parts have
1492 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1493 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1494 * upon next call.
1495 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001496 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001497 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1498 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001499 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001500 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001501const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001502 enum ht_state state, const char *ptr, const char *end,
1503 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001504{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001505 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001506
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001507 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001508 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001509 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001510 if (likely(HTTP_IS_TOKEN(*ptr)))
1511 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001512
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001513 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001514 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001515 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1516 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001517
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001518 if (likely(HTTP_IS_CRLF(*ptr))) {
1519 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001520 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001521 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001522 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001523 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001524 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001525 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001526 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001527 msg->sl.rq.v_l = 0;
1528 goto http_msg_rqline_eol;
1529 }
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001530 msg->err_state = HTTP_MSG_RQMETH;
Willy Tarreau7552c032009-03-01 11:10:40 +01001531 state = HTTP_MSG_ERROR;
1532 break;
1533
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001534 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001535 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001536 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001537 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001538 goto http_msg_rquri;
1539 }
1540 if (likely(HTTP_IS_SPHT(*ptr)))
1541 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1542 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1543 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001544
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001545 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001546 http_msg_rquri:
Willy Tarreau5f10ea32016-11-05 17:52:06 +01001547#if defined(__x86_64__) || \
1548 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
1549 defined(__ARM_ARCH_7A__)
1550 /* speedup: skip bytes not between 0x21 and 0x7e inclusive */
1551 while (ptr <= end - sizeof(int)) {
1552 int x = *(int *)ptr - 0x21212121;
1553 if (x & 0x80808080)
1554 break;
1555
1556 x -= 0x5e5e5e5e;
1557 if (!(x & 0x80808080))
1558 break;
1559
1560 ptr += sizeof(int);
1561 }
1562#endif
Willy Tarreau2afff9c2017-01-04 14:44:46 +01001563 if (ptr >= end) {
1564 state = HTTP_MSG_RQURI;
1565 goto http_msg_ood;
1566 }
Willy Tarreau5f10ea32016-11-05 17:52:06 +01001567 http_msg_rquri2:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001568 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau5f10ea32016-11-05 17:52:06 +01001569 EAT_AND_JUMP_OR_RETURN(http_msg_rquri2, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001570
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001571 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001572 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001573 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1574 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001575
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001576 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001577 /* non-ASCII chars are forbidden unless option
1578 * accept-invalid-http-request is enabled in the frontend.
1579 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001580 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001581 if (msg->err_pos < -1)
1582 goto invalid_char;
1583 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001584 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001585 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1586 }
1587
1588 if (likely(HTTP_IS_CRLF(*ptr))) {
1589 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1590 goto http_msg_req09_uri_e;
1591 }
1592
1593 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001594 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001595 msg->err_pos = ptr - msg_start;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001596 msg->err_state = HTTP_MSG_RQURI;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001597 state = HTTP_MSG_ERROR;
1598 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001599
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001600 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001601 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001602 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001603 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001604 goto http_msg_rqver;
1605 }
1606 if (likely(HTTP_IS_SPHT(*ptr)))
1607 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1608 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1609 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001610
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001611 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001612 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001613 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001614 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001615
1616 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001617 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001618 http_msg_rqline_eol:
1619 /* We have seen the end of line. Note that we do not
1620 * necessarily have the \n yet, but at least we know that we
1621 * have EITHER \r OR \n, otherwise the request would not be
1622 * complete. We can then record the request length and return
1623 * to the caller which will be able to register it.
1624 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001625 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001626 return ptr;
1627 }
1628
1629 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001630 msg->err_state = HTTP_MSG_RQVER;
Willy Tarreau7552c032009-03-01 11:10:40 +01001631 state = HTTP_MSG_ERROR;
1632 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001633
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001634 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001635#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001636 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1637 exit(1);
1638#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001639 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001640 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001641
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001642 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001643 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001644 if (ret_state)
1645 *ret_state = state;
1646 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001647 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001648 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001649}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001650
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001651/*
1652 * Returns the data from Authorization header. Function may be called more
1653 * than once so data is stored in txn->auth_data. When no header is found
1654 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001655 * searching again for something we are unable to find anyway. However, if
1656 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +02001657 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001658 */
1659
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001660/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1661 * set according to global.tune.bufsize.
1662 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001663char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001664
1665int
Willy Tarreau87b09662015-04-03 00:22:06 +02001666get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001667{
1668
Willy Tarreaueee5b512015-04-03 23:46:31 +02001669 struct http_txn *txn = s->txn;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001670 struct chunk auth_method;
1671 struct hdr_ctx ctx;
1672 char *h, *p;
1673 int len;
1674
1675#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +02001676 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001677#endif
1678
1679 if (txn->auth.method == HTTP_AUTH_WRONG)
1680 return 0;
1681
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001682 txn->auth.method = HTTP_AUTH_WRONG;
1683
1684 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001685
1686 if (txn->flags & TX_USE_PX_CONN) {
1687 h = "Proxy-Authorization";
1688 len = strlen(h);
1689 } else {
1690 h = "Authorization";
1691 len = strlen(h);
1692 }
1693
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001694 if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001695 return 0;
1696
1697 h = ctx.line + ctx.val;
1698
1699 p = memchr(h, ' ', ctx.vlen);
Willy Tarreau5c557d12016-03-13 08:17:02 +01001700 len = p - h;
1701 if (!p || len <= 0)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001702 return 0;
1703
David Carlier7365f7d2016-04-04 11:54:42 +01001704 if (chunk_initlen(&auth_method, h, 0, len) != 1)
1705 return 0;
1706
Willy Tarreau5c557d12016-03-13 08:17:02 +01001707 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001708
1709 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1710
1711 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001712 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001713
1714 if (len < 0)
1715 return 0;
1716
1717
1718 get_http_auth_buff[len] = '\0';
1719
1720 p = strchr(get_http_auth_buff, ':');
1721
1722 if (!p)
1723 return 0;
1724
1725 txn->auth.user = get_http_auth_buff;
1726 *p = '\0';
1727 txn->auth.pass = p+1;
1728
1729 txn->auth.method = HTTP_AUTH_BASIC;
1730 return 1;
1731 }
1732
1733 return 0;
1734}
1735
Willy Tarreau58f10d72006-12-04 02:26:12 +01001736
Willy Tarreau8973c702007-01-21 23:58:29 +01001737/*
1738 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001739 * depending on the initial msg->msg_state. The caller is responsible for
1740 * ensuring that the message does not wrap. The function can be preempted
1741 * everywhere when data are missing and recalled at the exact same location
1742 * with no information loss. The message may even be realigned between two
1743 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001744 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001745 * fields. Note that msg->sol will be initialized after completing the first
1746 * state, so that none of the msg pointers has to be initialized prior to the
1747 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001748 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001749void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001750{
Willy Tarreau3770f232013-12-07 00:01:53 +01001751 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001752 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001753 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001754
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001755 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001756 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001757 ptr = buf->p + msg->next;
1758 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001759
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001760 if (unlikely(ptr >= end))
1761 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001762
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001763 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001764 /*
1765 * First, states that are specific to the response only.
1766 * We check them first so that request and headers are
1767 * closer to each other (accessed more often).
1768 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001769 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001770 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001771 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001772 /* we have a start of message, but we have to check
1773 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001774 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001775 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001776 if (unlikely(ptr != buf->p)) {
1777 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001778 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001779 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001780 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001781 }
Willy Tarreau26927362012-05-18 23:22:52 +02001782 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001783 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001784 hdr_idx_init(idx);
1785 state = HTTP_MSG_RPVER;
1786 goto http_msg_rpver;
1787 }
1788
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001789 if (unlikely(!HTTP_IS_CRLF(*ptr))) {
1790 state = HTTP_MSG_RPBEFORE;
Willy Tarreau8973c702007-01-21 23:58:29 +01001791 goto http_msg_invalid;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001792 }
Willy Tarreau8973c702007-01-21 23:58:29 +01001793
1794 if (unlikely(*ptr == '\n'))
1795 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1796 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1797 /* stop here */
1798
Willy Tarreau8973c702007-01-21 23:58:29 +01001799 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001800 http_msg_rpbefore_cr:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001801 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_RPBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001802 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1803 /* stop here */
1804
Willy Tarreau8973c702007-01-21 23:58:29 +01001805 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001806 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001807 case HTTP_MSG_RPVER_SP:
1808 case HTTP_MSG_RPCODE:
1809 case HTTP_MSG_RPCODE_SP:
1810 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001811 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001812 state, ptr, end,
1813 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001814 if (unlikely(!ptr))
1815 return;
1816
1817 /* we have a full response and we know that we have either a CR
1818 * or an LF at <ptr>.
1819 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001820 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1821
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001822 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001823 if (likely(*ptr == '\r'))
1824 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1825 goto http_msg_rpline_end;
1826
Willy Tarreau8973c702007-01-21 23:58:29 +01001827 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001828 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001829 /* msg->sol must point to the first of CR or LF. */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001830 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_RPLINE_END);
Willy Tarreau8973c702007-01-21 23:58:29 +01001831 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1832 /* stop here */
1833
1834 /*
1835 * Second, states that are specific to the request only
1836 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001837 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001838 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001839 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001840 /* we have a start of message, but we have to check
1841 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001842 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001843 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001844 if (likely(ptr != buf->p)) {
1845 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001846 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001847 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001848 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001849 }
Willy Tarreau26927362012-05-18 23:22:52 +02001850 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001851 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001852 state = HTTP_MSG_RQMETH;
1853 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001854 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001855
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001856 if (unlikely(!HTTP_IS_CRLF(*ptr))) {
1857 state = HTTP_MSG_RQBEFORE;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001858 goto http_msg_invalid;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001859 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001860
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001861 if (unlikely(*ptr == '\n'))
1862 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1863 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001864 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001865
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001866 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001867 http_msg_rqbefore_cr:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001868 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001869 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001870 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001871
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001872 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001873 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001874 case HTTP_MSG_RQMETH_SP:
1875 case HTTP_MSG_RQURI:
1876 case HTTP_MSG_RQURI_SP:
1877 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001878 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001879 state, ptr, end,
1880 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001881 if (unlikely(!ptr))
1882 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001883
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001884 /* we have a full request and we know that we have either a CR
1885 * or an LF at <ptr>.
1886 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001887 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001888
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001889 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001890 if (likely(*ptr == '\r'))
1891 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001892 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001893
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001894 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001895 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001896 /* check for HTTP/0.9 request : no version information available.
1897 * msg->sol must point to the first of CR or LF.
1898 */
1899 if (unlikely(msg->sl.rq.v_l == 0))
1900 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001901
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001902 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001903 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001904 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001905
Willy Tarreau8973c702007-01-21 23:58:29 +01001906 /*
1907 * Common states below
1908 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001909 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001910 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001911 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001912 if (likely(!HTTP_IS_CRLF(*ptr))) {
1913 goto http_msg_hdr_name;
1914 }
1915
1916 if (likely(*ptr == '\r'))
1917 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1918 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001919
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001920 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001921 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001922 /* assumes msg->sol points to the first char */
1923 if (likely(HTTP_IS_TOKEN(*ptr)))
1924 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001925
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001926 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001927 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001928
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001929 if (likely(msg->err_pos < -1) || *ptr == '\n') {
1930 state = HTTP_MSG_HDR_NAME;
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001931 goto http_msg_invalid;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001932 }
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001933
1934 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001935 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001936
1937 /* and we still accept this non-token character */
1938 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001939
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001940 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001941 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001942 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001943 if (likely(HTTP_IS_SPHT(*ptr)))
1944 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001945
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001946 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001947 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001948
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001949 if (likely(!HTTP_IS_CRLF(*ptr))) {
1950 goto http_msg_hdr_val;
1951 }
1952
1953 if (likely(*ptr == '\r'))
1954 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1955 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001956
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001957 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001958 http_msg_hdr_l1_lf:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001959 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_HDR_L1_LF);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001960 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001961
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001962 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001963 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001964 if (likely(HTTP_IS_SPHT(*ptr))) {
1965 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001966 for (; buf->p + msg->sov < ptr; msg->sov++)
1967 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001968 goto http_msg_hdr_l1_sp;
1969 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001970 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001971 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001972 goto http_msg_complete_header;
1973
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001974 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001975 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001976 /* assumes msg->sol points to the first char, and msg->sov
1977 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001978 */
Willy Tarreau0431f9d2016-11-05 17:35:40 +01001979
1980 /* speedup: we'll skip packs of 4 or 8 bytes not containing bytes 0x0D
1981 * and lower. In fact since most of the time is spent in the loop, we
1982 * also remove the sign bit test so that bytes 0x8e..0x0d break the
1983 * loop, but we don't care since they're very rare in header values.
1984 */
1985#if defined(__x86_64__)
1986 while (ptr <= end - sizeof(long)) {
1987 if ((*(long *)ptr - 0x0e0e0e0e0e0e0e0eULL) & 0x8080808080808080ULL)
1988 goto http_msg_hdr_val2;
1989 ptr += sizeof(long);
1990 }
1991#endif
1992#if defined(__x86_64__) || \
1993 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
1994 defined(__ARM_ARCH_7A__)
1995 while (ptr <= end - sizeof(int)) {
1996 if ((*(int*)ptr - 0x0e0e0e0e) & 0x80808080)
1997 goto http_msg_hdr_val2;
1998 ptr += sizeof(int);
1999 }
2000#endif
Willy Tarreau2afff9c2017-01-04 14:44:46 +01002001 if (ptr >= end) {
2002 state = HTTP_MSG_HDR_VAL;
2003 goto http_msg_ood;
2004 }
Willy Tarreau0431f9d2016-11-05 17:35:40 +01002005 http_msg_hdr_val2:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002006 if (likely(!HTTP_IS_CRLF(*ptr)))
Willy Tarreau0431f9d2016-11-05 17:35:40 +01002007 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val2, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002008
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002009 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002010 /* Note: we could also copy eol into ->eoh so that we have the
2011 * real header end in case it ends with lots of LWS, but is this
2012 * really needed ?
2013 */
2014 if (likely(*ptr == '\r'))
2015 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
2016 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002017
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002018 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02002019 http_msg_hdr_l2_lf:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002020 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_HDR_L2_LF);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002021 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002022
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002023 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02002024 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002025 if (unlikely(HTTP_IS_SPHT(*ptr))) {
2026 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002027 for (; buf->p + msg->eol < ptr; msg->eol++)
2028 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002029 goto http_msg_hdr_val;
2030 }
2031 http_msg_complete_header:
2032 /*
2033 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002034 * Assumes msg->sol points to the first char, msg->sov points
2035 * to the first character of the value and msg->eol to the
2036 * first CR or LF so we know how the line ends. We insert last
2037 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002038 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002039 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002040 idx, idx->tail) < 0)) {
2041 state = HTTP_MSG_HDR_L2_LWS;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002042 goto http_msg_invalid;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002043 }
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002044
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002045 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002046 if (likely(!HTTP_IS_CRLF(*ptr))) {
2047 goto http_msg_hdr_name;
2048 }
2049
2050 if (likely(*ptr == '\r'))
2051 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
2052 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002053
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002054 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02002055 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01002056 /* Assumes msg->sol points to the first of either CR or LF.
2057 * Sets ->sov and ->next to the total header length, ->eoh to
2058 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
2059 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002060 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_LAST_LF);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002061 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002062 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01002063 msg->eoh = msg->sol;
2064 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01002065 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002066 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002067 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02002068
2069 case HTTP_MSG_ERROR:
2070 /* this may only happen if we call http_msg_analyser() twice with an error */
2071 break;
2072
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002073 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01002074#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002075 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
2076 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002077#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01002078 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002079 }
2080 http_msg_ood:
2081 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002082 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002083 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002084 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002085
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002086 http_msg_invalid:
2087 /* invalid message */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002088 msg->err_state = state;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002089 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002090 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002091 return;
2092}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01002093
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002094/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
2095 * conversion succeeded, 0 in case of error. If the request was already 1.X,
2096 * nothing is done and 1 is returned.
2097 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002098static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002099{
2100 int delta;
2101 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002102 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002103
2104 if (msg->sl.rq.v_l != 0)
2105 return 1;
2106
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03002107 /* RFC 1945 allows only GET for HTTP/0.9 requests */
2108 if (txn->meth != HTTP_METH_GET)
2109 return 0;
2110
Willy Tarreau9b28e032012-10-12 23:49:43 +02002111 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002112
2113 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03002114 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
2115 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002116 }
2117 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002118 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01002119 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002120 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02002121 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002122 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002123 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002124 NULL, NULL);
2125 if (unlikely(!cur_end))
2126 return 0;
2127
2128 /* we have a full HTTP/1.0 request now and we know that
2129 * we have either a CR or an LF at <ptr>.
2130 */
2131 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
2132 return 1;
2133}
2134
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002135/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002136 * and "keep-alive" values. If we already know that some headers may safely
2137 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002138 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
2139 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01002140 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002141 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
2142 * found, and TX_CON_*_SET is adjusted depending on what is left so only
2143 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002144 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01002145 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002146void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01002147{
Willy Tarreau5b154472009-12-21 20:11:07 +01002148 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002149 const char *hdr_val = "Connection";
2150 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01002151
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002152 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01002153 return;
2154
Willy Tarreau88d349d2010-01-25 12:15:43 +01002155 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2156 hdr_val = "Proxy-Connection";
2157 hdr_len = 16;
2158 }
2159
Willy Tarreau5b154472009-12-21 20:11:07 +01002160 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002161 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002162 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002163 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2164 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002165 if (to_del & 2)
2166 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002167 else
2168 txn->flags |= TX_CON_KAL_SET;
2169 }
2170 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2171 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002172 if (to_del & 1)
2173 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002174 else
2175 txn->flags |= TX_CON_CLO_SET;
2176 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01002177 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
2178 txn->flags |= TX_HDR_CONN_UPG;
2179 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002180 }
2181
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002182 txn->flags |= TX_HDR_CONN_PRS;
2183 return;
2184}
Willy Tarreau5b154472009-12-21 20:11:07 +01002185
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002186/* Apply desired changes on the Connection: header. Values may be removed and/or
2187 * added depending on the <wanted> flags, which are exclusively composed of
2188 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
2189 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
2190 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002191void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002192{
2193 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002194 const char *hdr_val = "Connection";
2195 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002196
2197 ctx.idx = 0;
2198
Willy Tarreau88d349d2010-01-25 12:15:43 +01002199
2200 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2201 hdr_val = "Proxy-Connection";
2202 hdr_len = 16;
2203 }
2204
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002205 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002206 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002207 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2208 if (wanted & TX_CON_KAL_SET)
2209 txn->flags |= TX_CON_KAL_SET;
2210 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002211 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01002212 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002213 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2214 if (wanted & TX_CON_CLO_SET)
2215 txn->flags |= TX_CON_CLO_SET;
2216 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002217 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002218 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002219 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002220
2221 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
2222 return;
2223
2224 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
2225 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002226 hdr_val = "Connection: close";
2227 hdr_len = 17;
2228 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2229 hdr_val = "Proxy-Connection: close";
2230 hdr_len = 23;
2231 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002232 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002233 }
2234
2235 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
2236 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002237 hdr_val = "Connection: keep-alive";
2238 hdr_len = 22;
2239 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2240 hdr_val = "Proxy-Connection: keep-alive";
2241 hdr_len = 28;
2242 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002243 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002244 }
2245 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01002246}
2247
Christopher Faulet113f7de2015-12-14 14:52:13 +01002248/* Parse the chunk size at msg->next. Once done, caller should adjust ->next to
2249 * point to the first byte of data after the chunk size, so that we know we can
2250 * forward exactly msg->next bytes. msg->sol contains the exact number of bytes
2251 * forming the chunk size. That way it is always possible to differentiate
2252 * between the start of the body and the start of the data. Return the number
2253 * of byte parsed on success, 0 when some data is missing, <0 on error. Note:
2254 * this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002255 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002256static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002257{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002258 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002259 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002260 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002261 const char *end = buf->data + buf->size;
2262 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01002263 unsigned int chunk = 0;
2264
2265 /* The chunk size is in the following form, though we are only
2266 * interested in the size and CRLF :
2267 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2268 */
2269 while (1) {
2270 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002271 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002272 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002273 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002274 if (c < 0) /* not a hex digit anymore */
2275 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02002276 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002277 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01002278 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002279 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002280 chunk = (chunk << 4) + c;
2281 }
2282
Willy Tarreaud98cf932009-12-27 22:54:55 +01002283 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02002284 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002285 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002286
Willy Tarreau2235b262016-11-05 15:50:20 +01002287 while (HTTP_IS_SPHT(*ptr)) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01002288 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002289 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02002290 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01002291 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002292 }
2293
Willy Tarreaud98cf932009-12-27 22:54:55 +01002294 /* Up to there, we know that at least one byte is present at *ptr. Check
2295 * for the end of chunk size.
2296 */
2297 while (1) {
2298 if (likely(HTTP_IS_CRLF(*ptr))) {
2299 /* we now have a CR or an LF at ptr */
2300 if (likely(*ptr == '\r')) {
2301 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002302 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002303 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002304 return 0;
2305 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002306
Willy Tarreaud98cf932009-12-27 22:54:55 +01002307 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002308 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002309 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002310 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002311 /* done */
2312 break;
2313 }
2314 else if (*ptr == ';') {
2315 /* chunk extension, ends at next CRLF */
2316 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002317 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002318 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002319 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002320
2321 while (!HTTP_IS_CRLF(*ptr)) {
2322 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002323 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002324 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002325 return 0;
2326 }
2327 /* we have a CRLF now, loop above */
2328 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002329 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002330 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002331 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002332 }
2333
Christopher Faulet113f7de2015-12-14 14:52:13 +01002334 /* OK we found our CRLF and now <ptr> points to the next byte, which may
2335 * or may not be present. We save the number of bytes parsed into
2336 * msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002337 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002338 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002339 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002340 msg->sol += buf->size;
Willy Tarreau124d9912011-03-01 20:30:48 +01002341 msg->chunk_len = chunk;
2342 msg->body_len += chunk;
Christopher Faulet113f7de2015-12-14 14:52:13 +01002343 return msg->sol;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002344 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002345 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002346 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002347}
2348
Christopher Faulet113f7de2015-12-14 14:52:13 +01002349/* This function skips trailers in the buffer associated with HTTP message
2350 * <msg>. The first visited position is msg->next. If the end of the trailers is
2351 * found, the function returns >0. So, the caller can automatically schedul it
2352 * to be forwarded, and switch msg->msg_state to HTTP_MSG_DONE. If not enough
2353 * data are available, the function does not change anything except maybe
2354 * msg->sol if it could parse some lines, and returns zero. If a parse error
2355 * is encountered, the function returns < 0 and does not change anything except
2356 * maybe msg->sol. Note that the message must already be in HTTP_MSG_TRAILERS
2357 * state before calling this function, which implies that all non-trailers data
2358 * have already been scheduled for forwarding, and that msg->next exactly
2359 * matches the length of trailers already parsed and not forwarded. It is also
2360 * important to note that this function is designed to be able to parse wrapped
2361 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002362 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002363static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002364{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002365 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002366
Christopher Faulet113f7de2015-12-14 14:52:13 +01002367 /* we have msg->next which points to next line. Look for CRLF. But
2368 * first, we reset msg->sol */
2369 msg->sol = 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002370 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002371 const char *p1 = NULL, *p2 = NULL;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002372 const char *start = b_ptr(buf, msg->next + msg->sol);
2373 const char *stop = bi_end(buf);
2374 const char *ptr = start;
2375 int bytes = 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002376
2377 /* scan current line and stop at LF or CRLF */
2378 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002379 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002380 return 0;
2381
2382 if (*ptr == '\n') {
2383 if (!p1)
2384 p1 = ptr;
2385 p2 = ptr;
2386 break;
2387 }
2388
2389 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002390 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002391 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002392 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002393 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002394 p1 = ptr;
2395 }
2396
2397 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002398 if (ptr >= buf->data + buf->size)
2399 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002400 }
2401
2402 /* after LF; point to beginning of next line */
2403 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002404 if (p2 >= buf->data + buf->size)
2405 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002406
Christopher Faulet2fb28802015-12-01 10:40:57 +01002407 bytes = p2 - start;
Willy Tarreau638cd022010-01-03 07:42:04 +01002408 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002409 bytes += buf->size;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002410 msg->sol += bytes;
Willy Tarreau638cd022010-01-03 07:42:04 +01002411
Christopher Fauletd7c91962015-04-30 11:48:27 +02002412 /* LF/CRLF at beginning of line => end of trailers at p2.
2413 * Everything was scheduled for forwarding, there's nothing left
Christopher Faulet2fb28802015-12-01 10:40:57 +01002414 * from this message. */
2415 if (p1 == start)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002416 return 1;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002417
Willy Tarreaud98cf932009-12-27 22:54:55 +01002418 /* OK, next line then */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002419 }
2420}
2421
Christopher Faulet113f7de2015-12-14 14:52:13 +01002422/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
2423 * a possible LF alone at the end of a chunk. The caller should adjust msg->next
2424 * in order to include this part into the next forwarding phase. Note that the
2425 * caller must ensure that ->p points to the first byte to parse. It returns
2426 * the number of bytes parsed on success, so the caller can set msg_state to
2427 * HTTP_MSG_CHUNK_SIZE. If not enough data are available, the function does not
2428 * change anything and returns zero. If a parse error is encountered, the
2429 * function returns < 0. Note: this function is designed to parse wrapped CRLF
2430 * at the end of the buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002431 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002432static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002433{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002434 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002435 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002436 int bytes;
2437
2438 /* NB: we'll check data availabilty at the end. It's not a
2439 * problem because whatever we match first will be checked
2440 * against the correct length.
2441 */
2442 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002443 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002444 if (*ptr == '\r') {
2445 bytes++;
2446 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002447 if (ptr >= buf->data + buf->size)
2448 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002449 }
2450
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002451 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002452 return 0;
2453
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002454 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002455 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002456 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002457 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01002458 return bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002459}
Willy Tarreau5b154472009-12-21 20:11:07 +01002460
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002461/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2462 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2463 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2464 * Unparsable qvalues return 1000 as "q=1.000".
2465 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002466int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002467{
2468 int q = 1000;
2469
Willy Tarreau506c69a2014-07-08 00:59:48 +02002470 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002471 goto out;
2472 q = (*qvalue++ - '0') * 1000;
2473
2474 if (*qvalue++ != '.')
2475 goto out;
2476
Willy Tarreau506c69a2014-07-08 00:59:48 +02002477 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002478 goto out;
2479 q += (*qvalue++ - '0') * 100;
2480
Willy Tarreau506c69a2014-07-08 00:59:48 +02002481 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002482 goto out;
2483 q += (*qvalue++ - '0') * 10;
2484
Willy Tarreau506c69a2014-07-08 00:59:48 +02002485 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002486 goto out;
2487 q += (*qvalue++ - '0') * 1;
2488 out:
2489 if (q > 1000)
2490 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002491 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002492 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002493 return q;
2494}
William Lallemand82fe75c2012-10-23 10:25:10 +02002495
Willy Tarreau87b09662015-04-03 00:22:06 +02002496void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002497{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002498 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002499 int tmp = TX_CON_WANT_KAL;
2500
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002501 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2502 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002503 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2504 tmp = TX_CON_WANT_TUN;
2505
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002506 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002507 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2508 tmp = TX_CON_WANT_TUN;
2509 }
2510
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002511 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002512 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2513 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002514 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002515 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2516 tmp = TX_CON_WANT_CLO;
2517 else
2518 tmp = TX_CON_WANT_SCL;
2519 }
2520
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002521 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002522 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2523 tmp = TX_CON_WANT_CLO;
2524
2525 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2526 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2527
2528 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2529 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2530 /* parse the Connection header and possibly clean it */
2531 int to_del = 0;
2532 if ((msg->flags & HTTP_MSGF_VER_11) ||
2533 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002534 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002535 to_del |= 2; /* remove "keep-alive" */
2536 if (!(msg->flags & HTTP_MSGF_VER_11))
2537 to_del |= 1; /* remove "close" */
2538 http_parse_connection_header(txn, msg, to_del);
2539 }
2540
2541 /* check if client or config asks for explicit close in KAL/SCL */
2542 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2543 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2544 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2545 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2546 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002547 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002548 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2549}
William Lallemand82fe75c2012-10-23 10:25:10 +02002550
Willy Tarreaud787e662009-07-07 10:14:51 +02002551/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2552 * processing can continue on next analysers, or zero if it either needs more
2553 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002554 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02002555 * when it has nothing left to do, and may remove any analyser when it wants to
2556 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002557 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002558int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002559{
Willy Tarreau59234e92008-11-30 23:51:27 +01002560 /*
2561 * We will parse the partial (or complete) lines.
2562 * We will check the request syntax, and also join multi-line
2563 * headers. An index of all the lines will be elaborated while
2564 * parsing.
2565 *
2566 * For the parsing, we use a 28 states FSM.
2567 *
2568 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002569 * req->buf->p = beginning of request
2570 * req->buf->p + msg->eoh = end of processed headers / start of current one
2571 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002572 * msg->eol = end of current header or line (LF or CRLF)
2573 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002574 *
2575 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02002576 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002577 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2578 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002579 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002580
Willy Tarreau59234e92008-11-30 23:51:27 +01002581 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002582 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002583 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01002584 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002585 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002586
Willy Tarreau87b09662015-04-03 00:22:06 +02002587 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 +01002588 now_ms, __FUNCTION__,
2589 s,
2590 req,
2591 req->rex, req->wex,
2592 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002593 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002594 req->analysers);
2595
Willy Tarreau52a0c602009-08-16 22:45:38 +02002596 /* we're speaking HTTP here, so let's speak HTTP to the client */
2597 s->srv_error = http_return_srv_error;
2598
Willy Tarreau83e3af02009-12-28 17:39:57 +01002599 /* There's a protected area at the end of the buffer for rewriting
2600 * purposes. We don't want to start to parse the request if the
2601 * protected area is affected, because we may have to move processed
2602 * data later, which is much more complicated.
2603 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002604 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002605
2606 /* This point is executed when some data is avalaible for analysis,
2607 * so we log the end of the idle time. */
2608 if (s->logs.t_idle == -1)
2609 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
2610
Willy Tarreau379357a2013-06-08 12:55:46 +02002611 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01002612 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002613 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002614 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002615 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002616 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002617 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002618 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002619 return 0;
2620 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002621 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2622 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2623 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002624 }
2625
Willy Tarreau065e8332010-01-08 00:30:20 +01002626 /* Note that we have the same problem with the response ; we
2627 * may want to send a redirect, error or anything which requires
2628 * some spare space. So we'll ensure that we have at least
2629 * maxrewrite bytes available in the response buffer before
2630 * processing that one. This will only affect pipelined
2631 * keep-alive requests.
2632 */
2633 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002634 unlikely(!channel_is_rewritable(&s->res) ||
2635 bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) ||
2636 bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) {
2637 if (s->res.buf->o) {
2638 if (s->res.flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002639 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002640 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002641 channel_dont_connect(req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002642 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
2643 s->res.flags |= CF_WAKE_WRITE;
2644 s->res.analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002645 return 0;
2646 }
2647 }
2648
Willy Tarreau9b28e032012-10-12 23:49:43 +02002649 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002650 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002651 }
2652
Willy Tarreau59234e92008-11-30 23:51:27 +01002653 /* 1: we might have to print this header in debug mode */
2654 if (unlikely((global.mode & MODE_DEBUG) &&
2655 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02002656 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002657 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002658
Willy Tarreau9b28e032012-10-12 23:49:43 +02002659 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002660 /* this is a bit complex : in case of error on the request line,
2661 * we know that rq.l is still zero, so we display only the part
2662 * up to the end of the line (truncated by debug_hdr).
2663 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002664 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002665 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002666
Willy Tarreau59234e92008-11-30 23:51:27 +01002667 sol += hdr_idx_first_pos(&txn->hdr_idx);
2668 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002669
Willy Tarreau59234e92008-11-30 23:51:27 +01002670 while (cur_idx) {
2671 eol = sol + txn->hdr_idx.v[cur_idx].len;
2672 debug_hdr("clihdr", s, sol, eol);
2673 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2674 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002675 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002676 }
2677
Willy Tarreau58f10d72006-12-04 02:26:12 +01002678
Willy Tarreau59234e92008-11-30 23:51:27 +01002679 /*
2680 * Now we quickly check if we have found a full valid request.
2681 * If not so, we check the FD and buffer states before leaving.
2682 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002683 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002684 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02002685 * on a keep-alive stream, if we encounter and error, close, t/o,
2686 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002687 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02002688 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002689 * Last, we may increase some tracked counters' http request errors on
2690 * the cases that are deliberately the client's fault. For instance,
2691 * a timeout or connection reset is not counted as an error. However
2692 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002693 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002694
Willy Tarreau655dce92009-11-08 13:10:58 +01002695 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002696 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002697 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002698 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002699 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002700 stream_inc_http_req_ctr(s);
2701 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002702 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002703 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002704 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002705
Willy Tarreau59234e92008-11-30 23:51:27 +01002706 /* 1: Since we are in header mode, if there's no space
2707 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02002708 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01002709 * must terminate it now.
2710 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002711 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002712 /* FIXME: check if URI is set and return Status
2713 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002714 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002715 stream_inc_http_req_ctr(s);
2716 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002717 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002718 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002719 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002720 goto return_bad_req;
2721 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002722
Willy Tarreau59234e92008-11-30 23:51:27 +01002723 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002724 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002725 if (!(s->flags & SF_ERR_MASK))
2726 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002727
Willy Tarreaufcffa692010-01-10 14:21:19 +01002728 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002729 goto failed_keep_alive;
2730
Willy Tarreau0f228a02015-05-01 15:37:53 +02002731 if (sess->fe->options & PR_O_IGNORE_PRB)
2732 goto failed_keep_alive;
2733
Willy Tarreau59234e92008-11-30 23:51:27 +01002734 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002735 if (msg->err_pos >= 0) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002736 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002737 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002738 }
2739
Willy Tarreaudc979f22012-12-04 10:39:01 +01002740 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002741 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01002742 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002743 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002744 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02002745 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002746 proxy_inc_fe_req_ctr(sess->fe);
2747 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002748 if (sess->listener->counters)
2749 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002750
Willy Tarreaue7dff022015-04-03 01:14:29 +02002751 if (!(s->flags & SF_FINST_MASK))
2752 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002753 return 0;
2754 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002755
Willy Tarreau59234e92008-11-30 23:51:27 +01002756 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002757 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002758 if (!(s->flags & SF_ERR_MASK))
2759 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002760
Willy Tarreaufcffa692010-01-10 14:21:19 +01002761 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002762 goto failed_keep_alive;
2763
Willy Tarreau0f228a02015-05-01 15:37:53 +02002764 if (sess->fe->options & PR_O_IGNORE_PRB)
2765 goto failed_keep_alive;
2766
Willy Tarreau59234e92008-11-30 23:51:27 +01002767 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002768 if (msg->err_pos >= 0) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002769 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002770 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002771 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002772 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002773 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01002774 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002775 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_408));
Christopher Faulet0184ea72017-01-05 14:06:34 +01002776 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002777
Willy Tarreau87b09662015-04-03 00:22:06 +02002778 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002779 proxy_inc_fe_req_ctr(sess->fe);
2780 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002781 if (sess->listener->counters)
2782 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002783
Willy Tarreaue7dff022015-04-03 01:14:29 +02002784 if (!(s->flags & SF_FINST_MASK))
2785 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002786 return 0;
2787 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002788
Willy Tarreau59234e92008-11-30 23:51:27 +01002789 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002790 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002791 if (!(s->flags & SF_ERR_MASK))
2792 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002793
Willy Tarreaufcffa692010-01-10 14:21:19 +01002794 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002795 goto failed_keep_alive;
2796
Willy Tarreau0f228a02015-05-01 15:37:53 +02002797 if (sess->fe->options & PR_O_IGNORE_PRB)
2798 goto failed_keep_alive;
2799
Willy Tarreau4076a152009-04-02 15:18:36 +02002800 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002801 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002802 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002803 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01002804 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002805 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Christopher Faulet0184ea72017-01-05 14:06:34 +01002806 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02002807 stream_inc_http_err_ctr(s);
2808 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002809 proxy_inc_fe_req_ctr(sess->fe);
2810 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002811 if (sess->listener->counters)
2812 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002813
Willy Tarreaue7dff022015-04-03 01:14:29 +02002814 if (!(s->flags & SF_FINST_MASK))
2815 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002816 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002817 }
2818
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002819 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002820 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002821 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002822#ifdef TCP_QUICKACK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002823 if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i &&
2824 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002825 /* We need more data, we have to re-enable quick-ack in case we
2826 * previously disabled it, otherwise we might cause the client
2827 * to delay next data.
2828 */
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002829 setsockopt(__objt_conn(sess->origin)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002830 }
2831#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002832
Willy Tarreaufcffa692010-01-10 14:21:19 +01002833 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2834 /* If the client starts to talk, let's fall back to
2835 * request timeout processing.
2836 */
2837 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002838 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002839 }
2840
Willy Tarreau59234e92008-11-30 23:51:27 +01002841 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002842 if (!tick_isset(req->analyse_exp)) {
2843 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2844 (txn->flags & TX_WAIT_NEXT_RQ) &&
2845 tick_isset(s->be->timeout.httpka))
2846 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2847 else
2848 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2849 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002850
Willy Tarreau59234e92008-11-30 23:51:27 +01002851 /* we're not ready yet */
2852 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002853
2854 failed_keep_alive:
2855 /* Here we process low-level errors for keep-alive requests. In
2856 * short, if the request is not the first one and it experiences
2857 * a timeout, read error or shutdown, we just silently close so
2858 * that the client can try again.
2859 */
2860 txn->status = 0;
2861 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01002862 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002863 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002864 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002865 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002866 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002867 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002868 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002869
Willy Tarreaud787e662009-07-07 10:14:51 +02002870 /* OK now we have a complete HTTP request with indexed headers. Let's
2871 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002872 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002873 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002874 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002875 * byte after the last LF. msg->sov points to the first byte of data.
2876 * msg->eol cannot be trusted because it may have been left uninitialized
2877 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002878 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002879
Willy Tarreau87b09662015-04-03 00:22:06 +02002880 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002881 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002882
Willy Tarreaub16a5742010-01-10 14:46:16 +01002883 if (txn->flags & TX_WAIT_NEXT_RQ) {
2884 /* kill the pending keep-alive timeout */
2885 txn->flags &= ~TX_WAIT_NEXT_RQ;
2886 req->analyse_exp = TICK_ETERNITY;
2887 }
2888
2889
Willy Tarreaud787e662009-07-07 10:14:51 +02002890 /* Maybe we found in invalid header name while we were configured not
2891 * to block on that, so we have to capture it now.
2892 */
2893 if (unlikely(msg->err_pos >= 0))
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002894 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002895
Willy Tarreau59234e92008-11-30 23:51:27 +01002896 /*
2897 * 1: identify the method
2898 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002899 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002900
2901 /* we can make use of server redirect on GET and HEAD */
2902 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002903 s->flags |= SF_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002904
Willy Tarreau59234e92008-11-30 23:51:27 +01002905 /*
2906 * 2: check if the URI matches the monitor_uri.
2907 * We have to do this for every request which gets in, because
2908 * the monitor-uri is defined by the frontend.
2909 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002910 if (unlikely((sess->fe->monitor_uri_len != 0) &&
2911 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002912 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002913 sess->fe->monitor_uri,
2914 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002915 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002916 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002917 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002918 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002919
Willy Tarreaue7dff022015-04-03 01:14:29 +02002920 s->flags |= SF_MONITOR;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002921 sess->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002922
Willy Tarreau59234e92008-11-30 23:51:27 +01002923 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002924 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002925 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002926
Willy Tarreau59234e92008-11-30 23:51:27 +01002927 ret = acl_pass(ret);
2928 if (cond->pol == ACL_COND_UNLESS)
2929 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002930
Willy Tarreau59234e92008-11-30 23:51:27 +01002931 if (ret) {
2932 /* we fail this request, let's return 503 service unavail */
2933 txn->status = 503;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002934 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_503));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002935 if (!(s->flags & SF_ERR_MASK))
2936 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002937 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002938 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002939 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002940
Willy Tarreau59234e92008-11-30 23:51:27 +01002941 /* nothing to fail, let's reply normaly */
2942 txn->status = 200;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002943 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_200));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002944 if (!(s->flags & SF_ERR_MASK))
2945 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002946 goto return_prx_cond;
2947 }
2948
2949 /*
2950 * 3: Maybe we have to copy the original REQURI for the logs ?
2951 * Note: we cannot log anymore if the request has been
2952 * classified as invalid.
2953 */
2954 if (unlikely(s->logs.logwait & LW_REQ)) {
2955 /* we have a complete HTTP request that we must log */
2956 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2957 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002958
Willy Tarreau59234e92008-11-30 23:51:27 +01002959 if (urilen >= REQURI_LEN)
2960 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002961 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002962 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002963
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002964 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002965 s->do_log(s);
2966 } else {
2967 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002968 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002969 }
Willy Tarreau06619262006-12-17 08:37:22 +01002970
Willy Tarreau91852eb2015-05-01 13:26:00 +02002971 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
2972 * exactly one digit "." one digit. This check may be disabled using
2973 * option accept-invalid-http-request.
2974 */
2975 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
2976 if (msg->sl.rq.v_l != 8) {
2977 msg->err_pos = msg->sl.rq.v;
2978 goto return_bad_req;
2979 }
2980
2981 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
2982 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
2983 req->buf->p[msg->sl.rq.v + 6] != '.' ||
2984 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
2985 msg->err_pos = msg->sl.rq.v + 4;
2986 goto return_bad_req;
2987 }
2988 }
Willy Tarreau13317662015-05-01 13:47:08 +02002989 else {
2990 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
2991 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
2992 goto return_bad_req;
2993 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02002994
Willy Tarreau5b154472009-12-21 20:11:07 +01002995 /* ... and check if the request is HTTP/1.1 or above */
2996 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002997 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2998 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2999 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003000 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01003001
3002 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01003003 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 +01003004
Willy Tarreau88d349d2010-01-25 12:15:43 +01003005 /* if the frontend has "option http-use-proxy-header", we'll check if
3006 * we have what looks like a proxied connection instead of a connection,
3007 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
3008 * Note that this is *not* RFC-compliant, however browsers and proxies
3009 * happen to do that despite being non-standard :-(
3010 * We consider that a request not beginning with either '/' or '*' is
3011 * a proxied connection, which covers both "scheme://location" and
3012 * CONNECT ip:port.
3013 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003014 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003015 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01003016 txn->flags |= TX_USE_PX_CONN;
3017
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003018 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003019 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003020
Willy Tarreau59234e92008-11-30 23:51:27 +01003021 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02003022 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02003023 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02003024 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02003025
Willy Tarreau557f1992015-05-01 10:05:17 +02003026 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
3027 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003028 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003029 * The length of a message body is determined by one of the following
3030 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02003031 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003032 * 1. Any response to a HEAD request and any response with a 1xx
3033 * (Informational), 204 (No Content), or 304 (Not Modified) status
3034 * code is always terminated by the first empty line after the
3035 * header fields, regardless of the header fields present in the
3036 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003037 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003038 * 2. Any 2xx (Successful) response to a CONNECT request implies that
3039 * the connection will become a tunnel immediately after the empty
3040 * line that concludes the header fields. A client MUST ignore any
3041 * Content-Length or Transfer-Encoding header fields received in
3042 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003043 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003044 * 3. If a Transfer-Encoding header field is present and the chunked
3045 * transfer coding (Section 4.1) is the final encoding, the message
3046 * body length is determined by reading and decoding the chunked
3047 * data until the transfer coding indicates the data is complete.
3048 *
3049 * If a Transfer-Encoding header field is present in a response and
3050 * the chunked transfer coding is not the final encoding, the
3051 * message body length is determined by reading the connection until
3052 * it is closed by the server. If a Transfer-Encoding header field
3053 * is present in a request and the chunked transfer coding is not
3054 * the final encoding, the message body length cannot be determined
3055 * reliably; the server MUST respond with the 400 (Bad Request)
3056 * status code and then close the connection.
3057 *
3058 * If a message is received with both a Transfer-Encoding and a
3059 * Content-Length header field, the Transfer-Encoding overrides the
3060 * Content-Length. Such a message might indicate an attempt to
3061 * perform request smuggling (Section 9.5) or response splitting
3062 * (Section 9.4) and ought to be handled as an error. A sender MUST
3063 * remove the received Content-Length field prior to forwarding such
3064 * a message downstream.
3065 *
3066 * 4. If a message is received without Transfer-Encoding and with
3067 * either multiple Content-Length header fields having differing
3068 * field-values or a single Content-Length header field having an
3069 * invalid value, then the message framing is invalid and the
3070 * recipient MUST treat it as an unrecoverable error. If this is a
3071 * request message, the server MUST respond with a 400 (Bad Request)
3072 * status code and then close the connection. If this is a response
3073 * message received by a proxy, the proxy MUST close the connection
3074 * to the server, discard the received response, and send a 502 (Bad
3075 * Gateway) response to the client. If this is a response message
3076 * received by a user agent, the user agent MUST close the
3077 * connection to the server and discard the received response.
3078 *
3079 * 5. If a valid Content-Length header field is present without
3080 * Transfer-Encoding, its decimal value defines the expected message
3081 * body length in octets. If the sender closes the connection or
3082 * the recipient times out before the indicated number of octets are
3083 * received, the recipient MUST consider the message to be
3084 * incomplete and close the connection.
3085 *
3086 * 6. If this is a request message and none of the above are true, then
3087 * the message body length is zero (no message body is present).
3088 *
3089 * 7. Otherwise, this is a response message without a declared message
3090 * body length, so the message body length is determined by the
3091 * number of octets received prior to the server closing the
3092 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003093 */
3094
Willy Tarreau32b47f42009-10-18 20:55:02 +02003095 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003096 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau4979d5c2015-05-01 10:06:30 +02003097 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003098 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003099 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
3100 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02003101 /* chunked not last, return badreq */
3102 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003103 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003104 }
3105
Willy Tarreau1c913912015-04-30 10:57:51 +02003106 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02003107 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02003108 if (msg->flags & HTTP_MSGF_TE_CHNK) {
3109 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
3110 http_remove_header2(msg, &txn->hdr_idx, &ctx);
3111 }
Willy Tarreau34dfc602015-05-01 10:09:49 +02003112 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02003113 signed long long cl;
3114
Willy Tarreauad14f752011-09-02 20:33:27 +02003115 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003116 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003117 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003118 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003119
Willy Tarreauad14f752011-09-02 20:33:27 +02003120 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003121 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003122 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02003123 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003124
Willy Tarreauad14f752011-09-02 20:33:27 +02003125 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003126 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003127 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003128 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003129
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003130 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003131 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003132 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02003133 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003134
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003135 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003136 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003137 }
3138
Willy Tarreau34dfc602015-05-01 10:09:49 +02003139 /* even bodyless requests have a known length */
3140 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003141
Willy Tarreau179085c2014-04-28 16:48:56 +02003142 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3143 * only change if both the request and the config reference something else.
3144 * Option httpclose by itself sets tunnel mode where headers are mangled.
3145 * However, if another mode is set, it will affect it (eg: server-close/
3146 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3147 * if FE and BE have the same settings (common). The method consists in
3148 * checking if options changed between the two calls (implying that either
3149 * one is non-null, or one of them is non-null and we are there for the first
3150 * time.
3151 */
3152 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003153 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02003154 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003155
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02003156 /* we may have to wait for the request's body */
3157 if ((s->be->options & PR_O_WREQ_BODY) &&
3158 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
3159 req->analysers |= AN_REQ_HTTP_BODY;
3160
Willy Tarreaud787e662009-07-07 10:14:51 +02003161 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003162 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003163 req->analyse_exp = TICK_ETERNITY;
3164 return 1;
3165
3166 return_bad_req:
3167 /* We centralize bad requests processing here */
3168 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3169 /* we detected a parsing error. We want to archive this request
3170 * in the dedicated proxy area for later troubleshooting.
3171 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003172 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003173 }
3174
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003175 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02003176 txn->req.msg_state = HTTP_MSG_ERROR;
3177 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01003178 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003179
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003180 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003181 if (sess->listener->counters)
3182 sess->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003183
3184 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003185 if (!(s->flags & SF_ERR_MASK))
3186 s->flags |= SF_ERR_PRXCOND;
3187 if (!(s->flags & SF_FINST_MASK))
3188 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02003189
Christopher Faulet0184ea72017-01-05 14:06:34 +01003190 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02003191 req->analyse_exp = TICK_ETERNITY;
3192 return 0;
3193}
3194
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003195
Willy Tarreau347a35d2013-11-22 17:51:09 +01003196/* This function prepares an applet to handle the stats. It can deal with the
3197 * "100-continue" expectation, check that admin rules are met for POST requests,
3198 * and program a response message if something was unexpected. It cannot fail
3199 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003200 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003201 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02003202 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003203 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003204int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003205{
3206 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01003207 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02003208 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003209 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003210 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003211 struct uri_auth *uri_auth = s->be->uri_auth;
3212 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003213 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003214
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003215 appctx = si_appctx(si);
3216 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3217 appctx->st1 = appctx->st2 = 0;
3218 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3219 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003220 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003221 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003222
3223 uri = msg->chn->buf->p + msg->sl.rq.u;
3224 lookup = uri + uri_auth->uri_len;
3225
3226 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3227 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003228 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003229 break;
3230 }
3231 }
3232
3233 if (uri_auth->refresh) {
3234 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3235 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003236 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003237 break;
3238 }
3239 }
3240 }
3241
3242 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3243 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003244 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003245 break;
3246 }
3247 }
3248
Willy Tarreau1e62df92016-01-11 18:57:53 +01003249 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
3250 if (memcmp(h, ";typed", 6) == 0) {
3251 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
3252 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
3253 break;
3254 }
3255 }
3256
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003257 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3258 if (memcmp(h, ";st=", 4) == 0) {
3259 int i;
3260 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003261 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003262 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3263 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003264 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003265 break;
3266 }
3267 }
3268 break;
3269 }
3270 }
3271
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003272 appctx->ctx.stats.scope_str = 0;
3273 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003274 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3275 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3276 int itx = 0;
3277 const char *h2;
3278 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3279 const char *err;
3280
3281 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3282 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003283 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003284 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3285 itx++;
3286 h++;
3287 }
3288
3289 if (itx > STAT_SCOPE_TXT_MAXLEN)
3290 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003291 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003292
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003293 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003294 memcpy(scope_txt, h2, itx);
3295 scope_txt[itx] = '\0';
3296 err = invalid_char(scope_txt);
3297 if (err) {
3298 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003299 appctx->ctx.stats.scope_str = 0;
3300 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003301 }
3302 break;
3303 }
3304 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003305
3306 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003307 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003308 int ret = 1;
3309
3310 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003311 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 +01003312 ret = acl_pass(ret);
3313 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3314 ret = !ret;
3315 }
3316
3317 if (ret) {
3318 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003319 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003320 break;
3321 }
3322 }
3323
3324 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003325 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003326 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003327 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003328 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
3329 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003330 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003331 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003332 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003333 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3334 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003335 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003336 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003337 else {
3338 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003339 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003340 }
3341
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003342 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003343 return 1;
3344}
3345
Lukas Tribus67db8df2013-06-23 17:37:13 +02003346/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3347 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3348 */
Vincent Bernat6e615892016-05-18 16:17:44 +02003349void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003350{
3351#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02003352 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003353 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3354#endif
3355#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02003356 if (from->ss_family == AF_INET6) {
3357 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02003358 /* v4-mapped addresses need IP_TOS */
3359 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3360 else
3361 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3362 }
3363#endif
3364}
3365
Willy Tarreau87b09662015-04-03 00:22:06 +02003366int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003367 const char* name, unsigned int name_len,
3368 const char *str, struct my_regex *re,
3369 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06003370{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003371 struct hdr_ctx ctx;
3372 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003373 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003374 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3375 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003376 struct chunk *output = get_trash_chunk();
3377
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003378 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06003379
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003380 /* Choose the header browsing function. */
3381 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003382 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003383 http_find_hdr_func = http_find_header2;
3384 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003385 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003386 http_find_hdr_func = http_find_full_header2;
3387 break;
3388 default: /* impossible */
3389 return -1;
3390 }
3391
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003392 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
3393 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06003394 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003395 char *val = ctx.line + ctx.val;
3396 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003397
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003398 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
3399 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003400
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003401 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003402 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003403 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003404
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003405 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003406
3407 hdr->len += delta;
3408 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003409
3410 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003411 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003412 }
3413
3414 return 0;
3415}
3416
Willy Tarreau87b09662015-04-03 00:22:06 +02003417static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003418 const char* name, unsigned int name_len,
3419 struct list *fmt, struct my_regex *re,
3420 int action)
3421{
3422 struct chunk *replace = get_trash_chunk();
3423
3424 replace->len = build_logline(s, replace->str, replace->size, fmt);
3425 if (replace->len >= replace->size - 1)
3426 return -1;
3427
3428 return http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
3429}
3430
Willy Tarreau87b09662015-04-03 00:22:06 +02003431/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003432 * transaction <txn>. Returns the verdict of the first rule that prevents
3433 * further processing of the request (auth, deny, ...), and defaults to
3434 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3435 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02003436 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
3437 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
3438 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003439 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003440enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02003441http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003442{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003443 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003444 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003445 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003446 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003447 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003448 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02003449 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02003450 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003451
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003452 /* If "the current_rule_list" match the executed rule list, we are in
3453 * resume condition. If a resume is needed it is always in the action
3454 * and never in the ACL or converters. In this case, we initialise the
3455 * current rule, and go to the action execution point.
3456 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003457 if (s->current_rule) {
3458 rule = s->current_rule;
3459 s->current_rule = NULL;
3460 if (s->current_rule_list == rules)
3461 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003462 }
3463 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003464
Willy Tarreauff011f22011-01-06 17:51:27 +01003465 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003466
Willy Tarreau96257ec2012-12-27 10:46:37 +01003467 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003468 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003469 int ret;
3470
Willy Tarreau192252e2015-04-04 01:47:55 +02003471 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003472 ret = acl_pass(ret);
3473
Willy Tarreauff011f22011-01-06 17:51:27 +01003474 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003475 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003476
3477 if (!ret) /* condition not matched */
3478 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003479 }
3480
Willy Tarreauacc98002015-09-27 23:34:39 +02003481 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003482resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003483 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003484 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003485 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003486
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003487 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02003488 if (deny_status)
3489 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003490 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003491
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003492 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01003493 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003494 if (deny_status)
3495 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003496 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003497
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003498 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003499 /* Auth might be performed on regular http-req rules as well as on stats */
3500 auth_realm = rule->arg.auth.realm;
3501 if (!auth_realm) {
3502 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3503 auth_realm = STATS_DEFAULT_REALM;
3504 else
3505 auth_realm = px->id;
3506 }
3507 /* send 401/407 depending on whether we use a proxy or not. We still
3508 * count one error, because normal browsing won't significantly
3509 * increase the counter but brute force attempts will.
3510 */
3511 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3512 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01003513 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02003514 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003515 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003516
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003517 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003518 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3519 return HTTP_RULE_RES_BADREQ;
3520 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003521
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003522 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003523 s->task->nice = rule->arg.nice;
3524 break;
3525
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003526 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003527 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Vincent Bernat6e615892016-05-18 16:17:44 +02003528 inet_set_tos(cli_conn->t.sock.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003529 break;
3530
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003531 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003532#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003533 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003534 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003535#endif
3536 break;
3537
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003538 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003539 s->logs.level = rule->arg.loglevel;
3540 break;
3541
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003542 case ACT_HTTP_REPLACE_HDR:
3543 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003544 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3545 rule->arg.hdr_add.name_len,
3546 &rule->arg.hdr_add.fmt,
3547 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003548 return HTTP_RULE_RES_BADREQ;
3549 break;
3550
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003551 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003552 ctx.idx = 0;
3553 /* remove all occurrences of the header */
3554 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3555 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3556 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003557 }
Willy Tarreau85603282015-01-21 20:39:27 +01003558 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003559
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003560 case ACT_HTTP_SET_HDR:
3561 case ACT_HTTP_ADD_HDR:
Thierry Fournier4b788f72016-06-01 13:35:36 +02003562 /* The scope of the trash buffer must be limited to this function. The
3563 * build_logline() function can execute a lot of other function which
3564 * can use the trash buffer. So for limiting the scope of this global
3565 * buffer, we build first the header value using build_logline, and
3566 * after we store the header name.
3567 */
3568 len = rule->arg.hdr_add.name_len + 2,
3569 len += build_logline(s, trash.str + len, trash.size - len, &rule->arg.hdr_add.fmt);
Willy Tarreau96257ec2012-12-27 10:46:37 +01003570 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
Thierry Fournier4b788f72016-06-01 13:35:36 +02003571 trash.str[rule->arg.hdr_add.name_len] = ':';
3572 trash.str[rule->arg.hdr_add.name_len + 1] = ' ';
3573 trash.len = len;
Willy Tarreau85603282015-01-21 20:39:27 +01003574
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003575 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003576 /* remove all occurrences of the header */
3577 ctx.idx = 0;
3578 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3579 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3580 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3581 }
3582 }
3583
Willy Tarreau96257ec2012-12-27 10:46:37 +01003584 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3585 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003586
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003587 case ACT_HTTP_DEL_ACL:
3588 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003589 struct pat_ref *ref;
3590 char *key;
3591 int len;
3592
3593 /* collect reference */
3594 ref = pat_ref_lookup(rule->arg.map.ref);
3595 if (!ref)
3596 continue;
3597
3598 /* collect key */
3599 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3600 key = trash.str;
3601 key[len] = '\0';
3602
3603 /* perform update */
3604 /* returned code: 1=ok, 0=ko */
3605 pat_ref_delete(ref, key);
3606
3607 break;
3608 }
3609
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003610 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003611 struct pat_ref *ref;
3612 char *key;
3613 struct chunk *trash_key;
3614 int len;
3615
3616 trash_key = get_trash_chunk();
3617
3618 /* collect reference */
3619 ref = pat_ref_lookup(rule->arg.map.ref);
3620 if (!ref)
3621 continue;
3622
3623 /* collect key */
3624 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3625 key = trash_key->str;
3626 key[len] = '\0';
3627
3628 /* perform update */
3629 /* add entry only if it does not already exist */
3630 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003631 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003632
3633 break;
3634 }
3635
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003636 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003637 struct pat_ref *ref;
3638 char *key, *value;
3639 struct chunk *trash_key, *trash_value;
3640 int len;
3641
3642 trash_key = get_trash_chunk();
3643 trash_value = get_trash_chunk();
3644
3645 /* collect reference */
3646 ref = pat_ref_lookup(rule->arg.map.ref);
3647 if (!ref)
3648 continue;
3649
3650 /* collect key */
3651 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3652 key = trash_key->str;
3653 key[len] = '\0';
3654
3655 /* collect value */
3656 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3657 value = trash_value->str;
3658 value[len] = '\0';
3659
3660 /* perform update */
3661 if (pat_ref_find_elt(ref, key) != NULL)
3662 /* update entry if it exists */
3663 pat_ref_set(ref, key, value, NULL);
3664 else
3665 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003666 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003667
3668 break;
3669 }
William Lallemand73025dd2014-04-24 14:38:37 +02003670
Thierry FOURNIER42148732015-09-02 17:17:33 +02003671 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02003672 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
3673 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003674
Willy Tarreauacc98002015-09-27 23:34:39 +02003675 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003676 case ACT_RET_ERR:
3677 case ACT_RET_CONT:
3678 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003679 case ACT_RET_STOP:
3680 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003681 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003682 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003683 return HTTP_RULE_RES_YIELD;
3684 }
William Lallemand73025dd2014-04-24 14:38:37 +02003685 break;
3686
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003687 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02003688 /* Note: only the first valid tracking parameter of each
3689 * applies.
3690 */
3691
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003692 if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02003693 struct stktable *t;
3694 struct stksess *ts;
3695 struct stktable_key *key;
3696 void *ptr;
3697
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02003698 t = rule->arg.trk_ctr.table.t;
3699 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 +02003700
3701 if (key && (ts = stktable_get_entry(t, key))) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003702 stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02003703
3704 /* let's count a new HTTP request as it's the first time we do it */
3705 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3706 if (ptr)
3707 stktable_data_cast(ptr, http_req_cnt)++;
3708
3709 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3710 if (ptr)
3711 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3712 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3713
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003714 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003715 if (sess->fe != s->be)
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003716 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02003717 }
3718 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003719 break;
3720
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003721 /* other flags exists, but normaly, they never be matched. */
3722 default:
3723 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003724 }
3725 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003726
3727 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003728 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003729}
3730
Willy Tarreau71241ab2012-12-27 11:30:54 +01003731
Willy Tarreau51d861a2015-05-22 17:30:48 +02003732/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3733 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3734 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3735 * is returned, the process can continue the evaluation of next rule list. If
3736 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3737 * is returned, it means the operation could not be processed and a server error
3738 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3739 * deny rule. If *YIELD is returned, the caller must call again the function
3740 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003741 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003742static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003743http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003744{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003745 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003746 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003747 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003748 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003749 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02003750 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003751
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003752 /* If "the current_rule_list" match the executed rule list, we are in
3753 * resume condition. If a resume is needed it is always in the action
3754 * and never in the ACL or converters. In this case, we initialise the
3755 * current rule, and go to the action execution point.
3756 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003757 if (s->current_rule) {
3758 rule = s->current_rule;
3759 s->current_rule = NULL;
3760 if (s->current_rule_list == rules)
3761 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003762 }
3763 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003764
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003765 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003766
3767 /* check optional condition */
3768 if (rule->cond) {
3769 int ret;
3770
Willy Tarreau192252e2015-04-04 01:47:55 +02003771 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003772 ret = acl_pass(ret);
3773
3774 if (rule->cond->pol == ACL_COND_UNLESS)
3775 ret = !ret;
3776
3777 if (!ret) /* condition not matched */
3778 continue;
3779 }
3780
Willy Tarreauacc98002015-09-27 23:34:39 +02003781 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003782resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003783 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003784 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003785 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003786
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003787 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003788 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003789 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003790
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003791 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003792 s->task->nice = rule->arg.nice;
3793 break;
3794
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003795 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003796 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Vincent Bernat6e615892016-05-18 16:17:44 +02003797 inet_set_tos(cli_conn->t.sock.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003798 break;
3799
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003800 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003801#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003802 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003803 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003804#endif
3805 break;
3806
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003807 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003808 s->logs.level = rule->arg.loglevel;
3809 break;
3810
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003811 case ACT_HTTP_REPLACE_HDR:
3812 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003813 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3814 rule->arg.hdr_add.name_len,
3815 &rule->arg.hdr_add.fmt,
3816 &rule->arg.hdr_add.re, rule->action))
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003817 return HTTP_RULE_RES_STOP; /* note: we should report an error here */
Sasha Pachev218f0642014-06-16 12:05:59 -06003818 break;
3819
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003820 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003821 ctx.idx = 0;
3822 /* remove all occurrences of the header */
3823 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3824 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3825 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3826 }
Willy Tarreau85603282015-01-21 20:39:27 +01003827 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003828
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003829 case ACT_HTTP_SET_HDR:
3830 case ACT_HTTP_ADD_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003831 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3832 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3833 trash.len = rule->arg.hdr_add.name_len;
3834 trash.str[trash.len++] = ':';
3835 trash.str[trash.len++] = ' ';
3836 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 +01003837
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003838 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003839 /* remove all occurrences of the header */
3840 ctx.idx = 0;
3841 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3842 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3843 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3844 }
3845 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003846 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3847 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003848
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003849 case ACT_HTTP_DEL_ACL:
3850 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003851 struct pat_ref *ref;
3852 char *key;
3853 int len;
3854
3855 /* collect reference */
3856 ref = pat_ref_lookup(rule->arg.map.ref);
3857 if (!ref)
3858 continue;
3859
3860 /* collect key */
3861 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3862 key = trash.str;
3863 key[len] = '\0';
3864
3865 /* perform update */
3866 /* returned code: 1=ok, 0=ko */
3867 pat_ref_delete(ref, key);
3868
3869 break;
3870 }
3871
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003872 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003873 struct pat_ref *ref;
3874 char *key;
3875 struct chunk *trash_key;
3876 int len;
3877
3878 trash_key = get_trash_chunk();
3879
3880 /* collect reference */
3881 ref = pat_ref_lookup(rule->arg.map.ref);
3882 if (!ref)
3883 continue;
3884
3885 /* collect key */
3886 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3887 key = trash_key->str;
3888 key[len] = '\0';
3889
3890 /* perform update */
3891 /* check if the entry already exists */
3892 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003893 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003894
3895 break;
3896 }
3897
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003898 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003899 struct pat_ref *ref;
3900 char *key, *value;
3901 struct chunk *trash_key, *trash_value;
3902 int len;
3903
3904 trash_key = get_trash_chunk();
3905 trash_value = get_trash_chunk();
3906
3907 /* collect reference */
3908 ref = pat_ref_lookup(rule->arg.map.ref);
3909 if (!ref)
3910 continue;
3911
3912 /* collect key */
3913 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3914 key = trash_key->str;
3915 key[len] = '\0';
3916
3917 /* collect value */
3918 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3919 value = trash_value->str;
3920 value[len] = '\0';
3921
3922 /* perform update */
3923 if (pat_ref_find_elt(ref, key) != NULL)
3924 /* update entry if it exists */
3925 pat_ref_set(ref, key, value, NULL);
3926 else
3927 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003928 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003929
3930 break;
3931 }
William Lallemand73025dd2014-04-24 14:38:37 +02003932
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003933 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02003934 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3935 return HTTP_RULE_RES_BADREQ;
3936 return HTTP_RULE_RES_DONE;
3937
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003938 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3939 /* Note: only the first valid tracking parameter of each
3940 * applies.
3941 */
3942
3943 if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) {
3944 struct stktable *t;
3945 struct stksess *ts;
3946 struct stktable_key *key;
3947 void *ptr;
3948
3949 t = rule->arg.trk_ctr.table.t;
3950 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
3951
3952 if (key && (ts = stktable_get_entry(t, key))) {
3953 stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts);
3954
3955 /* let's count a new HTTP request as it's the first time we do it */
3956 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3957 if (ptr)
3958 stktable_data_cast(ptr, http_req_cnt)++;
3959
3960 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3961 if (ptr)
3962 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3963 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3964
3965 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3966 if (sess->fe != s->be)
3967 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3968
3969 /* When the client triggers a 4xx from the server, it's most often due
3970 * to a missing object or permission. These events should be tracked
3971 * because if they happen often, it may indicate a brute force or a
3972 * vulnerability scan. Normally this is done when receiving the response
3973 * but here we're tracking after this ought to have been done so we have
3974 * to do it on purpose.
3975 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02003976 if ((unsigned)(txn->status - 400) < 100) {
3977 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3978 if (ptr)
3979 stktable_data_cast(ptr, http_err_cnt)++;
3980
3981 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3982 if (ptr)
3983 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3984 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3985 }
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003986 }
3987 }
3988 break;
3989
Thierry FOURNIER42148732015-09-02 17:17:33 +02003990 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02003991 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
3992 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003993
Willy Tarreauacc98002015-09-27 23:34:39 +02003994 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003995 case ACT_RET_ERR:
3996 case ACT_RET_CONT:
3997 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003998 case ACT_RET_STOP:
3999 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004000 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02004001 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004002 return HTTP_RULE_RES_YIELD;
4003 }
William Lallemand73025dd2014-04-24 14:38:37 +02004004 break;
4005
Thierry FOURNIER22e49012015-08-05 19:13:48 +02004006 /* other flags exists, but normaly, they never be matched. */
4007 default:
4008 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004009 }
4010 }
4011
4012 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01004013 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004014}
4015
4016
Willy Tarreau71241ab2012-12-27 11:30:54 +01004017/* Perform an HTTP redirect based on the information in <rule>. The function
4018 * returns non-zero on success, or zero in case of a, irrecoverable error such
4019 * as too large a request to build a valid response.
4020 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004021static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01004022{
Willy Tarreaub329a312015-05-22 16:27:37 +02004023 struct http_msg *req = &txn->req;
4024 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004025 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004026 const char *location;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004027 struct chunk *chunk;
4028 int ret = 0;
4029
4030 chunk = alloc_trash_chunk();
4031 if (!chunk)
4032 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004033
4034 /* build redirect message */
4035 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04004036 case 308:
4037 msg_fmt = HTTP_308;
4038 break;
4039 case 307:
4040 msg_fmt = HTTP_307;
4041 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004042 case 303:
4043 msg_fmt = HTTP_303;
4044 break;
4045 case 301:
4046 msg_fmt = HTTP_301;
4047 break;
4048 case 302:
4049 default:
4050 msg_fmt = HTTP_302;
4051 break;
4052 }
4053
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004054 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
4055 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004056
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004057 location = chunk->str + chunk->len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004058
Willy Tarreau71241ab2012-12-27 11:30:54 +01004059 switch(rule->type) {
4060 case REDIRECT_TYPE_SCHEME: {
4061 const char *path;
4062 const char *host;
4063 struct hdr_ctx ctx;
4064 int pathlen;
4065 int hostlen;
4066
4067 host = "";
4068 hostlen = 0;
4069 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02004070 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004071 host = ctx.line + ctx.val;
4072 hostlen = ctx.vlen;
4073 }
4074
4075 path = http_get_path(txn);
4076 /* build message using path */
4077 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004078 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004079 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4080 int qs = 0;
4081 while (qs < pathlen) {
4082 if (path[qs] == '?') {
4083 pathlen = qs;
4084 break;
4085 }
4086 qs++;
4087 }
4088 }
4089 } else {
4090 path = "/";
4091 pathlen = 1;
4092 }
4093
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004094 if (rule->rdr_str) { /* this is an old "redirect" rule */
4095 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004096 if (chunk->len + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
4097 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004098
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004099 /* add scheme */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004100 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
4101 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004102 }
4103 else {
4104 /* add scheme with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004105 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004106
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004107 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004108 if (chunk->len + 3 + hostlen + pathlen > chunk->size - 4)
4109 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004110 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004111 /* add "://" */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004112 memcpy(chunk->str + chunk->len, "://", 3);
4113 chunk->len += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004114
4115 /* add host */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004116 memcpy(chunk->str + chunk->len, host, hostlen);
4117 chunk->len += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004118
4119 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004120 memcpy(chunk->str + chunk->len, path, pathlen);
4121 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004122
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004123 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004124 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004125 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004126 if (chunk->len > chunk->size - 5)
4127 goto leave;
4128 chunk->str[chunk->len] = '/';
4129 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004130 }
4131
4132 break;
4133 }
4134 case REDIRECT_TYPE_PREFIX: {
4135 const char *path;
4136 int pathlen;
4137
4138 path = http_get_path(txn);
4139 /* build message using path */
4140 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004141 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004142 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4143 int qs = 0;
4144 while (qs < pathlen) {
4145 if (path[qs] == '?') {
4146 pathlen = qs;
4147 break;
4148 }
4149 qs++;
4150 }
4151 }
4152 } else {
4153 path = "/";
4154 pathlen = 1;
4155 }
4156
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004157 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004158 if (chunk->len + rule->rdr_len + pathlen > chunk->size - 4)
4159 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004160
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004161 /* add prefix. Note that if prefix == "/", we don't want to
4162 * add anything, otherwise it makes it hard for the user to
4163 * configure a self-redirection.
4164 */
4165 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004166 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
4167 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004168 }
4169 }
4170 else {
4171 /* add prefix with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004172 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004173
4174 /* Check length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004175 if (chunk->len + pathlen > chunk->size - 4)
4176 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004177 }
4178
4179 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004180 memcpy(chunk->str + chunk->len, path, pathlen);
4181 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004182
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004183 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004184 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004185 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004186 if (chunk->len > chunk->size - 5)
4187 goto leave;
4188 chunk->str[chunk->len] = '/';
4189 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004190 }
4191
4192 break;
4193 }
4194 case REDIRECT_TYPE_LOCATION:
4195 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004196 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004197 if (chunk->len + rule->rdr_len > chunk->size - 4)
4198 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004199
4200 /* add location */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004201 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
4202 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004203 }
4204 else {
4205 /* add location with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004206 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004207
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004208 /* Check left length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004209 if (chunk->len > chunk->size - 4)
4210 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004211 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004212 break;
4213 }
4214
4215 if (rule->cookie_len) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004216 memcpy(chunk->str + chunk->len, "\r\nSet-Cookie: ", 14);
4217 chunk->len += 14;
4218 memcpy(chunk->str + chunk->len, rule->cookie_str, rule->cookie_len);
4219 chunk->len += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004220 }
4221
4222 /* add end of headers and the keep-alive/close status.
4223 * We may choose to set keep-alive if the Location begins
4224 * with a slash, because the client will come back to the
4225 * same server.
4226 */
4227 txn->status = rule->code;
4228 /* let's log the request time */
4229 s->logs.tv_request = now;
4230
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004231 if (*location == '/' &&
Willy Tarreaub329a312015-05-22 16:27:37 +02004232 (req->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau2de8a502015-05-28 17:23:54 +02004233 ((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004234 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4235 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4236 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02004237 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004238 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004239 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: keep-alive", 30);
4240 chunk->len += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004241 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004242 memcpy(chunk->str + chunk->len, "\r\nConnection: keep-alive", 24);
4243 chunk->len += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004244 }
4245 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004246 memcpy(chunk->str + chunk->len, "\r\n\r\n", 4);
4247 chunk->len += 4;
4248 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
4249 bo_inject(res->chn, chunk->str, chunk->len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004250 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02004251 bi_fast_delete(req->chn->buf, req->sov);
4252 req->next -= req->sov;
4253 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01004254 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
4255 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->req.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02004256 req->msg_state = HTTP_MSG_CLOSED;
4257 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02004258 /* Trim any possible response */
4259 res->chn->buf->i = 0;
4260 res->next = res->sov = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004261 } else {
4262 /* keep-alive not possible */
4263 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004264 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4265 chunk->len += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004266 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004267 memcpy(chunk->str + chunk->len, "\r\nConnection: close\r\n\r\n", 23);
4268 chunk->len += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004269 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004270 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004271 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004272 }
4273
Willy Tarreaue7dff022015-04-03 01:14:29 +02004274 if (!(s->flags & SF_ERR_MASK))
4275 s->flags |= SF_ERR_LOCAL;
4276 if (!(s->flags & SF_FINST_MASK))
4277 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004278
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004279 ret = 1;
4280 leave:
4281 free_trash_chunk(chunk);
4282 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004283}
4284
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004285/* This stream analyser runs all HTTP request processing which is common to
4286 * frontends and backends, which means blocking ACLs, filters, connection-close,
4287 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004288 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004289 * either needs more data or wants to immediately abort the request (eg: deny,
4290 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004291 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004292int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004293{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004294 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004295 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02004296 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004297 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004298 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004299 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02004300 int deny_status = HTTP_ERR_403;
Willy Tarreaud787e662009-07-07 10:14:51 +02004301
Willy Tarreau655dce92009-11-08 13:10:58 +01004302 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004303 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004304 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004305 }
4306
Willy Tarreau87b09662015-04-03 00:22:06 +02004307 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 +02004308 now_ms, __FUNCTION__,
4309 s,
4310 req,
4311 req->rex, req->wex,
4312 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004313 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004314 req->analysers);
4315
Willy Tarreau65410832014-04-28 21:25:43 +02004316 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02004317 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02004318
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004319 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004320 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02004321 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01004322
Willy Tarreau0b748332014-04-29 00:13:29 +02004323 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004324 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4325 goto return_prx_yield;
4326
Willy Tarreau0b748332014-04-29 00:13:29 +02004327 case HTTP_RULE_RES_CONT:
4328 case HTTP_RULE_RES_STOP: /* nothing to do */
4329 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004330
Willy Tarreau0b748332014-04-29 00:13:29 +02004331 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4332 if (txn->flags & TX_CLTARPIT)
4333 goto tarpit;
4334 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004335
Willy Tarreau0b748332014-04-29 00:13:29 +02004336 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4337 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004338
Willy Tarreau0b748332014-04-29 00:13:29 +02004339 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004340 goto done;
4341
Willy Tarreau0b748332014-04-29 00:13:29 +02004342 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4343 goto return_bad_req;
4344 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004345 }
4346
Willy Tarreau52542592014-04-28 18:33:26 +02004347 /* OK at this stage, we know that the request was accepted according to
4348 * the http-request rules, we can check for the stats. Note that the
4349 * URI is detected *before* the req* rules in order not to be affected
4350 * by a possible reqrep, while they are processed *after* so that a
4351 * reqdeny can still block them. This clearly needs to change in 1.6!
4352 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004353 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004354 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004355 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004356 txn->status = 500;
4357 s->logs.tv_request = now;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004358 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004359
Willy Tarreaue7dff022015-04-03 01:14:29 +02004360 if (!(s->flags & SF_ERR_MASK))
4361 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02004362 goto return_prx_cond;
4363 }
4364
4365 /* parse the whole stats request and extract the relevant information */
4366 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02004367 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02004368 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004369
Willy Tarreau0b748332014-04-29 00:13:29 +02004370 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4371 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004372
Willy Tarreau0b748332014-04-29 00:13:29 +02004373 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4374 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004375 }
4376
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004377 /* evaluate the req* rules except reqadd */
4378 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004379 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004380 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004381
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004382 if (txn->flags & TX_CLDENY)
4383 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004384
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004385 if (txn->flags & TX_CLTARPIT)
4386 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004387 }
Willy Tarreau06619262006-12-17 08:37:22 +01004388
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004389 /* add request headers from the rule sets in the same order */
4390 list_for_each_entry(wl, &px->req_add, list) {
4391 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004392 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004393 ret = acl_pass(ret);
4394 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4395 ret = !ret;
4396 if (!ret)
4397 continue;
4398 }
4399
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004400 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004401 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004402 }
4403
Willy Tarreau52542592014-04-28 18:33:26 +02004404
4405 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004406 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004407 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004408 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
4409 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004410
Willy Tarreaue7dff022015-04-03 01:14:29 +02004411 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
4412 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
4413 if (!(s->flags & SF_FINST_MASK))
4414 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004415
Willy Tarreau70730dd2014-04-24 18:06:27 +02004416 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01004417 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
4418 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004419 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004420 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004421 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004422
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004423 /* check whether we have some ACLs set to redirect this request */
4424 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004425 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004426 int ret;
4427
Willy Tarreau192252e2015-04-04 01:47:55 +02004428 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004429 ret = acl_pass(ret);
4430 if (rule->cond->pol == ACL_COND_UNLESS)
4431 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004432 if (!ret)
4433 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004434 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004435 if (!http_apply_redirect_rule(rule, s, txn))
4436 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004437 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004438 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004439
Willy Tarreau2be39392010-01-03 17:24:51 +01004440 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4441 * If this happens, then the data will not come immediately, so we must
4442 * send all what we have without waiting. Note that due to the small gain
4443 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004444 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004445 * itself once used.
4446 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004447 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004448
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004449 done: /* done with this analyser, continue with next ones that the calling
4450 * points will have set, if any.
4451 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004452 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004453 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4454 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004455 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004456
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004457 tarpit:
4458 /* When a connection is tarpitted, we use the tarpit timeout,
4459 * which may be the same as the connect timeout if unspecified.
4460 * If unset, then set it to zero because we really want it to
4461 * eventually expire. We build the tarpit as an analyser.
4462 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004463 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004464
4465 /* wipe the request out so that we can drop the connection early
4466 * if the client closes first.
4467 */
4468 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004469
4470 /* Allow cookie logging
4471 */
4472 if (s->be->cookie_name || sess->fe->capture_name)
4473 manage_client_side_cookies(s, req);
4474
Christopher Faulet0184ea72017-01-05 14:06:34 +01004475 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004476 req->analysers |= AN_REQ_HTTP_TARPIT;
4477 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4478 if (!req->analyse_exp)
4479 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02004480 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004481 sess->fe->fe_counters.denied_req++;
4482 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004483 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004484 if (sess->listener->counters)
4485 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004486 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004487
4488 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004489
4490 /* Allow cookie logging
4491 */
4492 if (s->be->cookie_name || sess->fe->capture_name)
4493 manage_client_side_cookies(s, req);
4494
Willy Tarreau0b748332014-04-29 00:13:29 +02004495 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02004496 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004497 s->logs.tv_request = now;
Willy Tarreau58727ec2016-05-25 16:23:59 +02004498 http_reply_and_close(s, txn->status, http_error_message(s, deny_status));
Willy Tarreau87b09662015-04-03 00:22:06 +02004499 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004500 sess->fe->fe_counters.denied_req++;
4501 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004502 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004503 if (sess->listener->counters)
4504 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004505 goto return_prx_cond;
4506
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004507 return_bad_req:
4508 /* We centralize bad requests processing here */
4509 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4510 /* we detected a parsing error. We want to archive this request
4511 * in the dedicated proxy area for later troubleshooting.
4512 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004513 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004514 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004515
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004516 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004517 txn->req.msg_state = HTTP_MSG_ERROR;
4518 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004519 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004520
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004521 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004522 if (sess->listener->counters)
4523 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004524
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004525 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02004526 if (!(s->flags & SF_ERR_MASK))
4527 s->flags |= SF_ERR_PRXCOND;
4528 if (!(s->flags & SF_FINST_MASK))
4529 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004530
Christopher Faulet0184ea72017-01-05 14:06:34 +01004531 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004532 req->analyse_exp = TICK_ETERNITY;
4533 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004534
4535 return_prx_yield:
4536 channel_dont_connect(req);
4537 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004538}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004539
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004540/* This function performs all the processing enabled for the current request.
4541 * It returns 1 if the processing can continue on next analysers, or zero if it
4542 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004543 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004544 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004545int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004546{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004547 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004548 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004549 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02004550 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004551
Willy Tarreau655dce92009-11-08 13:10:58 +01004552 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004553 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004554 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004555 return 0;
4556 }
4557
Willy Tarreau87b09662015-04-03 00:22:06 +02004558 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 +02004559 now_ms, __FUNCTION__,
4560 s,
4561 req,
4562 req->rex, req->wex,
4563 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004564 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004565 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004566
Willy Tarreau59234e92008-11-30 23:51:27 +01004567 /*
4568 * Right now, we know that we have processed the entire headers
4569 * and that unwanted requests have been filtered out. We can do
4570 * whatever we want with the remaining request. Also, now we
4571 * may have separate values for ->fe, ->be.
4572 */
Willy Tarreau06619262006-12-17 08:37:22 +01004573
Willy Tarreau59234e92008-11-30 23:51:27 +01004574 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004575 * If HTTP PROXY is set we simply get remote server address parsing
4576 * incoming request. Note that this requires that a connection is
4577 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004578 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004579 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004580 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004581 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004582
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004583 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau973a5422015-08-05 21:47:23 +02004584 if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004585 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004586 txn->req.msg_state = HTTP_MSG_ERROR;
4587 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01004588 req->analysers &= AN_REQ_FLT_END;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004589 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004590
Willy Tarreaue7dff022015-04-03 01:14:29 +02004591 if (!(s->flags & SF_ERR_MASK))
4592 s->flags |= SF_ERR_RESOURCE;
4593 if (!(s->flags & SF_FINST_MASK))
4594 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004595
4596 return 0;
4597 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004598
4599 path = http_get_path(txn);
4600 url2sa(req->buf->p + msg->sl.rq.u,
4601 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004602 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004603 /* if the path was found, we have to remove everything between
4604 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4605 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4606 * u_l characters by a single "/".
4607 */
4608 if (path) {
4609 char *cur_ptr = req->buf->p;
4610 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4611 int delta;
4612
4613 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4614 http_msg_move_end(&txn->req, delta);
4615 cur_end += delta;
4616 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4617 goto return_bad_req;
4618 }
4619 else {
4620 char *cur_ptr = req->buf->p;
4621 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4622 int delta;
4623
4624 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4625 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4626 http_msg_move_end(&txn->req, delta);
4627 cur_end += delta;
4628 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4629 goto return_bad_req;
4630 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004631 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004632
Willy Tarreau59234e92008-11-30 23:51:27 +01004633 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004634 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004635 * Note that doing so might move headers in the request, but
4636 * the fields will stay coherent and the URI will not move.
4637 * This should only be performed in the backend.
4638 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004639 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004640 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004641
William Lallemanda73203e2012-03-12 12:48:57 +01004642 /* add unique-id if "header-unique-id" is specified */
4643
Thierry Fournierf4011dd2016-03-29 17:23:51 +02004644 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004645 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4646 goto return_bad_req;
4647 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004648 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004649 }
William Lallemanda73203e2012-03-12 12:48:57 +01004650
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004651 if (sess->fe->header_unique_id && s->unique_id) {
4652 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004653 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004654 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004655 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004656 goto return_bad_req;
4657 }
4658
Cyril Bontéb21570a2009-11-29 20:04:48 +01004659 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004660 * 9: add X-Forwarded-For if either the frontend or the backend
4661 * asks for it.
4662 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004663 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004664 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004665 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
4666 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
4667 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004668 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004669 /* The header is set to be added only if none is present
4670 * and we found it, so don't do anything.
4671 */
4672 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004673 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004674 /* Add an X-Forwarded-For header unless the source IP is
4675 * in the 'except' network range.
4676 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004677 if ((!sess->fe->except_mask.s_addr ||
4678 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
4679 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01004680 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004681 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004682 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004683 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004684 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004685 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004686
4687 /* Note: we rely on the backend to get the header name to be used for
4688 * x-forwarded-for, because the header is really meant for the backends.
4689 * However, if the backend did not specify any option, we have to rely
4690 * on the frontend's header name.
4691 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004692 if (s->be->fwdfor_hdr_len) {
4693 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004694 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004695 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004696 len = sess->fe->fwdfor_hdr_len;
4697 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004698 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004699 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 +01004700
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004701 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004702 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004703 }
4704 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004705 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004706 /* FIXME: for the sake of completeness, we should also support
4707 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004708 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004709 int len;
4710 char pn[INET6_ADDRSTRLEN];
4711 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004712 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004713 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004714
Willy Tarreau59234e92008-11-30 23:51:27 +01004715 /* Note: we rely on the backend to get the header name to be used for
4716 * x-forwarded-for, because the header is really meant for the backends.
4717 * However, if the backend did not specify any option, we have to rely
4718 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004719 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004720 if (s->be->fwdfor_hdr_len) {
4721 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004722 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004723 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004724 len = sess->fe->fwdfor_hdr_len;
4725 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004726 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004727 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004728
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004729 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004730 goto return_bad_req;
4731 }
4732 }
4733
4734 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004735 * 10: add X-Original-To if either the frontend or the backend
4736 * asks for it.
4737 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004738 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004739
4740 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004741 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004742 /* Add an X-Original-To header unless the destination IP is
4743 * in the 'except' network range.
4744 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004745 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004746
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004747 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004748 ((!sess->fe->except_mask_to.s_addr ||
4749 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
4750 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004751 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004752 (((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 +02004753 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004754 int len;
4755 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004756 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004757
4758 /* Note: we rely on the backend to get the header name to be used for
4759 * x-original-to, because the header is really meant for the backends.
4760 * However, if the backend did not specify any option, we have to rely
4761 * on the frontend's header name.
4762 */
4763 if (s->be->orgto_hdr_len) {
4764 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004765 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004766 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004767 len = sess->fe->orgto_hdr_len;
4768 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004769 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004770 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 +02004771
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004772 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004773 goto return_bad_req;
4774 }
4775 }
4776 }
4777
Willy Tarreau50fc7772012-11-11 22:19:57 +01004778 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4779 * If an "Upgrade" token is found, the header is left untouched in order not to have
4780 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4781 * "Upgrade" is present in the Connection header.
4782 */
4783 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4784 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004785 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004786 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004787 unsigned int want_flags = 0;
4788
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004789 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004790 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004791 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004792 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004793 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004794 want_flags |= TX_CON_CLO_SET;
4795 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004796 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004797 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004798 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004799 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004800 want_flags |= TX_CON_KAL_SET;
4801 }
4802
4803 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004804 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004805 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004806
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004807
Willy Tarreau522d6c02009-12-06 18:49:18 +01004808 /* If we have no server assigned yet and we're balancing on url_param
4809 * with a POST request, we may be interested in checking the body for
4810 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004811 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004812 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02004813 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004814 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004815 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004816 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004817 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004818
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004819 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004820 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004821 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004822#ifdef TCP_QUICKACK
4823 /* We expect some data from the client. Unless we know for sure
4824 * we already have a full request, we have to re-enable quick-ack
4825 * in case we previously disabled it, otherwise we might cause
4826 * the client to delay further data.
4827 */
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004828 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004829 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004830 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004831 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004832 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004833#endif
4834 }
Willy Tarreau03945942009-12-22 16:50:27 +01004835
Willy Tarreau59234e92008-11-30 23:51:27 +01004836 /*************************************************************
4837 * OK, that's finished for the headers. We have done what we *
4838 * could. Let's switch to the DATA state. *
4839 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004840 req->analyse_exp = TICK_ETERNITY;
4841 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004842
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004843 /* if the server closes the connection, we want to immediately react
4844 * and close the socket to save packets and syscalls.
4845 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004846 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
Willy Tarreau350f4872014-11-28 14:42:25 +01004847 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004848
Willy Tarreau59234e92008-11-30 23:51:27 +01004849 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004850 /* OK let's go on with the BODY now */
4851 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004852
Willy Tarreau59234e92008-11-30 23:51:27 +01004853 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004854 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004855 /* we detected a parsing error. We want to archive this request
4856 * in the dedicated proxy area for later troubleshooting.
4857 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004858 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004859 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004860
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004861 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01004862 txn->req.msg_state = HTTP_MSG_ERROR;
4863 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01004864 req->analysers &= AN_REQ_FLT_END;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004865 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004866
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004867 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004868 if (sess->listener->counters)
4869 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004870
Willy Tarreaue7dff022015-04-03 01:14:29 +02004871 if (!(s->flags & SF_ERR_MASK))
4872 s->flags |= SF_ERR_PRXCOND;
4873 if (!(s->flags & SF_FINST_MASK))
4874 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004875 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004876}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004877
Willy Tarreau60b85b02008-11-30 23:28:40 +01004878/* This function is an analyser which processes the HTTP tarpit. It always
4879 * returns zero, at the beginning because it prevents any other processing
4880 * from occurring, and at the end because it terminates the request.
4881 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004882int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004883{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004884 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004885
4886 /* This connection is being tarpitted. The CLIENT side has
4887 * already set the connect expiration date to the right
4888 * timeout. We just have to check that the client is still
4889 * there and that the timeout has not expired.
4890 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004891 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004892 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004893 !tick_is_expired(req->analyse_exp, now_ms))
4894 return 0;
4895
4896 /* We will set the queue timer to the time spent, just for
4897 * logging purposes. We fake a 500 server error, so that the
4898 * attacker will not suspect his connection has been tarpitted.
4899 * It will not cause trouble to the logs because we can exclude
4900 * the tarpitted connections by filtering on the 'PT' status flags.
4901 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004902 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4903
4904 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004905 if (!(req->flags & CF_READ_ERROR))
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004906 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004907
Christopher Faulet0184ea72017-01-05 14:06:34 +01004908 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004909 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004910
Willy Tarreaue7dff022015-04-03 01:14:29 +02004911 if (!(s->flags & SF_ERR_MASK))
4912 s->flags |= SF_ERR_PRXCOND;
4913 if (!(s->flags & SF_FINST_MASK))
4914 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004915 return 0;
4916}
4917
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004918/* This function is an analyser which waits for the HTTP request body. It waits
4919 * for either the buffer to be full, or the full advertised contents to have
4920 * reached the buffer. It must only be called after the standard HTTP request
4921 * processing has occurred, because it expects the request to be parsed and will
4922 * look for the Expect header. It may send a 100-Continue interim response. It
4923 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4924 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4925 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004926 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004927int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004928{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004929 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004930 struct http_txn *txn = s->txn;
4931 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004932
4933 /* We have to parse the HTTP request body to find any required data.
4934 * "balance url_param check_post" should have been the only way to get
4935 * into this. We were brought here after HTTP header analysis, so all
4936 * related structures are ready.
4937 */
4938
Willy Tarreau890988f2014-04-10 11:59:33 +02004939 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4940 /* This is the first call */
4941 if (msg->msg_state < HTTP_MSG_BODY)
4942 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004943
Willy Tarreau890988f2014-04-10 11:59:33 +02004944 if (msg->msg_state < HTTP_MSG_100_SENT) {
4945 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4946 * send an HTTP/1.1 100 Continue intermediate response.
4947 */
4948 if (msg->flags & HTTP_MSGF_VER_11) {
4949 struct hdr_ctx ctx;
4950 ctx.idx = 0;
4951 /* Expect is allowed in 1.1, look for it */
4952 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4953 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004954 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01004955 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02004956 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004957 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004958 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004959 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004960
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004961 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004962 * req->buf->p still points to the beginning of the message. We
4963 * must save the body in msg->next because it survives buffer
4964 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004965 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004966 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004967
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004968 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004969 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4970 else
4971 msg->msg_state = HTTP_MSG_DATA;
4972 }
4973
Willy Tarreau890988f2014-04-10 11:59:33 +02004974 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4975 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004976 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004977 goto missing_data;
4978
4979 /* OK we have everything we need now */
4980 goto http_end;
4981 }
4982
4983 /* OK here we're parsing a chunked-encoded message */
4984
Willy Tarreau522d6c02009-12-06 18:49:18 +01004985 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004986 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004987 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004988 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004989 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004990 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004991
Willy Tarreau115acb92009-12-26 13:56:06 +01004992 if (!ret)
4993 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004994 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004995 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004996 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004997 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01004998 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004999 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01005000 }
5001
Willy Tarreaud98cf932009-12-27 22:54:55 +01005002 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02005003 * We have the first data byte is in msg->sov + msg->sol. We're waiting
5004 * for at least a whole chunk or the whole content length bytes after
5005 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01005006 */
Willy Tarreau890988f2014-04-10 11:59:33 +02005007 if (msg->msg_state == HTTP_MSG_TRAILERS)
5008 goto http_end;
5009
Willy Tarreaue115b492015-05-01 23:05:14 +02005010 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01005011 goto http_end;
5012
5013 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02005014 /* we get here if we need to wait for more data. If the buffer is full,
5015 * we have the maximum we can expect.
5016 */
5017 if (buffer_full(req->buf, global.tune.maxrewrite))
5018 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01005019
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005020 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01005021 txn->status = 408;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005022 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02005023
Willy Tarreaue7dff022015-04-03 01:14:29 +02005024 if (!(s->flags & SF_ERR_MASK))
5025 s->flags |= SF_ERR_CLITO;
5026 if (!(s->flags & SF_FINST_MASK))
5027 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005028 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01005029 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01005030
5031 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02005032 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01005033 /* Not enough data. We'll re-use the http-request
5034 * timeout here. Ideally, we should set the timeout
5035 * relative to the accept() date. We just set the
5036 * request timeout once at the beginning of the
5037 * request.
5038 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005039 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01005040 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02005041 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01005042 return 0;
5043 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01005044
5045 http_end:
5046 /* The situation will not evolve, so let's give up on the analysis. */
5047 s->logs.tv_request = now; /* update the request timer to reflect full request */
5048 req->analysers &= ~an_bit;
5049 req->analyse_exp = TICK_ETERNITY;
5050 return 1;
5051
5052 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005053 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005054 txn->req.msg_state = HTTP_MSG_ERROR;
5055 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005056 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01005057
Willy Tarreaue7dff022015-04-03 01:14:29 +02005058 if (!(s->flags & SF_ERR_MASK))
5059 s->flags |= SF_ERR_PRXCOND;
5060 if (!(s->flags & SF_FINST_MASK))
5061 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005062
Willy Tarreau522d6c02009-12-06 18:49:18 +01005063 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005064 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005065 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005066 if (sess->listener->counters)
5067 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005068 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01005069}
5070
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005071/* send a server's name with an outgoing request over an established connection.
5072 * Note: this function is designed to be called once the request has been scheduled
5073 * for being forwarded. This is the reason why it rewinds the buffer before
5074 * proceeding.
5075 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01005076int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05005077
5078 struct hdr_ctx ctx;
5079
Mark Lamourinec2247f02012-01-04 13:02:01 -05005080 char *hdr_name = be->server_id_hdr_name;
5081 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02005082 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005083 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005084 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005085
William Lallemandd9e90662012-01-30 17:27:17 +01005086 ctx.idx = 0;
5087
Willy Tarreau211cdec2014-04-17 20:18:08 +02005088 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005089 if (old_o) {
5090 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005091 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02005092 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02005093 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005094 }
5095
Willy Tarreau9b28e032012-10-12 23:49:43 +02005096 old_i = chn->buf->i;
5097 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 -05005098 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005099 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005100 }
5101
5102 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005103 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005104 memcpy(hdr_val, hdr_name, hdr_name_len);
5105 hdr_val += hdr_name_len;
5106 *hdr_val++ = ':';
5107 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005108 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
5109 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005110
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005111 if (old_o) {
5112 /* If this was a forwarded request, we must readjust the amount of
5113 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02005114 * variations. Note that the current state is >= HTTP_MSG_BODY,
5115 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005116 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02005117 old_o += chn->buf->i - old_i;
5118 b_adv(chn->buf, old_o);
5119 txn->req.next -= old_o;
5120 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005121 }
5122
Mark Lamourinec2247f02012-01-04 13:02:01 -05005123 return 0;
5124}
5125
Willy Tarreau610ecce2010-01-04 21:15:02 +01005126/* Terminate current transaction and prepare a new one. This is very tricky
5127 * right now but it works.
5128 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005129void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005130{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005131 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005132 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01005133 struct proxy *be = s->be;
Willy Tarreau323a2d92015-08-04 19:00:17 +02005134 struct connection *srv_conn;
5135 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02005136 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01005137
Willy Tarreau610ecce2010-01-04 21:15:02 +01005138 /* FIXME: We need a more portable way of releasing a backend's and a
5139 * server's connections. We need a safer way to reinitialize buffer
5140 * flags. We also need a more accurate method for computing per-request
5141 * data.
5142 */
Willy Tarreau323a2d92015-08-04 19:00:17 +02005143 srv_conn = objt_conn(s->si[1].end);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005144
Willy Tarreau4213a112013-12-15 10:25:42 +01005145 /* unless we're doing keep-alive, we want to quickly close the connection
5146 * to the server.
5147 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005148 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005149 !si_conn_ready(&s->si[1])) {
5150 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
5151 si_shutr(&s->si[1]);
5152 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01005153 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005154
Willy Tarreaue7dff022015-04-03 01:14:29 +02005155 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau858b1032015-12-07 17:04:59 +01005156 be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01005157 if (unlikely(s->srv_conn))
5158 sess_change_server(s, NULL);
5159 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005160
5161 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02005162 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005163
Willy Tarreaueee5b512015-04-03 23:46:31 +02005164 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005165 int n;
5166
Willy Tarreaueee5b512015-04-03 23:46:31 +02005167 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005168 if (n < 1 || n > 5)
5169 n = 0;
5170
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005171 if (fe->mode == PR_MODE_HTTP) {
5172 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005173 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02005174 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01005175 (be->mode == PR_MODE_HTTP)) {
5176 be->be_counters.p.http.rsp[n]++;
5177 be->be_counters.p.http.cum_req++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005178 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005179 }
5180
5181 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005182 s->logs.bytes_in -= s->req.buf->i;
5183 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005184
5185 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005186 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005187 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005188 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005189 s->do_log(s);
5190 }
5191
Willy Tarreaud713bcc2014-06-25 15:36:04 +02005192 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02005193 stream_stop_content_counters(s);
5194 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02005195
Willy Tarreau610ecce2010-01-04 21:15:02 +01005196 s->logs.accept_date = date; /* user-visible date for logging */
5197 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02005198 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
5199 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005200 tv_zero(&s->logs.tv_request);
5201 s->logs.t_queue = -1;
5202 s->logs.t_connect = -1;
5203 s->logs.t_data = -1;
5204 s->logs.t_close = 0;
5205 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
5206 s->logs.srv_queue_size = 0; /* we will get this number soon */
5207
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005208 s->logs.bytes_in = s->req.total = s->req.buf->i;
5209 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005210
5211 if (s->pend_pos)
5212 pendconn_free(s->pend_pos);
5213
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005214 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005215 if (s->flags & SF_CURR_SESS) {
5216 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005217 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005218 }
Willy Tarreau858b1032015-12-07 17:04:59 +01005219 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005220 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01005221 }
5222
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005223 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005224
Willy Tarreau4213a112013-12-15 10:25:42 +01005225 /* only release our endpoint if we don't intend to reuse the
5226 * connection.
5227 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005228 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005229 !si_conn_ready(&s->si[1])) {
5230 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02005231 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005232 }
5233
Willy Tarreau350f4872014-11-28 14:42:25 +01005234 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
5235 s->si[1].err_type = SI_ET_NONE;
5236 s->si[1].conn_retries = 0; /* used for logging too */
5237 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02005238 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 +01005239 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);
5240 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 +02005241 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
5242 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
5243 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005244
Willy Tarreaueee5b512015-04-03 23:46:31 +02005245 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005246 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02005247 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005248
5249 if (prev_status == 401 || prev_status == 407) {
5250 /* In HTTP keep-alive mode, if we receive a 401, we still have
5251 * a chance of being able to send the visitor again to the same
5252 * server over the same connection. This is required by some
5253 * broken protocols such as NTLM, and anyway whenever there is
5254 * an opportunity for sending the challenge to the proper place,
5255 * it's better to do it (at least it helps with debugging).
5256 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005257 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02005258 if (srv_conn)
5259 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01005260 }
5261
Willy Tarreau53f96852016-02-02 18:50:47 +01005262 /* Never ever allow to reuse a connection from a non-reuse backend */
5263 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
5264 srv_conn->flags |= CO_FL_PRIVATE;
5265
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005266 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005267 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005268
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005269 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005270 s->req.flags |= CF_NEVER_WAIT;
5271 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005272 }
5273
Willy Tarreau610ecce2010-01-04 21:15:02 +01005274 /* if the request buffer is not empty, it means we're
5275 * about to process another request, so send pending
5276 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005277 * Just don't do this if the buffer is close to be full,
5278 * because the request will wait for it to flush a little
5279 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005280 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005281 if (s->req.buf->i) {
5282 if (s->res.buf->o &&
5283 !buffer_full(s->res.buf, global.tune.maxrewrite) &&
5284 bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)
5285 s->res.flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005286 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005287
Willy Tarreau714ea782015-11-25 20:11:11 +01005288 /* we're removing the analysers, we MUST re-enable events detection.
5289 * We don't enable close on the response channel since it's either
5290 * already closed, or in keep-alive with an idle connection handler.
5291 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005292 channel_auto_read(&s->req);
5293 channel_auto_close(&s->req);
5294 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005295
Willy Tarreau1c59bd52015-11-02 20:20:11 +01005296 /* we're in keep-alive with an idle connection, monitor it if not already done */
5297 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02005298 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005299 if (!srv)
5300 si_idle_conn(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01005301 else if (srv_conn->flags & CO_FL_PRIVATE)
Willy Tarreau8dff9982015-08-04 20:45:52 +02005302 si_idle_conn(&s->si[1], &srv->priv_conns);
Willy Tarreau449d74a2015-08-05 17:16:33 +02005303 else if (prev_flags & TX_NOT_FIRST)
5304 /* note: we check the request, not the connection, but
5305 * this is valid for strategies SAFE and AGGR, and in
5306 * case of ALWS, we don't care anyway.
5307 */
5308 si_idle_conn(&s->si[1], &srv->safe_conns);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005309 else
5310 si_idle_conn(&s->si[1], &srv->idle_conns);
Willy Tarreau4320eaa2015-08-05 11:08:30 +02005311 }
Willy Tarreau27375622013-12-17 00:00:28 +01005312
Christopher Faulet3e344292015-11-24 16:24:13 +01005313 if (HAS_FILTERS(s)) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01005314 s->req.analysers &= AN_REQ_FLT_END;
5315 s->res.analysers &= AN_RES_FLT_END;
Christopher Fauletd7c91962015-04-30 11:48:27 +02005316 }
Christopher Faulet3e344292015-11-24 16:24:13 +01005317 else {
5318 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
5319 s->res.analysers = 0;
5320 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005321}
5322
5323
5324/* This function updates the request state machine according to the response
5325 * state machine and buffer flags. It returns 1 if it changes anything (flag
5326 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5327 * it is only used to find when a request/response couple is complete. Both
5328 * this function and its equivalent should loop until both return zero. It
5329 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5330 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005331int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005332{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005333 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005334 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005335 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005336 unsigned int old_state = txn->req.msg_state;
5337
Willy Tarreau610ecce2010-01-04 21:15:02 +01005338 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5339 return 0;
5340
5341 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005342 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005343 * We can shut the read side unless we want to abort_on_close,
5344 * or we have a POST request. The issue with POST requests is
5345 * that some browsers still send a CRLF after the request, and
5346 * this CRLF must be read so that it does not remain in the kernel
5347 * buffers, otherwise a close could cause an RST on some systems
5348 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005349 * Note that if we're using keep-alive on the client side, we'd
5350 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005351 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005352 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005353 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005354 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5355 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5356 !(s->be->options & PR_O_ABRT_CLOSE) &&
5357 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005358 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005359
Willy Tarreau40f151a2012-12-20 12:10:09 +01005360 /* if the server closes the connection, we want to immediately react
5361 * and close the socket to save packets and syscalls.
5362 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005363 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005364
Willy Tarreau7f876a12015-11-18 11:59:55 +01005365 /* In any case we've finished parsing the request so we must
5366 * disable Nagle when sending data because 1) we're not going
5367 * to shut this side, and 2) the server is waiting for us to
5368 * send pending data.
5369 */
5370 chn->flags |= CF_NEVER_WAIT;
5371
Willy Tarreau610ecce2010-01-04 21:15:02 +01005372 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5373 goto wait_other_side;
5374
5375 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5376 /* The server has not finished to respond, so we
5377 * don't want to move in order not to upset it.
5378 */
5379 goto wait_other_side;
5380 }
5381
5382 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5383 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005384 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005385 txn->req.msg_state = HTTP_MSG_TUNNEL;
5386 goto wait_other_side;
5387 }
5388
5389 /* When we get here, it means that both the request and the
5390 * response have finished receiving. Depending on the connection
5391 * mode, we'll have to wait for the last bytes to leave in either
5392 * direction, and sometimes for a close to be effective.
5393 */
5394
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005395 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5396 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005397 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5398 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005399 }
5400 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5401 /* Option forceclose is set, or either side wants to close,
5402 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005403 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005404 * once both states are CLOSED.
5405 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005406 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5407 channel_shutr_now(chn);
5408 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005409 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005410 }
5411 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005412 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5413 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005414 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005415 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5416 channel_auto_read(chn);
5417 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005418 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005419 }
5420
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005421 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005422 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005423 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005424
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005425 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005426 txn->req.msg_state = HTTP_MSG_CLOSING;
5427 goto http_msg_closing;
5428 }
5429 else {
5430 txn->req.msg_state = HTTP_MSG_CLOSED;
5431 goto http_msg_closed;
5432 }
5433 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005434 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005435 }
5436
5437 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5438 http_msg_closing:
5439 /* nothing else to forward, just waiting for the output buffer
5440 * to be empty and for the shutw_now to take effect.
5441 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005442 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005443 txn->req.msg_state = HTTP_MSG_CLOSED;
5444 goto http_msg_closed;
5445 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005446 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005447 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005448 txn->req.msg_state = HTTP_MSG_ERROR;
5449 goto wait_other_side;
5450 }
5451 }
5452
5453 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5454 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005455 /* see above in MSG_DONE why we only do this in these states */
5456 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5457 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5458 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005459 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005460 goto wait_other_side;
5461 }
5462
5463 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005464 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005465}
5466
5467
5468/* This function updates the response state machine according to the request
5469 * state machine and buffer flags. It returns 1 if it changes anything (flag
5470 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5471 * it is only used to find when a request/response couple is complete. Both
5472 * this function and its equivalent should loop until both return zero. It
5473 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5474 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005475int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005476{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005477 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005478 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005479 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005480 unsigned int old_state = txn->rsp.msg_state;
5481
Willy Tarreau610ecce2010-01-04 21:15:02 +01005482 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5483 return 0;
5484
5485 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5486 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005487 * still monitor the server connection for a possible close
5488 * while the request is being uploaded, so we don't disable
5489 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005490 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005491 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005492
5493 if (txn->req.msg_state == HTTP_MSG_ERROR)
5494 goto wait_other_side;
5495
5496 if (txn->req.msg_state < HTTP_MSG_DONE) {
5497 /* The client seems to still be sending data, probably
5498 * because we got an error response during an upload.
5499 * We have the choice of either breaking the connection
5500 * or letting it pass through. Let's do the later.
5501 */
5502 goto wait_other_side;
5503 }
5504
5505 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5506 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005507 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005508 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005509 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005510 goto wait_other_side;
5511 }
5512
5513 /* When we get here, it means that both the request and the
5514 * response have finished receiving. Depending on the connection
5515 * mode, we'll have to wait for the last bytes to leave in either
5516 * direction, and sometimes for a close to be effective.
5517 */
5518
5519 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5520 /* Server-close mode : shut read and wait for the request
5521 * side to close its output buffer. The caller will detect
5522 * when we're in DONE and the other is in CLOSED and will
5523 * catch that for the final cleanup.
5524 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005525 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5526 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005527 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005528 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5529 /* Option forceclose is set, or either side wants to close,
5530 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005531 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005532 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005533 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005534 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5535 channel_shutr_now(chn);
5536 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005537 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005538 }
5539 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005540 /* The last possible modes are keep-alive and tunnel. Tunnel will
5541 * need to forward remaining data. Keep-alive will need to monitor
5542 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005543 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005544 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005545 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005546 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5547 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005548 }
5549
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005550 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005551 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005552 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005553 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5554 goto http_msg_closing;
5555 }
5556 else {
5557 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5558 goto http_msg_closed;
5559 }
5560 }
5561 goto wait_other_side;
5562 }
5563
5564 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5565 http_msg_closing:
5566 /* nothing else to forward, just waiting for the output buffer
5567 * to be empty and for the shutw_now to take effect.
5568 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005569 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005570 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5571 goto http_msg_closed;
5572 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005573 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005574 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005575 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005576 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005577 if (objt_server(s->target))
5578 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005579 goto wait_other_side;
5580 }
5581 }
5582
5583 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5584 http_msg_closed:
5585 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005586 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005587 channel_auto_close(chn);
5588 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005589 goto wait_other_side;
5590 }
5591
5592 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005593 /* We force the response to leave immediately if we're waiting for the
5594 * other side, since there is no pending shutdown to push it out.
5595 */
5596 if (!channel_is_empty(chn))
5597 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005598 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005599}
5600
5601
5602/* Resync the request and response state machines. Return 1 if either state
5603 * changes.
5604 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005605int http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005606{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005607 struct http_txn *txn = s->txn;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005608 int old_req_state = txn->req.msg_state;
5609 int old_res_state = txn->rsp.msg_state;
5610
Willy Tarreau610ecce2010-01-04 21:15:02 +01005611 http_sync_req_state(s);
5612 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005613 if (!http_sync_res_state(s))
5614 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005615 if (!http_sync_req_state(s))
5616 break;
5617 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005618
Willy Tarreau610ecce2010-01-04 21:15:02 +01005619 /* OK, both state machines agree on a compatible state.
5620 * There are a few cases we're interested in :
5621 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5622 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5623 * directions, so let's simply disable both analysers.
5624 * - HTTP_MSG_CLOSED on the response only means we must abort the
5625 * request.
5626 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5627 * with server-close mode means we've completed one request and we
5628 * must re-initialize the server connection.
5629 */
5630
5631 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5632 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5633 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5634 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01005635 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005636 channel_auto_close(&s->req);
5637 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005638 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005639 channel_auto_close(&s->res);
5640 channel_auto_read(&s->res);
Christopher Faulet6962f4e2016-11-28 14:21:27 +01005641 if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s))
Christopher Faulet0184ea72017-01-05 14:06:34 +01005642 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet6962f4e2016-11-28 14:21:27 +01005643 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s))
Christopher Faulet0184ea72017-01-05 14:06:34 +01005644 s->res.analysers |= AN_RES_FLT_XFER_DATA;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005645 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005646 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005647 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005648 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005649 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01005650 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005651 channel_auto_close(&s->res);
5652 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005653 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005654 channel_abort(&s->req);
5655 channel_auto_close(&s->req);
5656 channel_auto_read(&s->req);
5657 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005658 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005659 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5660 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005661 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005662 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5663 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5664 /* server-close/keep-alive: terminate this transaction,
5665 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02005666 * a fresh-new transaction, but only once we're sure there's
5667 * enough room in the request and response buffer to process
5668 * another request. The request buffer must not hold any
5669 * pending output data and the request buffer must not have
5670 * output data occupying the reserve.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005671 */
Willy Tarreau3de5bd62016-05-02 16:07:07 +02005672 if (s->req.buf->o)
5673 s->req.flags |= CF_WAKE_WRITE;
5674 else if (channel_congested(&s->res))
5675 s->res.flags |= CF_WAKE_WRITE;
5676 else
5677 http_end_txn_clean_session(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005678 }
5679
Willy Tarreau610ecce2010-01-04 21:15:02 +01005680 return txn->req.msg_state != old_req_state ||
5681 txn->rsp.msg_state != old_res_state;
5682}
5683
Willy Tarreaud98cf932009-12-27 22:54:55 +01005684/* This function is an analyser which forwards request body (including chunk
5685 * sizes if any). It is called as soon as we must forward, even if we forward
5686 * zero byte. The only situation where it must not be called is when we're in
5687 * tunnel mode and we want to forward till the close. It's used both to forward
5688 * remaining data and to resync after end of body. It expects the msg_state to
5689 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005690 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005691 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005692 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005693 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005694int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005695{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005696 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005697 struct http_txn *txn = s->txn;
5698 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01005699 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005700
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005701 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5702 return 0;
5703
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005704 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005705 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005706 /* Output closed while we were sending data. We must abort and
5707 * wake the other side up.
5708 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005709 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005710 msg->msg_state = HTTP_MSG_ERROR;
5711 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005712 return 1;
5713 }
5714
Willy Tarreaud98cf932009-12-27 22:54:55 +01005715 /* Note that we don't have to send 100-continue back because we don't
5716 * need the data to complete our job, and it's up to the server to
5717 * decide whether to return 100, 417 or anything else in return of
5718 * an "Expect: 100-continue" header.
5719 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02005720 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005721 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5722 ? HTTP_MSG_CHUNK_SIZE
5723 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005724
5725 /* TODO/filters: when http-buffer-request option is set or if a
5726 * rule on url_param exists, the first chunk size could be
5727 * already parsed. In that case, msg->next is after the chunk
5728 * size (including the CRLF after the size). So this case should
5729 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005730 }
5731
Willy Tarreau7ba23542014-04-17 21:50:00 +02005732 /* Some post-connect processing might want us to refrain from starting to
5733 * forward data. Currently, the only reason for this is "balance url_param"
5734 * whichs need to parse/process the request after we've enabled forwarding.
5735 */
5736 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005737 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005738 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005739 req->flags |= CF_WAKE_CONNECT;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005740 goto missing_data_or_waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005741 }
5742 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5743 }
5744
Willy Tarreau80a92c02014-03-12 10:41:13 +01005745 /* in most states, we should abort in case of early close */
5746 channel_auto_close(req);
5747
Willy Tarreauefdf0942014-04-24 20:08:57 +02005748 if (req->to_forward) {
5749 /* We can't process the buffer's contents yet */
5750 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005751 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005752 }
5753
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005754 if (msg->msg_state < HTTP_MSG_DONE) {
5755 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5756 ? http_msg_forward_chunked_body(s, msg)
5757 : http_msg_forward_body(s, msg));
5758 if (!ret)
5759 goto missing_data_or_waiting;
5760 if (ret < 0)
5761 goto return_bad_req;
5762 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005763
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005764 /* other states, DONE...TUNNEL */
5765 /* we don't want to forward closes on DONE except in tunnel mode. */
5766 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5767 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005768
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005769 if (http_resync_states(s)) {
5770 /* some state changes occurred, maybe the analyser
5771 * was disabled too. */
5772 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5773 if (req->flags & CF_SHUTW) {
5774 /* request errors are most likely due to the
5775 * server aborting the transfer. */
5776 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005777 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005778 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005779 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005780 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005781 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005782 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005783 }
5784
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005785 /* If "option abortonclose" is set on the backend, we want to monitor
5786 * the client's connection and forward any shutdown notification to the
5787 * server, which will decide whether to close or to go on processing the
5788 * request. We only do that in tunnel mode, and not in other modes since
5789 * it can be abused to exhaust source ports. */
5790 if (s->be->options & PR_O_ABRT_CLOSE) {
5791 channel_auto_read(req);
5792 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
5793 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
5794 s->si[1].flags |= SI_FL_NOLINGER;
5795 channel_auto_close(req);
5796 }
5797 else if (s->txn->meth == HTTP_METH_POST) {
5798 /* POST requests may require to read extra CRLF sent by broken
5799 * browsers and which could cause an RST to be sent upon close
5800 * on some systems (eg: Linux). */
5801 channel_auto_read(req);
5802 }
5803 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005804
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005805 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005806 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005807 if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005808 if (!(s->flags & SF_ERR_MASK))
5809 s->flags |= SF_ERR_CLICL;
5810 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005811 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005812 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005813 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005814 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005815 }
5816
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005817 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005818 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005819 if (objt_server(s->target))
5820 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005821
5822 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005823 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005824
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005825 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005826 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005827 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005828
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005829 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005830 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005831 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005832 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005833 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005834
Willy Tarreau5c620922011-05-11 19:56:11 +02005835 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005836 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005837 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005838 * modes are already handled by the stream sock layer. We must not do
5839 * this in content-length mode because it could present the MSG_MORE
5840 * flag with the last block of forwarded data, which would cause an
5841 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005842 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005843 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005844 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005845
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005846 return 0;
5847
Willy Tarreaud98cf932009-12-27 22:54:55 +01005848 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005849 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005850 if (sess->listener->counters)
5851 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005852
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005853 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005854 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005855 txn->req.msg_state = HTTP_MSG_ERROR;
5856 if (txn->status) {
5857 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005858 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005859 } else {
5860 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005861 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005862 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01005863 req->analysers &= AN_REQ_FLT_END;
5864 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 +01005865
Willy Tarreaue7dff022015-04-03 01:14:29 +02005866 if (!(s->flags & SF_ERR_MASK))
5867 s->flags |= SF_ERR_PRXCOND;
5868 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005869 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005870 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005871 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005872 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005873 }
5874 return 0;
5875
5876 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005877 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005878 txn->req.msg_state = HTTP_MSG_ERROR;
5879 if (txn->status) {
5880 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005881 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005882 } else {
5883 txn->status = 502;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005884 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005885 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01005886 req->analysers &= AN_REQ_FLT_END;
5887 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 +01005888
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005889 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005890 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005891 if (objt_server(s->target))
5892 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005893
Willy Tarreaue7dff022015-04-03 01:14:29 +02005894 if (!(s->flags & SF_ERR_MASK))
5895 s->flags |= SF_ERR_SRVCL;
5896 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005897 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005898 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005899 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005900 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005901 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005902 return 0;
5903}
5904
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005905/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5906 * processing can continue on next analysers, or zero if it either needs more
5907 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005908 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005909 * when it has nothing left to do, and may remove any analyser when it wants to
5910 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005911 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005912int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005913{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005914 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005915 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005916 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005917 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005918 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005919 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005920 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005921
Willy Tarreau87b09662015-04-03 00:22:06 +02005922 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 +02005923 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005924 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005925 rep,
5926 rep->rex, rep->wex,
5927 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005928 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005929 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005930
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005931 /*
5932 * Now parse the partial (or complete) lines.
5933 * We will check the response syntax, and also join multi-line
5934 * headers. An index of all the lines will be elaborated while
5935 * parsing.
5936 *
5937 * For the parsing, we use a 28 states FSM.
5938 *
5939 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005940 * rep->buf->p = beginning of response
5941 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5942 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005943 * msg->eol = end of current header or line (LF or CRLF)
5944 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005945 */
5946
Willy Tarreau628c40c2014-04-24 19:11:26 +02005947 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005948 /* There's a protected area at the end of the buffer for rewriting
5949 * purposes. We don't want to start to parse the request if the
5950 * protected area is affected, because we may have to move processed
5951 * data later, which is much more complicated.
5952 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005953 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005954 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005955 /* some data has still not left the buffer, wake us once that's done */
5956 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5957 goto abort_response;
5958 channel_dont_close(rep);
5959 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005960 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005961 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005962 }
5963
Willy Tarreau379357a2013-06-08 12:55:46 +02005964 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5965 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5966 buffer_slow_realign(rep->buf);
5967
Willy Tarreau9b28e032012-10-12 23:49:43 +02005968 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005969 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005970 }
5971
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005972 /* 1: we might have to print this header in debug mode */
5973 if (unlikely((global.mode & MODE_DEBUG) &&
5974 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005975 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005976 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005977
Willy Tarreau9b28e032012-10-12 23:49:43 +02005978 sol = rep->buf->p;
5979 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005980 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005981
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005982 sol += hdr_idx_first_pos(&txn->hdr_idx);
5983 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005984
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005985 while (cur_idx) {
5986 eol = sol + txn->hdr_idx.v[cur_idx].len;
5987 debug_hdr("srvhdr", s, sol, eol);
5988 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5989 cur_idx = txn->hdr_idx.v[cur_idx].next;
5990 }
5991 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005992
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005993 /*
5994 * Now we quickly check if we have found a full valid response.
5995 * If not so, we check the FD and buffer states before leaving.
5996 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005997 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005998 * responses are checked first.
5999 *
6000 * Depending on whether the client is still there or not, we
6001 * may send an error response back or not. Note that normally
6002 * we should only check for HTTP status there, and check I/O
6003 * errors somewhere else.
6004 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006005
Willy Tarreau655dce92009-11-08 13:10:58 +01006006 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006007 /* Invalid response */
6008 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6009 /* we detected a parsing error. We want to archive this response
6010 * in the dedicated proxy area for later troubleshooting.
6011 */
6012 hdr_response_bad:
6013 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006014 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006015
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006016 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006017 if (objt_server(s->target)) {
6018 objt_server(s->target)->counters.failed_resp++;
6019 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006020 }
Willy Tarreau64648412010-03-05 10:41:54 +01006021 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006022 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006023 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006024 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006025 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006026 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006027 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006028
Willy Tarreaue7dff022015-04-03 01:14:29 +02006029 if (!(s->flags & SF_ERR_MASK))
6030 s->flags |= SF_ERR_PRXCOND;
6031 if (!(s->flags & SF_FINST_MASK))
6032 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006033
6034 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006035 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006036
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006037 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006038 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02006039 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02006040 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006041 goto hdr_response_bad;
6042 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006043
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006044 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006045 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006046 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006047 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006048 else if (txn->flags & TX_NOT_FIRST)
6049 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02006050
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006051 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006052 if (objt_server(s->target)) {
6053 objt_server(s->target)->counters.failed_resp++;
6054 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006055 }
Willy Tarreau461f6622008-08-15 23:43:19 +02006056
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006057 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006058 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006059 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006060 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006061 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006062 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02006063
Willy Tarreaue7dff022015-04-03 01:14:29 +02006064 if (!(s->flags & SF_ERR_MASK))
6065 s->flags |= SF_ERR_SRVCL;
6066 if (!(s->flags & SF_FINST_MASK))
6067 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02006068 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006069 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006070
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02006071 /* read timeout : return a 504 to the client. */
6072 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006073 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006074 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006075
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006076 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006077 if (objt_server(s->target)) {
6078 objt_server(s->target)->counters.failed_resp++;
6079 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006080 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006081
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006082 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006083 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006084 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01006085 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006086 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006087 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02006088
Willy Tarreaue7dff022015-04-03 01:14:29 +02006089 if (!(s->flags & SF_ERR_MASK))
6090 s->flags |= SF_ERR_SRVTO;
6091 if (!(s->flags & SF_FINST_MASK))
6092 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006093 return 0;
6094 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02006095
Willy Tarreauf003d372012-11-26 13:35:37 +01006096 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006097 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006098 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01006099 s->be->be_counters.cli_aborts++;
6100 if (objt_server(s->target))
6101 objt_server(s->target)->counters.cli_aborts++;
6102
Christopher Faulet0184ea72017-01-05 14:06:34 +01006103 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01006104 channel_auto_close(rep);
6105
6106 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01006107 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006108 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreauf003d372012-11-26 13:35:37 +01006109
Willy Tarreaue7dff022015-04-03 01:14:29 +02006110 if (!(s->flags & SF_ERR_MASK))
6111 s->flags |= SF_ERR_CLICL;
6112 if (!(s->flags & SF_FINST_MASK))
6113 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01006114
Willy Tarreau87b09662015-04-03 00:22:06 +02006115 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01006116 return 0;
6117 }
6118
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006119 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006120 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006121 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006122 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006123 else if (txn->flags & TX_NOT_FIRST)
6124 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006125
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006126 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006127 if (objt_server(s->target)) {
6128 objt_server(s->target)->counters.failed_resp++;
6129 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006130 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006131
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006132 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006133 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006134 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006135 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006136 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006137 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01006138
Willy Tarreaue7dff022015-04-03 01:14:29 +02006139 if (!(s->flags & SF_ERR_MASK))
6140 s->flags |= SF_ERR_SRVCL;
6141 if (!(s->flags & SF_FINST_MASK))
6142 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006143 return 0;
6144 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006145
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006146 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006147 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006148 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006149 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006150 else if (txn->flags & TX_NOT_FIRST)
6151 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006152
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006153 s->be->be_counters.failed_resp++;
Christopher Faulet0184ea72017-01-05 14:06:34 +01006154 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006155 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006156
Willy Tarreaue7dff022015-04-03 01:14:29 +02006157 if (!(s->flags & SF_ERR_MASK))
6158 s->flags |= SF_ERR_CLICL;
6159 if (!(s->flags & SF_FINST_MASK))
6160 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006161
Willy Tarreau87b09662015-04-03 00:22:06 +02006162 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006163 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006164 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006165
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006166 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01006167 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006168 return 0;
6169 }
6170
6171 /* More interesting part now : we know that we have a complete
6172 * response which at least looks like HTTP. We have an indicator
6173 * of each header's length, so we can parse them quickly.
6174 */
6175
6176 if (unlikely(msg->err_pos >= 0))
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006177 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006178
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006179 /*
6180 * 1: get the status code
6181 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006182 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006183 if (n < 1 || n > 5)
6184 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02006185 /* when the client triggers a 4xx from the server, it's most often due
6186 * to a missing object or permission. These events should be tracked
6187 * because if they happen often, it may indicate a brute force or a
6188 * vulnerability scan.
6189 */
6190 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02006191 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02006192
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006193 if (objt_server(s->target))
6194 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006195
Willy Tarreau91852eb2015-05-01 13:26:00 +02006196 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
6197 * exactly one digit "." one digit. This check may be disabled using
6198 * option accept-invalid-http-response.
6199 */
6200 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
6201 if (msg->sl.st.v_l != 8) {
6202 msg->err_pos = 0;
6203 goto hdr_response_bad;
6204 }
6205
6206 if (rep->buf->p[4] != '/' ||
6207 !isdigit((unsigned char)rep->buf->p[5]) ||
6208 rep->buf->p[6] != '.' ||
6209 !isdigit((unsigned char)rep->buf->p[7])) {
6210 msg->err_pos = 4;
6211 goto hdr_response_bad;
6212 }
6213 }
6214
Willy Tarreau5b154472009-12-21 20:11:07 +01006215 /* check if the response is HTTP/1.1 or above */
6216 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006217 ((rep->buf->p[5] > '1') ||
6218 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006219 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006220
6221 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006222 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 +01006223
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006224 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006225 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006226
Willy Tarreau9b28e032012-10-12 23:49:43 +02006227 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006228
Willy Tarreau39650402010-03-15 19:44:39 +01006229 /* Adjust server's health based on status code. Note: status codes 501
6230 * and 505 are triggered on demand by client request, so we must not
6231 * count them as server failures.
6232 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006233 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006234 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006235 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006236 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006237 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006238 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006239
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006240 /*
6241 * 2: check for cacheability.
6242 */
6243
6244 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006245 case 100:
6246 /*
6247 * We may be facing a 100-continue response, in which case this
6248 * is not the right response, and we're waiting for the next one.
6249 * Let's allow this response to go to the client and wait for the
6250 * next one.
6251 */
6252 hdr_idx_init(&txn->hdr_idx);
6253 msg->next -= channel_forward(rep, msg->next);
6254 msg->msg_state = HTTP_MSG_RPBEFORE;
6255 txn->status = 0;
6256 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01006257 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02006258 goto next_one;
6259
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006260 case 200:
6261 case 203:
6262 case 206:
6263 case 300:
6264 case 301:
6265 case 410:
6266 /* RFC2616 @13.4:
6267 * "A response received with a status code of
6268 * 200, 203, 206, 300, 301 or 410 MAY be stored
6269 * by a cache (...) unless a cache-control
6270 * directive prohibits caching."
6271 *
6272 * RFC2616 @9.5: POST method :
6273 * "Responses to this method are not cacheable,
6274 * unless the response includes appropriate
6275 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006276 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006277 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006278 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006279 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6280 break;
6281 default:
6282 break;
6283 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006284
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006285 /*
6286 * 3: we may need to capture headers
6287 */
6288 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006289 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006290 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006291 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006292
Willy Tarreau557f1992015-05-01 10:05:17 +02006293 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6294 * by RFC7230#3.3.3 :
6295 *
6296 * The length of a message body is determined by one of the following
6297 * (in order of precedence):
6298 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006299 * 1. Any 2xx (Successful) response to a CONNECT request implies that
6300 * the connection will become a tunnel immediately after the empty
6301 * line that concludes the header fields. A client MUST ignore
6302 * any Content-Length or Transfer-Encoding header fields received
6303 * in such a message. Any 101 response (Switching Protocols) is
6304 * managed in the same manner.
6305 *
6306 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02006307 * (Informational), 204 (No Content), or 304 (Not Modified) status
6308 * code is always terminated by the first empty line after the
6309 * header fields, regardless of the header fields present in the
6310 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006311 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006312 * 3. If a Transfer-Encoding header field is present and the chunked
6313 * transfer coding (Section 4.1) is the final encoding, the message
6314 * body length is determined by reading and decoding the chunked
6315 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006316 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006317 * If a Transfer-Encoding header field is present in a response and
6318 * the chunked transfer coding is not the final encoding, the
6319 * message body length is determined by reading the connection until
6320 * it is closed by the server. If a Transfer-Encoding header field
6321 * is present in a request and the chunked transfer coding is not
6322 * the final encoding, the message body length cannot be determined
6323 * reliably; the server MUST respond with the 400 (Bad Request)
6324 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006325 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006326 * If a message is received with both a Transfer-Encoding and a
6327 * Content-Length header field, the Transfer-Encoding overrides the
6328 * Content-Length. Such a message might indicate an attempt to
6329 * perform request smuggling (Section 9.5) or response splitting
6330 * (Section 9.4) and ought to be handled as an error. A sender MUST
6331 * remove the received Content-Length field prior to forwarding such
6332 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006333 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006334 * 4. If a message is received without Transfer-Encoding and with
6335 * either multiple Content-Length header fields having differing
6336 * field-values or a single Content-Length header field having an
6337 * invalid value, then the message framing is invalid and the
6338 * recipient MUST treat it as an unrecoverable error. If this is a
6339 * request message, the server MUST respond with a 400 (Bad Request)
6340 * status code and then close the connection. If this is a response
6341 * message received by a proxy, the proxy MUST close the connection
6342 * to the server, discard the received response, and send a 502 (Bad
6343 * Gateway) response to the client. If this is a response message
6344 * received by a user agent, the user agent MUST close the
6345 * connection to the server and discard the received response.
6346 *
6347 * 5. If a valid Content-Length header field is present without
6348 * Transfer-Encoding, its decimal value defines the expected message
6349 * body length in octets. If the sender closes the connection or
6350 * the recipient times out before the indicated number of octets are
6351 * received, the recipient MUST consider the message to be
6352 * incomplete and close the connection.
6353 *
6354 * 6. If this is a request message and none of the above are true, then
6355 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006356 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006357 * 7. Otherwise, this is a response message without a declared message
6358 * body length, so the message body length is determined by the
6359 * number of octets received prior to the server closing the
6360 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006361 */
6362
6363 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006364 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006365 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006366 * FIXME: should we parse anyway and return an error on chunked encoding ?
6367 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006368 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6369 txn->status == 101)) {
6370 /* Either we've established an explicit tunnel, or we're
6371 * switching the protocol. In both cases, we're very unlikely
6372 * to understand the next protocols. We have to switch to tunnel
6373 * mode, so that we transfer the request and responses then let
6374 * this protocol pass unmodified. When we later implement specific
6375 * parsers for such protocols, we'll want to check the Upgrade
6376 * header which contains information about that protocol for
6377 * responses with status 101 (eg: see RFC2817 about TLS).
6378 */
6379 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6380 msg->flags |= HTTP_MSGF_XFER_LEN;
6381 goto end;
6382 }
6383
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006384 if (txn->meth == HTTP_METH_HEAD ||
6385 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006386 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006387 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006388 goto skip_content_length;
6389 }
6390
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006391 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006392 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02006393 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006394 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006395 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6396 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006397 /* bad transfer-encoding (chunked followed by something else) */
6398 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006399 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006400 break;
6401 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006402 }
6403
Willy Tarreau1c913912015-04-30 10:57:51 +02006404 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006405 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006406 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02006407 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6408 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6409 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006410 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006411 signed long long cl;
6412
Willy Tarreauad14f752011-09-02 20:33:27 +02006413 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006414 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006415 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006416 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006417
Willy Tarreauad14f752011-09-02 20:33:27 +02006418 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006419 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006420 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006421 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006422
Willy Tarreauad14f752011-09-02 20:33:27 +02006423 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006424 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006425 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006426 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006427
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006428 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006429 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006430 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006431 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006432
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006433 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006434 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006435 }
6436
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006437 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006438 /* Now we have to check if we need to modify the Connection header.
6439 * This is more difficult on the response than it is on the request,
6440 * because we can have two different HTTP versions and we don't know
6441 * how the client will interprete a response. For instance, let's say
6442 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6443 * HTTP/1.1 response without any header. Maybe it will bound itself to
6444 * HTTP/1.0 because it only knows about it, and will consider the lack
6445 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6446 * the lack of header as a keep-alive. Thus we will use two flags
6447 * indicating how a request MAY be understood by the client. In case
6448 * of multiple possibilities, we'll fix the header to be explicit. If
6449 * ambiguous cases such as both close and keepalive are seen, then we
6450 * will fall back to explicit close. Note that we won't take risks with
6451 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006452 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006453 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006454 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6455 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
6456 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6457 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006458 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006459
Willy Tarreau70dffda2014-01-30 03:07:23 +01006460 /* this situation happens when combining pretend-keepalive with httpclose. */
6461 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006462 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006463 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006464 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6465
Willy Tarreau60466522010-01-18 19:08:45 +01006466 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006467 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006468 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6469 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006470
Willy Tarreau60466522010-01-18 19:08:45 +01006471 /* now adjust header transformations depending on current state */
6472 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6473 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6474 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006475 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006476 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006477 }
Willy Tarreau60466522010-01-18 19:08:45 +01006478 else { /* SCL / KAL */
6479 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006480 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006481 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006482 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006483
Willy Tarreau60466522010-01-18 19:08:45 +01006484 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006485 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006486
Willy Tarreau60466522010-01-18 19:08:45 +01006487 /* Some keep-alive responses are converted to Server-close if
6488 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006489 */
Willy Tarreau60466522010-01-18 19:08:45 +01006490 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6491 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006492 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006493 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006494 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006495 }
6496
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006497 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02006498 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006499 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006500
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006501 /* end of job, return OK */
6502 rep->analysers &= ~an_bit;
6503 rep->analyse_exp = TICK_ETERNITY;
6504 channel_auto_close(rep);
6505 return 1;
6506
6507 abort_keep_alive:
6508 /* A keep-alive request to the server failed on a network error.
6509 * The client is required to retry. We need to close without returning
6510 * any other information so that the client retries.
6511 */
6512 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01006513 rep->analysers &= AN_RES_FLT_END;
6514 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006515 channel_auto_close(rep);
6516 s->logs.logwait = 0;
6517 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006518 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006519 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006520 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006521 return 0;
6522}
6523
6524/* This function performs all the processing enabled for the current response.
6525 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006526 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006527 * other functions. It works like process_request (see indications above).
6528 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006529int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006530{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006531 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006532 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006533 struct http_msg *msg = &txn->rsp;
6534 struct proxy *cur_proxy;
6535 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006536 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006537
Willy Tarreau87b09662015-04-03 00:22:06 +02006538 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 +02006539 now_ms, __FUNCTION__,
6540 s,
6541 rep,
6542 rep->rex, rep->wex,
6543 rep->flags,
6544 rep->buf->i,
6545 rep->analysers);
6546
6547 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6548 return 0;
6549
Willy Tarreau70730dd2014-04-24 18:06:27 +02006550 /* The stats applet needs to adjust the Connection header but we don't
6551 * apply any filter there.
6552 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006553 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6554 rep->analysers &= ~an_bit;
6555 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006556 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006557 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006558
Willy Tarreau58975672014-04-24 21:13:57 +02006559 /*
6560 * We will have to evaluate the filters.
6561 * As opposed to version 1.2, now they will be evaluated in the
6562 * filters order and not in the header order. This means that
6563 * each filter has to be validated among all headers.
6564 *
6565 * Filters are tried with ->be first, then with ->fe if it is
6566 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006567 *
6568 * Maybe we are in resume condiion. In this case I choose the
6569 * "struct proxy" which contains the rule list matching the resume
6570 * pointer. If none of theses "struct proxy" match, I initialise
6571 * the process with the first one.
6572 *
6573 * In fact, I check only correspondance betwwen the current list
6574 * pointer and the ->fe rule list. If it doesn't match, I initialize
6575 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006576 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006577 if (s->current_rule_list == &sess->fe->http_res_rules)
6578 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006579 else
6580 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006581 while (1) {
6582 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006583
Willy Tarreau58975672014-04-24 21:13:57 +02006584 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02006585 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02006586 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006587
Willy Tarreau51d861a2015-05-22 17:30:48 +02006588 if (ret == HTTP_RULE_RES_BADREQ)
6589 goto return_srv_prx_502;
6590
6591 if (ret == HTTP_RULE_RES_DONE) {
6592 rep->analysers &= ~an_bit;
6593 rep->analyse_exp = TICK_ETERNITY;
6594 return 1;
6595 }
6596 }
6597
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006598 /* we need to be called again. */
6599 if (ret == HTTP_RULE_RES_YIELD) {
6600 channel_dont_close(rep);
6601 return 0;
6602 }
6603
Willy Tarreau58975672014-04-24 21:13:57 +02006604 /* try headers filters */
6605 if (rule_set->rsp_exp != NULL) {
6606 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6607 return_bad_resp:
6608 if (objt_server(s->target)) {
6609 objt_server(s->target)->counters.failed_resp++;
6610 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006611 }
Willy Tarreau58975672014-04-24 21:13:57 +02006612 s->be->be_counters.failed_resp++;
6613 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01006614 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02006615 txn->status = 502;
6616 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006617 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006618 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006619 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006620 if (!(s->flags & SF_ERR_MASK))
6621 s->flags |= SF_ERR_PRXCOND;
6622 if (!(s->flags & SF_FINST_MASK))
6623 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006624 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006625 }
Willy Tarreau58975672014-04-24 21:13:57 +02006626 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006627
Willy Tarreau58975672014-04-24 21:13:57 +02006628 /* has the response been denied ? */
6629 if (txn->flags & TX_SVDENY) {
6630 if (objt_server(s->target))
6631 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006632
Willy Tarreau58975672014-04-24 21:13:57 +02006633 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006634 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006635 if (sess->listener->counters)
6636 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006637
Willy Tarreau58975672014-04-24 21:13:57 +02006638 goto return_srv_prx_502;
6639 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006640
Willy Tarreau58975672014-04-24 21:13:57 +02006641 /* add response headers from the rule sets in the same order */
6642 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006643 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006644 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006645 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006646 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02006647 ret = acl_pass(ret);
6648 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6649 ret = !ret;
6650 if (!ret)
6651 continue;
6652 }
6653 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6654 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006655 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006656
Willy Tarreau58975672014-04-24 21:13:57 +02006657 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006658 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006659 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006660 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006661 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006662
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006663 /* After this point, this anayzer can't return yield, so we can
6664 * remove the bit corresponding to this analyzer from the list.
6665 *
6666 * Note that the intermediate returns and goto found previously
6667 * reset the analyzers.
6668 */
6669 rep->analysers &= ~an_bit;
6670 rep->analyse_exp = TICK_ETERNITY;
6671
Willy Tarreau58975672014-04-24 21:13:57 +02006672 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006673 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006674 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006675
Willy Tarreau58975672014-04-24 21:13:57 +02006676 /*
6677 * Now check for a server cookie.
6678 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006679 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02006680 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006681
Willy Tarreau58975672014-04-24 21:13:57 +02006682 /*
6683 * Check for cache-control or pragma headers if required.
6684 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006685 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 +02006686 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006687
Willy Tarreau58975672014-04-24 21:13:57 +02006688 /*
6689 * Add server cookie in the response if needed
6690 */
6691 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6692 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006693 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006694 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6695 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6696 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6697 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6698 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006699 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006700 /* the server is known, it's not the one the client requested, or the
6701 * cookie's last seen date needs to be refreshed. We have to
6702 * insert a set-cookie here, except if we want to insert only on POST
6703 * requests and this one isn't. Note that servers which don't have cookies
6704 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006705 */
Willy Tarreau58975672014-04-24 21:13:57 +02006706 if (!objt_server(s->target)->cookie) {
6707 chunk_printf(&trash,
6708 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6709 s->be->cookie_name);
6710 }
6711 else {
6712 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006713
Willy Tarreau58975672014-04-24 21:13:57 +02006714 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6715 /* emit last_date, which is mandatory */
6716 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6717 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6718 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006719
Willy Tarreau58975672014-04-24 21:13:57 +02006720 if (s->be->cookie_maxlife) {
6721 /* emit first_date, which is either the original one or
6722 * the current date.
6723 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006724 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006725 s30tob64(txn->cookie_first_date ?
6726 txn->cookie_first_date >> 2 :
6727 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006728 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006729 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006730 }
Willy Tarreau58975672014-04-24 21:13:57 +02006731 chunk_appendf(&trash, "; path=/");
6732 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006733
Willy Tarreau58975672014-04-24 21:13:57 +02006734 if (s->be->cookie_domain)
6735 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006736
Willy Tarreau58975672014-04-24 21:13:57 +02006737 if (s->be->ck_opts & PR_CK_HTTPONLY)
6738 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006739
Willy Tarreau58975672014-04-24 21:13:57 +02006740 if (s->be->ck_opts & PR_CK_SECURE)
6741 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006742
Willy Tarreau58975672014-04-24 21:13:57 +02006743 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6744 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006745
Willy Tarreau58975672014-04-24 21:13:57 +02006746 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006747 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006748 /* the server did not change, only the date was updated */
6749 txn->flags |= TX_SCK_UPDATED;
6750 else
6751 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006752
Willy Tarreau58975672014-04-24 21:13:57 +02006753 /* Here, we will tell an eventual cache on the client side that we don't
6754 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6755 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6756 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006757 */
Willy Tarreau58975672014-04-24 21:13:57 +02006758 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006759
Willy Tarreau58975672014-04-24 21:13:57 +02006760 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006761
Willy Tarreau58975672014-04-24 21:13:57 +02006762 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6763 "Cache-control: private", 22) < 0))
6764 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006765 }
Willy Tarreau58975672014-04-24 21:13:57 +02006766 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006767
Willy Tarreau58975672014-04-24 21:13:57 +02006768 /*
6769 * Check if result will be cacheable with a cookie.
6770 * We'll block the response if security checks have caught
6771 * nasty things such as a cacheable cookie.
6772 */
6773 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6774 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6775 (s->be->options & PR_O_CHK_CACHE)) {
6776 /* we're in presence of a cacheable response containing
6777 * a set-cookie header. We'll block it as requested by
6778 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006779 */
Willy Tarreau58975672014-04-24 21:13:57 +02006780 if (objt_server(s->target))
6781 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006782
Willy Tarreau58975672014-04-24 21:13:57 +02006783 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006784 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006785 if (sess->listener->counters)
6786 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006787
Willy Tarreau58975672014-04-24 21:13:57 +02006788 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6789 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6790 send_log(s->be, LOG_ALERT,
6791 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6792 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6793 goto return_srv_prx_502;
6794 }
Willy Tarreau03945942009-12-22 16:50:27 +01006795
Willy Tarreau70730dd2014-04-24 18:06:27 +02006796 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006797 /*
6798 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6799 * If an "Upgrade" token is found, the header is left untouched in order
6800 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006801 * if anything but "Upgrade" is present in the Connection header. We don't
6802 * want to touch any 101 response either since it's switching to another
6803 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006804 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006805 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006806 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006807 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006808 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6809 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006810
Willy Tarreau58975672014-04-24 21:13:57 +02006811 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6812 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6813 /* we want a keep-alive response here. Keep-alive header
6814 * required if either side is not 1.1.
6815 */
6816 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6817 want_flags |= TX_CON_KAL_SET;
6818 }
6819 else {
6820 /* we want a close response here. Close header required if
6821 * the server is 1.1, regardless of the client.
6822 */
6823 if (msg->flags & HTTP_MSGF_VER_11)
6824 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006825 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006826
Willy Tarreau58975672014-04-24 21:13:57 +02006827 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6828 http_change_connection_header(txn, msg, want_flags);
6829 }
6830
6831 skip_header_mangling:
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006832 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_DATA_FILTERS(s, rep) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006833 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01006834 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
Willy Tarreau58975672014-04-24 21:13:57 +02006835 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Christopher Fauletd7c91962015-04-30 11:48:27 +02006836 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006837
Willy Tarreau58975672014-04-24 21:13:57 +02006838 /* if the user wants to log as soon as possible, without counting
6839 * bytes from the server, then this is the right moment. We have
6840 * to temporarily assign bytes_out to log what we currently have.
6841 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006842 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006843 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6844 s->logs.bytes_out = txn->rsp.eoh;
6845 s->do_log(s);
6846 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006847 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006848 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006849}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006850
Willy Tarreaud98cf932009-12-27 22:54:55 +01006851/* This function is an analyser which forwards response body (including chunk
6852 * sizes if any). It is called as soon as we must forward, even if we forward
6853 * zero byte. The only situation where it must not be called is when we're in
6854 * tunnel mode and we want to forward till the close. It's used both to forward
6855 * remaining data and to resync after end of body. It expects the msg_state to
6856 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006857 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006858 *
6859 * It is capable of compressing response data both in content-length mode and
6860 * in chunked mode. The state machines follows different flows depending on
6861 * whether content-length and chunked modes are used, since there are no
6862 * trailers in content-length :
6863 *
6864 * chk-mode cl-mode
6865 * ,----- BODY -----.
6866 * / \
6867 * V size > 0 V chk-mode
6868 * .--> SIZE -------------> DATA -------------> CRLF
6869 * | | size == 0 | last byte |
6870 * | v final crlf v inspected |
6871 * | TRAILERS -----------> DONE |
6872 * | |
6873 * `----------------------------------------------'
6874 *
6875 * Compression only happens in the DATA state, and must be flushed in final
6876 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6877 * is performed at once on final states for all bytes parsed, or when leaving
6878 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006879 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006880int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006881{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006882 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006883 struct http_txn *txn = s->txn;
6884 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01006885 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006886
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006887 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6888 return 0;
6889
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006890 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006891 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006892 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006893 /* Output closed while we were sending data. We must abort and
6894 * wake the other side up.
6895 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006896 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006897 msg->msg_state = HTTP_MSG_ERROR;
6898 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006899 return 1;
6900 }
6901
Willy Tarreau4fe41902010-06-07 22:27:41 +02006902 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006903 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006904
Christopher Fauletd7c91962015-04-30 11:48:27 +02006905 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006906 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
6907 ? HTTP_MSG_CHUNK_SIZE
6908 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006909 }
6910
Willy Tarreauefdf0942014-04-24 20:08:57 +02006911 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006912 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02006913 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006914 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02006915 }
6916
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006917 if (msg->msg_state < HTTP_MSG_DONE) {
6918 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
6919 ? http_msg_forward_chunked_body(s, msg)
6920 : http_msg_forward_body(s, msg));
6921 if (!ret)
6922 goto missing_data_or_waiting;
6923 if (ret < 0)
6924 goto return_bad_res;
6925 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02006926
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006927 /* other states, DONE...TUNNEL */
6928 /* for keep-alive we don't want to forward closes on DONE */
6929 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6930 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
6931 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006932
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006933 if (http_resync_states(s)) {
6934 /* some state changes occurred, maybe the analyser was disabled
6935 * too. */
6936 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6937 if (res->flags & CF_SHUTW) {
6938 /* response errors are most likely due to the
6939 * client aborting the transfer. */
6940 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01006941 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006942 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006943 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006944 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006945 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006946 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006947 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02006948 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006949
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006950 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01006951 if (res->flags & CF_SHUTW)
6952 goto aborted_xfer;
6953
6954 /* stop waiting for data if the input is closed before the end. If the
6955 * client side was already closed, it means that the client has aborted,
6956 * so we don't want to count this as a server abort. Otherwise it's a
6957 * server abort.
6958 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006959 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006960 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006961 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006962 /* If we have some pending data, we continue the processing */
6963 if (!buffer_pending(res->buf)) {
6964 if (!(s->flags & SF_ERR_MASK))
6965 s->flags |= SF_ERR_SRVCL;
6966 s->be->be_counters.srv_aborts++;
6967 if (objt_server(s->target))
6968 objt_server(s->target)->counters.srv_aborts++;
6969 goto return_bad_res_stats_ok;
6970 }
Willy Tarreau40dba092010-03-04 18:14:51 +01006971 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006972
Willy Tarreau40dba092010-03-04 18:14:51 +01006973 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006974 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006975 goto return_bad_res;
6976
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006977 /* When TE: chunked is used, we need to get there again to parse
6978 * remaining chunks even if the server has closed, so we don't want to
6979 * set CF_DONTCLOSE. Similarly, if the body length is undefined, if
6980 * keep-alive is set on the client side or if there are filters
6981 * registered on the stream, we don't want to forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006982 */
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006983 if ((msg->flags & HTTP_MSGF_TE_CHNK) || !(msg->flags & HTTP_MSGF_XFER_LEN) ||
6984 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006985 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6986 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006987 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006988
Willy Tarreau5c620922011-05-11 19:56:11 +02006989 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006990 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006991 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006992 * modes are already handled by the stream sock layer. We must not do
6993 * this in content-length mode because it could present the MSG_MORE
6994 * flag with the last block of forwarded data, which would cause an
6995 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006996 */
Christopher Faulet92d36382015-11-05 13:35:03 +01006997 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006998 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006999
Willy Tarreau87b09662015-04-03 00:22:06 +02007000 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01007001 return 0;
7002
Willy Tarreau40dba092010-03-04 18:14:51 +01007003 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007004 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007005 if (objt_server(s->target))
7006 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007007
7008 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01007009 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007010 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01007011 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01007012 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01007013 res->analysers &= AN_RES_FLT_END;
7014 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 +01007015 if (objt_server(s->target))
7016 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007017
Willy Tarreaue7dff022015-04-03 01:14:29 +02007018 if (!(s->flags & SF_ERR_MASK))
7019 s->flags |= SF_ERR_PRXCOND;
7020 if (!(s->flags & SF_FINST_MASK))
7021 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007022 return 0;
7023
7024 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01007025 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007026 txn->rsp.msg_state = HTTP_MSG_ERROR;
7027 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01007028 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01007029 res->analysers &= AN_RES_FLT_END;
7030 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 +01007031
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007032 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007033 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007034 if (objt_server(s->target))
7035 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007036
Willy Tarreaue7dff022015-04-03 01:14:29 +02007037 if (!(s->flags & SF_ERR_MASK))
7038 s->flags |= SF_ERR_CLICL;
7039 if (!(s->flags & SF_FINST_MASK))
7040 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007041 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007042}
7043
7044
7045static inline int
7046http_msg_forward_body(struct stream *s, struct http_msg *msg)
7047{
7048 struct channel *chn = msg->chn;
7049 int ret;
7050
7051 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
7052
7053 if (msg->msg_state == HTTP_MSG_ENDING)
7054 goto ending;
7055
7056 /* Neither content-length, nor transfer-encoding was found, so we must
7057 * read the body until the server connection is closed. In that case, we
7058 * eat data as they come. Of course, this happens for response only. */
7059 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
7060 unsigned long long len = (chn->buf->i - msg->next);
7061 msg->chunk_len += len;
7062 msg->body_len += len;
7063 }
Christopher Fauletda02e172015-12-04 09:25:05 +01007064 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
7065 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
7066 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007067 msg->next += ret;
7068 msg->chunk_len -= ret;
7069 if (msg->chunk_len) {
7070 /* input empty or output full */
7071 if (chn->buf->i > msg->next)
7072 chn->flags |= CF_WAKE_WRITE;
7073 goto missing_data_or_waiting;
7074 }
7075
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01007076 /* The server still sending data that should be filtered */
7077 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && !(chn->flags & CF_SHUTR))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007078 goto missing_data_or_waiting;
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01007079
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007080 msg->msg_state = HTTP_MSG_ENDING;
7081
7082 ending:
7083 /* we may have some pending data starting at res->buf->p such as a last
7084 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01007085 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
7086 /* default_ret */ msg->next,
7087 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007088 b_adv(chn->buf, ret);
7089 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007090 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
7091 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02007092 if (msg->next)
7093 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007094
Christopher Fauletda02e172015-12-04 09:25:05 +01007095 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
7096 /* default_ret */ 1,
7097 /* on_error */ goto error,
7098 /* on_wait */ goto waiting);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007099 msg->msg_state = HTTP_MSG_DONE;
7100 return 1;
7101
7102 missing_data_or_waiting:
7103 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01007104 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
7105 /* default_ret */ msg->next,
7106 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007107 b_adv(chn->buf, ret);
7108 msg->next -= ret;
7109 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
7110 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01007111 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007112 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02007113 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007114 return 0;
7115 error:
7116 return -1;
7117}
7118
7119static inline int
7120http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
7121{
7122 struct channel *chn = msg->chn;
7123 int ret;
7124
7125 /* Here we have the guarantee to be in one of the following state:
7126 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
7127 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
7128
7129 switch_states:
7130 switch (msg->msg_state) {
7131 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01007132 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
7133 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
7134 /* on_error */ goto error);
7135 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007136 msg->chunk_len -= ret;
7137 if (msg->chunk_len) {
7138 /* input empty or output full */
7139 if (chn->buf->i > msg->next)
7140 chn->flags |= CF_WAKE_WRITE;
7141 goto missing_data_or_waiting;
7142 }
7143
7144 /* nothing left to forward for this chunk*/
7145 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
7146 /* fall through for HTTP_MSG_CHUNK_CRLF */
7147
7148 case HTTP_MSG_CHUNK_CRLF:
7149 /* we want the CRLF after the data */
7150 ret = http_skip_chunk_crlf(msg);
7151 if (ret == 0)
7152 goto missing_data_or_waiting;
7153 if (ret < 0)
7154 goto chunk_parsing_error;
Christopher Faulet113f7de2015-12-14 14:52:13 +01007155 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007156 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
7157 /* fall through for HTTP_MSG_CHUNK_SIZE */
7158
7159 case HTTP_MSG_CHUNK_SIZE:
7160 /* read the chunk size and assign it to ->chunk_len,
7161 * then set ->next to point to the body and switch to
7162 * DATA or TRAILERS state.
7163 */
7164 ret = http_parse_chunk_size(msg);
7165 if (ret == 0)
7166 goto missing_data_or_waiting;
7167 if (ret < 0)
7168 goto chunk_parsing_error;
Christopher Faulet113f7de2015-12-14 14:52:13 +01007169 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007170 if (msg->chunk_len) {
7171 msg->msg_state = HTTP_MSG_DATA;
7172 goto switch_states;
7173 }
7174 msg->msg_state = HTTP_MSG_TRAILERS;
7175 /* fall through for HTTP_MSG_TRAILERS */
7176
7177 case HTTP_MSG_TRAILERS:
7178 ret = http_forward_trailers(msg);
7179 if (ret < 0)
7180 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01007181 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
7182 /* default_ret */ 1,
7183 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007184 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007185 if (!ret)
7186 goto missing_data_or_waiting;
7187 break;
7188
7189 case HTTP_MSG_ENDING:
7190 goto ending;
7191
7192 default:
7193 /* This should no happen in this function */
7194 goto error;
7195 }
7196
7197 msg->msg_state = HTTP_MSG_ENDING;
7198 ending:
7199 /* we may have some pending data starting at res->buf->p such as a last
7200 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01007201 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007202 /* default_ret */ msg->next,
7203 /* on_error */ goto error);
7204 b_adv(chn->buf, ret);
7205 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007206 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
7207 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02007208 if (msg->next)
7209 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007210
Christopher Fauletda02e172015-12-04 09:25:05 +01007211 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007212 /* default_ret */ 1,
7213 /* on_error */ goto error,
7214 /* on_wait */ goto waiting);
7215 msg->msg_state = HTTP_MSG_DONE;
7216 return 1;
7217
7218 missing_data_or_waiting:
7219 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01007220 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007221 /* default_ret */ msg->next,
7222 /* on_error */ goto error);
7223 b_adv(chn->buf, ret);
7224 msg->next -= ret;
7225 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
7226 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01007227 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007228 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02007229 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007230 return 0;
7231
7232 chunk_parsing_error:
7233 if (msg->err_pos >= 0) {
7234 if (chn->flags & CF_ISRESP)
7235 http_capture_bad_message(&s->be->invalid_rep, s, msg,
7236 msg->msg_state, strm_fe(s));
7237 else
7238 http_capture_bad_message(&strm_fe(s)->invalid_req, s,
7239 msg, msg->msg_state, s->be);
7240 }
7241 error:
7242 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007243}
7244
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007245
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007246/* Iterate the same filter through all request headers.
7247 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007248 * Since it can manage the switch to another backend, it updates the per-proxy
7249 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007250 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007251int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007252{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007253 char *cur_ptr, *cur_end, *cur_next;
7254 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007255 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007256 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007257 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01007258
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007259 last_hdr = 0;
7260
Willy Tarreau9b28e032012-10-12 23:49:43 +02007261 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007262 old_idx = 0;
7263
7264 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007265 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007266 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007267 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007268 (exp->action == ACT_ALLOW ||
7269 exp->action == ACT_DENY ||
7270 exp->action == ACT_TARPIT))
7271 return 0;
7272
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007273 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007274 if (!cur_idx)
7275 break;
7276
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007277 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007278 cur_ptr = cur_next;
7279 cur_end = cur_ptr + cur_hdr->len;
7280 cur_next = cur_end + cur_hdr->cr + 1;
7281
7282 /* Now we have one header between cur_ptr and cur_end,
7283 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007284 */
7285
Willy Tarreau15a53a42015-01-21 13:39:42 +01007286 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007287 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007288 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007289 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007290 last_hdr = 1;
7291 break;
7292
7293 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007294 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007295 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007296 break;
7297
7298 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007299 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007300 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007301 break;
7302
7303 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007304 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7305 if (trash.len < 0)
7306 return -1;
7307
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007308 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007309 /* FIXME: if the user adds a newline in the replacement, the
7310 * index will not be recalculated for now, and the new line
7311 * will not be counted as a new header.
7312 */
7313
7314 cur_end += delta;
7315 cur_next += delta;
7316 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007317 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007318 break;
7319
7320 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007321 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007322 cur_next += delta;
7323
Willy Tarreaufa355d42009-11-29 18:12:29 +01007324 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007325 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7326 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007327 cur_hdr->len = 0;
7328 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007329 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007330 break;
7331
7332 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007333 }
7334
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007335 /* keep the link from this header to next one in case of later
7336 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007337 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007338 old_idx = cur_idx;
7339 }
7340 return 0;
7341}
7342
7343
7344/* Apply the filter to the request line.
7345 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7346 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007347 * Since it can manage the switch to another backend, it updates the per-proxy
7348 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007349 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007350int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007351{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007352 char *cur_ptr, *cur_end;
7353 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007354 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007355 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007356
Willy Tarreau3d300592007-03-18 18:34:41 +01007357 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007358 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007359 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007360 (exp->action == ACT_ALLOW ||
7361 exp->action == ACT_DENY ||
7362 exp->action == ACT_TARPIT))
7363 return 0;
7364 else if (exp->action == ACT_REMOVE)
7365 return 0;
7366
7367 done = 0;
7368
Willy Tarreau9b28e032012-10-12 23:49:43 +02007369 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007370 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007371
7372 /* Now we have the request line between cur_ptr and cur_end */
7373
Willy Tarreau15a53a42015-01-21 13:39:42 +01007374 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007375 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007376 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007377 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007378 done = 1;
7379 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007380
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007381 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007382 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007383 done = 1;
7384 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007385
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007386 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007387 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007388 done = 1;
7389 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007390
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007391 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007392 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7393 if (trash.len < 0)
7394 return -1;
7395
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007396 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007397 /* FIXME: if the user adds a newline in the replacement, the
7398 * index will not be recalculated for now, and the new line
7399 * will not be counted as a new header.
7400 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007401
Willy Tarreaufa355d42009-11-29 18:12:29 +01007402 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007403 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007404 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007405 HTTP_MSG_RQMETH,
7406 cur_ptr, cur_end + 1,
7407 NULL, NULL);
7408 if (unlikely(!cur_end))
7409 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007410
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007411 /* we have a full request and we know that we have either a CR
7412 * or an LF at <ptr>.
7413 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007414 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7415 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007416 /* there is no point trying this regex on headers */
7417 return 1;
7418 }
7419 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007420 return done;
7421}
Willy Tarreau97de6242006-12-27 17:18:38 +01007422
Willy Tarreau58f10d72006-12-04 02:26:12 +01007423
Willy Tarreau58f10d72006-12-04 02:26:12 +01007424
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007425/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007426 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007427 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007428 * unparsable request. Since it can manage the switch to another backend, it
7429 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007430 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007431int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007432{
Willy Tarreau192252e2015-04-04 01:47:55 +02007433 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007434 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007435 struct hdr_exp *exp;
7436
7437 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007438 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007439
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007440 /*
7441 * The interleaving of transformations and verdicts
7442 * makes it difficult to decide to continue or stop
7443 * the evaluation.
7444 */
7445
Willy Tarreau6c123b12010-01-28 20:22:06 +01007446 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7447 break;
7448
Willy Tarreau3d300592007-03-18 18:34:41 +01007449 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007450 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007451 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007452 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007453
7454 /* if this filter had a condition, evaluate it now and skip to
7455 * next filter if the condition does not match.
7456 */
7457 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007458 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007459 ret = acl_pass(ret);
7460 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7461 ret = !ret;
7462
7463 if (!ret)
7464 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007465 }
7466
7467 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007468 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007469 if (unlikely(ret < 0))
7470 return -1;
7471
7472 if (likely(ret == 0)) {
7473 /* The filter did not match the request, it can be
7474 * iterated through all headers.
7475 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007476 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7477 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007478 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007479 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007480 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007481}
7482
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007483
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007484/* Find the end of a cookie value contained between <s> and <e>. It works the
7485 * same way as with headers above except that the semi-colon also ends a token.
7486 * See RFC2965 for more information. Note that it requires a valid header to
7487 * return a valid result.
7488 */
7489char *find_cookie_value_end(char *s, const char *e)
7490{
7491 int quoted, qdpair;
7492
7493 quoted = qdpair = 0;
7494 for (; s < e; s++) {
7495 if (qdpair) qdpair = 0;
7496 else if (quoted) {
7497 if (*s == '\\') qdpair = 1;
7498 else if (*s == '"') quoted = 0;
7499 }
7500 else if (*s == '"') quoted = 1;
7501 else if (*s == ',' || *s == ';') return s;
7502 }
7503 return s;
7504}
7505
7506/* Delete a value in a header between delimiters <from> and <next> in buffer
7507 * <buf>. The number of characters displaced is returned, and the pointer to
7508 * the first delimiter is updated if required. The function tries as much as
7509 * possible to respect the following principles :
7510 * - replace <from> delimiter by the <next> one unless <from> points to a
7511 * colon, in which case <next> is simply removed
7512 * - set exactly one space character after the new first delimiter, unless
7513 * there are not enough characters in the block being moved to do so.
7514 * - remove unneeded spaces before the previous delimiter and after the new
7515 * one.
7516 *
7517 * It is the caller's responsibility to ensure that :
7518 * - <from> points to a valid delimiter or the colon ;
7519 * - <next> points to a valid delimiter or the final CR/LF ;
7520 * - there are non-space chars before <from> ;
7521 * - there is a CR/LF at or after <next>.
7522 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007523int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007524{
7525 char *prev = *from;
7526
7527 if (*prev == ':') {
7528 /* We're removing the first value, preserve the colon and add a
7529 * space if possible.
7530 */
Willy Tarreau2235b262016-11-05 15:50:20 +01007531 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007532 next++;
7533 prev++;
7534 if (prev < next)
7535 *prev++ = ' ';
7536
Willy Tarreau2235b262016-11-05 15:50:20 +01007537 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007538 next++;
7539 } else {
7540 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01007541 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007542 prev--;
7543 *from = prev;
7544
7545 /* copy the delimiter and if possible a space if we're
7546 * not at the end of the line.
7547 */
Willy Tarreau2235b262016-11-05 15:50:20 +01007548 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007549 *prev++ = *next++;
7550 if (prev + 1 < next)
7551 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01007552 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007553 next++;
7554 }
7555 }
7556 return buffer_replace2(buf, prev, next, NULL, 0);
7557}
7558
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007559/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007560 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007561 * desirable to call it only when needed. This code is quite complex because
7562 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7563 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007564 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007565void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007566{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007567 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007568 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007569 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007570 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007571 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7572 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007573
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007574 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007575 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007576 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007577
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007578 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007579 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007580 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007581
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007582 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007583 hdr_beg = hdr_next;
7584 hdr_end = hdr_beg + cur_hdr->len;
7585 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007586
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007587 /* We have one full header between hdr_beg and hdr_end, and the
7588 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007589 * "Cookie:" headers.
7590 */
7591
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007592 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007593 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007594 old_idx = cur_idx;
7595 continue;
7596 }
7597
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007598 del_from = NULL; /* nothing to be deleted */
7599 preserve_hdr = 0; /* assume we may kill the whole header */
7600
Willy Tarreau58f10d72006-12-04 02:26:12 +01007601 /* Now look for cookies. Conforming to RFC2109, we have to support
7602 * attributes whose name begin with a '$', and associate them with
7603 * the right cookie, if we want to delete this cookie.
7604 * So there are 3 cases for each cookie read :
7605 * 1) it's a special attribute, beginning with a '$' : ignore it.
7606 * 2) it's a server id cookie that we *MAY* want to delete : save
7607 * some pointers on it (last semi-colon, beginning of cookie...)
7608 * 3) it's an application cookie : we *MAY* have to delete a previous
7609 * "special" cookie.
7610 * At the end of loop, if a "special" cookie remains, we may have to
7611 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007612 * *MUST* delete it.
7613 *
7614 * Note: RFC2965 is unclear about the processing of spaces around
7615 * the equal sign in the ATTR=VALUE form. A careful inspection of
7616 * the RFC explicitly allows spaces before it, and not within the
7617 * tokens (attrs or values). An inspection of RFC2109 allows that
7618 * too but section 10.1.3 lets one think that spaces may be allowed
7619 * after the equal sign too, resulting in some (rare) buggy
7620 * implementations trying to do that. So let's do what servers do.
7621 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7622 * allowed quoted strings in values, with any possible character
7623 * after a backslash, including control chars and delimitors, which
7624 * causes parsing to become ambiguous. Browsers also allow spaces
7625 * within values even without quotes.
7626 *
7627 * We have to keep multiple pointers in order to support cookie
7628 * removal at the beginning, middle or end of header without
7629 * corrupting the header. All of these headers are valid :
7630 *
7631 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7632 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7633 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7634 * | | | | | | | | |
7635 * | | | | | | | | hdr_end <--+
7636 * | | | | | | | +--> next
7637 * | | | | | | +----> val_end
7638 * | | | | | +-----------> val_beg
7639 * | | | | +--------------> equal
7640 * | | | +----------------> att_end
7641 * | | +---------------------> att_beg
7642 * | +--------------------------> prev
7643 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007644 */
7645
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007646 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7647 /* Iterate through all cookies on this line */
7648
7649 /* find att_beg */
7650 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007651 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007652 att_beg++;
7653
7654 /* find att_end : this is the first character after the last non
7655 * space before the equal. It may be equal to hdr_end.
7656 */
7657 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007658
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007659 while (equal < hdr_end) {
7660 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007661 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007662 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007663 continue;
7664 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007665 }
7666
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007667 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7668 * is between <att_beg> and <equal>, both may be identical.
7669 */
7670
7671 /* look for end of cookie if there is an equal sign */
7672 if (equal < hdr_end && *equal == '=') {
7673 /* look for the beginning of the value */
7674 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007675 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007676 val_beg++;
7677
7678 /* find the end of the value, respecting quotes */
7679 next = find_cookie_value_end(val_beg, hdr_end);
7680
7681 /* make val_end point to the first white space or delimitor after the value */
7682 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007683 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007684 val_end--;
7685 } else {
7686 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007687 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007688
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007689 /* We have nothing to do with attributes beginning with '$'. However,
7690 * they will automatically be removed if a header before them is removed,
7691 * since they're supposed to be linked together.
7692 */
7693 if (*att_beg == '$')
7694 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007695
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007696 /* Ignore cookies with no equal sign */
7697 if (equal == next) {
7698 /* This is not our cookie, so we must preserve it. But if we already
7699 * scheduled another cookie for removal, we cannot remove the
7700 * complete header, but we can remove the previous block itself.
7701 */
7702 preserve_hdr = 1;
7703 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007704 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007705 val_end += delta;
7706 next += delta;
7707 hdr_end += delta;
7708 hdr_next += delta;
7709 cur_hdr->len += delta;
7710 http_msg_move_end(&txn->req, delta);
7711 prev = del_from;
7712 del_from = NULL;
7713 }
7714 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007715 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007716
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007717 /* if there are spaces around the equal sign, we need to
7718 * strip them otherwise we'll get trouble for cookie captures,
7719 * or even for rewrites. Since this happens extremely rarely,
7720 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007721 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007722 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7723 int stripped_before = 0;
7724 int stripped_after = 0;
7725
7726 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007727 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007728 equal += stripped_before;
7729 val_beg += stripped_before;
7730 }
7731
7732 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007733 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007734 val_beg += stripped_after;
7735 stripped_before += stripped_after;
7736 }
7737
7738 val_end += stripped_before;
7739 next += stripped_before;
7740 hdr_end += stripped_before;
7741 hdr_next += stripped_before;
7742 cur_hdr->len += stripped_before;
7743 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007744 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007745 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007746
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007747 /* First, let's see if we want to capture this cookie. We check
7748 * that we don't already have a client side cookie, because we
7749 * can only capture one. Also as an optimisation, we ignore
7750 * cookies shorter than the declared name.
7751 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007752 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7753 (val_end - att_beg >= sess->fe->capture_namelen) &&
7754 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007755 int log_len = val_end - att_beg;
7756
7757 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7758 Alert("HTTP logging : out of memory.\n");
7759 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007760 if (log_len > sess->fe->capture_len)
7761 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007762 memcpy(txn->cli_cookie, att_beg, log_len);
7763 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007764 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007765 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007766
Willy Tarreaubca99692010-10-06 19:25:55 +02007767 /* Persistence cookies in passive, rewrite or insert mode have the
7768 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007769 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007770 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007771 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007772 * For cookies in prefix mode, the form is :
7773 *
7774 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007775 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007776 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7777 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7778 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007779 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007780
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007781 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7782 * have the server ID between val_beg and delim, and the original cookie between
7783 * delim+1 and val_end. Otherwise, delim==val_end :
7784 *
7785 * Cookie: NAME=SRV; # in all but prefix modes
7786 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7787 * | || || | |+-> next
7788 * | || || | +--> val_end
7789 * | || || +---------> delim
7790 * | || |+------------> val_beg
7791 * | || +-------------> att_end = equal
7792 * | |+-----------------> att_beg
7793 * | +------------------> prev
7794 * +-------------------------> hdr_beg
7795 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007796
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007797 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007798 for (delim = val_beg; delim < val_end; delim++)
7799 if (*delim == COOKIE_DELIM)
7800 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007801 } else {
7802 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007803 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007804 /* Now check if the cookie contains a date field, which would
7805 * appear after a vertical bar ('|') just after the server name
7806 * and before the delimiter.
7807 */
7808 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7809 if (vbar1) {
7810 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007811 * right is the last seen date. It is a base64 encoded
7812 * 30-bit value representing the UNIX date since the
7813 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007814 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007815 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007816 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007817 if (val_end - vbar1 >= 5) {
7818 val = b64tos30(vbar1);
7819 if (val > 0)
7820 txn->cookie_last_date = val << 2;
7821 }
7822 /* look for a second vertical bar */
7823 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7824 if (vbar1 && (val_end - vbar1 > 5)) {
7825 val = b64tos30(vbar1 + 1);
7826 if (val > 0)
7827 txn->cookie_first_date = val << 2;
7828 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007829 }
7830 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007831
Willy Tarreauf64d1412010-10-07 20:06:11 +02007832 /* if the cookie has an expiration date and the proxy wants to check
7833 * it, then we do that now. We first check if the cookie is too old,
7834 * then only if it has expired. We detect strict overflow because the
7835 * time resolution here is not great (4 seconds). Cookies with dates
7836 * in the future are ignored if their offset is beyond one day. This
7837 * allows an admin to fix timezone issues without expiring everyone
7838 * and at the same time avoids keeping unwanted side effects for too
7839 * long.
7840 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007841 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7842 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007843 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007844 txn->flags &= ~TX_CK_MASK;
7845 txn->flags |= TX_CK_OLD;
7846 delim = val_beg; // let's pretend we have not found the cookie
7847 txn->cookie_first_date = 0;
7848 txn->cookie_last_date = 0;
7849 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007850 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7851 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007852 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007853 txn->flags &= ~TX_CK_MASK;
7854 txn->flags |= TX_CK_EXPIRED;
7855 delim = val_beg; // let's pretend we have not found the cookie
7856 txn->cookie_first_date = 0;
7857 txn->cookie_last_date = 0;
7858 }
7859
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007860 /* Here, we'll look for the first running server which supports the cookie.
7861 * This allows to share a same cookie between several servers, for example
7862 * to dedicate backup servers to specific servers only.
7863 * However, to prevent clients from sticking to cookie-less backup server
7864 * when they have incidentely learned an empty cookie, we simply ignore
7865 * empty cookies and mark them as invalid.
7866 * The same behaviour is applied when persistence must be ignored.
7867 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007868 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007869 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007870
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007871 while (srv) {
7872 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7873 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007874 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007875 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007876 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007877 /* we found the server and we can use it */
7878 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007879 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007880 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007881 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007882 break;
7883 } else {
7884 /* we found a server, but it's down,
7885 * mark it as such and go on in case
7886 * another one is available.
7887 */
7888 txn->flags &= ~TX_CK_MASK;
7889 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007890 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007891 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007892 srv = srv->next;
7893 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007894
Willy Tarreauf64d1412010-10-07 20:06:11 +02007895 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007896 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007897 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007898 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007899 txn->flags |= TX_CK_UNUSED;
7900 else
7901 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007902 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007903
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007904 /* depending on the cookie mode, we may have to either :
7905 * - delete the complete cookie if we're in insert+indirect mode, so that
7906 * the server never sees it ;
7907 * - remove the server id from the cookie value, and tag the cookie as an
7908 * application cookie so that it does not get accidentely removed later,
7909 * if we're in cookie prefix mode
7910 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007911 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007912 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007913
Willy Tarreau9b28e032012-10-12 23:49:43 +02007914 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007915 val_end += delta;
7916 next += delta;
7917 hdr_end += delta;
7918 hdr_next += delta;
7919 cur_hdr->len += delta;
7920 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007921
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007922 del_from = NULL;
7923 preserve_hdr = 1; /* we want to keep this cookie */
7924 }
7925 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007926 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007927 del_from = prev;
7928 }
7929 } else {
7930 /* This is not our cookie, so we must preserve it. But if we already
7931 * scheduled another cookie for removal, we cannot remove the
7932 * complete header, but we can remove the previous block itself.
7933 */
7934 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007935
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007936 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007937 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007938 if (att_beg >= del_from)
7939 att_beg += delta;
7940 if (att_end >= del_from)
7941 att_end += delta;
7942 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007943 val_end += delta;
7944 next += delta;
7945 hdr_end += delta;
7946 hdr_next += delta;
7947 cur_hdr->len += delta;
7948 http_msg_move_end(&txn->req, delta);
7949 prev = del_from;
7950 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007951 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007952 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007953
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007954 /* continue with next cookie on this header line */
7955 att_beg = next;
7956 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007957
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007958 /* There are no more cookies on this line.
7959 * We may still have one (or several) marked for deletion at the
7960 * end of the line. We must do this now in two ways :
7961 * - if some cookies must be preserved, we only delete from the
7962 * mark to the end of line ;
7963 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007964 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007965 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007966 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007967 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007968 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007969 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007970 cur_hdr->len += delta;
7971 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007972 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007973
7974 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007975 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7976 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007977 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007978 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007979 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007980 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007981 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007982 }
7983
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007984 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007985 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007986 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007987}
7988
7989
Willy Tarreaua15645d2007-03-18 16:22:39 +01007990/* Iterate the same filter through all response headers contained in <rtr>.
7991 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7992 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007993int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007994{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007995 char *cur_ptr, *cur_end, *cur_next;
7996 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007997 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007998 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007999 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008000
8001 last_hdr = 0;
8002
Willy Tarreau9b28e032012-10-12 23:49:43 +02008003 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008004 old_idx = 0;
8005
8006 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008007 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008008 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008009 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008010 (exp->action == ACT_ALLOW ||
8011 exp->action == ACT_DENY))
8012 return 0;
8013
8014 cur_idx = txn->hdr_idx.v[old_idx].next;
8015 if (!cur_idx)
8016 break;
8017
8018 cur_hdr = &txn->hdr_idx.v[cur_idx];
8019 cur_ptr = cur_next;
8020 cur_end = cur_ptr + cur_hdr->len;
8021 cur_next = cur_end + cur_hdr->cr + 1;
8022
8023 /* Now we have one header between cur_ptr and cur_end,
8024 * and the next header starts at cur_next.
8025 */
8026
Willy Tarreau15a53a42015-01-21 13:39:42 +01008027 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008028 switch (exp->action) {
8029 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008030 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008031 last_hdr = 1;
8032 break;
8033
8034 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008035 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008036 last_hdr = 1;
8037 break;
8038
8039 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008040 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8041 if (trash.len < 0)
8042 return -1;
8043
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008044 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008045 /* FIXME: if the user adds a newline in the replacement, the
8046 * index will not be recalculated for now, and the new line
8047 * will not be counted as a new header.
8048 */
8049
8050 cur_end += delta;
8051 cur_next += delta;
8052 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008053 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008054 break;
8055
8056 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02008057 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008058 cur_next += delta;
8059
Willy Tarreaufa355d42009-11-29 18:12:29 +01008060 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008061 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8062 txn->hdr_idx.used--;
8063 cur_hdr->len = 0;
8064 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01008065 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008066 break;
8067
8068 }
8069 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008070
8071 /* keep the link from this header to next one in case of later
8072 * removal of next header.
8073 */
8074 old_idx = cur_idx;
8075 }
8076 return 0;
8077}
8078
8079
8080/* Apply the filter to the status line in the response buffer <rtr>.
8081 * Returns 0 if nothing has been done, 1 if the filter has been applied,
8082 * or -1 if a replacement resulted in an invalid status line.
8083 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008084int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008085{
Willy Tarreaua15645d2007-03-18 16:22:39 +01008086 char *cur_ptr, *cur_end;
8087 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008088 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008089 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008090
8091
Willy Tarreau3d300592007-03-18 18:34:41 +01008092 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008093 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008094 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008095 (exp->action == ACT_ALLOW ||
8096 exp->action == ACT_DENY))
8097 return 0;
8098 else if (exp->action == ACT_REMOVE)
8099 return 0;
8100
8101 done = 0;
8102
Willy Tarreau9b28e032012-10-12 23:49:43 +02008103 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008104 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008105
8106 /* Now we have the status line between cur_ptr and cur_end */
8107
Willy Tarreau15a53a42015-01-21 13:39:42 +01008108 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008109 switch (exp->action) {
8110 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008111 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008112 done = 1;
8113 break;
8114
8115 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008116 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008117 done = 1;
8118 break;
8119
8120 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008121 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8122 if (trash.len < 0)
8123 return -1;
8124
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008125 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008126 /* FIXME: if the user adds a newline in the replacement, the
8127 * index will not be recalculated for now, and the new line
8128 * will not be counted as a new header.
8129 */
8130
Willy Tarreaufa355d42009-11-29 18:12:29 +01008131 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008132 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008133 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02008134 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01008135 cur_ptr, cur_end + 1,
8136 NULL, NULL);
8137 if (unlikely(!cur_end))
8138 return -1;
8139
8140 /* we have a full respnse and we know that we have either a CR
8141 * or an LF at <ptr>.
8142 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008143 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008144 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01008145 /* there is no point trying this regex on headers */
8146 return 1;
8147 }
8148 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008149 return done;
8150}
8151
8152
8153
8154/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008155 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008156 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
8157 * unparsable response.
8158 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008159int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008160{
Willy Tarreau192252e2015-04-04 01:47:55 +02008161 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008162 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008163 struct hdr_exp *exp;
8164
8165 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008166 int ret;
8167
8168 /*
8169 * The interleaving of transformations and verdicts
8170 * makes it difficult to decide to continue or stop
8171 * the evaluation.
8172 */
8173
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008174 if (txn->flags & TX_SVDENY)
8175 break;
8176
Willy Tarreau3d300592007-03-18 18:34:41 +01008177 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008178 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
8179 exp->action == ACT_PASS)) {
8180 exp = exp->next;
8181 continue;
8182 }
8183
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008184 /* if this filter had a condition, evaluate it now and skip to
8185 * next filter if the condition does not match.
8186 */
8187 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02008188 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008189 ret = acl_pass(ret);
8190 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8191 ret = !ret;
8192 if (!ret)
8193 continue;
8194 }
8195
Willy Tarreaua15645d2007-03-18 16:22:39 +01008196 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008197 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008198 if (unlikely(ret < 0))
8199 return -1;
8200
8201 if (likely(ret == 0)) {
8202 /* The filter did not match the response, it can be
8203 * iterated through all headers.
8204 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008205 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8206 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008207 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008208 }
8209 return 0;
8210}
8211
8212
Willy Tarreaua15645d2007-03-18 16:22:39 +01008213/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008214 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008215 * desirable to call it only when needed. This function is also used when we
8216 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008217 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008218void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008219{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008220 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008221 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01008222 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008223 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008224 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008225 char *hdr_beg, *hdr_end, *hdr_next;
8226 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008227
Willy Tarreaua15645d2007-03-18 16:22:39 +01008228 /* Iterate through the headers.
8229 * we start with the start line.
8230 */
8231 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008232 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008233
8234 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8235 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008236 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008237
8238 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008239 hdr_beg = hdr_next;
8240 hdr_end = hdr_beg + cur_hdr->len;
8241 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008242
Willy Tarreau24581ba2010-08-31 22:39:35 +02008243 /* We have one full header between hdr_beg and hdr_end, and the
8244 * next header starts at hdr_next. We're only interested in
8245 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008246 */
8247
Willy Tarreau24581ba2010-08-31 22:39:35 +02008248 is_cookie2 = 0;
8249 prev = hdr_beg + 10;
8250 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008251 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008252 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8253 if (!val) {
8254 old_idx = cur_idx;
8255 continue;
8256 }
8257 is_cookie2 = 1;
8258 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008259 }
8260
Willy Tarreau24581ba2010-08-31 22:39:35 +02008261 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8262 * <prev> points to the colon.
8263 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008264 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008265
Willy Tarreau24581ba2010-08-31 22:39:35 +02008266 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8267 * check-cache is enabled) and we are not interested in checking
8268 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008269 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02008270 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008271 return;
8272
Willy Tarreau24581ba2010-08-31 22:39:35 +02008273 /* OK so now we know we have to process this response cookie.
8274 * The format of the Set-Cookie header is slightly different
8275 * from the format of the Cookie header in that it does not
8276 * support the comma as a cookie delimiter (thus the header
8277 * cannot be folded) because the Expires attribute described in
8278 * the original Netscape's spec may contain an unquoted date
8279 * with a comma inside. We have to live with this because
8280 * many browsers don't support Max-Age and some browsers don't
8281 * support quoted strings. However the Set-Cookie2 header is
8282 * clean.
8283 *
8284 * We have to keep multiple pointers in order to support cookie
8285 * removal at the beginning, middle or end of header without
8286 * corrupting the header (in case of set-cookie2). A special
8287 * pointer, <scav> points to the beginning of the set-cookie-av
8288 * fields after the first semi-colon. The <next> pointer points
8289 * either to the end of line (set-cookie) or next unquoted comma
8290 * (set-cookie2). All of these headers are valid :
8291 *
8292 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8293 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8294 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8295 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8296 * | | | | | | | | | |
8297 * | | | | | | | | +-> next hdr_end <--+
8298 * | | | | | | | +------------> scav
8299 * | | | | | | +--------------> val_end
8300 * | | | | | +--------------------> val_beg
8301 * | | | | +----------------------> equal
8302 * | | | +------------------------> att_end
8303 * | | +----------------------------> att_beg
8304 * | +------------------------------> prev
8305 * +-----------------------------------------> hdr_beg
8306 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008307
Willy Tarreau24581ba2010-08-31 22:39:35 +02008308 for (; prev < hdr_end; prev = next) {
8309 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008310
Willy Tarreau24581ba2010-08-31 22:39:35 +02008311 /* find att_beg */
8312 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01008313 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008314 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008315
Willy Tarreau24581ba2010-08-31 22:39:35 +02008316 /* find att_end : this is the first character after the last non
8317 * space before the equal. It may be equal to hdr_end.
8318 */
8319 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008320
Willy Tarreau24581ba2010-08-31 22:39:35 +02008321 while (equal < hdr_end) {
8322 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8323 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01008324 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008325 continue;
8326 att_end = equal;
8327 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008328
Willy Tarreau24581ba2010-08-31 22:39:35 +02008329 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8330 * is between <att_beg> and <equal>, both may be identical.
8331 */
8332
8333 /* look for end of cookie if there is an equal sign */
8334 if (equal < hdr_end && *equal == '=') {
8335 /* look for the beginning of the value */
8336 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01008337 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008338 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008339
Willy Tarreau24581ba2010-08-31 22:39:35 +02008340 /* find the end of the value, respecting quotes */
8341 next = find_cookie_value_end(val_beg, hdr_end);
8342
8343 /* make val_end point to the first white space or delimitor after the value */
8344 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01008345 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008346 val_end--;
8347 } else {
8348 /* <equal> points to next comma, semi-colon or EOL */
8349 val_beg = val_end = next = equal;
8350 }
8351
8352 if (next < hdr_end) {
8353 /* Set-Cookie2 supports multiple cookies, and <next> points to
8354 * a colon or semi-colon before the end. So skip all attr-value
8355 * pairs and look for the next comma. For Set-Cookie, since
8356 * commas are permitted in values, skip to the end.
8357 */
8358 if (is_cookie2)
8359 next = find_hdr_value_end(next, hdr_end);
8360 else
8361 next = hdr_end;
8362 }
8363
8364 /* Now everything is as on the diagram above */
8365
8366 /* Ignore cookies with no equal sign */
8367 if (equal == val_end)
8368 continue;
8369
8370 /* If there are spaces around the equal sign, we need to
8371 * strip them otherwise we'll get trouble for cookie captures,
8372 * or even for rewrites. Since this happens extremely rarely,
8373 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008374 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008375 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8376 int stripped_before = 0;
8377 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008378
Willy Tarreau24581ba2010-08-31 22:39:35 +02008379 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008380 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008381 equal += stripped_before;
8382 val_beg += stripped_before;
8383 }
8384
8385 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008386 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008387 val_beg += stripped_after;
8388 stripped_before += stripped_after;
8389 }
8390
8391 val_end += stripped_before;
8392 next += stripped_before;
8393 hdr_end += stripped_before;
8394 hdr_next += stripped_before;
8395 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008396 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008397 }
8398
8399 /* First, let's see if we want to capture this cookie. We check
8400 * that we don't already have a server side cookie, because we
8401 * can only capture one. Also as an optimisation, we ignore
8402 * cookies shorter than the declared name.
8403 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008404 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008405 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008406 (val_end - att_beg >= sess->fe->capture_namelen) &&
8407 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008408 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008409 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008410 Alert("HTTP logging : out of memory.\n");
8411 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008412 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008413 if (log_len > sess->fe->capture_len)
8414 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008415 memcpy(txn->srv_cookie, att_beg, log_len);
8416 txn->srv_cookie[log_len] = 0;
8417 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008418 }
8419
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008420 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008421 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008422 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008423 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8424 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008425 /* assume passive cookie by default */
8426 txn->flags &= ~TX_SCK_MASK;
8427 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008428
8429 /* If the cookie is in insert mode on a known server, we'll delete
8430 * this occurrence because we'll insert another one later.
8431 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008432 * a direct access.
8433 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008434 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008435 /* The "preserve" flag was set, we don't want to touch the
8436 * server's cookie.
8437 */
8438 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008439 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008440 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008441 /* this cookie must be deleted */
8442 if (*prev == ':' && next == hdr_end) {
8443 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008444 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008445 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8446 txn->hdr_idx.used--;
8447 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008448 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008449 hdr_next += delta;
8450 http_msg_move_end(&txn->rsp, delta);
8451 /* note: while both invalid now, <next> and <hdr_end>
8452 * are still equal, so the for() will stop as expected.
8453 */
8454 } else {
8455 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008456 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008457 next = prev;
8458 hdr_end += delta;
8459 hdr_next += delta;
8460 cur_hdr->len += delta;
8461 http_msg_move_end(&txn->rsp, delta);
8462 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008463 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008464 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008465 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008466 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008467 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008468 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008469 * with this server since we know it.
8470 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008471 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008472 next += delta;
8473 hdr_end += delta;
8474 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008475 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008476 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008477
Willy Tarreauf1348312010-10-07 15:54:11 +02008478 txn->flags &= ~TX_SCK_MASK;
8479 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008480 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008481 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008482 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008483 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008484 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008485 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008486 next += delta;
8487 hdr_end += delta;
8488 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008489 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008490 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008491
Willy Tarreau827aee92011-03-10 16:55:02 +01008492 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008493 txn->flags &= ~TX_SCK_MASK;
8494 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008495 }
8496 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008497 /* that's done for this cookie, check the next one on the same
8498 * line when next != hdr_end (only if is_cookie2).
8499 */
8500 }
8501 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008502 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008503 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008504}
8505
8506
Willy Tarreaua15645d2007-03-18 16:22:39 +01008507/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008508 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008509 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008510void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008511{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008512 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008513 char *p1, *p2;
8514
8515 char *cur_ptr, *cur_end, *cur_next;
8516 int cur_idx;
8517
Willy Tarreau5df51872007-11-25 16:20:08 +01008518 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008519 return;
8520
8521 /* Iterate through the headers.
8522 * we start with the start line.
8523 */
8524 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008525 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008526
8527 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8528 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008529 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008530
8531 cur_hdr = &txn->hdr_idx.v[cur_idx];
8532 cur_ptr = cur_next;
8533 cur_end = cur_ptr + cur_hdr->len;
8534 cur_next = cur_end + cur_hdr->cr + 1;
8535
8536 /* We have one full header between cur_ptr and cur_end, and the
8537 * next header starts at cur_next. We're only interested in
8538 * "Cookie:" headers.
8539 */
8540
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008541 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8542 if (val) {
8543 if ((cur_end - (cur_ptr + val) >= 8) &&
8544 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8545 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8546 return;
8547 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008548 }
8549
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008550 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8551 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008552 continue;
8553
8554 /* OK, right now we know we have a cache-control header at cur_ptr */
8555
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008556 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008557
8558 if (p1 >= cur_end) /* no more info */
8559 continue;
8560
8561 /* p1 is at the beginning of the value */
8562 p2 = p1;
8563
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008564 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008565 p2++;
8566
8567 /* we have a complete value between p1 and p2 */
8568 if (p2 < cur_end && *p2 == '=') {
8569 /* we have something of the form no-cache="set-cookie" */
8570 if ((cur_end - p1 >= 21) &&
8571 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8572 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008573 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008574 continue;
8575 }
8576
8577 /* OK, so we know that either p2 points to the end of string or to a comma */
8578 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008579 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008580 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8581 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8582 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008583 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008584 return;
8585 }
8586
8587 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008588 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008589 continue;
8590 }
8591 }
8592}
8593
Willy Tarreau58f10d72006-12-04 02:26:12 +01008594
Willy Tarreaub2513902006-12-17 14:52:38 +01008595/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008596 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008597 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008598 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008599 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008600 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008601 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008602 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008603 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008604int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008605{
8606 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008607 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008608 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008609
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008610 if (!uri_auth)
8611 return 0;
8612
Cyril Bonté70be45d2010-10-12 00:14:35 +02008613 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008614 return 0;
8615
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008616 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008617 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008618 return 0;
8619
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008620 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008621 return 0;
8622
Willy Tarreaub2513902006-12-17 14:52:38 +01008623 return 1;
8624}
8625
Willy Tarreau4076a152009-04-02 15:18:36 +02008626/*
8627 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008628 * By default it tries to report the error position as msg->err_pos. However if
8629 * this one is not set, it will then report msg->next, which is the last known
8630 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008631 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008632 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008633void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008634 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008635 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008636{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008637 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008638 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008639 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008640
Willy Tarreauf3764b72016-03-31 13:45:10 +02008641 es->len = MIN(chn->buf->i, global.tune.bufsize);
Willy Tarreau9b28e032012-10-12 23:49:43 +02008642 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008643 len1 = MIN(len1, es->len);
8644 len2 = es->len - len1; /* remaining data if buffer wraps */
8645
Willy Tarreauf3764b72016-03-31 13:45:10 +02008646 if (!es->buf)
8647 es->buf = malloc(global.tune.bufsize);
8648
8649 if (es->buf) {
8650 memcpy(es->buf, chn->buf->p, len1);
8651 if (len2)
8652 memcpy(es->buf + len1, chn->buf->data, len2);
8653 }
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008654
Willy Tarreau4076a152009-04-02 15:18:36 +02008655 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008656 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008657 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008658 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008659
Willy Tarreau4076a152009-04-02 15:18:36 +02008660 es->when = date; // user-visible date
8661 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008662 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008663 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008664 if (objt_conn(sess->origin))
8665 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008666 else
8667 memset(&es->src, 0, sizeof(es->src));
8668
Willy Tarreau078272e2010-12-12 12:46:33 +01008669 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008670 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008671 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008672 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008673 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008674 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008675 es->b_out = chn->buf->o;
8676 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008677 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008678 es->m_clen = msg->chunk_len;
8679 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008680}
Willy Tarreaub2513902006-12-17 14:52:38 +01008681
Willy Tarreau294c4732011-12-16 21:35:50 +01008682/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8683 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8684 * performed over the whole headers. Otherwise it must contain a valid header
8685 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8686 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8687 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8688 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008689 * -1. The value fetch stops at commas, so this function is suited for use with
8690 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008691 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008692 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008693unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008694 struct hdr_idx *idx, int occ,
8695 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008696{
Willy Tarreau294c4732011-12-16 21:35:50 +01008697 struct hdr_ctx local_ctx;
8698 char *ptr_hist[MAX_HDR_HISTORY];
8699 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008700 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008701 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008702
Willy Tarreau294c4732011-12-16 21:35:50 +01008703 if (!ctx) {
8704 local_ctx.idx = 0;
8705 ctx = &local_ctx;
8706 }
8707
Willy Tarreaubce70882009-09-07 11:51:47 +02008708 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008709 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008710 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008711 occ--;
8712 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008713 *vptr = ctx->line + ctx->val;
8714 *vlen = ctx->vlen;
8715 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008716 }
8717 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008718 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008719 }
8720
8721 /* negative occurrence, we scan all the list then walk back */
8722 if (-occ > MAX_HDR_HISTORY)
8723 return 0;
8724
Willy Tarreau294c4732011-12-16 21:35:50 +01008725 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008726 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008727 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8728 len_hist[hist_ptr] = ctx->vlen;
8729 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008730 hist_ptr = 0;
8731 found++;
8732 }
8733 if (-occ > found)
8734 return 0;
8735 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008736 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8737 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8738 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008739 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008740 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008741 if (hist_ptr >= MAX_HDR_HISTORY)
8742 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008743 *vptr = ptr_hist[hist_ptr];
8744 *vlen = len_hist[hist_ptr];
8745 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008746}
8747
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008748/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8749 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8750 * performed over the whole headers. Otherwise it must contain a valid header
8751 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8752 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8753 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8754 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8755 * -1. This function differs from http_get_hdr() in that it only returns full
8756 * line header values and does not stop at commas.
8757 * The return value is 0 if nothing was found, or non-zero otherwise.
8758 */
8759unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8760 struct hdr_idx *idx, int occ,
8761 struct hdr_ctx *ctx, char **vptr, int *vlen)
8762{
8763 struct hdr_ctx local_ctx;
8764 char *ptr_hist[MAX_HDR_HISTORY];
8765 int len_hist[MAX_HDR_HISTORY];
8766 unsigned int hist_ptr;
8767 int found;
8768
8769 if (!ctx) {
8770 local_ctx.idx = 0;
8771 ctx = &local_ctx;
8772 }
8773
8774 if (occ >= 0) {
8775 /* search from the beginning */
8776 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8777 occ--;
8778 if (occ <= 0) {
8779 *vptr = ctx->line + ctx->val;
8780 *vlen = ctx->vlen;
8781 return 1;
8782 }
8783 }
8784 return 0;
8785 }
8786
8787 /* negative occurrence, we scan all the list then walk back */
8788 if (-occ > MAX_HDR_HISTORY)
8789 return 0;
8790
8791 found = hist_ptr = 0;
8792 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8793 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8794 len_hist[hist_ptr] = ctx->vlen;
8795 if (++hist_ptr >= MAX_HDR_HISTORY)
8796 hist_ptr = 0;
8797 found++;
8798 }
8799 if (-occ > found)
8800 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008801
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008802 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008803 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8804 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8805 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008806 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008807 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008808 if (hist_ptr >= MAX_HDR_HISTORY)
8809 hist_ptr -= MAX_HDR_HISTORY;
8810 *vptr = ptr_hist[hist_ptr];
8811 *vlen = len_hist[hist_ptr];
8812 return 1;
8813}
8814
Willy Tarreaubaaee002006-06-26 02:48:02 +02008815/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008816 * Print a debug line with a header. Always stop at the first CR or LF char,
8817 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8818 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008819 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008820void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008821{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008822 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008823 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008824
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008825 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008826 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008827 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008828 objt_conn(s->si[1].end) ? (unsigned short)objt_conn(s->si[1].end)->t.sock.fd : -1);
Willy Tarreaue92693a2012-09-24 21:13:39 +02008829
8830 for (max = 0; start + max < end; max++)
8831 if (start[max] == '\r' || start[max] == '\n')
8832 break;
8833
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008834 UBOUND(max, trash.size - trash.len - 3);
8835 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8836 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008837 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008838}
8839
Willy Tarreaueee5b512015-04-03 23:46:31 +02008840
8841/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8842 * The hdr_idx is allocated as well. In case of allocation failure, everything
8843 * allocated is freed and NULL is returned. Otherwise the new transaction is
8844 * assigned to the stream and returned.
8845 */
8846struct http_txn *http_alloc_txn(struct stream *s)
8847{
8848 struct http_txn *txn = s->txn;
8849
8850 if (txn)
8851 return txn;
8852
8853 txn = pool_alloc2(pool2_http_txn);
8854 if (!txn)
8855 return txn;
8856
8857 txn->hdr_idx.size = global.tune.max_http_hdr;
8858 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8859 if (!txn->hdr_idx.v) {
8860 pool_free2(pool2_http_txn, txn);
8861 return NULL;
8862 }
8863
8864 s->txn = txn;
8865 return txn;
8866}
8867
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008868void http_txn_reset_req(struct http_txn *txn)
8869{
8870 txn->req.flags = 0;
8871 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8872 txn->req.next = 0;
8873 txn->req.chunk_len = 0LL;
8874 txn->req.body_len = 0LL;
8875 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8876}
8877
8878void http_txn_reset_res(struct http_txn *txn)
8879{
8880 txn->rsp.flags = 0;
8881 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8882 txn->rsp.next = 0;
8883 txn->rsp.chunk_len = 0LL;
8884 txn->rsp.body_len = 0LL;
8885 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8886}
8887
Willy Tarreau0937bc42009-12-22 15:03:09 +01008888/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008889 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008890 * the required fields are properly allocated and that we only need to (re)init
8891 * them. This should be used before processing any new request.
8892 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008893void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008894{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008895 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008896 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008897
8898 txn->flags = 0;
8899 txn->status = -1;
8900
Willy Tarreauf64d1412010-10-07 20:06:11 +02008901 txn->cookie_first_date = 0;
8902 txn->cookie_last_date = 0;
8903
Willy Tarreaueee5b512015-04-03 23:46:31 +02008904 txn->srv_cookie = NULL;
8905 txn->cli_cookie = NULL;
8906 txn->uri = NULL;
8907
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008908 http_txn_reset_req(txn);
8909 http_txn_reset_res(txn);
8910
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008911 txn->req.chn = &s->req;
8912 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008913
8914 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008915
8916 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8917 if (fe->options2 & PR_O2_REQBUG_OK)
8918 txn->req.err_pos = -1; /* let buggy requests pass */
8919
Willy Tarreau0937bc42009-12-22 15:03:09 +01008920 if (txn->hdr_idx.v)
8921 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008922
8923 vars_init(&s->vars_txn, SCOPE_TXN);
8924 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008925}
8926
8927/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008928void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008929{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008930 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008931 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008932
8933 /* these ones will have been dynamically allocated */
8934 pool_free2(pool2_requri, txn->uri);
8935 pool_free2(pool2_capture, txn->cli_cookie);
8936 pool_free2(pool2_capture, txn->srv_cookie);
William Lallemanda73203e2012-03-12 12:48:57 +01008937 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008938
William Lallemanda73203e2012-03-12 12:48:57 +01008939 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008940 txn->uri = NULL;
8941 txn->srv_cookie = NULL;
8942 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008943
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008944 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008945 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008946 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008947 pool_free2(h->pool, s->req_cap[h->index]);
8948 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008949 }
8950
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008951 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008952 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008953 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008954 pool_free2(h->pool, s->res_cap[h->index]);
8955 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008956 }
8957
Willy Tarreau6204cd92016-03-10 16:33:04 +01008958 vars_prune(&s->vars_txn, s->sess, s);
8959 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008960}
8961
8962/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008963void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008964{
8965 http_end_txn(s);
8966 http_init_txn(s);
8967
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008968 /* reinitialise the current rule list pointer to NULL. We are sure that
8969 * any rulelist match the NULL pointer.
8970 */
8971 s->current_rule_list = NULL;
8972
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008973 s->be = strm_fe(s);
8974 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008975 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008976 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008977 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008978 /* re-init store persistence */
8979 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008980 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008981
Willy Tarreau0937bc42009-12-22 15:03:09 +01008982 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008983
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008984 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008985
Willy Tarreau739cfba2010-01-25 23:11:14 +01008986 /* We must trim any excess data from the response buffer, because we
8987 * may have blocked an invalid response from a server that we don't
8988 * want to accidentely forward once we disable the analysers, nor do
8989 * we want those data to come along with next response. A typical
8990 * example of such data would be from a buggy server responding to
8991 * a HEAD with some data, or sending more than the advertised
8992 * content-length.
8993 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008994 if (unlikely(s->res.buf->i))
8995 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008996
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008997 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008998 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008999
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009000 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009001 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009002
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009003 s->req.rex = TICK_ETERNITY;
9004 s->req.wex = TICK_ETERNITY;
9005 s->req.analyse_exp = TICK_ETERNITY;
9006 s->res.rex = TICK_ETERNITY;
9007 s->res.wex = TICK_ETERNITY;
9008 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009009}
Willy Tarreau58f10d72006-12-04 02:26:12 +01009010
Sasha Pachev218f0642014-06-16 12:05:59 -06009011void free_http_res_rules(struct list *r)
9012{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009013 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06009014
9015 list_for_each_entry_safe(pr, tr, r, list) {
9016 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009017 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06009018 free(pr);
9019 }
9020}
9021
9022void free_http_req_rules(struct list *r)
9023{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009024 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01009025
9026 list_for_each_entry_safe(pr, tr, r, list) {
9027 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009028 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009029 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01009030
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009031 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01009032 free(pr);
9033 }
9034}
9035
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009036/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009037struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01009038{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009039 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009040 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009041 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009042 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01009043
Vincent Bernat02779b62016-04-03 13:48:43 +02009044 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01009045 if (!rule) {
9046 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009047 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009048 }
9049
CJ Ess108b1dd2015-04-07 12:03:37 -04009050 rule->deny_status = HTTP_ERR_403;
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009051 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009052 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01009053 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02009054 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04009055 int code;
9056 int hc;
9057
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009058 rule->action = ACT_ACTION_DENY;
Willy Tarreauff011f22011-01-06 17:51:27 +01009059 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04009060 if (strcmp(args[cur_arg], "deny_status") == 0) {
9061 cur_arg++;
9062 if (!args[cur_arg]) {
9063 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
9064 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9065 goto out_err;
9066 }
9067
9068 code = atol(args[cur_arg]);
9069 cur_arg++;
9070 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
9071 if (http_err_codes[hc] == code) {
9072 rule->deny_status = hc;
9073 break;
9074 }
9075 }
9076
9077 if (hc >= HTTP_ERR_SIZE) {
9078 Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n",
9079 file, linenum, code);
9080 }
9081 }
Willy Tarreauccbcc372012-12-27 12:37:57 +01009082 } else if (!strcmp(args[0], "tarpit")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009083 rule->action = ACT_HTTP_REQ_TARPIT;
Willy Tarreauccbcc372012-12-27 12:37:57 +01009084 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01009085 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009086 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01009087 cur_arg = 1;
9088
9089 while(*args[cur_arg]) {
9090 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009091 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01009092 cur_arg+=2;
9093 continue;
9094 } else
9095 break;
9096 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009097 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009098 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009099 cur_arg = 1;
9100
9101 if (!*args[cur_arg] ||
9102 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9103 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
9104 file, linenum, args[0]);
9105 goto out_err;
9106 }
9107 rule->arg.nice = atoi(args[cur_arg]);
9108 if (rule->arg.nice < -1024)
9109 rule->arg.nice = -1024;
9110 else if (rule->arg.nice > 1024)
9111 rule->arg.nice = 1024;
9112 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009113 } else if (!strcmp(args[0], "set-tos")) {
9114#ifdef IP_TOS
9115 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009116 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009117 cur_arg = 1;
9118
9119 if (!*args[cur_arg] ||
9120 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9121 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9122 file, linenum, args[0]);
9123 goto out_err;
9124 }
9125
9126 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9127 if (err && *err != '\0') {
9128 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9129 file, linenum, err, args[0]);
9130 goto out_err;
9131 }
9132 cur_arg++;
9133#else
9134 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9135 goto out_err;
9136#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009137 } else if (!strcmp(args[0], "set-mark")) {
9138#ifdef SO_MARK
9139 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009140 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009141 cur_arg = 1;
9142
9143 if (!*args[cur_arg] ||
9144 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9145 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9146 file, linenum, args[0]);
9147 goto out_err;
9148 }
9149
9150 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9151 if (err && *err != '\0') {
9152 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9153 file, linenum, err, args[0]);
9154 goto out_err;
9155 }
9156 cur_arg++;
9157 global.last_checks |= LSTCHK_NETADM;
9158#else
9159 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9160 goto out_err;
9161#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009162 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009163 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009164 cur_arg = 1;
9165
9166 if (!*args[cur_arg] ||
9167 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9168 bad_log_level:
9169 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9170 file, linenum, args[0]);
9171 goto out_err;
9172 }
9173 if (strcmp(args[cur_arg], "silent") == 0)
9174 rule->arg.loglevel = -1;
9175 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9176 goto bad_log_level;
9177 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009178 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009179 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009180 cur_arg = 1;
9181
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009182 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9183 (*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 +01009184 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9185 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009186 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009187 }
9188
9189 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9190 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9191 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009192
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009193 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009194 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009195 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 +01009196 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9197 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9198 file, linenum, args[0], error);
9199 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009200 goto out_err;
9201 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009202 free(proxy->conf.lfs_file);
9203 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9204 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009205 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009206 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009207 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009208 cur_arg = 1;
9209
9210 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009211 (*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 -06009212 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9213 file, linenum, args[0]);
9214 goto out_err;
9215 }
9216
9217 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9218 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9219 LIST_INIT(&rule->arg.hdr_add.fmt);
9220
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009221 error = NULL;
9222 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9223 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9224 args[cur_arg + 1], error);
9225 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009226 goto out_err;
9227 }
9228
9229 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009230 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009231 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 +01009232 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9233 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9234 file, linenum, args[0], error);
9235 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009236 goto out_err;
9237 }
Sasha Pachev218f0642014-06-16 12:05:59 -06009238
9239 free(proxy->conf.lfs_file);
9240 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9241 proxy->conf.lfs_line = proxy->conf.args.line;
9242 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009243 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009244 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009245 cur_arg = 1;
9246
9247 if (!*args[cur_arg] ||
9248 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9249 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9250 file, linenum, args[0]);
9251 goto out_err;
9252 }
9253
9254 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9255 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9256
9257 proxy->conf.args.ctx = ARGC_HRQ;
9258 free(proxy->conf.lfs_file);
9259 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9260 proxy->conf.lfs_line = proxy->conf.args.line;
9261 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009262 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9263 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009264 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009265 struct sample_expr *expr;
9266 unsigned int where;
9267 char *err = NULL;
9268
9269 cur_arg = 1;
9270 proxy->conf.args.ctx = ARGC_TRK;
9271
9272 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9273 if (!expr) {
9274 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9275 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9276 free(err);
9277 goto out_err;
9278 }
9279
9280 where = 0;
9281 if (proxy->cap & PR_CAP_FE)
9282 where |= SMP_VAL_FE_HRQ_HDR;
9283 if (proxy->cap & PR_CAP_BE)
9284 where |= SMP_VAL_BE_HRQ_HDR;
9285
9286 if (!(expr->fetch->val & where)) {
9287 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9288 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9289 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9290 args[cur_arg-1], sample_src_names(expr->fetch->use));
9291 free(expr);
9292 goto out_err;
9293 }
9294
9295 if (strcmp(args[cur_arg], "table") == 0) {
9296 cur_arg++;
9297 if (!args[cur_arg]) {
9298 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9299 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9300 free(expr);
9301 goto out_err;
9302 }
9303 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009304 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02009305 cur_arg++;
9306 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009307 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009308 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009309 } else if (strcmp(args[0], "redirect") == 0) {
9310 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009311 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009312
Willy Tarreaube4653b2015-05-28 15:26:58 +02009313 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009314 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9315 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9316 goto out_err;
9317 }
9318
9319 /* this redirect rule might already contain a parsed condition which
9320 * we'll pass to the http-request rule.
9321 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009322 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009323 rule->arg.redir = redir;
9324 rule->cond = redir->cond;
9325 redir->cond = NULL;
9326 cur_arg = 2;
9327 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009328 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9329 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009330 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009331 /*
9332 * '+ 8' for 'add-acl('
9333 * '- 9' for 'add-acl(' + trailing ')'
9334 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009335 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009336
9337 cur_arg = 1;
9338
9339 if (!*args[cur_arg] ||
9340 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9341 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9342 file, linenum, args[0]);
9343 goto out_err;
9344 }
9345
9346 LIST_INIT(&rule->arg.map.key);
9347 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009348 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009349 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009350 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9351 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9352 file, linenum, args[0], error);
9353 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009354 goto out_err;
9355 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009356 free(proxy->conf.lfs_file);
9357 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9358 proxy->conf.lfs_line = proxy->conf.args.line;
9359 cur_arg += 1;
9360 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9361 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009362 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009363 /*
9364 * '+ 8' for 'del-acl('
9365 * '- 9' for 'del-acl(' + trailing ')'
9366 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009367 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009368
9369 cur_arg = 1;
9370
9371 if (!*args[cur_arg] ||
9372 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9373 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9374 file, linenum, args[0]);
9375 goto out_err;
9376 }
9377
9378 LIST_INIT(&rule->arg.map.key);
9379 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009380 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009381 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009382 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9383 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9384 file, linenum, args[0], error);
9385 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009386 goto out_err;
9387 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009388 free(proxy->conf.lfs_file);
9389 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9390 proxy->conf.lfs_line = proxy->conf.args.line;
9391 cur_arg += 1;
9392 } else if (strncmp(args[0], "del-map", 7) == 0) {
9393 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009394 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009395 /*
9396 * '+ 8' for 'del-map('
9397 * '- 9' for 'del-map(' + trailing ')'
9398 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009399 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009400
9401 cur_arg = 1;
9402
9403 if (!*args[cur_arg] ||
9404 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9405 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9406 file, linenum, args[0]);
9407 goto out_err;
9408 }
9409
9410 LIST_INIT(&rule->arg.map.key);
9411 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009412 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009413 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009414 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9415 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9416 file, linenum, args[0], error);
9417 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009418 goto out_err;
9419 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009420 free(proxy->conf.lfs_file);
9421 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9422 proxy->conf.lfs_line = proxy->conf.args.line;
9423 cur_arg += 1;
9424 } else if (strncmp(args[0], "set-map", 7) == 0) {
9425 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009426 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009427 /*
9428 * '+ 8' for 'set-map('
9429 * '- 9' for 'set-map(' + trailing ')'
9430 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009431 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009432
9433 cur_arg = 1;
9434
9435 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9436 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9437 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9438 file, linenum, args[0]);
9439 goto out_err;
9440 }
9441
9442 LIST_INIT(&rule->arg.map.key);
9443 LIST_INIT(&rule->arg.map.value);
9444 proxy->conf.args.ctx = ARGC_HRQ;
9445
9446 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009447 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009448 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009449 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9450 Alert("parsing [%s:%d]: 'http-request %s' key: %s.\n",
9451 file, linenum, args[0], error);
9452 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009453 goto out_err;
9454 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009455
9456 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009457 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009458 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 +01009459 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9460 Alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n",
9461 file, linenum, args[0], error);
9462 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009463 goto out_err;
9464 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009465 free(proxy->conf.lfs_file);
9466 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9467 proxy->conf.lfs_line = proxy->conf.args.line;
9468
9469 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009470 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9471 char *errmsg = NULL;
9472 cur_arg = 1;
9473 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009474 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009475 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009476 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009477 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9478 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9479 free(errmsg);
9480 goto out_err;
9481 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009482 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009483 action_build_list(&http_req_keywords.list, &trash);
9484 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
9485 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009486 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02009487 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009488 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009489 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009490 }
9491
9492 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9493 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009494 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009495
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009496 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9497 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9498 file, linenum, args[0], errmsg);
9499 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009500 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009501 }
9502 rule->cond = cond;
9503 }
9504 else if (*args[cur_arg]) {
9505 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9506 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9507 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009508 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009509 }
9510
9511 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009512 out_err:
9513 free(rule);
9514 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009515}
9516
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009517/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009518struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009519{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009520 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009521 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009522 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009523 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009524
9525 rule = calloc(1, sizeof(*rule));
9526 if (!rule) {
9527 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9528 goto out_err;
9529 }
9530
9531 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009532 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009533 cur_arg = 1;
9534 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009535 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009536 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009537 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009538 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009539 cur_arg = 1;
9540
9541 if (!*args[cur_arg] ||
9542 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9543 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9544 file, linenum, args[0]);
9545 goto out_err;
9546 }
9547 rule->arg.nice = atoi(args[cur_arg]);
9548 if (rule->arg.nice < -1024)
9549 rule->arg.nice = -1024;
9550 else if (rule->arg.nice > 1024)
9551 rule->arg.nice = 1024;
9552 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009553 } else if (!strcmp(args[0], "set-tos")) {
9554#ifdef IP_TOS
9555 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009556 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009557 cur_arg = 1;
9558
9559 if (!*args[cur_arg] ||
9560 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9561 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9562 file, linenum, args[0]);
9563 goto out_err;
9564 }
9565
9566 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9567 if (err && *err != '\0') {
9568 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9569 file, linenum, err, args[0]);
9570 goto out_err;
9571 }
9572 cur_arg++;
9573#else
9574 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9575 goto out_err;
9576#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009577 } else if (!strcmp(args[0], "set-mark")) {
9578#ifdef SO_MARK
9579 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009580 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009581 cur_arg = 1;
9582
9583 if (!*args[cur_arg] ||
9584 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9585 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9586 file, linenum, args[0]);
9587 goto out_err;
9588 }
9589
9590 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9591 if (err && *err != '\0') {
9592 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9593 file, linenum, err, args[0]);
9594 goto out_err;
9595 }
9596 cur_arg++;
9597 global.last_checks |= LSTCHK_NETADM;
9598#else
9599 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9600 goto out_err;
9601#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009602 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009603 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009604 cur_arg = 1;
9605
9606 if (!*args[cur_arg] ||
9607 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9608 bad_log_level:
9609 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9610 file, linenum, args[0]);
9611 goto out_err;
9612 }
9613 if (strcmp(args[cur_arg], "silent") == 0)
9614 rule->arg.loglevel = -1;
Ruoshan Huangdd016782016-06-15 22:16:03 +08009615 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009616 goto bad_log_level;
9617 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009618 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009619 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009620 cur_arg = 1;
9621
9622 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9623 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9624 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9625 file, linenum, args[0]);
9626 goto out_err;
9627 }
9628
9629 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9630 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9631 LIST_INIT(&rule->arg.hdr_add.fmt);
9632
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009633 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009634 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009635 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 +01009636 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9637 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9638 file, linenum, args[0], error);
9639 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009640 goto out_err;
9641 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009642 free(proxy->conf.lfs_file);
9643 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9644 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009645 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009646 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009647 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009648 cur_arg = 1;
9649
9650 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009651 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9652 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009653 file, linenum, args[0]);
9654 goto out_err;
9655 }
9656
9657 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9658 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9659 LIST_INIT(&rule->arg.hdr_add.fmt);
9660
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009661 error = NULL;
9662 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9663 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9664 args[cur_arg + 1], error);
9665 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009666 goto out_err;
9667 }
9668
9669 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009670 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009671 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 +01009672 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9673 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9674 file, linenum, args[0], error);
9675 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009676 goto out_err;
9677 }
Sasha Pachev218f0642014-06-16 12:05:59 -06009678
9679 free(proxy->conf.lfs_file);
9680 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9681 proxy->conf.lfs_line = proxy->conf.args.line;
9682 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009683 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009684 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009685 cur_arg = 1;
9686
9687 if (!*args[cur_arg] ||
9688 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9689 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9690 file, linenum, args[0]);
9691 goto out_err;
9692 }
9693
9694 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9695 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9696
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009697 proxy->conf.args.ctx = ARGC_HRS;
9698 free(proxy->conf.lfs_file);
9699 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9700 proxy->conf.lfs_line = proxy->conf.args.line;
9701 cur_arg += 1;
9702 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9703 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009704 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009705 /*
9706 * '+ 8' for 'add-acl('
9707 * '- 9' for 'add-acl(' + trailing ')'
9708 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009709 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009710
9711 cur_arg = 1;
9712
9713 if (!*args[cur_arg] ||
9714 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9715 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9716 file, linenum, args[0]);
9717 goto out_err;
9718 }
9719
9720 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009721 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009722 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009723 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009724 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9725 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9726 file, linenum, args[0], error);
9727 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009728 goto out_err;
9729 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009730 free(proxy->conf.lfs_file);
9731 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9732 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009733
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009734 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009735 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9736 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009737 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009738 /*
9739 * '+ 8' for 'del-acl('
9740 * '- 9' for 'del-acl(' + trailing ')'
9741 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009742 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009743
9744 cur_arg = 1;
9745
9746 if (!*args[cur_arg] ||
9747 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9748 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9749 file, linenum, args[0]);
9750 goto out_err;
9751 }
9752
9753 LIST_INIT(&rule->arg.map.key);
9754 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009755 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009756 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009757 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9758 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9759 file, linenum, args[0], error);
9760 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009761 goto out_err;
9762 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009763 free(proxy->conf.lfs_file);
9764 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9765 proxy->conf.lfs_line = proxy->conf.args.line;
9766 cur_arg += 1;
9767 } else if (strncmp(args[0], "del-map", 7) == 0) {
9768 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009769 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009770 /*
9771 * '+ 8' for 'del-map('
9772 * '- 9' for 'del-map(' + trailing ')'
9773 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009774 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009775
9776 cur_arg = 1;
9777
9778 if (!*args[cur_arg] ||
9779 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9780 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9781 file, linenum, args[0]);
9782 goto out_err;
9783 }
9784
9785 LIST_INIT(&rule->arg.map.key);
9786 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009787 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009788 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009789 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9790 Alert("parsing [%s:%d]: 'http-response %s' %s.\n",
9791 file, linenum, args[0], error);
9792 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009793 goto out_err;
9794 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009795 free(proxy->conf.lfs_file);
9796 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9797 proxy->conf.lfs_line = proxy->conf.args.line;
9798 cur_arg += 1;
9799 } else if (strncmp(args[0], "set-map", 7) == 0) {
9800 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009801 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009802 /*
9803 * '+ 8' for 'set-map('
9804 * '- 9' for 'set-map(' + trailing ')'
9805 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009806 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009807
9808 cur_arg = 1;
9809
9810 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9811 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9812 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9813 file, linenum, args[0]);
9814 goto out_err;
9815 }
9816
9817 LIST_INIT(&rule->arg.map.key);
9818 LIST_INIT(&rule->arg.map.value);
9819
9820 proxy->conf.args.ctx = ARGC_HRS;
9821
9822 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009823 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009824 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009825 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9826 Alert("parsing [%s:%d]: 'http-response %s' name: %s.\n",
9827 file, linenum, args[0], error);
9828 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009829 goto out_err;
9830 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009831
9832 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009833 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009834 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 +01009835 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9836 Alert("parsing [%s:%d]: 'http-response %s' value: %s.\n",
9837 file, linenum, args[0], error);
9838 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009839 goto out_err;
9840 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009841
9842 free(proxy->conf.lfs_file);
9843 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9844 proxy->conf.lfs_line = proxy->conf.args.line;
9845
9846 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009847 } else if (strcmp(args[0], "redirect") == 0) {
9848 struct redirect_rule *redir;
9849 char *errmsg = NULL;
9850
9851 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
9852 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9853 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9854 goto out_err;
9855 }
9856
9857 /* this redirect rule might already contain a parsed condition which
9858 * we'll pass to the http-request rule.
9859 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009860 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009861 rule->arg.redir = redir;
9862 rule->cond = redir->cond;
9863 redir->cond = NULL;
9864 cur_arg = 2;
9865 return rule;
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009866 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9867 args[0][9] == '\0' && args[0][8] >= '0' &&
9868 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
9869 struct sample_expr *expr;
9870 unsigned int where;
9871 char *err = NULL;
9872
9873 cur_arg = 1;
9874 proxy->conf.args.ctx = ARGC_TRK;
9875
9876 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9877 if (!expr) {
9878 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9879 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9880 free(err);
9881 goto out_err;
9882 }
9883
9884 where = 0;
9885 if (proxy->cap & PR_CAP_FE)
9886 where |= SMP_VAL_FE_HRS_HDR;
9887 if (proxy->cap & PR_CAP_BE)
9888 where |= SMP_VAL_BE_HRS_HDR;
9889
9890 if (!(expr->fetch->val & where)) {
9891 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :"
9892 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9893 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9894 args[cur_arg-1], sample_src_names(expr->fetch->use));
9895 free(expr);
9896 goto out_err;
9897 }
9898
9899 if (strcmp(args[cur_arg], "table") == 0) {
9900 cur_arg++;
9901 if (!args[cur_arg]) {
9902 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n",
9903 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9904 free(expr);
9905 goto out_err;
9906 }
9907 /* we copy the table name for now, it will be resolved later */
9908 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
9909 cur_arg++;
9910 }
9911 rule->arg.trk_ctr.expr = expr;
9912 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
William Lallemand73025dd2014-04-24 14:38:37 +02009913 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9914 char *errmsg = NULL;
9915 cur_arg = 1;
9916 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009917 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009918 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009919 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009920 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9921 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9922 free(errmsg);
9923 goto out_err;
9924 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009925 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009926 action_build_list(&http_res_keywords.list, &trash);
9927 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9928 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009929 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02009930 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009931 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009932 goto out_err;
9933 }
9934
9935 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9936 struct acl_cond *cond;
9937 char *errmsg = NULL;
9938
9939 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9940 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9941 file, linenum, args[0], errmsg);
9942 free(errmsg);
9943 goto out_err;
9944 }
9945 rule->cond = cond;
9946 }
9947 else if (*args[cur_arg]) {
9948 Alert("parsing [%s:%d]: 'http-response %s' expects"
9949 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9950 file, linenum, args[0], args[cur_arg]);
9951 goto out_err;
9952 }
9953
9954 return rule;
9955 out_err:
9956 free(rule);
9957 return NULL;
9958}
9959
Willy Tarreau4baae242012-12-27 12:00:31 +01009960/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009961 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009962 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9963 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009964 */
9965struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009966 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009967{
9968 struct redirect_rule *rule;
9969 int cur_arg;
9970 int type = REDIRECT_TYPE_NONE;
9971 int code = 302;
9972 const char *destination = NULL;
9973 const char *cookie = NULL;
9974 int cookie_set = 0;
9975 unsigned int flags = REDIRECT_FLAG_NONE;
9976 struct acl_cond *cond = NULL;
9977
9978 cur_arg = 0;
9979 while (*(args[cur_arg])) {
9980 if (strcmp(args[cur_arg], "location") == 0) {
9981 if (!*args[cur_arg + 1])
9982 goto missing_arg;
9983
9984 type = REDIRECT_TYPE_LOCATION;
9985 cur_arg++;
9986 destination = args[cur_arg];
9987 }
9988 else if (strcmp(args[cur_arg], "prefix") == 0) {
9989 if (!*args[cur_arg + 1])
9990 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009991 type = REDIRECT_TYPE_PREFIX;
9992 cur_arg++;
9993 destination = args[cur_arg];
9994 }
9995 else if (strcmp(args[cur_arg], "scheme") == 0) {
9996 if (!*args[cur_arg + 1])
9997 goto missing_arg;
9998
9999 type = REDIRECT_TYPE_SCHEME;
10000 cur_arg++;
10001 destination = args[cur_arg];
10002 }
10003 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
10004 if (!*args[cur_arg + 1])
10005 goto missing_arg;
10006
10007 cur_arg++;
10008 cookie = args[cur_arg];
10009 cookie_set = 1;
10010 }
10011 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
10012 if (!*args[cur_arg + 1])
10013 goto missing_arg;
10014
10015 cur_arg++;
10016 cookie = args[cur_arg];
10017 cookie_set = 0;
10018 }
10019 else if (strcmp(args[cur_arg], "code") == 0) {
10020 if (!*args[cur_arg + 1])
10021 goto missing_arg;
10022
10023 cur_arg++;
10024 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040010025 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +010010026 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040010027 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +010010028 args[cur_arg - 1], args[cur_arg]);
10029 return NULL;
10030 }
10031 }
10032 else if (!strcmp(args[cur_arg],"drop-query")) {
10033 flags |= REDIRECT_FLAG_DROP_QS;
10034 }
10035 else if (!strcmp(args[cur_arg],"append-slash")) {
10036 flags |= REDIRECT_FLAG_APPEND_SLASH;
10037 }
10038 else if (strcmp(args[cur_arg], "if") == 0 ||
10039 strcmp(args[cur_arg], "unless") == 0) {
10040 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
10041 if (!cond) {
10042 memprintf(errmsg, "error in condition: %s", *errmsg);
10043 return NULL;
10044 }
10045 break;
10046 }
10047 else {
10048 memprintf(errmsg,
10049 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
10050 args[cur_arg]);
10051 return NULL;
10052 }
10053 cur_arg++;
10054 }
10055
10056 if (type == REDIRECT_TYPE_NONE) {
10057 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
10058 return NULL;
10059 }
10060
Willy Tarreaube4653b2015-05-28 15:26:58 +020010061 if (dir && type != REDIRECT_TYPE_LOCATION) {
10062 memprintf(errmsg, "response only supports redirect type 'location'");
10063 return NULL;
10064 }
10065
Vincent Bernat3c2f2f22016-04-03 13:48:42 +020010066 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +010010067 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +010010068 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010069
10070 if (!use_fmt) {
10071 /* old-style static redirect rule */
10072 rule->rdr_str = strdup(destination);
10073 rule->rdr_len = strlen(destination);
10074 }
10075 else {
10076 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010077
10078 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
10079 * if prefix == "/", we don't want to add anything, otherwise it
10080 * makes it hard for the user to configure a self-redirection.
10081 */
Godbachd9722032014-12-18 15:44:58 +080010082 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010083 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010010084 if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
10085 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 +010010086 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
10087 errmsg)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010010088 return NULL;
10089 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +010010090 free(curproxy->conf.lfs_file);
10091 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
10092 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010093 }
10094 }
10095
Willy Tarreau4baae242012-12-27 12:00:31 +010010096 if (cookie) {
10097 /* depending on cookie_set, either we want to set the cookie, or to clear it.
10098 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
10099 */
10100 rule->cookie_len = strlen(cookie);
10101 if (cookie_set) {
10102 rule->cookie_str = malloc(rule->cookie_len + 10);
10103 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10104 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
10105 rule->cookie_len += 9;
10106 } else {
10107 rule->cookie_str = malloc(rule->cookie_len + 21);
10108 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10109 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
10110 rule->cookie_len += 20;
10111 }
10112 }
10113 rule->type = type;
10114 rule->code = code;
10115 rule->flags = flags;
10116 LIST_INIT(&rule->list);
10117 return rule;
10118
10119 missing_arg:
10120 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
10121 return NULL;
10122}
10123
Willy Tarreau8797c062007-05-07 00:55:35 +020010124/************************************************************************/
10125/* The code below is dedicated to ACL parsing and matching */
10126/************************************************************************/
10127
10128
Willy Tarreau14174bc2012-04-16 14:34:04 +020010129/* This function ensures that the prerequisites for an L7 fetch are ready,
10130 * which means that a request or response is ready. If some data is missing,
10131 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +020010132 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
10133 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010134 *
10135 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +020010136 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
10137 * decide whether or not an HTTP message is present ;
10138 * 0 if the requested data cannot be fetched or if it is certain that
10139 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010140 * 1 if an HTTP message is ready
10141 */
James Rosewell91a41cb2015-09-18 17:11:16 +010010142int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010143 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +020010144{
Willy Tarreau192252e2015-04-04 01:47:55 +020010145 struct http_txn *txn;
10146 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010147
Willy Tarreaube508f12016-03-10 11:47:01 +010010148 /* Note: it is possible that <s> is NULL when called before stream
10149 * initialization (eg: tcp-request connection), so this function is the
10150 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010151 */
Willy Tarreau192252e2015-04-04 01:47:55 +020010152 if (!s)
10153 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +010010154
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +020010155 if (!s->txn) {
10156 if (unlikely(!http_alloc_txn(s)))
10157 return 0; /* not enough memory */
10158 http_init_txn(s);
10159 }
Willy Tarreau192252e2015-04-04 01:47:55 +020010160 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +020010161 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010162
10163 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010164 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010165
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010166 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010167 /* If the buffer does not leave enough free space at the end,
10168 * we must first realign it.
10169 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010170 if (s->req.buf->p > s->req.buf->data &&
10171 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
10172 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +010010173
Willy Tarreau14174bc2012-04-16 14:34:04 +020010174 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +020010175 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +020010176 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010177
10178 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010179 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +020010180 http_msg_analyzer(msg, &txn->hdr_idx);
10181
10182 /* Still no valid request ? */
10183 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +020010184 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010185 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +020010186 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010187 }
10188 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +020010189 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010190 return 0;
10191 }
10192
10193 /* OK we just got a valid HTTP request. We have some minor
10194 * preparation to perform so that further checks can rely
10195 * on HTTP tests.
10196 */
Willy Tarreauaae75e32013-03-29 12:31:49 +010010197
10198 /* If the request was parsed but was too large, we must absolutely
10199 * return an error so that it is not processed. At the moment this
10200 * cannot happen, but if the parsers are to change in the future,
10201 * we want this check to be maintained.
10202 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010203 if (unlikely(s->req.buf->i + s->req.buf->p >
10204 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +010010205 msg->err_state = msg->msg_state;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010206 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010207 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010208 return 1;
10209 }
10210
Willy Tarreau9b28e032012-10-12 23:49:43 +020010211 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +020010212 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +020010213 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010214
Willy Tarreau506d0502013-07-06 13:29:24 +020010215 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
10216 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010217 }
10218
Willy Tarreau506d0502013-07-06 13:29:24 +020010219 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +020010220 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +020010221 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010222
10223 /* otherwise everything's ready for the request */
10224 }
Willy Tarreau24e32d82012-04-23 23:55:44 +020010225 else {
10226 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +020010227 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
10228 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010229 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +020010230 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010231 }
10232
10233 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010234 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010235 return 1;
10236}
Willy Tarreau8797c062007-05-07 00:55:35 +020010237
Willy Tarreau8797c062007-05-07 00:55:35 +020010238/* 1. Check on METHOD
10239 * We use the pre-parsed method if it is known, and store its number as an
10240 * integer. If it is unknown, we use the pointer and the length.
10241 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010242static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +020010243{
10244 int len, meth;
10245
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010010246 len = strlen(text);
10247 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +020010248
10249 pattern->val.i = meth;
10250 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +020010251 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +020010252 pattern->len = len;
10253 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010254 else {
10255 pattern->ptr.str = NULL;
10256 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010257 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010258 return 1;
10259}
10260
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010261/* This function fetches the method of current HTTP request and stores
10262 * it in the global pattern struct as a chunk. There are two possibilities :
10263 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10264 * in <len> and <ptr> is NULL ;
10265 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10266 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010267 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010268 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010269static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010270smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010271{
10272 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +010010273 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010274
Willy Tarreau24e32d82012-04-23 23:55:44 +020010275 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010276
Willy Tarreaube508f12016-03-10 11:47:01 +010010277 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010278 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010279 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010280 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010281 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010282 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10283 /* ensure the indexes are not affected */
10284 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010285 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010286 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
10287 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010288 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010289 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010290 return 1;
10291}
10292
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010293/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010294static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010295{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010296 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010297 struct pattern_list *lst;
10298 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010299
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010300 list_for_each_entry(lst, &expr->patterns, list) {
10301 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010302
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010303 /* well-known method */
10304 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010305 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010306 return pattern;
10307 else
10308 continue;
10309 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010310
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010311 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010312 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010313 continue;
10314
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010315 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010316 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
10317 (!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 +010010318 return pattern;
10319 }
10320 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010321}
10322
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010323static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010324smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010325{
Willy Tarreaube508f12016-03-10 11:47:01 +010010326 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010327 char *ptr;
10328 int len;
10329
Willy Tarreauc0239e02012-04-16 14:42:55 +020010330 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010331
Willy Tarreaube508f12016-03-10 11:47:01 +010010332 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010333 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010334 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010335
10336 while ((len-- > 0) && (*ptr++ != '/'));
10337 if (len <= 0)
10338 return 0;
10339
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010340 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010341 smp->data.u.str.str = ptr;
10342 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010343
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010344 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010345 return 1;
10346}
10347
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010348static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010349smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010350{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010351 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010352 char *ptr;
10353 int len;
10354
Willy Tarreauc0239e02012-04-16 14:42:55 +020010355 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010356
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010357 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010358 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10359 return 0;
10360
Willy Tarreau8797c062007-05-07 00:55:35 +020010361 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010362 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010363
10364 while ((len-- > 0) && (*ptr++ != '/'));
10365 if (len <= 0)
10366 return 0;
10367
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010368 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010369 smp->data.u.str.str = ptr;
10370 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010371
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010372 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010373 return 1;
10374}
10375
10376/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010377static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010378smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010379{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010380 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010381 char *ptr;
10382 int len;
10383
Willy Tarreauc0239e02012-04-16 14:42:55 +020010384 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010385
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010386 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010387 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10388 return 0;
10389
Willy Tarreau8797c062007-05-07 00:55:35 +020010390 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010391 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010392
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010393 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010394 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010395 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010396 return 1;
10397}
10398
Thierry Fournierf4011dd2016-03-29 17:23:51 +020010399static int
10400smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
10401{
10402 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
10403 return 0;
10404
10405 if (!smp->strm->unique_id) {
10406 if ((smp->strm->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
10407 return 0;
10408 smp->strm->unique_id[0] = '\0';
10409 }
10410 smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id,
10411 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
10412
10413 smp->data.type = SMP_T_STR;
10414 smp->data.u.str.str = smp->strm->unique_id;
10415 smp->flags = SMP_F_CONST;
10416 return 1;
10417}
10418
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010419/* returns the longest available part of the body. This requires that the body
10420 * has been waited for using http-buffer-request.
10421 */
10422static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010423smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010424{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010425 struct http_msg *msg;
10426 unsigned long len;
10427 unsigned long block1;
10428 char *body;
10429 struct chunk *temp;
10430
10431 CHECK_HTTP_MESSAGE_FIRST();
10432
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010433 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010434 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010435 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010436 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010437
10438 len = http_body_bytes(msg);
10439 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
10440
10441 block1 = len;
10442 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
10443 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
10444
10445 if (block1 == len) {
10446 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010447 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010448 smp->data.u.str.str = body;
10449 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010450 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
10451 }
10452 else {
10453 /* buffer is wrapped, we need to defragment it */
10454 temp = get_trash_chunk();
10455 memcpy(temp->str, body, block1);
10456 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010457 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010458 smp->data.u.str.str = temp->str;
10459 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010460 smp->flags = SMP_F_VOL_TEST;
10461 }
10462 return 1;
10463}
10464
10465
10466/* returns the available length of the body. This requires that the body
10467 * has been waited for using http-buffer-request.
10468 */
10469static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010470smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010471{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010472 struct http_msg *msg;
10473
10474 CHECK_HTTP_MESSAGE_FIRST();
10475
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010476 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010477 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010478 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010479 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010480
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010481 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010482 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010483
10484 smp->flags = SMP_F_VOL_TEST;
10485 return 1;
10486}
10487
10488
10489/* returns the advertised length of the body, or the advertised size of the
10490 * chunks available in the buffer. This requires that the body has been waited
10491 * for using http-buffer-request.
10492 */
10493static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010494smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010495{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010496 struct http_msg *msg;
10497
10498 CHECK_HTTP_MESSAGE_FIRST();
10499
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010500 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010501 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010502 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010503 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010504
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010505 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010506 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010507
10508 smp->flags = SMP_F_VOL_TEST;
10509 return 1;
10510}
10511
10512
Willy Tarreau8797c062007-05-07 00:55:35 +020010513/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010514static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010515smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010516{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010517 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010518
Willy Tarreauc0239e02012-04-16 14:42:55 +020010519 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010520
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010521 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010522 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010523 smp->data.u.str.len = txn->req.sl.rq.u_l;
10524 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010525 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010526 return 1;
10527}
10528
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010529static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010530smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010531{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010532 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010533 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010534
Willy Tarreauc0239e02012-04-16 14:42:55 +020010535 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010536
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010537 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010538 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 +020010539 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010540 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010541
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010542 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010543 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010544 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010545 return 1;
10546}
10547
10548static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010549smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010550{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010551 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010552 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010553
Willy Tarreauc0239e02012-04-16 14:42:55 +020010554 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010555
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010556 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010557 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 +020010558 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10559 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010560
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010561 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010562 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010563 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010564 return 1;
10565}
10566
Willy Tarreau185b5c42012-04-26 15:11:51 +020010567/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10568 * Accepts an optional argument of type string containing the header field name,
10569 * and an optional argument of type signed or unsigned integer to request an
10570 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010571 * headers are considered from the first one. It does not stop on commas and
10572 * returns full lines instead (useful for User-Agent or Date for example).
10573 */
10574static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010575smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010576{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010577 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010578 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010579 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010580 int occ = 0;
10581 const char *name_str = NULL;
10582 int name_len = 0;
10583
10584 if (!ctx) {
10585 /* first call */
10586 ctx = &static_hdr_ctx;
10587 ctx->idx = 0;
10588 smp->ctx.a[0] = ctx;
10589 }
10590
10591 if (args) {
10592 if (args[0].type != ARGT_STR)
10593 return 0;
10594 name_str = args[0].data.str.str;
10595 name_len = args[0].data.str.len;
10596
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010597 if (args[1].type == ARGT_SINT)
10598 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010599 }
10600
10601 CHECK_HTTP_MESSAGE_FIRST();
10602
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010603 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010604 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 +020010605
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010606 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10607 /* search for header from the beginning */
10608 ctx->idx = 0;
10609
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010610 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010611 /* no explicit occurrence and single fetch => last header by default */
10612 occ = -1;
10613
10614 if (!occ)
10615 /* prepare to report multiple occurrences for ACL fetches */
10616 smp->flags |= SMP_F_NOT_LAST;
10617
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010618 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010619 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010620 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 +020010621 return 1;
10622
10623 smp->flags &= ~SMP_F_NOT_LAST;
10624 return 0;
10625}
10626
10627/* 6. Check on HTTP header count. The number of occurrences is returned.
10628 * Accepts exactly 1 argument of type string. It does not stop on commas and
10629 * returns full lines instead (useful for User-Agent or Date for example).
10630 */
10631static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010632smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010633{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010634 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010635 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010636 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010637 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010638 const char *name = NULL;
10639 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010640
Willy Tarreau601a4d12015-04-01 19:16:09 +020010641 if (args && args->type == ARGT_STR) {
10642 name = args->data.str.str;
10643 len = args->data.str.len;
10644 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010645
10646 CHECK_HTTP_MESSAGE_FIRST();
10647
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010648 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010649 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 +020010650
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010651 ctx.idx = 0;
10652 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010653 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010654 cnt++;
10655
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010656 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010657 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010658 smp->flags = SMP_F_VOL_HDR;
10659 return 1;
10660}
10661
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010662static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010663smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010664{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010665 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010666 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010667 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010668 struct chunk *temp;
10669 char del = ',';
10670
10671 if (args && args->type == ARGT_STR)
10672 del = *args[0].data.str.str;
10673
10674 CHECK_HTTP_MESSAGE_FIRST();
10675
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010676 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010677 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 +020010678
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010679 temp = get_trash_chunk();
10680
10681 ctx.idx = 0;
10682 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10683 if (temp->len)
10684 temp->str[temp->len++] = del;
10685 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10686 temp->len += ctx.del;
10687 }
10688
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010689 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010690 smp->data.u.str.str = temp->str;
10691 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010692 smp->flags = SMP_F_VOL_HDR;
10693 return 1;
10694}
10695
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010696/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10697 * Accepts an optional argument of type string containing the header field name,
10698 * and an optional argument of type signed or unsigned integer to request an
10699 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010700 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010701 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010702static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010703smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010704{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010705 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010706 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010707 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010708 int occ = 0;
10709 const char *name_str = NULL;
10710 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010711
Willy Tarreaua890d072013-04-02 12:01:06 +020010712 if (!ctx) {
10713 /* first call */
10714 ctx = &static_hdr_ctx;
10715 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010716 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010717 }
10718
Willy Tarreau185b5c42012-04-26 15:11:51 +020010719 if (args) {
10720 if (args[0].type != ARGT_STR)
10721 return 0;
10722 name_str = args[0].data.str.str;
10723 name_len = args[0].data.str.len;
10724
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010725 if (args[1].type == ARGT_SINT)
10726 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010727 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010728
Willy Tarreaue333ec92012-04-16 16:26:40 +020010729 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010730
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010731 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010732 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 +020010733
Willy Tarreau185b5c42012-04-26 15:11:51 +020010734 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010735 /* search for header from the beginning */
10736 ctx->idx = 0;
10737
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010738 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010739 /* no explicit occurrence and single fetch => last header by default */
10740 occ = -1;
10741
10742 if (!occ)
10743 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010744 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010745
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010746 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010747 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010748 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 +020010749 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010750
Willy Tarreau37406352012-04-23 16:16:37 +020010751 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010752 return 0;
10753}
10754
Willy Tarreauc11416f2007-06-17 16:58:38 +020010755/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010756 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010757 */
10758static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010759smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010760{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010761 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010762 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010763 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010764 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010765 const char *name = NULL;
10766 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010767
Willy Tarreau601a4d12015-04-01 19:16:09 +020010768 if (args && args->type == ARGT_STR) {
10769 name = args->data.str.str;
10770 len = args->data.str.len;
10771 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010772
Willy Tarreaue333ec92012-04-16 16:26:40 +020010773 CHECK_HTTP_MESSAGE_FIRST();
10774
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010775 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010776 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 +020010777
Willy Tarreau33a7e692007-06-10 19:45:56 +020010778 ctx.idx = 0;
10779 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010780 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010781 cnt++;
10782
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010783 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010784 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010785 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010786 return 1;
10787}
10788
Willy Tarreau185b5c42012-04-26 15:11:51 +020010789/* Fetch an HTTP header's integer value. The integer value is returned. It
10790 * takes a mandatory argument of type string and an optional one of type int
10791 * to designate a specific occurrence. It returns an unsigned integer, which
10792 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010793 */
10794static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010795smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010796{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010797 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010798
Willy Tarreauf853c462012-04-23 18:53:56 +020010799 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010800 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010801 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010802 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010803
Willy Tarreaud53e2422012-04-16 17:21:11 +020010804 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010805}
10806
Cyril Bonté69fa9922012-10-25 00:01:06 +020010807/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10808 * and an optional one of type int to designate a specific occurrence.
10809 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010810 */
10811static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010812smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010813{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010814 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010815
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010816 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010817 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010818 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010819 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010820 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010821 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010822 if (smp->data.u.str.len < temp->size - 1) {
10823 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10824 temp->str[smp->data.u.str.len] = '\0';
10825 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010826 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010827 break;
10828 }
10829 }
10830 }
10831
Willy Tarreaud53e2422012-04-16 17:21:11 +020010832 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010833 if (!(smp->flags & SMP_F_NOT_LAST))
10834 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010835 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010836 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010837}
10838
Willy Tarreau737b0c12007-06-10 21:28:46 +020010839/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10840 * the first '/' after the possible hostname, and ends before the possible '?'.
10841 */
10842static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010843smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010844{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010845 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010846 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010847
Willy Tarreauc0239e02012-04-16 14:42:55 +020010848 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010849
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010850 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010851 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010852 ptr = http_get_path(txn);
10853 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010854 return 0;
10855
10856 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010857 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010858 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010859
10860 while (ptr < end && *ptr != '?')
10861 ptr++;
10862
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010863 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010864 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010865 return 1;
10866}
10867
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010868/* This produces a concatenation of the first occurrence of the Host header
10869 * followed by the path component if it begins with a slash ('/'). This means
10870 * that '*' will not be added, resulting in exactly the first Host entry.
10871 * If no Host header is found, then the path is returned as-is. The returned
10872 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010873 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010874 */
10875static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010876smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010877{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010878 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010879 char *ptr, *end, *beg;
10880 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010881 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010882
10883 CHECK_HTTP_MESSAGE_FIRST();
10884
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010885 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010886 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010887 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010888 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010889
10890 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010891 temp = get_trash_chunk();
10892 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010893 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010894 smp->data.u.str.str = temp->str;
10895 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010896
10897 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010898 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010899 beg = http_get_path(txn);
10900 if (!beg)
10901 beg = end;
10902
10903 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10904
10905 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010906 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10907 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010908 }
10909
10910 smp->flags = SMP_F_VOL_1ST;
10911 return 1;
10912}
10913
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010914/* This produces a 32-bit hash of the concatenation of the first occurrence of
10915 * the Host header followed by the path component if it begins with a slash ('/').
10916 * This means that '*' will not be added, resulting in exactly the first Host
10917 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010918 * is hashed using the path hash followed by a full avalanche hash and provides a
10919 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010920 * high-traffic sites without having to store whole paths.
10921 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010922int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010923smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010924{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010925 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010926 struct hdr_ctx ctx;
10927 unsigned int hash = 0;
10928 char *ptr, *beg, *end;
10929 int len;
10930
10931 CHECK_HTTP_MESSAGE_FIRST();
10932
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010933 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010934 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010935 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010936 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10937 ptr = ctx.line + ctx.val;
10938 len = ctx.vlen;
10939 while (len--)
10940 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10941 }
10942
10943 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010944 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010945 beg = http_get_path(txn);
10946 if (!beg)
10947 beg = end;
10948
10949 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10950
10951 if (beg < ptr && *beg == '/') {
10952 while (beg < ptr)
10953 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10954 }
10955 hash = full_hash(hash);
10956
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010957 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010958 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010959 smp->flags = SMP_F_VOL_1ST;
10960 return 1;
10961}
10962
Willy Tarreau4a550602012-12-09 14:53:32 +010010963/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010964 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10965 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10966 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010967 * that in environments where IPv6 is insignificant, truncating the output to
10968 * 8 bytes would still work.
10969 */
10970static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010971smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010972{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010973 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010974 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010975
10976 if (!cli_conn)
10977 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010978
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010979 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010980 return 0;
10981
Willy Tarreau47ca5452012-12-23 20:22:19 +010010982 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010983 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010984 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010985
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010986 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010987 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010988 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010989 temp->len += 4;
10990 break;
10991 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010992 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010993 temp->len += 16;
10994 break;
10995 default:
10996 return 0;
10997 }
10998
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010999 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011000 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010011001 return 1;
11002}
11003
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011004/* Extracts the query string, which comes after the question mark '?'. If no
11005 * question mark is found, nothing is returned. Otherwise it returns a sample
11006 * of type string carrying the whole query string.
11007 */
11008static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011009smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011010{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011011 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011012 char *ptr, *end;
11013
11014 CHECK_HTTP_MESSAGE_FIRST();
11015
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011016 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011017 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
11018 end = ptr + txn->req.sl.rq.u_l;
11019
11020 /* look up the '?' */
11021 do {
11022 if (ptr == end)
11023 return 0;
11024 } while (*ptr++ != '?');
11025
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011026 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011027 smp->data.u.str.str = ptr;
11028 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011029 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11030 return 1;
11031}
11032
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011033static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011034smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011035{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011036 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
11037 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
11038 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011039
Willy Tarreau24e32d82012-04-23 23:55:44 +020011040 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011041
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011042 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011043 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011044 return 1;
11045}
11046
Willy Tarreau7f18e522010-10-22 20:04:13 +020011047/* return a valid test if the current request is the first one on the connection */
11048static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011049smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020011050{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011051 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011052 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020011053 return 1;
11054}
11055
Willy Tarreau34db1082012-04-19 17:16:54 +020011056/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011057static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011058smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011059{
11060
Willy Tarreau24e32d82012-04-23 23:55:44 +020011061 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011062 return 0;
11063
Willy Tarreauc0239e02012-04-16 14:42:55 +020011064 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011065
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011066 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011067 return 0;
11068
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011069 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011070 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011071 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011072 return 1;
11073}
Willy Tarreau8797c062007-05-07 00:55:35 +020011074
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011075/* Accepts exactly 1 argument of type userlist */
11076static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011077smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011078{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011079 if (!args || args->type != ARGT_USR)
11080 return 0;
11081
11082 CHECK_HTTP_MESSAGE_FIRST();
11083
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011084 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011085 return 0;
11086
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011087 /* if the user does not belong to the userlist or has a wrong password,
11088 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010011089 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011090 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011091 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
11092 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010011093 return 0;
11094
11095 /* pat_match_auth() will need the user list */
11096 smp->ctx.a[0] = args->data.usr;
11097
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011098 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011099 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011100 smp->data.u.str.str = smp->strm->txn->auth.user;
11101 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011102
11103 return 1;
11104}
11105
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011106/* Try to find the next occurrence of a cookie name in a cookie header value.
11107 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
11108 * the cookie value is returned into *value and *value_l, and the function
11109 * returns a pointer to the next pointer to search from if the value was found.
11110 * Otherwise if the cookie was not found, NULL is returned and neither value
11111 * nor value_l are touched. The input <hdr> string should first point to the
11112 * header's value, and the <hdr_end> pointer must point to the first character
11113 * not part of the value. <list> must be non-zero if value may represent a list
11114 * of values (cookie headers). This makes it faster to abort parsing when no
11115 * list is expected.
11116 */
David Carlier4686f792015-09-25 14:10:50 +010011117char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011118extract_cookie_value(char *hdr, const char *hdr_end,
11119 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020011120 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011121{
11122 char *equal, *att_end, *att_beg, *val_beg, *val_end;
11123 char *next;
11124
11125 /* we search at least a cookie name followed by an equal, and more
11126 * generally something like this :
11127 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
11128 */
11129 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
11130 /* Iterate through all cookies on this line */
11131
Willy Tarreau2235b262016-11-05 15:50:20 +010011132 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011133 att_beg++;
11134
11135 /* find att_end : this is the first character after the last non
11136 * space before the equal. It may be equal to hdr_end.
11137 */
11138 equal = att_end = att_beg;
11139
11140 while (equal < hdr_end) {
11141 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
11142 break;
Willy Tarreau2235b262016-11-05 15:50:20 +010011143 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011144 continue;
11145 att_end = equal;
11146 }
11147
11148 /* here, <equal> points to '=', a delimitor or the end. <att_end>
11149 * is between <att_beg> and <equal>, both may be identical.
11150 */
11151
11152 /* look for end of cookie if there is an equal sign */
11153 if (equal < hdr_end && *equal == '=') {
11154 /* look for the beginning of the value */
11155 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +010011156 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011157 val_beg++;
11158
11159 /* find the end of the value, respecting quotes */
11160 next = find_cookie_value_end(val_beg, hdr_end);
11161
11162 /* make val_end point to the first white space or delimitor after the value */
11163 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +010011164 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011165 val_end--;
11166 } else {
11167 val_beg = val_end = next = equal;
11168 }
11169
11170 /* We have nothing to do with attributes beginning with '$'. However,
11171 * they will automatically be removed if a header before them is removed,
11172 * since they're supposed to be linked together.
11173 */
11174 if (*att_beg == '$')
11175 continue;
11176
11177 /* Ignore cookies with no equal sign */
11178 if (equal == next)
11179 continue;
11180
11181 /* Now we have the cookie name between att_beg and att_end, and
11182 * its value between val_beg and val_end.
11183 */
11184
11185 if (att_end - att_beg == cookie_name_l &&
11186 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
11187 /* let's return this value and indicate where to go on from */
11188 *value = val_beg;
11189 *value_l = val_end - val_beg;
11190 return next + 1;
11191 }
11192
11193 /* Set-Cookie headers only have the name in the first attr=value part */
11194 if (!list)
11195 break;
11196 }
11197
11198 return NULL;
11199}
11200
William Lallemanda43ba4e2014-01-28 18:14:25 +010011201/* Fetch a captured HTTP request header. The index is the position of
11202 * the "capture" option in the configuration file
11203 */
11204static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011205smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011206{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011207 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011208 int idx;
11209
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011210 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011211 return 0;
11212
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011213 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011214
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011215 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 +010011216 return 0;
11217
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011218 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011219 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011220 smp->data.u.str.str = smp->strm->req_cap[idx];
11221 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011222
11223 return 1;
11224}
11225
11226/* Fetch a captured HTTP response header. The index is the position of
11227 * the "capture" option in the configuration file
11228 */
11229static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011230smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011231{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011232 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011233 int idx;
11234
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011235 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011236 return 0;
11237
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011238 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011239
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011240 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 +010011241 return 0;
11242
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011243 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011244 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011245 smp->data.u.str.str = smp->strm->res_cap[idx];
11246 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011247
11248 return 1;
11249}
11250
William Lallemand65ad6e12014-01-31 15:08:02 +010011251/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
11252static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011253smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011254{
11255 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011256 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010011257 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011258
Willy Tarreau15e91e12015-04-04 00:52:09 +020011259 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011260 return 0;
11261
William Lallemand96a77852014-02-05 00:30:02 +010011262 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011263
William Lallemand96a77852014-02-05 00:30:02 +010011264 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11265 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011266
William Lallemand96a77852014-02-05 00:30:02 +010011267 temp = get_trash_chunk();
11268 temp->str = txn->uri;
11269 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011270 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011271 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011272 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011273
11274 return 1;
11275
11276}
11277
11278/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
11279static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011280smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011281{
11282 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011283 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010011284 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011285
Willy Tarreau15e91e12015-04-04 00:52:09 +020011286 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011287 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010011288
William Lallemand65ad6e12014-01-31 15:08:02 +010011289 ptr = txn->uri;
11290
11291 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11292 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010011293
William Lallemand65ad6e12014-01-31 15:08:02 +010011294 if (!*ptr)
11295 return 0;
11296
11297 ptr++; /* skip the space */
11298
11299 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010011300 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010011301 if (!ptr)
11302 return 0;
11303 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
11304 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011305
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011306 smp->data.u.str = *temp;
11307 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011308 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011309 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011310
11311 return 1;
11312}
11313
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011314/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
11315 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11316 */
11317static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011318smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011319{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011320 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011321
Willy Tarreau15e91e12015-04-04 00:52:09 +020011322 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011323 return 0;
11324
11325 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011326 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011327 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011328 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011329
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011330 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011331 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011332 smp->flags = SMP_F_CONST;
11333 return 1;
11334
11335}
11336
11337/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
11338 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11339 */
11340static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011341smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011342{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011343 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011344
Willy Tarreau15e91e12015-04-04 00:52:09 +020011345 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011346 return 0;
11347
11348 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011349 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011350 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011351 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011352
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011353 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011354 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011355 smp->flags = SMP_F_CONST;
11356 return 1;
11357
11358}
11359
William Lallemand65ad6e12014-01-31 15:08:02 +010011360
Willy Tarreaue333ec92012-04-16 16:26:40 +020011361/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011362 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011363 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011364 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011365 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011366 * Accepts exactly 1 argument of type string. If the input options indicate
11367 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011368 * The returned sample is of type CSTR. Can be used to parse cookies in other
11369 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011370 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011371int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011372{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011373 struct http_txn *txn;
11374 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011375 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011376 const struct http_msg *msg;
11377 const char *hdr_name;
11378 int hdr_name_len;
11379 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011380 int occ = 0;
11381 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011382
Willy Tarreau24e32d82012-04-23 23:55:44 +020011383 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011384 return 0;
11385
Willy Tarreaua890d072013-04-02 12:01:06 +020011386 if (!ctx) {
11387 /* first call */
11388 ctx = &static_hdr_ctx;
11389 ctx->idx = 0;
11390 smp->ctx.a[2] = ctx;
11391 }
11392
Willy Tarreaue333ec92012-04-16 16:26:40 +020011393 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011394
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011395 txn = smp->strm->txn;
11396 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011397
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011398 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011399 msg = &txn->req;
11400 hdr_name = "Cookie";
11401 hdr_name_len = 6;
11402 } else {
11403 msg = &txn->rsp;
11404 hdr_name = "Set-Cookie";
11405 hdr_name_len = 10;
11406 }
11407
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011408 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020011409 /* no explicit occurrence and single fetch => last cookie by default */
11410 occ = -1;
11411
11412 /* OK so basically here, either we want only one value and it's the
11413 * last one, or we want to iterate over all of them and we fetch the
11414 * next one.
11415 */
11416
Willy Tarreau9b28e032012-10-12 23:49:43 +020011417 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011418 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011419 /* search for the header from the beginning, we must first initialize
11420 * the search parameters.
11421 */
Willy Tarreau37406352012-04-23 16:16:37 +020011422 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011423 ctx->idx = 0;
11424 }
11425
Willy Tarreau28376d62012-04-26 21:26:10 +020011426 smp->flags |= SMP_F_VOL_HDR;
11427
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011428 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011429 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11430 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011431 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11432 goto out;
11433
Willy Tarreau24e32d82012-04-23 23:55:44 +020011434 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011435 continue;
11436
Willy Tarreau37406352012-04-23 16:16:37 +020011437 smp->ctx.a[0] = ctx->line + ctx->val;
11438 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011439 }
11440
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011441 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011442 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011443 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011444 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011445 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011446 &smp->data.u.str.str,
11447 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011448 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011449 found = 1;
11450 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011451 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020011452 smp->flags |= SMP_F_NOT_LAST;
11453 return 1;
11454 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011455 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011456 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011457 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011458 /* all cookie headers and values were scanned. If we're looking for the
11459 * last occurrence, we may return it now.
11460 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011461 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011462 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011463 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011464}
11465
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011466/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011467 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011468 * multiple cookies may be parsed on the same line. The returned sample is of
11469 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011470 */
11471static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011472smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011473{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011474 struct http_txn *txn;
11475 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011476 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011477 const struct http_msg *msg;
11478 const char *hdr_name;
11479 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011480 int cnt;
11481 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011482 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011483
Willy Tarreau24e32d82012-04-23 23:55:44 +020011484 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011485 return 0;
11486
Willy Tarreaue333ec92012-04-16 16:26:40 +020011487 CHECK_HTTP_MESSAGE_FIRST();
11488
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011489 txn = smp->strm->txn;
11490 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011491
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011492 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011493 msg = &txn->req;
11494 hdr_name = "Cookie";
11495 hdr_name_len = 6;
11496 } else {
11497 msg = &txn->rsp;
11498 hdr_name = "Set-Cookie";
11499 hdr_name_len = 10;
11500 }
11501
Willy Tarreau9b28e032012-10-12 23:49:43 +020011502 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011503 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011504 ctx.idx = 0;
11505 cnt = 0;
11506
11507 while (1) {
11508 /* Note: val_beg == NULL every time we need to fetch a new header */
11509 if (!val_beg) {
11510 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11511 break;
11512
Willy Tarreau24e32d82012-04-23 23:55:44 +020011513 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011514 continue;
11515
11516 val_beg = ctx.line + ctx.val;
11517 val_end = val_beg + ctx.vlen;
11518 }
11519
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011520 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011521 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011522 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011523 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011524 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011525 &smp->data.u.str.str,
11526 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011527 cnt++;
11528 }
11529 }
11530
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011531 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011532 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011533 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011534 return 1;
11535}
11536
Willy Tarreau51539362012-05-08 12:46:28 +020011537/* Fetch an cookie's integer value. The integer value is returned. It
11538 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11539 */
11540static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011541smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011542{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011543 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011544
11545 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011546 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011547 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020011548 }
11549
11550 return ret;
11551}
11552
Willy Tarreau8797c062007-05-07 00:55:35 +020011553/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011554/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011555/************************************************************************/
11556
David Cournapeau16023ee2010-12-23 20:55:41 +090011557/*
11558 * Given a path string and its length, find the position of beginning of the
11559 * query string. Returns NULL if no query string is found in the path.
11560 *
11561 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11562 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011563 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011564 */
bedis4c75cca2012-10-05 08:38:24 +020011565static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011566{
11567 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011568
bedis4c75cca2012-10-05 08:38:24 +020011569 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011570 return p ? p + 1 : NULL;
11571}
11572
bedis4c75cca2012-10-05 08:38:24 +020011573static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011574{
bedis4c75cca2012-10-05 08:38:24 +020011575 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011576}
11577
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011578/* after increasing a pointer value, it can exceed the first buffer
11579 * size. This function transform the value of <ptr> according with
11580 * the expected position. <chunks> is an array of the one or two
11581 * avalaible chunks. The first value is the start of the first chunk,
11582 * the second value if the end+1 of the first chunks. The third value
11583 * is NULL or the start of the second chunk and the fourth value is
11584 * the end+1 of the second chunk. The function returns 1 if does a
11585 * wrap, else returns 0.
11586 */
11587static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11588{
11589 if (*ptr < chunks[1])
11590 return 0;
11591 if (!chunks[2])
11592 return 0;
11593 *ptr = chunks[2] + ( *ptr - chunks[1] );
11594 return 1;
11595}
11596
David Cournapeau16023ee2010-12-23 20:55:41 +090011597/*
11598 * Given a url parameter, find the starting position of the first occurence,
11599 * or NULL if the parameter is not found.
11600 *
11601 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11602 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011603 *
Willy Tarreauf6625822015-12-27 14:51:01 +010011604 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011605 * or the second chunk. The caller must be check the position before using the
11606 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011607 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011608static const char *
11609find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011610 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011611 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011612{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011613 const char *pos, *last, *equal;
11614 const char **bufs = chunks;
11615 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011616
David Cournapeau16023ee2010-12-23 20:55:41 +090011617
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011618 pos = bufs[0];
11619 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010011620 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011621 /* Check the equal. */
11622 equal = pos + url_param_name_l;
11623 if (fix_pointer_if_wrap(chunks, &equal)) {
11624 if (equal >= chunks[3])
11625 return NULL;
11626 } else {
11627 if (equal >= chunks[1])
11628 return NULL;
11629 }
11630 if (*equal == '=') {
11631 if (pos + url_param_name_l > last) {
11632 /* process wrap case, we detect a wrap. In this case, the
11633 * comparison is performed in two parts.
11634 */
11635
11636 /* This is the end, we dont have any other chunk. */
11637 if (bufs != chunks || !bufs[2])
11638 return NULL;
11639
11640 /* Compute the length of each part of the comparison. */
11641 l1 = last - pos;
11642 l2 = url_param_name_l - l1;
11643
11644 /* The second buffer is too short to contain the compared string. */
11645 if (bufs[2] + l2 > bufs[3])
11646 return NULL;
11647
11648 if (memcmp(pos, url_param_name, l1) == 0 &&
11649 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11650 return pos;
11651
11652 /* Perform wrapping and jump the string who fail the comparison. */
11653 bufs += 2;
11654 pos = bufs[0] + l2;
11655 last = bufs[1];
11656
11657 } else {
11658 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011659 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11660 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011661 pos += url_param_name_l + 1;
11662 if (fix_pointer_if_wrap(chunks, &pos))
11663 last = bufs[2];
11664 }
11665 }
11666
11667 while (1) {
11668 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011669 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011670 pos++;
11671 if (pos < last)
11672 break;
11673 /* process buffer wrapping. */
11674 if (bufs != chunks || !bufs[2])
11675 return NULL;
11676 bufs += 2;
11677 pos = bufs[0];
11678 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011679 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011680 pos++;
11681 }
11682 return NULL;
11683}
11684
11685/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011686 * Given a url parameter name and a query string, find the next value.
11687 * An empty url_param_name matches the first available parameter.
11688 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
11689 * respectively provide a pointer to the value and its end.
11690 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090011691 */
11692static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011693find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011694 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011695 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011696{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011697 const char *arg_start, *qs_end;
11698 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011699
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011700 arg_start = chunks[0];
11701 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011702 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011703 /* Looks for an argument name. */
11704 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011705 url_param_name, url_param_name_l,
11706 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011707 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011708 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011709 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011710 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011711 if (!arg_start)
11712 return 0;
11713
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011714 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011715 while (1) {
11716 /* looks for the first argument. */
11717 value_start = memchr(arg_start, '=', qs_end - arg_start);
11718 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011719 /* Check for wrapping. */
11720 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011721 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011722 chunks[2]) {
11723 arg_start = chunks[2];
11724 qs_end = chunks[3];
11725 continue;
11726 }
11727 return 0;
11728 }
11729 break;
11730 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011731 value_start++;
11732 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011733 else {
11734 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011735 value_start = arg_start + url_param_name_l + 1;
11736
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011737 /* Check for pointer wrapping. */
11738 if (fix_pointer_if_wrap(chunks, &value_start)) {
11739 /* Update the end pointer. */
11740 qs_end = chunks[3];
11741
11742 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011743 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011744 return 0;
11745 }
11746 }
11747
David Cournapeau16023ee2010-12-23 20:55:41 +090011748 value_end = value_start;
11749
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011750 while (1) {
11751 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11752 value_end++;
11753 if (value_end < qs_end)
11754 break;
11755 /* process buffer wrapping. */
11756 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011757 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011758 chunks[2]) {
11759 value_end = chunks[2];
11760 qs_end = chunks[3];
11761 continue;
11762 }
11763 break;
11764 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011765
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011766 *vstart = value_start;
11767 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011768 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011769}
11770
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011771/* This scans a URL-encoded query string. It takes an optionally wrapping
11772 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11773 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11774 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011775 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011776static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011777smp_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 +090011778{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011779 const char *vstart, *vend;
11780 struct chunk *temp;
11781 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011782
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011783 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011784 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011785 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011786 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011787 return 0;
11788
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011789 /* Create sample. If the value is contiguous, return the pointer as CONST,
11790 * if the value is wrapped, copy-it in a buffer.
11791 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011792 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011793 if (chunks[2] &&
11794 vstart >= chunks[0] && vstart <= chunks[1] &&
11795 vend >= chunks[2] && vend <= chunks[3]) {
11796 /* Wrapped case. */
11797 temp = get_trash_chunk();
11798 memcpy(temp->str, vstart, chunks[1] - vstart);
11799 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011800 smp->data.u.str.str = temp->str;
11801 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011802 } else {
11803 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011804 smp->data.u.str.str = (char *)vstart;
11805 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011806 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11807 }
11808
11809 /* Update context, check wrapping. */
11810 chunks[0] = vend;
11811 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11812 chunks[1] = chunks[3];
11813 chunks[2] = NULL;
11814 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011815
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011816 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011817 smp->flags |= SMP_F_NOT_LAST;
11818
David Cournapeau16023ee2010-12-23 20:55:41 +090011819 return 1;
11820}
11821
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011822/* This function iterates over each parameter of the query string. It uses
11823 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011824 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11825 * An optional parameter name is passed in args[0], otherwise any parameter is
11826 * considered. It supports an optional delimiter argument for the beginning of
11827 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011828 */
11829static int
11830smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11831{
11832 struct http_msg *msg;
11833 char delim = '?';
11834 const char *name;
11835 int name_len;
11836
Dragan Dosen26f77e52015-05-25 10:02:11 +020011837 if (!args ||
11838 (args[0].type && args[0].type != ARGT_STR) ||
11839 (args[1].type && args[1].type != ARGT_STR))
11840 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011841
Dragan Dosen26f77e52015-05-25 10:02:11 +020011842 name = "";
11843 name_len = 0;
11844 if (args->type == ARGT_STR) {
11845 name = args->data.str.str;
11846 name_len = args->data.str.len;
11847 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011848
Dragan Dosen26f77e52015-05-25 10:02:11 +020011849 if (args[1].type)
11850 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011851
Dragan Dosen26f77e52015-05-25 10:02:11 +020011852 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011853 CHECK_HTTP_MESSAGE_FIRST();
11854
11855 msg = &smp->strm->txn->req;
11856
11857 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11858 msg->sl.rq.u_l, delim);
11859 if (!smp->ctx.a[0])
11860 return 0;
11861
11862 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011863
11864 /* Assume that the context is filled with NULL pointer
11865 * before the first call.
11866 * smp->ctx.a[2] = NULL;
11867 * smp->ctx.a[3] = NULL;
11868 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011869 }
11870
11871 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11872}
11873
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011874/* This function iterates over each parameter of the body. This requires
11875 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011876 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11877 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11878 * optional second part if the body wraps at the end of the buffer. An optional
11879 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011880 */
11881static int
11882smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11883{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011884 struct http_msg *msg;
11885 unsigned long len;
11886 unsigned long block1;
11887 char *body;
11888 const char *name;
11889 int name_len;
11890
11891 if (!args || (args[0].type && args[0].type != ARGT_STR))
11892 return 0;
11893
11894 name = "";
11895 name_len = 0;
11896 if (args[0].type == ARGT_STR) {
11897 name = args[0].data.str.str;
11898 name_len = args[0].data.str.len;
11899 }
11900
11901 if (!smp->ctx.a[0]) { // first call, find the query string
11902 CHECK_HTTP_MESSAGE_FIRST();
11903
11904 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010011905 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011906 else
Willy Tarreaube508f12016-03-10 11:47:01 +010011907 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011908
11909 len = http_body_bytes(msg);
11910 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11911
11912 block1 = len;
11913 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11914 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11915
11916 if (block1 == len) {
11917 /* buffer is not wrapped (or empty) */
11918 smp->ctx.a[0] = body;
11919 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011920
11921 /* Assume that the context is filled with NULL pointer
11922 * before the first call.
11923 * smp->ctx.a[2] = NULL;
11924 * smp->ctx.a[3] = NULL;
11925 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011926 }
11927 else {
11928 /* buffer is wrapped, we need to defragment it */
11929 smp->ctx.a[0] = body;
11930 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011931 smp->ctx.a[2] = msg->chn->buf->data;
11932 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011933 }
11934 }
11935 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11936}
11937
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011938/* Return the signed integer value for the specified url parameter (see url_param
11939 * above).
11940 */
11941static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011942smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011943{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011944 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011945
11946 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011947 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011948 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011949 }
11950
11951 return ret;
11952}
11953
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011954/* This produces a 32-bit hash of the concatenation of the first occurrence of
11955 * the Host header followed by the path component if it begins with a slash ('/').
11956 * This means that '*' will not be added, resulting in exactly the first Host
11957 * entry. If no Host header is found, then the path is used. The resulting value
11958 * is hashed using the url hash followed by a full avalanche hash and provides a
11959 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11960 * high-traffic sites without having to store whole paths.
11961 * this differs from the base32 functions in that it includes the url parameters
11962 * as well as the path
11963 */
11964static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011965smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011966{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011967 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011968 struct hdr_ctx ctx;
11969 unsigned int hash = 0;
11970 char *ptr, *beg, *end;
11971 int len;
11972
11973 CHECK_HTTP_MESSAGE_FIRST();
11974
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011975 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011976 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011977 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011978 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11979 ptr = ctx.line + ctx.val;
11980 len = ctx.vlen;
11981 while (len--)
11982 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11983 }
11984
11985 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011986 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 +000011987 beg = http_get_path(txn);
11988 if (!beg)
11989 beg = end;
11990
11991 for (ptr = beg; ptr < end ; ptr++);
11992
11993 if (beg < ptr && *beg == '/') {
11994 while (beg < ptr)
11995 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11996 }
11997 hash = full_hash(hash);
11998
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011999 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012000 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012001 smp->flags = SMP_F_VOL_1ST;
12002 return 1;
12003}
12004
12005/* This concatenates the source address with the 32-bit hash of the Host and
12006 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
12007 * per-url counters. The result is a binary block from 8 to 20 bytes depending
12008 * on the source address length. The URL hash is stored before the address so
12009 * that in environments where IPv6 is insignificant, truncating the output to
12010 * 8 bytes would still work.
12011 */
12012static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020012013smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012014{
12015 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012016 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012017
Dragan Dosendb5af612016-06-16 11:23:01 +020012018 if (!cli_conn)
12019 return 0;
12020
Thierry FOURNIER0786d052015-05-11 15:42:45 +020012021 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012022 return 0;
12023
12024 temp = get_trash_chunk();
Dragan Dosene5f41332016-06-16 11:08:08 +020012025 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
12026 temp->len += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012027
Willy Tarreaub363a1f2013-10-01 10:45:07 +020012028 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012029 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020012030 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012031 temp->len += 4;
12032 break;
12033 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020012034 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012035 temp->len += 16;
12036 break;
12037 default:
12038 return 0;
12039 }
12040
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012041 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012042 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012043 return 1;
12044}
12045
Willy Tarreau185b5c42012-04-26 15:11:51 +020012046/* This function is used to validate the arguments passed to any "hdr" fetch
12047 * keyword. These keywords support an optional positive or negative occurrence
12048 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
12049 * is assumed that the types are already the correct ones. Returns 0 on error,
12050 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
12051 * error message in case of error, that the caller is responsible for freeing.
12052 * The initial location must either be freeable or NULL.
12053 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010012054int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020012055{
12056 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020012057 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020012058 return 0;
12059 }
12060 return 1;
12061}
12062
Willy Tarreau276fae92013-07-25 14:36:01 +020012063/* takes an UINT value on input supposed to represent the time since EPOCH,
12064 * adds an optional offset found in args[0] and emits a string representing
12065 * the date in RFC-1123/5322 format.
12066 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012067static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020012068{
Cyril Bontéf78d8962016-01-22 19:40:28 +010012069 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020012070 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
12071 struct chunk *temp;
12072 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012073 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012074 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020012075
12076 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012077 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020012078 curr_date += args[0].data.sint;
12079
12080 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020012081 if (!tm)
12082 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020012083
12084 temp = get_trash_chunk();
12085 temp->len = snprintf(temp->str, temp->size - temp->len,
12086 "%s, %02d %s %04d %02d:%02d:%02d GMT",
12087 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
12088 tm->tm_hour, tm->tm_min, tm->tm_sec);
12089
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012090 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012091 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020012092 return 1;
12093}
12094
Thierry FOURNIERad903512014-04-11 17:51:01 +020012095/* Match language range with language tag. RFC2616 14.4:
12096 *
12097 * A language-range matches a language-tag if it exactly equals
12098 * the tag, or if it exactly equals a prefix of the tag such
12099 * that the first tag character following the prefix is "-".
12100 *
12101 * Return 1 if the strings match, else return 0.
12102 */
12103static inline int language_range_match(const char *range, int range_len,
12104 const char *tag, int tag_len)
12105{
12106 const char *end = range + range_len;
12107 const char *tend = tag + tag_len;
12108 while (range < end) {
12109 if (*range == '-' && tag == tend)
12110 return 1;
12111 if (*range != *tag || tag == tend)
12112 return 0;
12113 range++;
12114 tag++;
12115 }
12116 /* Return true only if the last char of the tag is matched. */
12117 return tag == tend;
12118}
12119
12120/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012121static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020012122{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012123 const char *al = smp->data.u.str.str;
12124 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012125 const char *token;
12126 int toklen;
12127 int qvalue;
12128 const char *str;
12129 const char *w;
12130 int best_q = 0;
12131
12132 /* Set the constant to the sample, because the output of the
12133 * function will be peek in the constant configuration string.
12134 */
12135 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012136 smp->data.u.str.size = 0;
12137 smp->data.u.str.str = "";
12138 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012139
12140 /* Parse the accept language */
12141 while (1) {
12142
12143 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012144 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012145 al++;
12146 if (al >= end)
12147 break;
12148
12149 /* Start of the fisrt word. */
12150 token = al;
12151
12152 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012153 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012154 al++;
12155 if (al == token)
12156 goto expect_comma;
12157
12158 /* Length of the token. */
12159 toklen = al - token;
12160 qvalue = 1000;
12161
12162 /* Check if the token exists in the list. If the token not exists,
12163 * jump to the next token.
12164 */
12165 str = args[0].data.str.str;
12166 w = str;
12167 while (1) {
12168 if (*str == ';' || *str == '\0') {
12169 if (language_range_match(token, toklen, w, str-w))
12170 goto look_for_q;
12171 if (*str == '\0')
12172 goto expect_comma;
12173 w = str + 1;
12174 }
12175 str++;
12176 }
12177 goto expect_comma;
12178
12179look_for_q:
12180
12181 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012182 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012183 al++;
12184 if (al >= end)
12185 goto process_value;
12186
12187 /* If ',' is found, process the result */
12188 if (*al == ',')
12189 goto process_value;
12190
12191 /* If the character is different from ';', look
12192 * for the end of the header part in best effort.
12193 */
12194 if (*al != ';')
12195 goto expect_comma;
12196
12197 /* Assumes that the char is ';', now expect "q=". */
12198 al++;
12199
12200 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012201 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012202 al++;
12203 if (al >= end)
12204 goto process_value;
12205
12206 /* Expect 'q'. If no 'q', continue in best effort */
12207 if (*al != 'q')
12208 goto process_value;
12209 al++;
12210
12211 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012212 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012213 al++;
12214 if (al >= end)
12215 goto process_value;
12216
12217 /* Expect '='. If no '=', continue in best effort */
12218 if (*al != '=')
12219 goto process_value;
12220 al++;
12221
12222 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012223 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012224 al++;
12225 if (al >= end)
12226 goto process_value;
12227
12228 /* Parse the q value. */
12229 qvalue = parse_qvalue(al, &al);
12230
12231process_value:
12232
12233 /* If the new q value is the best q value, then store the associated
12234 * language in the response. If qvalue is the biggest value (1000),
12235 * break the process.
12236 */
12237 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012238 smp->data.u.str.str = (char *)w;
12239 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012240 if (qvalue >= 1000)
12241 break;
12242 best_q = qvalue;
12243 }
12244
12245expect_comma:
12246
12247 /* Expect comma or end. If the end is detected, quit the loop. */
12248 while (al < end && *al != ',')
12249 al++;
12250 if (al >= end)
12251 break;
12252
12253 /* Comma is found, jump it and restart the analyzer. */
12254 al++;
12255 }
12256
12257 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012258 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
12259 smp->data.u.str.str = args[1].data.str.str;
12260 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012261 }
12262
12263 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012264 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012265}
12266
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012267/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012268static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012269{
12270 /* If the constant flag is set or if not size is avalaible at
12271 * the end of the buffer, copy the string in other buffer
12272 * before decoding.
12273 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012274 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012275 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012276 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
12277 smp->data.u.str.str = str->str;
12278 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012279 smp->flags &= ~SMP_F_CONST;
12280 }
12281
12282 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012283 smp->data.u.str.str[smp->data.u.str.len] = '\0';
12284 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012285 return 1;
12286}
12287
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012288static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
12289{
12290 struct proxy *fe = strm_fe(smp->strm);
12291 int idx, i;
12292 struct cap_hdr *hdr;
12293 int len;
12294
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012295 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012296 return 0;
12297
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012298 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012299
12300 /* Check the availibity of the capture id. */
12301 if (idx > fe->nb_req_cap - 1)
12302 return 0;
12303
12304 /* Look for the original configuration. */
12305 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
12306 hdr != NULL && i != idx ;
12307 i--, hdr = hdr->next);
12308 if (!hdr)
12309 return 0;
12310
12311 /* check for the memory allocation */
12312 if (smp->strm->req_cap[hdr->index] == NULL)
12313 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
12314 if (smp->strm->req_cap[hdr->index] == NULL)
12315 return 0;
12316
12317 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012318 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012319 if (len > hdr->len)
12320 len = hdr->len;
12321
12322 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012323 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012324 smp->strm->req_cap[idx][len] = '\0';
12325
12326 return 1;
12327}
12328
12329static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
12330{
12331 struct proxy *fe = strm_fe(smp->strm);
12332 int idx, i;
12333 struct cap_hdr *hdr;
12334 int len;
12335
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012336 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012337 return 0;
12338
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012339 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012340
12341 /* Check the availibity of the capture id. */
12342 if (idx > fe->nb_rsp_cap - 1)
12343 return 0;
12344
12345 /* Look for the original configuration. */
12346 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
12347 hdr != NULL && i != idx ;
12348 i--, hdr = hdr->next);
12349 if (!hdr)
12350 return 0;
12351
12352 /* check for the memory allocation */
12353 if (smp->strm->res_cap[hdr->index] == NULL)
12354 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
12355 if (smp->strm->res_cap[hdr->index] == NULL)
12356 return 0;
12357
12358 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012359 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012360 if (len > hdr->len)
12361 len = hdr->len;
12362
12363 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012364 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012365 smp->strm->res_cap[idx][len] = '\0';
12366
12367 return 1;
12368}
12369
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012370/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012371 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012372 * the relevant part of the request line accordingly. Then it updates various
12373 * pointers to the next elements which were moved, and the total buffer length.
12374 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012375 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
12376 * error, though this can be revisited when this code is finally exploited.
12377 *
12378 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
12379 * query string and 3 to replace uri.
12380 *
12381 * In query string case, the mark question '?' must be set at the start of the
12382 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012383 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012384int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012385 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012386{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012387 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012388 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012389 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012390 int delta;
12391
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012392 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012393 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012394 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012395 cur_end = cur_ptr + txn->req.sl.rq.m_l;
12396
12397 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012398 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012399 txn->req.sl.rq.m_l += delta;
12400 txn->req.sl.rq.u += delta;
12401 txn->req.sl.rq.v += delta;
12402 break;
12403
12404 case 1: // path
12405 cur_ptr = http_get_path(txn);
12406 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012407 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012408
12409 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012410 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 +010012411 cur_end++;
12412
12413 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012414 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012415 txn->req.sl.rq.u_l += delta;
12416 txn->req.sl.rq.v += delta;
12417 break;
12418
12419 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012420 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012421 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012422 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12423 while (cur_ptr < cur_end && *cur_ptr != '?')
12424 cur_ptr++;
12425
12426 /* skip the question mark or indicate that we must insert it
12427 * (but only if the format string is not empty then).
12428 */
12429 if (cur_ptr < cur_end)
12430 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012431 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012432 offset = 0;
12433
12434 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012435 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012436 txn->req.sl.rq.u_l += delta;
12437 txn->req.sl.rq.v += delta;
12438 break;
12439
12440 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012441 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012442 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12443
12444 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012445 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012446 txn->req.sl.rq.u_l += delta;
12447 txn->req.sl.rq.v += delta;
12448 break;
12449
12450 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012451 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012452 }
12453
12454 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012455 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020012456 txn->req.sl.rq.l += delta;
12457 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012458 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012459 return 0;
12460}
12461
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012462/* This function replace the HTTP status code and the associated message. The
12463 * variable <status> contains the new status code. This function never fails.
12464 */
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012465void http_set_status(unsigned int status, const char *reason, struct stream *s)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012466{
12467 struct http_txn *txn = s->txn;
12468 char *cur_ptr, *cur_end;
12469 int delta;
12470 char *res;
12471 int c_l;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012472 const char *msg = reason;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012473 int msg_len;
12474
12475 chunk_reset(&trash);
12476
12477 res = ultoa_o(status, trash.str, trash.size);
12478 c_l = res - trash.str;
12479
12480 trash.str[c_l] = ' ';
12481 trash.len = c_l + 1;
12482
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012483 /* Do we have a custom reason format string? */
12484 if (msg == NULL)
12485 msg = get_reason(status);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012486 msg_len = strlen(msg);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012487 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
12488 trash.len += msg_len;
12489
12490 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
12491 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
12492
12493 /* commit changes and adjust message */
12494 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
12495
12496 /* adjust res line offsets and lengths */
12497 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
12498 txn->rsp.sl.st.c_l = c_l;
12499 txn->rsp.sl.st.r_l = msg_len;
12500
12501 delta = trash.len - (cur_end - cur_ptr);
12502 txn->rsp.sl.st.l += delta;
12503 txn->hdr_idx.v[0].len += delta;
12504 http_msg_move_end(&txn->rsp, delta);
12505}
12506
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012507/* This function executes one of the set-{method,path,query,uri} actions. It
12508 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012509 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012510 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012511 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
12512 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012513 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012514enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012515 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012516{
12517 chunk_reset(&trash);
12518
12519 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012520 if (rule->arg.http.action == 2)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012521 trash.str[trash.len++] = '?';
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012522 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012523
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012524 http_replace_req_line(rule->arg.http.action, trash.str, trash.len, px, s);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012525 return ACT_RET_CONT;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012526}
12527
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012528/* This function is just a compliant action wrapper for "set-status". */
12529enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012530 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012531{
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012532 http_set_status(rule->arg.status.code, rule->arg.status.reason, s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012533 return ACT_RET_CONT;
12534}
12535
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012536/* parse an http-request action among :
12537 * set-method
12538 * set-path
12539 * set-query
12540 * set-uri
12541 *
12542 * All of them accept a single argument of type string representing a log-format.
12543 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
12544 * 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 +020012545 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012546 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012547enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
12548 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012549{
12550 int cur_arg = *orig_arg;
12551
Thierry FOURNIER42148732015-09-02 17:17:33 +020012552 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012553
12554 switch (args[0][4]) {
12555 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012556 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012557 rule->action_ptr = http_action_set_req_line;
12558 break;
12559 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012560 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012561 rule->action_ptr = http_action_set_req_line;
12562 break;
12563 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012564 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012565 rule->action_ptr = http_action_set_req_line;
12566 break;
12567 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012568 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012569 rule->action_ptr = http_action_set_req_line;
12570 break;
12571 default:
12572 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012573 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012574 }
12575
12576 if (!*args[cur_arg] ||
12577 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12578 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012579 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012580 }
12581
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012582 LIST_INIT(&rule->arg.http.logfmt);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012583 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010012584 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +010012585 (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 +010012586 return ACT_RET_PRS_ERR;
12587 }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012588
12589 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012590 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012591}
12592
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012593/* parse set-status action:
12594 * This action accepts a single argument of type int representing
12595 * an http status code. It returns ACT_RET_PRS_OK on success,
12596 * ACT_RET_PRS_ERR on error.
12597 */
12598enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12599 struct act_rule *rule, char **err)
12600{
12601 char *error;
12602
Thierry FOURNIER42148732015-09-02 17:17:33 +020012603 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012604 rule->action_ptr = action_http_set_status;
12605
12606 /* Check if an argument is available */
12607 if (!*args[*orig_arg]) {
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012608 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012609 return ACT_RET_PRS_ERR;
12610 }
12611
12612 /* convert status code as integer */
12613 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12614 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12615 memprintf(err, "expects an integer status code between 100 and 999");
12616 return ACT_RET_PRS_ERR;
12617 }
12618
12619 (*orig_arg)++;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012620
12621 /* set custom reason string */
12622 rule->arg.status.reason = NULL; // If null, we use the default reason for the status code.
12623 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
12624 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
12625 (*orig_arg)++;
12626 rule->arg.status.reason = strdup(args[*orig_arg]);
12627 (*orig_arg)++;
12628 }
12629
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012630 return ACT_RET_PRS_OK;
12631}
12632
Willy Tarreaua9083d02015-05-08 15:27:59 +020012633/* This function executes the "capture" action. It executes a fetch expression,
12634 * turns the result into a string and puts it in a capture slot. It always
12635 * returns 1. If an error occurs the action is cancelled, but the rule
12636 * processing continues.
12637 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012638enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012639 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012640{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012641 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012642 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012643 char **cap = s->req_cap;
12644 int len;
12645
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012646 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 +020012647 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012648 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012649
12650 if (cap[h->index] == NULL)
12651 cap[h->index] = pool_alloc2(h->pool);
12652
12653 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012654 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012655
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012656 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012657 if (len > h->len)
12658 len = h->len;
12659
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012660 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012661 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012662 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012663}
12664
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012665/* This function executes the "capture" action and store the result in a
12666 * capture slot if exists. It executes a fetch expression, turns the result
12667 * into a string and puts it in a capture slot. It always returns 1. If an
12668 * error occurs the action is cancelled, but the rule processing continues.
12669 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012670enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012671 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012672{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012673 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012674 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012675 char **cap = s->req_cap;
12676 struct proxy *fe = strm_fe(s);
12677 int len;
12678 int i;
12679
12680 /* Look for the original configuration. */
12681 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012682 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012683 i--, h = h->next);
12684 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012685 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012686
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012687 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 +020012688 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012689 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012690
12691 if (cap[h->index] == NULL)
12692 cap[h->index] = pool_alloc2(h->pool);
12693
12694 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012695 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012696
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012697 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012698 if (len > h->len)
12699 len = h->len;
12700
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012701 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012702 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012703 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012704}
12705
Willy Tarreaua9083d02015-05-08 15:27:59 +020012706/* parse an "http-request capture" action. It takes a single argument which is
12707 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012708 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012709 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012710 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012711enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12712 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012713{
12714 struct sample_expr *expr;
12715 struct cap_hdr *hdr;
12716 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012717 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012718
12719 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12720 if (strcmp(args[cur_arg], "if") == 0 ||
12721 strcmp(args[cur_arg], "unless") == 0)
12722 break;
12723
12724 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012725 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012726 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012727 }
12728
Willy Tarreaua9083d02015-05-08 15:27:59 +020012729 cur_arg = *orig_arg;
12730 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12731 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012732 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012733
12734 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12735 memprintf(err,
12736 "fetch method '%s' extracts information from '%s', none of which is available here",
12737 args[cur_arg-1], sample_src_names(expr->fetch->use));
12738 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012739 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012740 }
12741
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012742 if (!args[cur_arg] || !*args[cur_arg]) {
12743 memprintf(err, "expects 'len or 'id'");
12744 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012745 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012746 }
12747
Willy Tarreaua9083d02015-05-08 15:27:59 +020012748 if (strcmp(args[cur_arg], "len") == 0) {
12749 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012750
12751 if (!(px->cap & PR_CAP_FE)) {
12752 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012753 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012754 }
12755
12756 proxy->conf.args.ctx = ARGC_CAP;
12757
Willy Tarreaua9083d02015-05-08 15:27:59 +020012758 if (!args[cur_arg]) {
12759 memprintf(err, "missing length value");
12760 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012761 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012762 }
12763 /* we copy the table name for now, it will be resolved later */
12764 len = atoi(args[cur_arg]);
12765 if (len <= 0) {
12766 memprintf(err, "length must be > 0");
12767 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012768 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012769 }
12770 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012771
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012772 if (!len) {
12773 memprintf(err, "a positive 'len' argument is mandatory");
12774 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012775 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012776 }
12777
Vincent Bernat02779b62016-04-03 13:48:43 +020012778 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012779 hdr->next = px->req_cap;
12780 hdr->name = NULL; /* not a header capture */
12781 hdr->namelen = 0;
12782 hdr->len = len;
12783 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12784 hdr->index = px->nb_req_cap++;
12785
12786 px->req_cap = hdr;
12787 px->to_log |= LW_REQHDR;
12788
Thierry FOURNIER42148732015-09-02 17:17:33 +020012789 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012790 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012791 rule->arg.cap.expr = expr;
12792 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012793 }
12794
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012795 else if (strcmp(args[cur_arg], "id") == 0) {
12796 int id;
12797 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012798
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012799 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012800
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012801 if (!args[cur_arg]) {
12802 memprintf(err, "missing id value");
12803 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012804 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012805 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012806
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012807 id = strtol(args[cur_arg], &error, 10);
12808 if (*error != '\0') {
12809 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12810 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012811 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012812 }
12813 cur_arg++;
12814
12815 proxy->conf.args.ctx = ARGC_CAP;
12816
Thierry FOURNIER42148732015-09-02 17:17:33 +020012817 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012818 rule->action_ptr = http_action_req_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012819 rule->arg.capid.expr = expr;
12820 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012821 }
12822
12823 else {
12824 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12825 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012826 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012827 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012828
12829 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012830 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012831}
12832
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012833/* This function executes the "capture" action and store the result in a
12834 * capture slot if exists. It executes a fetch expression, turns the result
12835 * into a string and puts it in a capture slot. It always returns 1. If an
12836 * error occurs the action is cancelled, but the rule processing continues.
12837 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012838enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012839 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012840{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012841 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012842 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012843 char **cap = s->res_cap;
12844 struct proxy *fe = strm_fe(s);
12845 int len;
12846 int i;
12847
12848 /* Look for the original configuration. */
12849 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012850 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012851 i--, h = h->next);
12852 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012853 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012854
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012855 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 +020012856 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012857 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012858
12859 if (cap[h->index] == NULL)
12860 cap[h->index] = pool_alloc2(h->pool);
12861
12862 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012863 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012864
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012865 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012866 if (len > h->len)
12867 len = h->len;
12868
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012869 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012870 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012871 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012872}
12873
12874/* parse an "http-response capture" action. It takes a single argument which is
12875 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12876 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012877 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012878 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012879enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12880 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012881{
12882 struct sample_expr *expr;
12883 int cur_arg;
12884 int id;
12885 char *error;
12886
12887 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12888 if (strcmp(args[cur_arg], "if") == 0 ||
12889 strcmp(args[cur_arg], "unless") == 0)
12890 break;
12891
12892 if (cur_arg < *orig_arg + 3) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012893 memprintf(err, "expects <expression> id <idx>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012894 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012895 }
12896
12897 cur_arg = *orig_arg;
12898 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12899 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012900 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012901
12902 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12903 memprintf(err,
12904 "fetch method '%s' extracts information from '%s', none of which is available here",
12905 args[cur_arg-1], sample_src_names(expr->fetch->use));
12906 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012907 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012908 }
12909
12910 if (!args[cur_arg] || !*args[cur_arg]) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012911 memprintf(err, "expects 'id'");
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012912 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012913 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012914 }
12915
12916 if (strcmp(args[cur_arg], "id") != 0) {
12917 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12918 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012919 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012920 }
12921
12922 cur_arg++;
12923
12924 if (!args[cur_arg]) {
12925 memprintf(err, "missing id value");
12926 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012927 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012928 }
12929
12930 id = strtol(args[cur_arg], &error, 10);
12931 if (*error != '\0') {
12932 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12933 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012934 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012935 }
12936 cur_arg++;
12937
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012938 proxy->conf.args.ctx = ARGC_CAP;
12939
Thierry FOURNIER42148732015-09-02 17:17:33 +020012940 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012941 rule->action_ptr = http_action_res_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012942 rule->arg.capid.expr = expr;
12943 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012944
12945 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012946 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012947}
12948
William Lallemand73025dd2014-04-24 14:38:37 +020012949/*
12950 * Return the struct http_req_action_kw associated to a keyword.
12951 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012952struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012953{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012954 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012955}
12956
12957/*
12958 * Return the struct http_res_action_kw associated to a keyword.
12959 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012960struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012961{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012962 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012963}
12964
Willy Tarreau12207b32016-11-22 19:48:51 +010012965
12966/* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop
12967 * now.
12968 */
12969static int cli_parse_show_errors(char **args, struct appctx *appctx, void *private)
12970{
12971 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
12972 return 1;
12973
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012974 if (*args[2]) {
12975 struct proxy *px;
12976
12977 px = proxy_find_by_name(args[2], 0, 0);
12978 if (px)
12979 appctx->ctx.errors.iid = px->uuid;
12980 else
12981 appctx->ctx.errors.iid = atoi(args[2]);
12982
12983 if (!appctx->ctx.errors.iid) {
12984 appctx->ctx.cli.msg = "No such proxy.\n";
12985 appctx->st0 = CLI_ST_PRINT;
12986 return 1;
12987 }
12988 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012989 else
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012990 appctx->ctx.errors.iid = -1; // dump all proxies
12991
Willy Tarreau35069f82016-11-25 09:16:37 +010012992 appctx->ctx.errors.flag = 0;
12993 if (strcmp(args[3], "request") == 0)
12994 appctx->ctx.errors.flag |= 4; // ignore response
12995 else if (strcmp(args[3], "response") == 0)
12996 appctx->ctx.errors.flag |= 2; // ignore request
Willy Tarreau12207b32016-11-22 19:48:51 +010012997 appctx->ctx.errors.px = NULL;
Willy Tarreau12207b32016-11-22 19:48:51 +010012998 return 0;
12999}
13000
13001/* This function dumps all captured errors onto the stream interface's
13002 * read buffer. It returns 0 if the output buffer is full and it needs
13003 * to be called again, otherwise non-zero.
13004 */
13005static int cli_io_handler_show_errors(struct appctx *appctx)
13006{
13007 struct stream_interface *si = appctx->owner;
13008 extern const char *monthname[12];
13009
13010 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
13011 return 1;
13012
13013 chunk_reset(&trash);
13014
13015 if (!appctx->ctx.errors.px) {
13016 /* the function had not been called yet, let's prepare the
13017 * buffer for a response.
13018 */
13019 struct tm tm;
13020
13021 get_localtime(date.tv_sec, &tm);
13022 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
13023 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
13024 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
13025 error_snapshot_id);
13026
13027 if (bi_putchk(si_ic(si), &trash) == -1) {
13028 /* Socket buffer full. Let's try again later from the same point */
13029 si_applet_cant_put(si);
13030 return 0;
13031 }
13032
13033 appctx->ctx.errors.px = proxy;
Willy Tarreau12207b32016-11-22 19:48:51 +010013034 appctx->ctx.errors.bol = 0;
13035 appctx->ctx.errors.ptr = -1;
13036 }
13037
13038 /* we have two inner loops here, one for the proxy, the other one for
13039 * the buffer.
13040 */
13041 while (appctx->ctx.errors.px) {
13042 struct error_snapshot *es;
13043
Willy Tarreau35069f82016-11-25 09:16:37 +010013044 if ((appctx->ctx.errors.flag & 1) == 0) {
Willy Tarreau12207b32016-11-22 19:48:51 +010013045 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau35069f82016-11-25 09:16:37 +010013046 if (appctx->ctx.errors.flag & 2) // skip req
13047 goto next;
13048 }
13049 else {
Willy Tarreau12207b32016-11-22 19:48:51 +010013050 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau35069f82016-11-25 09:16:37 +010013051 if (appctx->ctx.errors.flag & 4) // skip resp
13052 goto next;
13053 }
Willy Tarreau12207b32016-11-22 19:48:51 +010013054
13055 if (!es->when.tv_sec)
13056 goto next;
13057
13058 if (appctx->ctx.errors.iid >= 0 &&
13059 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
13060 es->oe->uuid != appctx->ctx.errors.iid)
13061 goto next;
13062
13063 if (appctx->ctx.errors.ptr < 0) {
13064 /* just print headers now */
13065
13066 char pn[INET6_ADDRSTRLEN];
13067 struct tm tm;
13068 int port;
13069
13070 get_localtime(es->when.tv_sec, &tm);
13071 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
13072 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
13073 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
13074
13075 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
13076 case AF_INET:
13077 case AF_INET6:
13078 port = get_host_port(&es->src);
13079 break;
13080 default:
13081 port = 0;
13082 }
13083
Willy Tarreau35069f82016-11-25 09:16:37 +010013084 switch (appctx->ctx.errors.flag & 1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010013085 case 0:
13086 chunk_appendf(&trash,
13087 " frontend %s (#%d): invalid request\n"
13088 " backend %s (#%d)",
13089 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
13090 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
13091 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
13092 break;
13093 case 1:
13094 chunk_appendf(&trash,
13095 " backend %s (#%d): invalid response\n"
13096 " frontend %s (#%d)",
13097 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
13098 es->oe->id, es->oe->uuid);
13099 break;
13100 }
13101
13102 chunk_appendf(&trash,
13103 ", server %s (#%d), event #%u\n"
13104 " src %s:%d, session #%d, session flags 0x%08x\n"
Willy Tarreau10e61cb2017-01-04 14:51:22 +010013105 " HTTP msg state %s(%d), msg flags 0x%08x, tx flags 0x%08x\n"
Willy Tarreau12207b32016-11-22 19:48:51 +010013106 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
13107 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
13108 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
13109 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
13110 es->ev_id,
13111 pn, port, es->sid, es->s_flags,
Willy Tarreau10e61cb2017-01-04 14:51:22 +010013112 http_msg_state_str(es->state), es->state, es->m_flags, es->t_flags,
Willy Tarreau12207b32016-11-22 19:48:51 +010013113 es->m_clen, es->m_blen,
13114 es->b_flags, es->b_out, es->b_tot,
13115 es->len, es->b_wrap, es->pos);
13116
13117 if (bi_putchk(si_ic(si), &trash) == -1) {
13118 /* Socket buffer full. Let's try again later from the same point */
13119 si_applet_cant_put(si);
13120 return 0;
13121 }
13122 appctx->ctx.errors.ptr = 0;
13123 appctx->ctx.errors.sid = es->sid;
13124 }
13125
13126 if (appctx->ctx.errors.sid != es->sid) {
13127 /* the snapshot changed while we were dumping it */
13128 chunk_appendf(&trash,
13129 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
13130 if (bi_putchk(si_ic(si), &trash) == -1) {
13131 si_applet_cant_put(si);
13132 return 0;
13133 }
13134 goto next;
13135 }
13136
13137 /* OK, ptr >= 0, so we have to dump the current line */
13138 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
13139 int newptr;
13140 int newline;
13141
13142 newline = appctx->ctx.errors.bol;
13143 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
13144 if (newptr == appctx->ctx.errors.ptr)
13145 return 0;
13146
13147 if (bi_putchk(si_ic(si), &trash) == -1) {
13148 /* Socket buffer full. Let's try again later from the same point */
13149 si_applet_cant_put(si);
13150 return 0;
13151 }
13152 appctx->ctx.errors.ptr = newptr;
13153 appctx->ctx.errors.bol = newline;
13154 };
13155 next:
13156 appctx->ctx.errors.bol = 0;
13157 appctx->ctx.errors.ptr = -1;
Willy Tarreau35069f82016-11-25 09:16:37 +010013158 appctx->ctx.errors.flag ^= 1;
13159 if (!(appctx->ctx.errors.flag & 1))
Willy Tarreau12207b32016-11-22 19:48:51 +010013160 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau12207b32016-11-22 19:48:51 +010013161 }
13162
13163 /* dump complete */
13164 return 1;
13165}
13166
13167/* register cli keywords */
13168static struct cli_kw_list cli_kws = {{ },{
13169 { { "show", "errors", NULL },
13170 "show errors : report last request and response errors for each proxy",
13171 cli_parse_show_errors, cli_io_handler_show_errors, NULL,
13172 },
13173 {{},}
13174}};
13175
Willy Tarreau4a568972010-05-12 08:08:50 +020013176/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013177/* All supported ACL keywords must be declared here. */
13178/************************************************************************/
13179
13180/* Note: must not be declared <const> as its list will be overwritten.
13181 * Please take care of keeping this list alphabetically sorted.
13182 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020013183static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013184 { "base", "base", PAT_MATCH_STR },
13185 { "base_beg", "base", PAT_MATCH_BEG },
13186 { "base_dir", "base", PAT_MATCH_DIR },
13187 { "base_dom", "base", PAT_MATCH_DOM },
13188 { "base_end", "base", PAT_MATCH_END },
13189 { "base_len", "base", PAT_MATCH_LEN },
13190 { "base_reg", "base", PAT_MATCH_REG },
13191 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020013192
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013193 { "cook", "req.cook", PAT_MATCH_STR },
13194 { "cook_beg", "req.cook", PAT_MATCH_BEG },
13195 { "cook_dir", "req.cook", PAT_MATCH_DIR },
13196 { "cook_dom", "req.cook", PAT_MATCH_DOM },
13197 { "cook_end", "req.cook", PAT_MATCH_END },
13198 { "cook_len", "req.cook", PAT_MATCH_LEN },
13199 { "cook_reg", "req.cook", PAT_MATCH_REG },
13200 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013201
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013202 { "hdr", "req.hdr", PAT_MATCH_STR },
13203 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
13204 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
13205 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
13206 { "hdr_end", "req.hdr", PAT_MATCH_END },
13207 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
13208 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
13209 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013210
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013211 /* these two declarations uses strings with list storage (in place
13212 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
13213 * and delete functions are relative to the list management. The parse
13214 * and match method are related to the corresponding fetch methods. This
13215 * is very particular ACL declaration mode.
13216 */
13217 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
13218 { "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 +020013219
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013220 { "path", "path", PAT_MATCH_STR },
13221 { "path_beg", "path", PAT_MATCH_BEG },
13222 { "path_dir", "path", PAT_MATCH_DIR },
13223 { "path_dom", "path", PAT_MATCH_DOM },
13224 { "path_end", "path", PAT_MATCH_END },
13225 { "path_len", "path", PAT_MATCH_LEN },
13226 { "path_reg", "path", PAT_MATCH_REG },
13227 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013228
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013229 { "req_ver", "req.ver", PAT_MATCH_STR },
13230 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013231
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013232 { "scook", "res.cook", PAT_MATCH_STR },
13233 { "scook_beg", "res.cook", PAT_MATCH_BEG },
13234 { "scook_dir", "res.cook", PAT_MATCH_DIR },
13235 { "scook_dom", "res.cook", PAT_MATCH_DOM },
13236 { "scook_end", "res.cook", PAT_MATCH_END },
13237 { "scook_len", "res.cook", PAT_MATCH_LEN },
13238 { "scook_reg", "res.cook", PAT_MATCH_REG },
13239 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013240
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013241 { "shdr", "res.hdr", PAT_MATCH_STR },
13242 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
13243 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
13244 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
13245 { "shdr_end", "res.hdr", PAT_MATCH_END },
13246 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
13247 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
13248 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013249
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013250 { "url", "url", PAT_MATCH_STR },
13251 { "url_beg", "url", PAT_MATCH_BEG },
13252 { "url_dir", "url", PAT_MATCH_DIR },
13253 { "url_dom", "url", PAT_MATCH_DOM },
13254 { "url_end", "url", PAT_MATCH_END },
13255 { "url_len", "url", PAT_MATCH_LEN },
13256 { "url_reg", "url", PAT_MATCH_REG },
13257 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013258
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013259 { "urlp", "urlp", PAT_MATCH_STR },
13260 { "urlp_beg", "urlp", PAT_MATCH_BEG },
13261 { "urlp_dir", "urlp", PAT_MATCH_DIR },
13262 { "urlp_dom", "urlp", PAT_MATCH_DOM },
13263 { "urlp_end", "urlp", PAT_MATCH_END },
13264 { "urlp_len", "urlp", PAT_MATCH_LEN },
13265 { "urlp_reg", "urlp", PAT_MATCH_REG },
13266 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013267
Willy Tarreau8ed669b2013-01-11 15:49:37 +010013268 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013269}};
13270
13271/************************************************************************/
13272/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020013273/************************************************************************/
13274/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020013275static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013276 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013277 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013278 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
13279
Willy Tarreau87b09662015-04-03 00:22:06 +020013280 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013281 { "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 +020013282
13283 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013284 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
13285 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
13286 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020013287
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013288 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
13289 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010013290
Willy Tarreau409bcde2013-01-08 00:31:00 +010013291 /* cookie is valid in both directions (eg: for "stick ...") but cook*
13292 * are only here to match the ACL's name, are request-only and are used
13293 * for ACL compatibility only.
13294 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013295 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13296 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013297 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
13298 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013299
13300 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
13301 * only here to match the ACL's name, are request-only and are used for
13302 * ACL compatibility only.
13303 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013304 { "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 +020013305 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013306 { "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 +020013307 { "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 +010013308
Willy Tarreau0a0daec2013-04-02 22:44:58 +020013309 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013310 { "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 +010013311 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010013312 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013313 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010013314 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013315
13316 /* HTTP protocol on the request path */
13317 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013318 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013319
13320 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013321 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
13322 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013323
Willy Tarreaua5910cc2015-05-02 00:46:08 +020013324 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013325 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
13326 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020013327 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020013328
Willy Tarreau18ed2562013-01-14 15:56:36 +010013329 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013330 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
13331 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013332
Willy Tarreau18ed2562013-01-14 15:56:36 +010013333 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013334 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013335 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
13336 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013337
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013338 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013339 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013340 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013341 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013342 { "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 +010013343 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013344 { "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 +010013345
13346 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013347 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013348 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13349 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013350
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013351 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013352 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013353 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013354 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013355 { "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 +010013356 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013357 { "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 +010013358
Willy Tarreau409bcde2013-01-08 00:31:00 +010013359 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013360 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013361 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13362 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013363 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010013364
13365 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013366 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013367 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013368 { "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 +020013369 { "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 +010013370
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013371 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020013372 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013373 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013374 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000013375 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013376 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013377 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020013378 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13379 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013380 { "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 +010013381 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020013382}};
13383
Willy Tarreau8797c062007-05-07 00:55:35 +020013384
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013385/************************************************************************/
13386/* All supported converter keywords must be declared here. */
13387/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020013388/* Note: must not be declared <const> as its list will be overwritten */
13389static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013390 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020013391 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013392 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
13393 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020013394 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020013395 { NULL, NULL, 0, 0, 0 },
13396}};
13397
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020013398
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013399/************************************************************************/
13400/* All supported http-request action keywords must be declared here. */
13401/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013402struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013403 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020013404 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013405 { "set-method", parse_set_req_line },
13406 { "set-path", parse_set_req_line },
13407 { "set-query", parse_set_req_line },
13408 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020013409 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013410 }
13411};
13412
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013413struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013414 .kw = {
13415 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020013416 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013417 { NULL, NULL }
13418 }
13419};
13420
Willy Tarreau8797c062007-05-07 00:55:35 +020013421__attribute__((constructor))
13422static void __http_protocol_init(void)
13423{
13424 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020013425 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020013426 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013427 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013428 http_res_keywords_register(&http_res_actions);
Willy Tarreau12207b32016-11-22 19:48:51 +010013429 cli_register_kw(&cli_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020013430}
13431
13432
Willy Tarreau58f10d72006-12-04 02:26:12 +010013433/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020013434 * Local variables:
13435 * c-indent-level: 8
13436 * c-basic-offset: 8
13437 * End:
13438 */