blob: 23a7dc452c540346feb4b791982ba464c6b33df2 [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 Faulet3e344292015-11-24 16:24:13 +01006832 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_FILTERS(s) ||
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 Faulet92d36382015-11-05 13:35:03 +01006983 if ((msg->flags & HTTP_MSGF_TE_CHNK) || !msg->body_len ||
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006984 HAS_FILTERS(s) ||
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 Faulet32359572016-11-29 14:49:54 +01007076 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && !(chn->flags & CF_SHUTR) &&
7077 HAS_DATA_FILTERS(s, chn)) {
7078 /* The server still sending data that should be filtered */
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007079 goto missing_data_or_waiting;
7080 }
7081 msg->msg_state = HTTP_MSG_ENDING;
7082
7083 ending:
7084 /* we may have some pending data starting at res->buf->p such as a last
7085 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01007086 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
7087 /* default_ret */ msg->next,
7088 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007089 b_adv(chn->buf, ret);
7090 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007091 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
7092 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02007093 if (msg->next)
7094 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007095
Christopher Fauletda02e172015-12-04 09:25:05 +01007096 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
7097 /* default_ret */ 1,
7098 /* on_error */ goto error,
7099 /* on_wait */ goto waiting);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007100 msg->msg_state = HTTP_MSG_DONE;
7101 return 1;
7102
7103 missing_data_or_waiting:
7104 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01007105 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
7106 /* default_ret */ msg->next,
7107 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007108 b_adv(chn->buf, ret);
7109 msg->next -= ret;
7110 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
7111 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01007112 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007113 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02007114 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007115 return 0;
7116 error:
7117 return -1;
7118}
7119
7120static inline int
7121http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
7122{
7123 struct channel *chn = msg->chn;
7124 int ret;
7125
7126 /* Here we have the guarantee to be in one of the following state:
7127 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
7128 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
7129
7130 switch_states:
7131 switch (msg->msg_state) {
7132 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01007133 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
7134 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
7135 /* on_error */ goto error);
7136 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007137 msg->chunk_len -= ret;
7138 if (msg->chunk_len) {
7139 /* input empty or output full */
7140 if (chn->buf->i > msg->next)
7141 chn->flags |= CF_WAKE_WRITE;
7142 goto missing_data_or_waiting;
7143 }
7144
7145 /* nothing left to forward for this chunk*/
7146 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
7147 /* fall through for HTTP_MSG_CHUNK_CRLF */
7148
7149 case HTTP_MSG_CHUNK_CRLF:
7150 /* we want the CRLF after the data */
7151 ret = http_skip_chunk_crlf(msg);
7152 if (ret == 0)
7153 goto missing_data_or_waiting;
7154 if (ret < 0)
7155 goto chunk_parsing_error;
Christopher Faulet113f7de2015-12-14 14:52:13 +01007156 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007157 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
7158 /* fall through for HTTP_MSG_CHUNK_SIZE */
7159
7160 case HTTP_MSG_CHUNK_SIZE:
7161 /* read the chunk size and assign it to ->chunk_len,
7162 * then set ->next to point to the body and switch to
7163 * DATA or TRAILERS state.
7164 */
7165 ret = http_parse_chunk_size(msg);
7166 if (ret == 0)
7167 goto missing_data_or_waiting;
7168 if (ret < 0)
7169 goto chunk_parsing_error;
Christopher Faulet113f7de2015-12-14 14:52:13 +01007170 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007171 if (msg->chunk_len) {
7172 msg->msg_state = HTTP_MSG_DATA;
7173 goto switch_states;
7174 }
7175 msg->msg_state = HTTP_MSG_TRAILERS;
7176 /* fall through for HTTP_MSG_TRAILERS */
7177
7178 case HTTP_MSG_TRAILERS:
7179 ret = http_forward_trailers(msg);
7180 if (ret < 0)
7181 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01007182 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
7183 /* default_ret */ 1,
7184 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007185 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007186 if (!ret)
7187 goto missing_data_or_waiting;
7188 break;
7189
7190 case HTTP_MSG_ENDING:
7191 goto ending;
7192
7193 default:
7194 /* This should no happen in this function */
7195 goto error;
7196 }
7197
7198 msg->msg_state = HTTP_MSG_ENDING;
7199 ending:
7200 /* we may have some pending data starting at res->buf->p such as a last
7201 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01007202 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007203 /* default_ret */ msg->next,
7204 /* on_error */ goto error);
7205 b_adv(chn->buf, ret);
7206 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007207 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
7208 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02007209 if (msg->next)
7210 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007211
Christopher Fauletda02e172015-12-04 09:25:05 +01007212 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007213 /* default_ret */ 1,
7214 /* on_error */ goto error,
7215 /* on_wait */ goto waiting);
7216 msg->msg_state = HTTP_MSG_DONE;
7217 return 1;
7218
7219 missing_data_or_waiting:
7220 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01007221 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007222 /* default_ret */ msg->next,
7223 /* on_error */ goto error);
7224 b_adv(chn->buf, ret);
7225 msg->next -= ret;
7226 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
7227 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01007228 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007229 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02007230 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007231 return 0;
7232
7233 chunk_parsing_error:
7234 if (msg->err_pos >= 0) {
7235 if (chn->flags & CF_ISRESP)
7236 http_capture_bad_message(&s->be->invalid_rep, s, msg,
7237 msg->msg_state, strm_fe(s));
7238 else
7239 http_capture_bad_message(&strm_fe(s)->invalid_req, s,
7240 msg, msg->msg_state, s->be);
7241 }
7242 error:
7243 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007244}
7245
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007246
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007247/* Iterate the same filter through all request headers.
7248 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007249 * Since it can manage the switch to another backend, it updates the per-proxy
7250 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007251 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007252int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007253{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007254 char *cur_ptr, *cur_end, *cur_next;
7255 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007256 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007257 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007258 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01007259
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007260 last_hdr = 0;
7261
Willy Tarreau9b28e032012-10-12 23:49:43 +02007262 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007263 old_idx = 0;
7264
7265 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007266 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007267 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007268 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007269 (exp->action == ACT_ALLOW ||
7270 exp->action == ACT_DENY ||
7271 exp->action == ACT_TARPIT))
7272 return 0;
7273
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007274 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007275 if (!cur_idx)
7276 break;
7277
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007278 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007279 cur_ptr = cur_next;
7280 cur_end = cur_ptr + cur_hdr->len;
7281 cur_next = cur_end + cur_hdr->cr + 1;
7282
7283 /* Now we have one header between cur_ptr and cur_end,
7284 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007285 */
7286
Willy Tarreau15a53a42015-01-21 13:39:42 +01007287 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007288 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007289 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007290 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007291 last_hdr = 1;
7292 break;
7293
7294 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007295 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007296 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007297 break;
7298
7299 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007300 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007301 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007302 break;
7303
7304 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007305 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7306 if (trash.len < 0)
7307 return -1;
7308
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007309 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007310 /* FIXME: if the user adds a newline in the replacement, the
7311 * index will not be recalculated for now, and the new line
7312 * will not be counted as a new header.
7313 */
7314
7315 cur_end += delta;
7316 cur_next += delta;
7317 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007318 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007319 break;
7320
7321 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007322 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007323 cur_next += delta;
7324
Willy Tarreaufa355d42009-11-29 18:12:29 +01007325 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007326 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7327 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007328 cur_hdr->len = 0;
7329 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007330 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007331 break;
7332
7333 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007334 }
7335
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007336 /* keep the link from this header to next one in case of later
7337 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007338 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007339 old_idx = cur_idx;
7340 }
7341 return 0;
7342}
7343
7344
7345/* Apply the filter to the request line.
7346 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7347 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007348 * Since it can manage the switch to another backend, it updates the per-proxy
7349 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007350 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007351int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007352{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007353 char *cur_ptr, *cur_end;
7354 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007355 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007356 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007357
Willy Tarreau3d300592007-03-18 18:34:41 +01007358 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007359 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007360 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007361 (exp->action == ACT_ALLOW ||
7362 exp->action == ACT_DENY ||
7363 exp->action == ACT_TARPIT))
7364 return 0;
7365 else if (exp->action == ACT_REMOVE)
7366 return 0;
7367
7368 done = 0;
7369
Willy Tarreau9b28e032012-10-12 23:49:43 +02007370 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007371 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007372
7373 /* Now we have the request line between cur_ptr and cur_end */
7374
Willy Tarreau15a53a42015-01-21 13:39:42 +01007375 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007376 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007377 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007378 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007379 done = 1;
7380 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007381
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007382 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007383 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007384 done = 1;
7385 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007386
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007387 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007388 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007389 done = 1;
7390 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007391
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007392 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007393 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7394 if (trash.len < 0)
7395 return -1;
7396
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007397 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007398 /* FIXME: if the user adds a newline in the replacement, the
7399 * index will not be recalculated for now, and the new line
7400 * will not be counted as a new header.
7401 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007402
Willy Tarreaufa355d42009-11-29 18:12:29 +01007403 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007404 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007405 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007406 HTTP_MSG_RQMETH,
7407 cur_ptr, cur_end + 1,
7408 NULL, NULL);
7409 if (unlikely(!cur_end))
7410 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007411
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007412 /* we have a full request and we know that we have either a CR
7413 * or an LF at <ptr>.
7414 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007415 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7416 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007417 /* there is no point trying this regex on headers */
7418 return 1;
7419 }
7420 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007421 return done;
7422}
Willy Tarreau97de6242006-12-27 17:18:38 +01007423
Willy Tarreau58f10d72006-12-04 02:26:12 +01007424
Willy Tarreau58f10d72006-12-04 02:26:12 +01007425
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007426/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007427 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007428 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007429 * unparsable request. Since it can manage the switch to another backend, it
7430 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007431 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007432int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007433{
Willy Tarreau192252e2015-04-04 01:47:55 +02007434 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007435 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007436 struct hdr_exp *exp;
7437
7438 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007439 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007440
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007441 /*
7442 * The interleaving of transformations and verdicts
7443 * makes it difficult to decide to continue or stop
7444 * the evaluation.
7445 */
7446
Willy Tarreau6c123b12010-01-28 20:22:06 +01007447 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7448 break;
7449
Willy Tarreau3d300592007-03-18 18:34:41 +01007450 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007451 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007452 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007453 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007454
7455 /* if this filter had a condition, evaluate it now and skip to
7456 * next filter if the condition does not match.
7457 */
7458 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007459 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007460 ret = acl_pass(ret);
7461 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7462 ret = !ret;
7463
7464 if (!ret)
7465 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007466 }
7467
7468 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007469 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007470 if (unlikely(ret < 0))
7471 return -1;
7472
7473 if (likely(ret == 0)) {
7474 /* The filter did not match the request, it can be
7475 * iterated through all headers.
7476 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007477 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7478 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007479 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007480 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007481 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007482}
7483
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007484
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007485/* Find the end of a cookie value contained between <s> and <e>. It works the
7486 * same way as with headers above except that the semi-colon also ends a token.
7487 * See RFC2965 for more information. Note that it requires a valid header to
7488 * return a valid result.
7489 */
7490char *find_cookie_value_end(char *s, const char *e)
7491{
7492 int quoted, qdpair;
7493
7494 quoted = qdpair = 0;
7495 for (; s < e; s++) {
7496 if (qdpair) qdpair = 0;
7497 else if (quoted) {
7498 if (*s == '\\') qdpair = 1;
7499 else if (*s == '"') quoted = 0;
7500 }
7501 else if (*s == '"') quoted = 1;
7502 else if (*s == ',' || *s == ';') return s;
7503 }
7504 return s;
7505}
7506
7507/* Delete a value in a header between delimiters <from> and <next> in buffer
7508 * <buf>. The number of characters displaced is returned, and the pointer to
7509 * the first delimiter is updated if required. The function tries as much as
7510 * possible to respect the following principles :
7511 * - replace <from> delimiter by the <next> one unless <from> points to a
7512 * colon, in which case <next> is simply removed
7513 * - set exactly one space character after the new first delimiter, unless
7514 * there are not enough characters in the block being moved to do so.
7515 * - remove unneeded spaces before the previous delimiter and after the new
7516 * one.
7517 *
7518 * It is the caller's responsibility to ensure that :
7519 * - <from> points to a valid delimiter or the colon ;
7520 * - <next> points to a valid delimiter or the final CR/LF ;
7521 * - there are non-space chars before <from> ;
7522 * - there is a CR/LF at or after <next>.
7523 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007524int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007525{
7526 char *prev = *from;
7527
7528 if (*prev == ':') {
7529 /* We're removing the first value, preserve the colon and add a
7530 * space if possible.
7531 */
Willy Tarreau2235b262016-11-05 15:50:20 +01007532 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007533 next++;
7534 prev++;
7535 if (prev < next)
7536 *prev++ = ' ';
7537
Willy Tarreau2235b262016-11-05 15:50:20 +01007538 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007539 next++;
7540 } else {
7541 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01007542 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007543 prev--;
7544 *from = prev;
7545
7546 /* copy the delimiter and if possible a space if we're
7547 * not at the end of the line.
7548 */
Willy Tarreau2235b262016-11-05 15:50:20 +01007549 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007550 *prev++ = *next++;
7551 if (prev + 1 < next)
7552 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01007553 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007554 next++;
7555 }
7556 }
7557 return buffer_replace2(buf, prev, next, NULL, 0);
7558}
7559
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007560/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007561 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007562 * desirable to call it only when needed. This code is quite complex because
7563 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7564 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007565 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007566void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007567{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007568 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007569 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007570 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007571 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007572 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7573 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007574
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007575 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007576 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007577 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007578
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007579 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007580 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007581 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007582
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007583 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007584 hdr_beg = hdr_next;
7585 hdr_end = hdr_beg + cur_hdr->len;
7586 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007587
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007588 /* We have one full header between hdr_beg and hdr_end, and the
7589 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007590 * "Cookie:" headers.
7591 */
7592
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007593 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007594 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007595 old_idx = cur_idx;
7596 continue;
7597 }
7598
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007599 del_from = NULL; /* nothing to be deleted */
7600 preserve_hdr = 0; /* assume we may kill the whole header */
7601
Willy Tarreau58f10d72006-12-04 02:26:12 +01007602 /* Now look for cookies. Conforming to RFC2109, we have to support
7603 * attributes whose name begin with a '$', and associate them with
7604 * the right cookie, if we want to delete this cookie.
7605 * So there are 3 cases for each cookie read :
7606 * 1) it's a special attribute, beginning with a '$' : ignore it.
7607 * 2) it's a server id cookie that we *MAY* want to delete : save
7608 * some pointers on it (last semi-colon, beginning of cookie...)
7609 * 3) it's an application cookie : we *MAY* have to delete a previous
7610 * "special" cookie.
7611 * At the end of loop, if a "special" cookie remains, we may have to
7612 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007613 * *MUST* delete it.
7614 *
7615 * Note: RFC2965 is unclear about the processing of spaces around
7616 * the equal sign in the ATTR=VALUE form. A careful inspection of
7617 * the RFC explicitly allows spaces before it, and not within the
7618 * tokens (attrs or values). An inspection of RFC2109 allows that
7619 * too but section 10.1.3 lets one think that spaces may be allowed
7620 * after the equal sign too, resulting in some (rare) buggy
7621 * implementations trying to do that. So let's do what servers do.
7622 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7623 * allowed quoted strings in values, with any possible character
7624 * after a backslash, including control chars and delimitors, which
7625 * causes parsing to become ambiguous. Browsers also allow spaces
7626 * within values even without quotes.
7627 *
7628 * We have to keep multiple pointers in order to support cookie
7629 * removal at the beginning, middle or end of header without
7630 * corrupting the header. All of these headers are valid :
7631 *
7632 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7633 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7634 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7635 * | | | | | | | | |
7636 * | | | | | | | | hdr_end <--+
7637 * | | | | | | | +--> next
7638 * | | | | | | +----> val_end
7639 * | | | | | +-----------> val_beg
7640 * | | | | +--------------> equal
7641 * | | | +----------------> att_end
7642 * | | +---------------------> att_beg
7643 * | +--------------------------> prev
7644 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007645 */
7646
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007647 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7648 /* Iterate through all cookies on this line */
7649
7650 /* find att_beg */
7651 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007652 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007653 att_beg++;
7654
7655 /* find att_end : this is the first character after the last non
7656 * space before the equal. It may be equal to hdr_end.
7657 */
7658 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007659
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007660 while (equal < hdr_end) {
7661 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007662 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007663 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007664 continue;
7665 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007666 }
7667
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007668 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7669 * is between <att_beg> and <equal>, both may be identical.
7670 */
7671
7672 /* look for end of cookie if there is an equal sign */
7673 if (equal < hdr_end && *equal == '=') {
7674 /* look for the beginning of the value */
7675 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007676 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007677 val_beg++;
7678
7679 /* find the end of the value, respecting quotes */
7680 next = find_cookie_value_end(val_beg, hdr_end);
7681
7682 /* make val_end point to the first white space or delimitor after the value */
7683 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007684 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007685 val_end--;
7686 } else {
7687 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007688 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007689
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007690 /* We have nothing to do with attributes beginning with '$'. However,
7691 * they will automatically be removed if a header before them is removed,
7692 * since they're supposed to be linked together.
7693 */
7694 if (*att_beg == '$')
7695 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007696
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007697 /* Ignore cookies with no equal sign */
7698 if (equal == next) {
7699 /* This is not our cookie, so we must preserve it. But if we already
7700 * scheduled another cookie for removal, we cannot remove the
7701 * complete header, but we can remove the previous block itself.
7702 */
7703 preserve_hdr = 1;
7704 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007705 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007706 val_end += delta;
7707 next += delta;
7708 hdr_end += delta;
7709 hdr_next += delta;
7710 cur_hdr->len += delta;
7711 http_msg_move_end(&txn->req, delta);
7712 prev = del_from;
7713 del_from = NULL;
7714 }
7715 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007716 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007717
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007718 /* if there are spaces around the equal sign, we need to
7719 * strip them otherwise we'll get trouble for cookie captures,
7720 * or even for rewrites. Since this happens extremely rarely,
7721 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007722 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007723 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7724 int stripped_before = 0;
7725 int stripped_after = 0;
7726
7727 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007728 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007729 equal += stripped_before;
7730 val_beg += stripped_before;
7731 }
7732
7733 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007734 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007735 val_beg += stripped_after;
7736 stripped_before += stripped_after;
7737 }
7738
7739 val_end += stripped_before;
7740 next += stripped_before;
7741 hdr_end += stripped_before;
7742 hdr_next += stripped_before;
7743 cur_hdr->len += stripped_before;
7744 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007745 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007746 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007747
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007748 /* First, let's see if we want to capture this cookie. We check
7749 * that we don't already have a client side cookie, because we
7750 * can only capture one. Also as an optimisation, we ignore
7751 * cookies shorter than the declared name.
7752 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007753 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7754 (val_end - att_beg >= sess->fe->capture_namelen) &&
7755 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007756 int log_len = val_end - att_beg;
7757
7758 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7759 Alert("HTTP logging : out of memory.\n");
7760 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007761 if (log_len > sess->fe->capture_len)
7762 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007763 memcpy(txn->cli_cookie, att_beg, log_len);
7764 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007765 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007766 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007767
Willy Tarreaubca99692010-10-06 19:25:55 +02007768 /* Persistence cookies in passive, rewrite or insert mode have the
7769 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007770 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007771 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007772 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007773 * For cookies in prefix mode, the form is :
7774 *
7775 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007776 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007777 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7778 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7779 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007780 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007781
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007782 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7783 * have the server ID between val_beg and delim, and the original cookie between
7784 * delim+1 and val_end. Otherwise, delim==val_end :
7785 *
7786 * Cookie: NAME=SRV; # in all but prefix modes
7787 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7788 * | || || | |+-> next
7789 * | || || | +--> val_end
7790 * | || || +---------> delim
7791 * | || |+------------> val_beg
7792 * | || +-------------> att_end = equal
7793 * | |+-----------------> att_beg
7794 * | +------------------> prev
7795 * +-------------------------> hdr_beg
7796 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007797
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007798 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007799 for (delim = val_beg; delim < val_end; delim++)
7800 if (*delim == COOKIE_DELIM)
7801 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007802 } else {
7803 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007804 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007805 /* Now check if the cookie contains a date field, which would
7806 * appear after a vertical bar ('|') just after the server name
7807 * and before the delimiter.
7808 */
7809 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7810 if (vbar1) {
7811 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007812 * right is the last seen date. It is a base64 encoded
7813 * 30-bit value representing the UNIX date since the
7814 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007815 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007816 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007817 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007818 if (val_end - vbar1 >= 5) {
7819 val = b64tos30(vbar1);
7820 if (val > 0)
7821 txn->cookie_last_date = val << 2;
7822 }
7823 /* look for a second vertical bar */
7824 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7825 if (vbar1 && (val_end - vbar1 > 5)) {
7826 val = b64tos30(vbar1 + 1);
7827 if (val > 0)
7828 txn->cookie_first_date = val << 2;
7829 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007830 }
7831 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007832
Willy Tarreauf64d1412010-10-07 20:06:11 +02007833 /* if the cookie has an expiration date and the proxy wants to check
7834 * it, then we do that now. We first check if the cookie is too old,
7835 * then only if it has expired. We detect strict overflow because the
7836 * time resolution here is not great (4 seconds). Cookies with dates
7837 * in the future are ignored if their offset is beyond one day. This
7838 * allows an admin to fix timezone issues without expiring everyone
7839 * and at the same time avoids keeping unwanted side effects for too
7840 * long.
7841 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007842 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7843 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007844 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007845 txn->flags &= ~TX_CK_MASK;
7846 txn->flags |= TX_CK_OLD;
7847 delim = val_beg; // let's pretend we have not found the cookie
7848 txn->cookie_first_date = 0;
7849 txn->cookie_last_date = 0;
7850 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007851 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7852 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007853 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007854 txn->flags &= ~TX_CK_MASK;
7855 txn->flags |= TX_CK_EXPIRED;
7856 delim = val_beg; // let's pretend we have not found the cookie
7857 txn->cookie_first_date = 0;
7858 txn->cookie_last_date = 0;
7859 }
7860
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007861 /* Here, we'll look for the first running server which supports the cookie.
7862 * This allows to share a same cookie between several servers, for example
7863 * to dedicate backup servers to specific servers only.
7864 * However, to prevent clients from sticking to cookie-less backup server
7865 * when they have incidentely learned an empty cookie, we simply ignore
7866 * empty cookies and mark them as invalid.
7867 * The same behaviour is applied when persistence must be ignored.
7868 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007869 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007870 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007871
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007872 while (srv) {
7873 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7874 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007875 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007876 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007877 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007878 /* we found the server and we can use it */
7879 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007880 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007881 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007882 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007883 break;
7884 } else {
7885 /* we found a server, but it's down,
7886 * mark it as such and go on in case
7887 * another one is available.
7888 */
7889 txn->flags &= ~TX_CK_MASK;
7890 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007891 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007892 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007893 srv = srv->next;
7894 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007895
Willy Tarreauf64d1412010-10-07 20:06:11 +02007896 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007897 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007898 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007899 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007900 txn->flags |= TX_CK_UNUSED;
7901 else
7902 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007903 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007904
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007905 /* depending on the cookie mode, we may have to either :
7906 * - delete the complete cookie if we're in insert+indirect mode, so that
7907 * the server never sees it ;
7908 * - remove the server id from the cookie value, and tag the cookie as an
7909 * application cookie so that it does not get accidentely removed later,
7910 * if we're in cookie prefix mode
7911 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007912 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007913 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007914
Willy Tarreau9b28e032012-10-12 23:49:43 +02007915 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007916 val_end += delta;
7917 next += delta;
7918 hdr_end += delta;
7919 hdr_next += delta;
7920 cur_hdr->len += delta;
7921 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007922
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007923 del_from = NULL;
7924 preserve_hdr = 1; /* we want to keep this cookie */
7925 }
7926 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007927 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007928 del_from = prev;
7929 }
7930 } else {
7931 /* This is not our cookie, so we must preserve it. But if we already
7932 * scheduled another cookie for removal, we cannot remove the
7933 * complete header, but we can remove the previous block itself.
7934 */
7935 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007936
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007937 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007938 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007939 if (att_beg >= del_from)
7940 att_beg += delta;
7941 if (att_end >= del_from)
7942 att_end += delta;
7943 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007944 val_end += delta;
7945 next += delta;
7946 hdr_end += delta;
7947 hdr_next += delta;
7948 cur_hdr->len += delta;
7949 http_msg_move_end(&txn->req, delta);
7950 prev = del_from;
7951 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007952 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007953 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007954
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007955 /* continue with next cookie on this header line */
7956 att_beg = next;
7957 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007958
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007959 /* There are no more cookies on this line.
7960 * We may still have one (or several) marked for deletion at the
7961 * end of the line. We must do this now in two ways :
7962 * - if some cookies must be preserved, we only delete from the
7963 * mark to the end of line ;
7964 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007965 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007966 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007967 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007968 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007969 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007970 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007971 cur_hdr->len += delta;
7972 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007973 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007974
7975 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007976 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7977 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007978 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007979 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007980 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007981 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007982 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007983 }
7984
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007985 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007986 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007987 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007988}
7989
7990
Willy Tarreaua15645d2007-03-18 16:22:39 +01007991/* Iterate the same filter through all response headers contained in <rtr>.
7992 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7993 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007994int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007995{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007996 char *cur_ptr, *cur_end, *cur_next;
7997 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007998 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007999 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008000 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008001
8002 last_hdr = 0;
8003
Willy Tarreau9b28e032012-10-12 23:49:43 +02008004 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008005 old_idx = 0;
8006
8007 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008008 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008009 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008010 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008011 (exp->action == ACT_ALLOW ||
8012 exp->action == ACT_DENY))
8013 return 0;
8014
8015 cur_idx = txn->hdr_idx.v[old_idx].next;
8016 if (!cur_idx)
8017 break;
8018
8019 cur_hdr = &txn->hdr_idx.v[cur_idx];
8020 cur_ptr = cur_next;
8021 cur_end = cur_ptr + cur_hdr->len;
8022 cur_next = cur_end + cur_hdr->cr + 1;
8023
8024 /* Now we have one header between cur_ptr and cur_end,
8025 * and the next header starts at cur_next.
8026 */
8027
Willy Tarreau15a53a42015-01-21 13:39:42 +01008028 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008029 switch (exp->action) {
8030 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008031 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008032 last_hdr = 1;
8033 break;
8034
8035 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008036 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008037 last_hdr = 1;
8038 break;
8039
8040 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008041 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8042 if (trash.len < 0)
8043 return -1;
8044
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008045 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008046 /* FIXME: if the user adds a newline in the replacement, the
8047 * index will not be recalculated for now, and the new line
8048 * will not be counted as a new header.
8049 */
8050
8051 cur_end += delta;
8052 cur_next += delta;
8053 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008054 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008055 break;
8056
8057 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02008058 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008059 cur_next += delta;
8060
Willy Tarreaufa355d42009-11-29 18:12:29 +01008061 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008062 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8063 txn->hdr_idx.used--;
8064 cur_hdr->len = 0;
8065 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01008066 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008067 break;
8068
8069 }
8070 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008071
8072 /* keep the link from this header to next one in case of later
8073 * removal of next header.
8074 */
8075 old_idx = cur_idx;
8076 }
8077 return 0;
8078}
8079
8080
8081/* Apply the filter to the status line in the response buffer <rtr>.
8082 * Returns 0 if nothing has been done, 1 if the filter has been applied,
8083 * or -1 if a replacement resulted in an invalid status line.
8084 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008085int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008086{
Willy Tarreaua15645d2007-03-18 16:22:39 +01008087 char *cur_ptr, *cur_end;
8088 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008089 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008090 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008091
8092
Willy Tarreau3d300592007-03-18 18:34:41 +01008093 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008094 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008095 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008096 (exp->action == ACT_ALLOW ||
8097 exp->action == ACT_DENY))
8098 return 0;
8099 else if (exp->action == ACT_REMOVE)
8100 return 0;
8101
8102 done = 0;
8103
Willy Tarreau9b28e032012-10-12 23:49:43 +02008104 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008105 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008106
8107 /* Now we have the status line between cur_ptr and cur_end */
8108
Willy Tarreau15a53a42015-01-21 13:39:42 +01008109 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008110 switch (exp->action) {
8111 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008112 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008113 done = 1;
8114 break;
8115
8116 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008117 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008118 done = 1;
8119 break;
8120
8121 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008122 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8123 if (trash.len < 0)
8124 return -1;
8125
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008126 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008127 /* FIXME: if the user adds a newline in the replacement, the
8128 * index will not be recalculated for now, and the new line
8129 * will not be counted as a new header.
8130 */
8131
Willy Tarreaufa355d42009-11-29 18:12:29 +01008132 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008133 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008134 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02008135 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01008136 cur_ptr, cur_end + 1,
8137 NULL, NULL);
8138 if (unlikely(!cur_end))
8139 return -1;
8140
8141 /* we have a full respnse and we know that we have either a CR
8142 * or an LF at <ptr>.
8143 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008144 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008145 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01008146 /* there is no point trying this regex on headers */
8147 return 1;
8148 }
8149 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008150 return done;
8151}
8152
8153
8154
8155/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008156 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008157 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
8158 * unparsable response.
8159 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008160int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008161{
Willy Tarreau192252e2015-04-04 01:47:55 +02008162 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008163 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008164 struct hdr_exp *exp;
8165
8166 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008167 int ret;
8168
8169 /*
8170 * The interleaving of transformations and verdicts
8171 * makes it difficult to decide to continue or stop
8172 * the evaluation.
8173 */
8174
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008175 if (txn->flags & TX_SVDENY)
8176 break;
8177
Willy Tarreau3d300592007-03-18 18:34:41 +01008178 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008179 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
8180 exp->action == ACT_PASS)) {
8181 exp = exp->next;
8182 continue;
8183 }
8184
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008185 /* if this filter had a condition, evaluate it now and skip to
8186 * next filter if the condition does not match.
8187 */
8188 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02008189 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008190 ret = acl_pass(ret);
8191 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8192 ret = !ret;
8193 if (!ret)
8194 continue;
8195 }
8196
Willy Tarreaua15645d2007-03-18 16:22:39 +01008197 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008198 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008199 if (unlikely(ret < 0))
8200 return -1;
8201
8202 if (likely(ret == 0)) {
8203 /* The filter did not match the response, it can be
8204 * iterated through all headers.
8205 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008206 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8207 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008208 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008209 }
8210 return 0;
8211}
8212
8213
Willy Tarreaua15645d2007-03-18 16:22:39 +01008214/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008215 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008216 * desirable to call it only when needed. This function is also used when we
8217 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008218 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008219void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008220{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008221 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008222 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01008223 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008224 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008225 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008226 char *hdr_beg, *hdr_end, *hdr_next;
8227 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008228
Willy Tarreaua15645d2007-03-18 16:22:39 +01008229 /* Iterate through the headers.
8230 * we start with the start line.
8231 */
8232 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008233 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008234
8235 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8236 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008237 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008238
8239 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008240 hdr_beg = hdr_next;
8241 hdr_end = hdr_beg + cur_hdr->len;
8242 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008243
Willy Tarreau24581ba2010-08-31 22:39:35 +02008244 /* We have one full header between hdr_beg and hdr_end, and the
8245 * next header starts at hdr_next. We're only interested in
8246 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008247 */
8248
Willy Tarreau24581ba2010-08-31 22:39:35 +02008249 is_cookie2 = 0;
8250 prev = hdr_beg + 10;
8251 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008252 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008253 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8254 if (!val) {
8255 old_idx = cur_idx;
8256 continue;
8257 }
8258 is_cookie2 = 1;
8259 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008260 }
8261
Willy Tarreau24581ba2010-08-31 22:39:35 +02008262 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8263 * <prev> points to the colon.
8264 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008265 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008266
Willy Tarreau24581ba2010-08-31 22:39:35 +02008267 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8268 * check-cache is enabled) and we are not interested in checking
8269 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008270 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02008271 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008272 return;
8273
Willy Tarreau24581ba2010-08-31 22:39:35 +02008274 /* OK so now we know we have to process this response cookie.
8275 * The format of the Set-Cookie header is slightly different
8276 * from the format of the Cookie header in that it does not
8277 * support the comma as a cookie delimiter (thus the header
8278 * cannot be folded) because the Expires attribute described in
8279 * the original Netscape's spec may contain an unquoted date
8280 * with a comma inside. We have to live with this because
8281 * many browsers don't support Max-Age and some browsers don't
8282 * support quoted strings. However the Set-Cookie2 header is
8283 * clean.
8284 *
8285 * We have to keep multiple pointers in order to support cookie
8286 * removal at the beginning, middle or end of header without
8287 * corrupting the header (in case of set-cookie2). A special
8288 * pointer, <scav> points to the beginning of the set-cookie-av
8289 * fields after the first semi-colon. The <next> pointer points
8290 * either to the end of line (set-cookie) or next unquoted comma
8291 * (set-cookie2). All of these headers are valid :
8292 *
8293 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8294 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8295 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8296 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8297 * | | | | | | | | | |
8298 * | | | | | | | | +-> next hdr_end <--+
8299 * | | | | | | | +------------> scav
8300 * | | | | | | +--------------> val_end
8301 * | | | | | +--------------------> val_beg
8302 * | | | | +----------------------> equal
8303 * | | | +------------------------> att_end
8304 * | | +----------------------------> att_beg
8305 * | +------------------------------> prev
8306 * +-----------------------------------------> hdr_beg
8307 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008308
Willy Tarreau24581ba2010-08-31 22:39:35 +02008309 for (; prev < hdr_end; prev = next) {
8310 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008311
Willy Tarreau24581ba2010-08-31 22:39:35 +02008312 /* find att_beg */
8313 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01008314 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008315 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008316
Willy Tarreau24581ba2010-08-31 22:39:35 +02008317 /* find att_end : this is the first character after the last non
8318 * space before the equal. It may be equal to hdr_end.
8319 */
8320 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008321
Willy Tarreau24581ba2010-08-31 22:39:35 +02008322 while (equal < hdr_end) {
8323 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8324 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01008325 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008326 continue;
8327 att_end = equal;
8328 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008329
Willy Tarreau24581ba2010-08-31 22:39:35 +02008330 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8331 * is between <att_beg> and <equal>, both may be identical.
8332 */
8333
8334 /* look for end of cookie if there is an equal sign */
8335 if (equal < hdr_end && *equal == '=') {
8336 /* look for the beginning of the value */
8337 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01008338 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008339 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008340
Willy Tarreau24581ba2010-08-31 22:39:35 +02008341 /* find the end of the value, respecting quotes */
8342 next = find_cookie_value_end(val_beg, hdr_end);
8343
8344 /* make val_end point to the first white space or delimitor after the value */
8345 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01008346 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008347 val_end--;
8348 } else {
8349 /* <equal> points to next comma, semi-colon or EOL */
8350 val_beg = val_end = next = equal;
8351 }
8352
8353 if (next < hdr_end) {
8354 /* Set-Cookie2 supports multiple cookies, and <next> points to
8355 * a colon or semi-colon before the end. So skip all attr-value
8356 * pairs and look for the next comma. For Set-Cookie, since
8357 * commas are permitted in values, skip to the end.
8358 */
8359 if (is_cookie2)
8360 next = find_hdr_value_end(next, hdr_end);
8361 else
8362 next = hdr_end;
8363 }
8364
8365 /* Now everything is as on the diagram above */
8366
8367 /* Ignore cookies with no equal sign */
8368 if (equal == val_end)
8369 continue;
8370
8371 /* If there are spaces around the equal sign, we need to
8372 * strip them otherwise we'll get trouble for cookie captures,
8373 * or even for rewrites. Since this happens extremely rarely,
8374 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008375 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008376 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8377 int stripped_before = 0;
8378 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008379
Willy Tarreau24581ba2010-08-31 22:39:35 +02008380 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008381 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008382 equal += stripped_before;
8383 val_beg += stripped_before;
8384 }
8385
8386 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008387 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008388 val_beg += stripped_after;
8389 stripped_before += stripped_after;
8390 }
8391
8392 val_end += stripped_before;
8393 next += stripped_before;
8394 hdr_end += stripped_before;
8395 hdr_next += stripped_before;
8396 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008397 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008398 }
8399
8400 /* First, let's see if we want to capture this cookie. We check
8401 * that we don't already have a server side cookie, because we
8402 * can only capture one. Also as an optimisation, we ignore
8403 * cookies shorter than the declared name.
8404 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008405 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008406 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008407 (val_end - att_beg >= sess->fe->capture_namelen) &&
8408 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008409 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008410 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008411 Alert("HTTP logging : out of memory.\n");
8412 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008413 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008414 if (log_len > sess->fe->capture_len)
8415 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008416 memcpy(txn->srv_cookie, att_beg, log_len);
8417 txn->srv_cookie[log_len] = 0;
8418 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008419 }
8420
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008421 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008422 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008423 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008424 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8425 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008426 /* assume passive cookie by default */
8427 txn->flags &= ~TX_SCK_MASK;
8428 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008429
8430 /* If the cookie is in insert mode on a known server, we'll delete
8431 * this occurrence because we'll insert another one later.
8432 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008433 * a direct access.
8434 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008435 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008436 /* The "preserve" flag was set, we don't want to touch the
8437 * server's cookie.
8438 */
8439 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008440 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008441 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008442 /* this cookie must be deleted */
8443 if (*prev == ':' && next == hdr_end) {
8444 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008445 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008446 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8447 txn->hdr_idx.used--;
8448 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008449 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008450 hdr_next += delta;
8451 http_msg_move_end(&txn->rsp, delta);
8452 /* note: while both invalid now, <next> and <hdr_end>
8453 * are still equal, so the for() will stop as expected.
8454 */
8455 } else {
8456 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008457 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008458 next = prev;
8459 hdr_end += delta;
8460 hdr_next += delta;
8461 cur_hdr->len += delta;
8462 http_msg_move_end(&txn->rsp, delta);
8463 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008464 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008465 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008466 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008467 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008468 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008469 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008470 * with this server since we know it.
8471 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008472 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008473 next += delta;
8474 hdr_end += delta;
8475 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008476 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008477 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008478
Willy Tarreauf1348312010-10-07 15:54:11 +02008479 txn->flags &= ~TX_SCK_MASK;
8480 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008481 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008482 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008483 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008484 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008485 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008486 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008487 next += delta;
8488 hdr_end += delta;
8489 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008490 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008491 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008492
Willy Tarreau827aee92011-03-10 16:55:02 +01008493 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008494 txn->flags &= ~TX_SCK_MASK;
8495 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008496 }
8497 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008498 /* that's done for this cookie, check the next one on the same
8499 * line when next != hdr_end (only if is_cookie2).
8500 */
8501 }
8502 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008503 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008504 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008505}
8506
8507
Willy Tarreaua15645d2007-03-18 16:22:39 +01008508/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008509 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008510 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008511void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008512{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008513 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008514 char *p1, *p2;
8515
8516 char *cur_ptr, *cur_end, *cur_next;
8517 int cur_idx;
8518
Willy Tarreau5df51872007-11-25 16:20:08 +01008519 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008520 return;
8521
8522 /* Iterate through the headers.
8523 * we start with the start line.
8524 */
8525 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008526 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008527
8528 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8529 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008530 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008531
8532 cur_hdr = &txn->hdr_idx.v[cur_idx];
8533 cur_ptr = cur_next;
8534 cur_end = cur_ptr + cur_hdr->len;
8535 cur_next = cur_end + cur_hdr->cr + 1;
8536
8537 /* We have one full header between cur_ptr and cur_end, and the
8538 * next header starts at cur_next. We're only interested in
8539 * "Cookie:" headers.
8540 */
8541
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008542 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8543 if (val) {
8544 if ((cur_end - (cur_ptr + val) >= 8) &&
8545 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8546 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8547 return;
8548 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008549 }
8550
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008551 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8552 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008553 continue;
8554
8555 /* OK, right now we know we have a cache-control header at cur_ptr */
8556
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008557 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008558
8559 if (p1 >= cur_end) /* no more info */
8560 continue;
8561
8562 /* p1 is at the beginning of the value */
8563 p2 = p1;
8564
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008565 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008566 p2++;
8567
8568 /* we have a complete value between p1 and p2 */
8569 if (p2 < cur_end && *p2 == '=') {
8570 /* we have something of the form no-cache="set-cookie" */
8571 if ((cur_end - p1 >= 21) &&
8572 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8573 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008574 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008575 continue;
8576 }
8577
8578 /* OK, so we know that either p2 points to the end of string or to a comma */
8579 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008580 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008581 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8582 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8583 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008584 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008585 return;
8586 }
8587
8588 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008589 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008590 continue;
8591 }
8592 }
8593}
8594
Willy Tarreau58f10d72006-12-04 02:26:12 +01008595
Willy Tarreaub2513902006-12-17 14:52:38 +01008596/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008597 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008598 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008599 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008600 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008601 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008602 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008603 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008604 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008605int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008606{
8607 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008608 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008609 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008610
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008611 if (!uri_auth)
8612 return 0;
8613
Cyril Bonté70be45d2010-10-12 00:14:35 +02008614 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008615 return 0;
8616
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008617 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008618 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008619 return 0;
8620
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008621 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008622 return 0;
8623
Willy Tarreaub2513902006-12-17 14:52:38 +01008624 return 1;
8625}
8626
Willy Tarreau4076a152009-04-02 15:18:36 +02008627/*
8628 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008629 * By default it tries to report the error position as msg->err_pos. However if
8630 * this one is not set, it will then report msg->next, which is the last known
8631 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008632 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008633 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008634void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008635 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008636 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008637{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008638 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008639 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008640 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008641
Willy Tarreauf3764b72016-03-31 13:45:10 +02008642 es->len = MIN(chn->buf->i, global.tune.bufsize);
Willy Tarreau9b28e032012-10-12 23:49:43 +02008643 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008644 len1 = MIN(len1, es->len);
8645 len2 = es->len - len1; /* remaining data if buffer wraps */
8646
Willy Tarreauf3764b72016-03-31 13:45:10 +02008647 if (!es->buf)
8648 es->buf = malloc(global.tune.bufsize);
8649
8650 if (es->buf) {
8651 memcpy(es->buf, chn->buf->p, len1);
8652 if (len2)
8653 memcpy(es->buf + len1, chn->buf->data, len2);
8654 }
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008655
Willy Tarreau4076a152009-04-02 15:18:36 +02008656 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008657 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008658 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008659 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008660
Willy Tarreau4076a152009-04-02 15:18:36 +02008661 es->when = date; // user-visible date
8662 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008663 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008664 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008665 if (objt_conn(sess->origin))
8666 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008667 else
8668 memset(&es->src, 0, sizeof(es->src));
8669
Willy Tarreau078272e2010-12-12 12:46:33 +01008670 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008671 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008672 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008673 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008674 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008675 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008676 es->b_out = chn->buf->o;
8677 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008678 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008679 es->m_clen = msg->chunk_len;
8680 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008681}
Willy Tarreaub2513902006-12-17 14:52:38 +01008682
Willy Tarreau294c4732011-12-16 21:35:50 +01008683/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8684 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8685 * performed over the whole headers. Otherwise it must contain a valid header
8686 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8687 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8688 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8689 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008690 * -1. The value fetch stops at commas, so this function is suited for use with
8691 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008692 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008693 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008694unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008695 struct hdr_idx *idx, int occ,
8696 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008697{
Willy Tarreau294c4732011-12-16 21:35:50 +01008698 struct hdr_ctx local_ctx;
8699 char *ptr_hist[MAX_HDR_HISTORY];
8700 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008701 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008702 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008703
Willy Tarreau294c4732011-12-16 21:35:50 +01008704 if (!ctx) {
8705 local_ctx.idx = 0;
8706 ctx = &local_ctx;
8707 }
8708
Willy Tarreaubce70882009-09-07 11:51:47 +02008709 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008710 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008711 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008712 occ--;
8713 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008714 *vptr = ctx->line + ctx->val;
8715 *vlen = ctx->vlen;
8716 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008717 }
8718 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008719 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008720 }
8721
8722 /* negative occurrence, we scan all the list then walk back */
8723 if (-occ > MAX_HDR_HISTORY)
8724 return 0;
8725
Willy Tarreau294c4732011-12-16 21:35:50 +01008726 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008727 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008728 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8729 len_hist[hist_ptr] = ctx->vlen;
8730 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008731 hist_ptr = 0;
8732 found++;
8733 }
8734 if (-occ > found)
8735 return 0;
8736 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008737 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8738 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8739 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008740 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008741 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008742 if (hist_ptr >= MAX_HDR_HISTORY)
8743 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008744 *vptr = ptr_hist[hist_ptr];
8745 *vlen = len_hist[hist_ptr];
8746 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008747}
8748
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008749/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8750 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8751 * performed over the whole headers. Otherwise it must contain a valid header
8752 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8753 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8754 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8755 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8756 * -1. This function differs from http_get_hdr() in that it only returns full
8757 * line header values and does not stop at commas.
8758 * The return value is 0 if nothing was found, or non-zero otherwise.
8759 */
8760unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8761 struct hdr_idx *idx, int occ,
8762 struct hdr_ctx *ctx, char **vptr, int *vlen)
8763{
8764 struct hdr_ctx local_ctx;
8765 char *ptr_hist[MAX_HDR_HISTORY];
8766 int len_hist[MAX_HDR_HISTORY];
8767 unsigned int hist_ptr;
8768 int found;
8769
8770 if (!ctx) {
8771 local_ctx.idx = 0;
8772 ctx = &local_ctx;
8773 }
8774
8775 if (occ >= 0) {
8776 /* search from the beginning */
8777 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8778 occ--;
8779 if (occ <= 0) {
8780 *vptr = ctx->line + ctx->val;
8781 *vlen = ctx->vlen;
8782 return 1;
8783 }
8784 }
8785 return 0;
8786 }
8787
8788 /* negative occurrence, we scan all the list then walk back */
8789 if (-occ > MAX_HDR_HISTORY)
8790 return 0;
8791
8792 found = hist_ptr = 0;
8793 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8794 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8795 len_hist[hist_ptr] = ctx->vlen;
8796 if (++hist_ptr >= MAX_HDR_HISTORY)
8797 hist_ptr = 0;
8798 found++;
8799 }
8800 if (-occ > found)
8801 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008802
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008803 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008804 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8805 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8806 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008807 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008808 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008809 if (hist_ptr >= MAX_HDR_HISTORY)
8810 hist_ptr -= MAX_HDR_HISTORY;
8811 *vptr = ptr_hist[hist_ptr];
8812 *vlen = len_hist[hist_ptr];
8813 return 1;
8814}
8815
Willy Tarreaubaaee002006-06-26 02:48:02 +02008816/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008817 * Print a debug line with a header. Always stop at the first CR or LF char,
8818 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8819 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008820 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008821void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008822{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008823 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008824 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008825
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008826 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008827 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008828 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008829 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 +02008830
8831 for (max = 0; start + max < end; max++)
8832 if (start[max] == '\r' || start[max] == '\n')
8833 break;
8834
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008835 UBOUND(max, trash.size - trash.len - 3);
8836 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8837 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008838 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008839}
8840
Willy Tarreaueee5b512015-04-03 23:46:31 +02008841
8842/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8843 * The hdr_idx is allocated as well. In case of allocation failure, everything
8844 * allocated is freed and NULL is returned. Otherwise the new transaction is
8845 * assigned to the stream and returned.
8846 */
8847struct http_txn *http_alloc_txn(struct stream *s)
8848{
8849 struct http_txn *txn = s->txn;
8850
8851 if (txn)
8852 return txn;
8853
8854 txn = pool_alloc2(pool2_http_txn);
8855 if (!txn)
8856 return txn;
8857
8858 txn->hdr_idx.size = global.tune.max_http_hdr;
8859 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8860 if (!txn->hdr_idx.v) {
8861 pool_free2(pool2_http_txn, txn);
8862 return NULL;
8863 }
8864
8865 s->txn = txn;
8866 return txn;
8867}
8868
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008869void http_txn_reset_req(struct http_txn *txn)
8870{
8871 txn->req.flags = 0;
8872 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8873 txn->req.next = 0;
8874 txn->req.chunk_len = 0LL;
8875 txn->req.body_len = 0LL;
8876 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8877}
8878
8879void http_txn_reset_res(struct http_txn *txn)
8880{
8881 txn->rsp.flags = 0;
8882 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8883 txn->rsp.next = 0;
8884 txn->rsp.chunk_len = 0LL;
8885 txn->rsp.body_len = 0LL;
8886 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8887}
8888
Willy Tarreau0937bc42009-12-22 15:03:09 +01008889/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008890 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008891 * the required fields are properly allocated and that we only need to (re)init
8892 * them. This should be used before processing any new request.
8893 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008894void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008895{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008896 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008897 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008898
8899 txn->flags = 0;
8900 txn->status = -1;
8901
Willy Tarreauf64d1412010-10-07 20:06:11 +02008902 txn->cookie_first_date = 0;
8903 txn->cookie_last_date = 0;
8904
Willy Tarreaueee5b512015-04-03 23:46:31 +02008905 txn->srv_cookie = NULL;
8906 txn->cli_cookie = NULL;
8907 txn->uri = NULL;
8908
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008909 http_txn_reset_req(txn);
8910 http_txn_reset_res(txn);
8911
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008912 txn->req.chn = &s->req;
8913 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008914
8915 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008916
8917 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8918 if (fe->options2 & PR_O2_REQBUG_OK)
8919 txn->req.err_pos = -1; /* let buggy requests pass */
8920
Willy Tarreau0937bc42009-12-22 15:03:09 +01008921 if (txn->hdr_idx.v)
8922 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008923
8924 vars_init(&s->vars_txn, SCOPE_TXN);
8925 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008926}
8927
8928/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008929void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008930{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008931 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008932 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008933
8934 /* these ones will have been dynamically allocated */
8935 pool_free2(pool2_requri, txn->uri);
8936 pool_free2(pool2_capture, txn->cli_cookie);
8937 pool_free2(pool2_capture, txn->srv_cookie);
William Lallemanda73203e2012-03-12 12:48:57 +01008938 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008939
William Lallemanda73203e2012-03-12 12:48:57 +01008940 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008941 txn->uri = NULL;
8942 txn->srv_cookie = NULL;
8943 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008944
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008945 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008946 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008947 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008948 pool_free2(h->pool, s->req_cap[h->index]);
8949 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008950 }
8951
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008952 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008953 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008954 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008955 pool_free2(h->pool, s->res_cap[h->index]);
8956 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008957 }
8958
Willy Tarreau6204cd92016-03-10 16:33:04 +01008959 vars_prune(&s->vars_txn, s->sess, s);
8960 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008961}
8962
8963/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008964void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008965{
8966 http_end_txn(s);
8967 http_init_txn(s);
8968
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008969 /* reinitialise the current rule list pointer to NULL. We are sure that
8970 * any rulelist match the NULL pointer.
8971 */
8972 s->current_rule_list = NULL;
8973
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008974 s->be = strm_fe(s);
8975 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008976 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008977 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008978 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008979 /* re-init store persistence */
8980 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008981 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008982
Willy Tarreau0937bc42009-12-22 15:03:09 +01008983 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008984
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008985 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008986
Willy Tarreau739cfba2010-01-25 23:11:14 +01008987 /* We must trim any excess data from the response buffer, because we
8988 * may have blocked an invalid response from a server that we don't
8989 * want to accidentely forward once we disable the analysers, nor do
8990 * we want those data to come along with next response. A typical
8991 * example of such data would be from a buggy server responding to
8992 * a HEAD with some data, or sending more than the advertised
8993 * content-length.
8994 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008995 if (unlikely(s->res.buf->i))
8996 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008997
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008998 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008999 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009000
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009001 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009002 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009003
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009004 s->req.rex = TICK_ETERNITY;
9005 s->req.wex = TICK_ETERNITY;
9006 s->req.analyse_exp = TICK_ETERNITY;
9007 s->res.rex = TICK_ETERNITY;
9008 s->res.wex = TICK_ETERNITY;
9009 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009010}
Willy Tarreau58f10d72006-12-04 02:26:12 +01009011
Sasha Pachev218f0642014-06-16 12:05:59 -06009012void free_http_res_rules(struct list *r)
9013{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009014 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06009015
9016 list_for_each_entry_safe(pr, tr, r, list) {
9017 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009018 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06009019 free(pr);
9020 }
9021}
9022
9023void free_http_req_rules(struct list *r)
9024{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009025 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01009026
9027 list_for_each_entry_safe(pr, tr, r, list) {
9028 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009029 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009030 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01009031
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009032 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01009033 free(pr);
9034 }
9035}
9036
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009037/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009038struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01009039{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009040 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009041 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009042 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009043 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01009044
Vincent Bernat02779b62016-04-03 13:48:43 +02009045 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01009046 if (!rule) {
9047 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009048 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009049 }
9050
CJ Ess108b1dd2015-04-07 12:03:37 -04009051 rule->deny_status = HTTP_ERR_403;
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009052 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009053 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01009054 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02009055 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04009056 int code;
9057 int hc;
9058
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009059 rule->action = ACT_ACTION_DENY;
Willy Tarreauff011f22011-01-06 17:51:27 +01009060 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04009061 if (strcmp(args[cur_arg], "deny_status") == 0) {
9062 cur_arg++;
9063 if (!args[cur_arg]) {
9064 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
9065 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9066 goto out_err;
9067 }
9068
9069 code = atol(args[cur_arg]);
9070 cur_arg++;
9071 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
9072 if (http_err_codes[hc] == code) {
9073 rule->deny_status = hc;
9074 break;
9075 }
9076 }
9077
9078 if (hc >= HTTP_ERR_SIZE) {
9079 Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n",
9080 file, linenum, code);
9081 }
9082 }
Willy Tarreauccbcc372012-12-27 12:37:57 +01009083 } else if (!strcmp(args[0], "tarpit")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009084 rule->action = ACT_HTTP_REQ_TARPIT;
Willy Tarreauccbcc372012-12-27 12:37:57 +01009085 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01009086 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009087 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01009088 cur_arg = 1;
9089
9090 while(*args[cur_arg]) {
9091 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009092 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01009093 cur_arg+=2;
9094 continue;
9095 } else
9096 break;
9097 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009098 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009099 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009100 cur_arg = 1;
9101
9102 if (!*args[cur_arg] ||
9103 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9104 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
9105 file, linenum, args[0]);
9106 goto out_err;
9107 }
9108 rule->arg.nice = atoi(args[cur_arg]);
9109 if (rule->arg.nice < -1024)
9110 rule->arg.nice = -1024;
9111 else if (rule->arg.nice > 1024)
9112 rule->arg.nice = 1024;
9113 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009114 } else if (!strcmp(args[0], "set-tos")) {
9115#ifdef IP_TOS
9116 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009117 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009118 cur_arg = 1;
9119
9120 if (!*args[cur_arg] ||
9121 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9122 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9123 file, linenum, args[0]);
9124 goto out_err;
9125 }
9126
9127 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9128 if (err && *err != '\0') {
9129 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9130 file, linenum, err, args[0]);
9131 goto out_err;
9132 }
9133 cur_arg++;
9134#else
9135 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9136 goto out_err;
9137#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009138 } else if (!strcmp(args[0], "set-mark")) {
9139#ifdef SO_MARK
9140 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009141 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009142 cur_arg = 1;
9143
9144 if (!*args[cur_arg] ||
9145 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9146 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9147 file, linenum, args[0]);
9148 goto out_err;
9149 }
9150
9151 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9152 if (err && *err != '\0') {
9153 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9154 file, linenum, err, args[0]);
9155 goto out_err;
9156 }
9157 cur_arg++;
9158 global.last_checks |= LSTCHK_NETADM;
9159#else
9160 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9161 goto out_err;
9162#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009163 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009164 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009165 cur_arg = 1;
9166
9167 if (!*args[cur_arg] ||
9168 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9169 bad_log_level:
9170 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9171 file, linenum, args[0]);
9172 goto out_err;
9173 }
9174 if (strcmp(args[cur_arg], "silent") == 0)
9175 rule->arg.loglevel = -1;
9176 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9177 goto bad_log_level;
9178 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009179 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009180 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009181 cur_arg = 1;
9182
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009183 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9184 (*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 +01009185 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9186 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009187 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009188 }
9189
9190 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9191 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9192 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009193
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009194 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009195 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009196 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 +01009197 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9198 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9199 file, linenum, args[0], error);
9200 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009201 goto out_err;
9202 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009203 free(proxy->conf.lfs_file);
9204 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9205 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009206 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009207 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009208 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009209 cur_arg = 1;
9210
9211 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009212 (*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 -06009213 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9214 file, linenum, args[0]);
9215 goto out_err;
9216 }
9217
9218 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9219 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9220 LIST_INIT(&rule->arg.hdr_add.fmt);
9221
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009222 error = NULL;
9223 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9224 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9225 args[cur_arg + 1], error);
9226 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009227 goto out_err;
9228 }
9229
9230 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009231 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009232 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 +01009233 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9234 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9235 file, linenum, args[0], error);
9236 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009237 goto out_err;
9238 }
Sasha Pachev218f0642014-06-16 12:05:59 -06009239
9240 free(proxy->conf.lfs_file);
9241 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9242 proxy->conf.lfs_line = proxy->conf.args.line;
9243 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009244 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009245 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009246 cur_arg = 1;
9247
9248 if (!*args[cur_arg] ||
9249 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9250 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9251 file, linenum, args[0]);
9252 goto out_err;
9253 }
9254
9255 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9256 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9257
9258 proxy->conf.args.ctx = ARGC_HRQ;
9259 free(proxy->conf.lfs_file);
9260 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9261 proxy->conf.lfs_line = proxy->conf.args.line;
9262 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009263 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9264 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009265 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009266 struct sample_expr *expr;
9267 unsigned int where;
9268 char *err = NULL;
9269
9270 cur_arg = 1;
9271 proxy->conf.args.ctx = ARGC_TRK;
9272
9273 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9274 if (!expr) {
9275 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9276 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9277 free(err);
9278 goto out_err;
9279 }
9280
9281 where = 0;
9282 if (proxy->cap & PR_CAP_FE)
9283 where |= SMP_VAL_FE_HRQ_HDR;
9284 if (proxy->cap & PR_CAP_BE)
9285 where |= SMP_VAL_BE_HRQ_HDR;
9286
9287 if (!(expr->fetch->val & where)) {
9288 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9289 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9290 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9291 args[cur_arg-1], sample_src_names(expr->fetch->use));
9292 free(expr);
9293 goto out_err;
9294 }
9295
9296 if (strcmp(args[cur_arg], "table") == 0) {
9297 cur_arg++;
9298 if (!args[cur_arg]) {
9299 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9300 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9301 free(expr);
9302 goto out_err;
9303 }
9304 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009305 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02009306 cur_arg++;
9307 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009308 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009309 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009310 } else if (strcmp(args[0], "redirect") == 0) {
9311 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009312 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009313
Willy Tarreaube4653b2015-05-28 15:26:58 +02009314 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009315 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9316 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9317 goto out_err;
9318 }
9319
9320 /* this redirect rule might already contain a parsed condition which
9321 * we'll pass to the http-request rule.
9322 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009323 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009324 rule->arg.redir = redir;
9325 rule->cond = redir->cond;
9326 redir->cond = NULL;
9327 cur_arg = 2;
9328 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009329 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9330 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009331 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009332 /*
9333 * '+ 8' for 'add-acl('
9334 * '- 9' for 'add-acl(' + trailing ')'
9335 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009336 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009337
9338 cur_arg = 1;
9339
9340 if (!*args[cur_arg] ||
9341 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9342 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9343 file, linenum, args[0]);
9344 goto out_err;
9345 }
9346
9347 LIST_INIT(&rule->arg.map.key);
9348 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009349 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009350 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009351 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9352 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9353 file, linenum, args[0], error);
9354 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009355 goto out_err;
9356 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009357 free(proxy->conf.lfs_file);
9358 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9359 proxy->conf.lfs_line = proxy->conf.args.line;
9360 cur_arg += 1;
9361 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9362 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009363 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009364 /*
9365 * '+ 8' for 'del-acl('
9366 * '- 9' for 'del-acl(' + trailing ')'
9367 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009368 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009369
9370 cur_arg = 1;
9371
9372 if (!*args[cur_arg] ||
9373 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9374 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9375 file, linenum, args[0]);
9376 goto out_err;
9377 }
9378
9379 LIST_INIT(&rule->arg.map.key);
9380 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009381 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009382 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009383 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9384 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9385 file, linenum, args[0], error);
9386 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009387 goto out_err;
9388 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009389 free(proxy->conf.lfs_file);
9390 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9391 proxy->conf.lfs_line = proxy->conf.args.line;
9392 cur_arg += 1;
9393 } else if (strncmp(args[0], "del-map", 7) == 0) {
9394 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009395 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009396 /*
9397 * '+ 8' for 'del-map('
9398 * '- 9' for 'del-map(' + trailing ')'
9399 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009400 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009401
9402 cur_arg = 1;
9403
9404 if (!*args[cur_arg] ||
9405 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9406 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9407 file, linenum, args[0]);
9408 goto out_err;
9409 }
9410
9411 LIST_INIT(&rule->arg.map.key);
9412 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009413 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009414 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009415 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9416 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9417 file, linenum, args[0], error);
9418 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009419 goto out_err;
9420 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009421 free(proxy->conf.lfs_file);
9422 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9423 proxy->conf.lfs_line = proxy->conf.args.line;
9424 cur_arg += 1;
9425 } else if (strncmp(args[0], "set-map", 7) == 0) {
9426 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009427 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009428 /*
9429 * '+ 8' for 'set-map('
9430 * '- 9' for 'set-map(' + trailing ')'
9431 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009432 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009433
9434 cur_arg = 1;
9435
9436 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9437 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9438 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9439 file, linenum, args[0]);
9440 goto out_err;
9441 }
9442
9443 LIST_INIT(&rule->arg.map.key);
9444 LIST_INIT(&rule->arg.map.value);
9445 proxy->conf.args.ctx = ARGC_HRQ;
9446
9447 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009448 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009449 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009450 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9451 Alert("parsing [%s:%d]: 'http-request %s' key: %s.\n",
9452 file, linenum, args[0], error);
9453 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009454 goto out_err;
9455 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009456
9457 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009458 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009459 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 +01009460 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9461 Alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n",
9462 file, linenum, args[0], error);
9463 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009464 goto out_err;
9465 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009466 free(proxy->conf.lfs_file);
9467 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9468 proxy->conf.lfs_line = proxy->conf.args.line;
9469
9470 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009471 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9472 char *errmsg = NULL;
9473 cur_arg = 1;
9474 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009475 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009476 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009477 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009478 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9479 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9480 free(errmsg);
9481 goto out_err;
9482 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009483 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009484 action_build_list(&http_req_keywords.list, &trash);
9485 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
9486 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009487 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02009488 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009489 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009490 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009491 }
9492
9493 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9494 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009495 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009496
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009497 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9498 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9499 file, linenum, args[0], errmsg);
9500 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009501 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009502 }
9503 rule->cond = cond;
9504 }
9505 else if (*args[cur_arg]) {
9506 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9507 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9508 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009509 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009510 }
9511
9512 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009513 out_err:
9514 free(rule);
9515 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009516}
9517
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009518/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009519struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009520{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009521 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009522 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009523 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009524 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009525
9526 rule = calloc(1, sizeof(*rule));
9527 if (!rule) {
9528 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9529 goto out_err;
9530 }
9531
9532 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009533 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009534 cur_arg = 1;
9535 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009536 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009537 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009538 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009539 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009540 cur_arg = 1;
9541
9542 if (!*args[cur_arg] ||
9543 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9544 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9545 file, linenum, args[0]);
9546 goto out_err;
9547 }
9548 rule->arg.nice = atoi(args[cur_arg]);
9549 if (rule->arg.nice < -1024)
9550 rule->arg.nice = -1024;
9551 else if (rule->arg.nice > 1024)
9552 rule->arg.nice = 1024;
9553 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009554 } else if (!strcmp(args[0], "set-tos")) {
9555#ifdef IP_TOS
9556 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009557 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009558 cur_arg = 1;
9559
9560 if (!*args[cur_arg] ||
9561 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9562 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9563 file, linenum, args[0]);
9564 goto out_err;
9565 }
9566
9567 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9568 if (err && *err != '\0') {
9569 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9570 file, linenum, err, args[0]);
9571 goto out_err;
9572 }
9573 cur_arg++;
9574#else
9575 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9576 goto out_err;
9577#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009578 } else if (!strcmp(args[0], "set-mark")) {
9579#ifdef SO_MARK
9580 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009581 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009582 cur_arg = 1;
9583
9584 if (!*args[cur_arg] ||
9585 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9586 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9587 file, linenum, args[0]);
9588 goto out_err;
9589 }
9590
9591 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9592 if (err && *err != '\0') {
9593 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9594 file, linenum, err, args[0]);
9595 goto out_err;
9596 }
9597 cur_arg++;
9598 global.last_checks |= LSTCHK_NETADM;
9599#else
9600 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9601 goto out_err;
9602#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009603 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009604 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009605 cur_arg = 1;
9606
9607 if (!*args[cur_arg] ||
9608 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9609 bad_log_level:
9610 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9611 file, linenum, args[0]);
9612 goto out_err;
9613 }
9614 if (strcmp(args[cur_arg], "silent") == 0)
9615 rule->arg.loglevel = -1;
Ruoshan Huangdd016782016-06-15 22:16:03 +08009616 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009617 goto bad_log_level;
9618 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009619 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009620 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009621 cur_arg = 1;
9622
9623 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9624 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9625 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9626 file, linenum, args[0]);
9627 goto out_err;
9628 }
9629
9630 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9631 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9632 LIST_INIT(&rule->arg.hdr_add.fmt);
9633
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009634 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009635 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009636 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 +01009637 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9638 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9639 file, linenum, args[0], error);
9640 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009641 goto out_err;
9642 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009643 free(proxy->conf.lfs_file);
9644 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9645 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009646 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009647 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009648 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009649 cur_arg = 1;
9650
9651 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009652 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9653 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009654 file, linenum, args[0]);
9655 goto out_err;
9656 }
9657
9658 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9659 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9660 LIST_INIT(&rule->arg.hdr_add.fmt);
9661
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009662 error = NULL;
9663 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9664 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9665 args[cur_arg + 1], error);
9666 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009667 goto out_err;
9668 }
9669
9670 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009671 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009672 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 +01009673 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9674 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9675 file, linenum, args[0], error);
9676 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009677 goto out_err;
9678 }
Sasha Pachev218f0642014-06-16 12:05:59 -06009679
9680 free(proxy->conf.lfs_file);
9681 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9682 proxy->conf.lfs_line = proxy->conf.args.line;
9683 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009684 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009685 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009686 cur_arg = 1;
9687
9688 if (!*args[cur_arg] ||
9689 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9690 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9691 file, linenum, args[0]);
9692 goto out_err;
9693 }
9694
9695 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9696 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9697
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009698 proxy->conf.args.ctx = ARGC_HRS;
9699 free(proxy->conf.lfs_file);
9700 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9701 proxy->conf.lfs_line = proxy->conf.args.line;
9702 cur_arg += 1;
9703 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9704 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009705 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009706 /*
9707 * '+ 8' for 'add-acl('
9708 * '- 9' for 'add-acl(' + trailing ')'
9709 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009710 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009711
9712 cur_arg = 1;
9713
9714 if (!*args[cur_arg] ||
9715 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9716 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9717 file, linenum, args[0]);
9718 goto out_err;
9719 }
9720
9721 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009722 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009723 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009724 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009725 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9726 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9727 file, linenum, args[0], error);
9728 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009729 goto out_err;
9730 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009731 free(proxy->conf.lfs_file);
9732 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9733 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009734
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009735 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009736 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9737 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009738 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009739 /*
9740 * '+ 8' for 'del-acl('
9741 * '- 9' for 'del-acl(' + trailing ')'
9742 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009743 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009744
9745 cur_arg = 1;
9746
9747 if (!*args[cur_arg] ||
9748 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9749 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9750 file, linenum, args[0]);
9751 goto out_err;
9752 }
9753
9754 LIST_INIT(&rule->arg.map.key);
9755 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009756 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009757 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009758 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9759 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9760 file, linenum, args[0], error);
9761 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009762 goto out_err;
9763 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009764 free(proxy->conf.lfs_file);
9765 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9766 proxy->conf.lfs_line = proxy->conf.args.line;
9767 cur_arg += 1;
9768 } else if (strncmp(args[0], "del-map", 7) == 0) {
9769 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009770 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009771 /*
9772 * '+ 8' for 'del-map('
9773 * '- 9' for 'del-map(' + trailing ')'
9774 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009775 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009776
9777 cur_arg = 1;
9778
9779 if (!*args[cur_arg] ||
9780 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9781 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9782 file, linenum, args[0]);
9783 goto out_err;
9784 }
9785
9786 LIST_INIT(&rule->arg.map.key);
9787 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009788 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009789 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009790 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9791 Alert("parsing [%s:%d]: 'http-response %s' %s.\n",
9792 file, linenum, args[0], error);
9793 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009794 goto out_err;
9795 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009796 free(proxy->conf.lfs_file);
9797 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9798 proxy->conf.lfs_line = proxy->conf.args.line;
9799 cur_arg += 1;
9800 } else if (strncmp(args[0], "set-map", 7) == 0) {
9801 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009802 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009803 /*
9804 * '+ 8' for 'set-map('
9805 * '- 9' for 'set-map(' + trailing ')'
9806 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009807 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009808
9809 cur_arg = 1;
9810
9811 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9812 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9813 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9814 file, linenum, args[0]);
9815 goto out_err;
9816 }
9817
9818 LIST_INIT(&rule->arg.map.key);
9819 LIST_INIT(&rule->arg.map.value);
9820
9821 proxy->conf.args.ctx = ARGC_HRS;
9822
9823 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009824 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009825 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009826 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9827 Alert("parsing [%s:%d]: 'http-response %s' name: %s.\n",
9828 file, linenum, args[0], error);
9829 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009830 goto out_err;
9831 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009832
9833 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009834 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009835 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 +01009836 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9837 Alert("parsing [%s:%d]: 'http-response %s' value: %s.\n",
9838 file, linenum, args[0], error);
9839 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009840 goto out_err;
9841 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009842
9843 free(proxy->conf.lfs_file);
9844 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9845 proxy->conf.lfs_line = proxy->conf.args.line;
9846
9847 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009848 } else if (strcmp(args[0], "redirect") == 0) {
9849 struct redirect_rule *redir;
9850 char *errmsg = NULL;
9851
9852 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
9853 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9854 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9855 goto out_err;
9856 }
9857
9858 /* this redirect rule might already contain a parsed condition which
9859 * we'll pass to the http-request rule.
9860 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009861 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009862 rule->arg.redir = redir;
9863 rule->cond = redir->cond;
9864 redir->cond = NULL;
9865 cur_arg = 2;
9866 return rule;
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009867 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9868 args[0][9] == '\0' && args[0][8] >= '0' &&
9869 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
9870 struct sample_expr *expr;
9871 unsigned int where;
9872 char *err = NULL;
9873
9874 cur_arg = 1;
9875 proxy->conf.args.ctx = ARGC_TRK;
9876
9877 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9878 if (!expr) {
9879 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9880 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9881 free(err);
9882 goto out_err;
9883 }
9884
9885 where = 0;
9886 if (proxy->cap & PR_CAP_FE)
9887 where |= SMP_VAL_FE_HRS_HDR;
9888 if (proxy->cap & PR_CAP_BE)
9889 where |= SMP_VAL_BE_HRS_HDR;
9890
9891 if (!(expr->fetch->val & where)) {
9892 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :"
9893 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9894 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9895 args[cur_arg-1], sample_src_names(expr->fetch->use));
9896 free(expr);
9897 goto out_err;
9898 }
9899
9900 if (strcmp(args[cur_arg], "table") == 0) {
9901 cur_arg++;
9902 if (!args[cur_arg]) {
9903 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n",
9904 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9905 free(expr);
9906 goto out_err;
9907 }
9908 /* we copy the table name for now, it will be resolved later */
9909 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
9910 cur_arg++;
9911 }
9912 rule->arg.trk_ctr.expr = expr;
9913 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
William Lallemand73025dd2014-04-24 14:38:37 +02009914 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9915 char *errmsg = NULL;
9916 cur_arg = 1;
9917 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009918 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009919 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009920 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009921 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9922 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9923 free(errmsg);
9924 goto out_err;
9925 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009926 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009927 action_build_list(&http_res_keywords.list, &trash);
9928 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9929 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009930 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02009931 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009932 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009933 goto out_err;
9934 }
9935
9936 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9937 struct acl_cond *cond;
9938 char *errmsg = NULL;
9939
9940 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9941 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9942 file, linenum, args[0], errmsg);
9943 free(errmsg);
9944 goto out_err;
9945 }
9946 rule->cond = cond;
9947 }
9948 else if (*args[cur_arg]) {
9949 Alert("parsing [%s:%d]: 'http-response %s' expects"
9950 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9951 file, linenum, args[0], args[cur_arg]);
9952 goto out_err;
9953 }
9954
9955 return rule;
9956 out_err:
9957 free(rule);
9958 return NULL;
9959}
9960
Willy Tarreau4baae242012-12-27 12:00:31 +01009961/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009962 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009963 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9964 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009965 */
9966struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009967 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009968{
9969 struct redirect_rule *rule;
9970 int cur_arg;
9971 int type = REDIRECT_TYPE_NONE;
9972 int code = 302;
9973 const char *destination = NULL;
9974 const char *cookie = NULL;
9975 int cookie_set = 0;
9976 unsigned int flags = REDIRECT_FLAG_NONE;
9977 struct acl_cond *cond = NULL;
9978
9979 cur_arg = 0;
9980 while (*(args[cur_arg])) {
9981 if (strcmp(args[cur_arg], "location") == 0) {
9982 if (!*args[cur_arg + 1])
9983 goto missing_arg;
9984
9985 type = REDIRECT_TYPE_LOCATION;
9986 cur_arg++;
9987 destination = args[cur_arg];
9988 }
9989 else if (strcmp(args[cur_arg], "prefix") == 0) {
9990 if (!*args[cur_arg + 1])
9991 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009992 type = REDIRECT_TYPE_PREFIX;
9993 cur_arg++;
9994 destination = args[cur_arg];
9995 }
9996 else if (strcmp(args[cur_arg], "scheme") == 0) {
9997 if (!*args[cur_arg + 1])
9998 goto missing_arg;
9999
10000 type = REDIRECT_TYPE_SCHEME;
10001 cur_arg++;
10002 destination = args[cur_arg];
10003 }
10004 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
10005 if (!*args[cur_arg + 1])
10006 goto missing_arg;
10007
10008 cur_arg++;
10009 cookie = args[cur_arg];
10010 cookie_set = 1;
10011 }
10012 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
10013 if (!*args[cur_arg + 1])
10014 goto missing_arg;
10015
10016 cur_arg++;
10017 cookie = args[cur_arg];
10018 cookie_set = 0;
10019 }
10020 else if (strcmp(args[cur_arg], "code") == 0) {
10021 if (!*args[cur_arg + 1])
10022 goto missing_arg;
10023
10024 cur_arg++;
10025 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040010026 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +010010027 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040010028 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +010010029 args[cur_arg - 1], args[cur_arg]);
10030 return NULL;
10031 }
10032 }
10033 else if (!strcmp(args[cur_arg],"drop-query")) {
10034 flags |= REDIRECT_FLAG_DROP_QS;
10035 }
10036 else if (!strcmp(args[cur_arg],"append-slash")) {
10037 flags |= REDIRECT_FLAG_APPEND_SLASH;
10038 }
10039 else if (strcmp(args[cur_arg], "if") == 0 ||
10040 strcmp(args[cur_arg], "unless") == 0) {
10041 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
10042 if (!cond) {
10043 memprintf(errmsg, "error in condition: %s", *errmsg);
10044 return NULL;
10045 }
10046 break;
10047 }
10048 else {
10049 memprintf(errmsg,
10050 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
10051 args[cur_arg]);
10052 return NULL;
10053 }
10054 cur_arg++;
10055 }
10056
10057 if (type == REDIRECT_TYPE_NONE) {
10058 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
10059 return NULL;
10060 }
10061
Willy Tarreaube4653b2015-05-28 15:26:58 +020010062 if (dir && type != REDIRECT_TYPE_LOCATION) {
10063 memprintf(errmsg, "response only supports redirect type 'location'");
10064 return NULL;
10065 }
10066
Vincent Bernat3c2f2f22016-04-03 13:48:42 +020010067 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +010010068 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +010010069 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010070
10071 if (!use_fmt) {
10072 /* old-style static redirect rule */
10073 rule->rdr_str = strdup(destination);
10074 rule->rdr_len = strlen(destination);
10075 }
10076 else {
10077 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010078
10079 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
10080 * if prefix == "/", we don't want to add anything, otherwise it
10081 * makes it hard for the user to configure a self-redirection.
10082 */
Godbachd9722032014-12-18 15:44:58 +080010083 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010084 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010010085 if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
10086 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 +010010087 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
10088 errmsg)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010010089 return NULL;
10090 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +010010091 free(curproxy->conf.lfs_file);
10092 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
10093 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010094 }
10095 }
10096
Willy Tarreau4baae242012-12-27 12:00:31 +010010097 if (cookie) {
10098 /* depending on cookie_set, either we want to set the cookie, or to clear it.
10099 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
10100 */
10101 rule->cookie_len = strlen(cookie);
10102 if (cookie_set) {
10103 rule->cookie_str = malloc(rule->cookie_len + 10);
10104 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10105 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
10106 rule->cookie_len += 9;
10107 } else {
10108 rule->cookie_str = malloc(rule->cookie_len + 21);
10109 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10110 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
10111 rule->cookie_len += 20;
10112 }
10113 }
10114 rule->type = type;
10115 rule->code = code;
10116 rule->flags = flags;
10117 LIST_INIT(&rule->list);
10118 return rule;
10119
10120 missing_arg:
10121 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
10122 return NULL;
10123}
10124
Willy Tarreau8797c062007-05-07 00:55:35 +020010125/************************************************************************/
10126/* The code below is dedicated to ACL parsing and matching */
10127/************************************************************************/
10128
10129
Willy Tarreau14174bc2012-04-16 14:34:04 +020010130/* This function ensures that the prerequisites for an L7 fetch are ready,
10131 * which means that a request or response is ready. If some data is missing,
10132 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +020010133 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
10134 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010135 *
10136 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +020010137 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
10138 * decide whether or not an HTTP message is present ;
10139 * 0 if the requested data cannot be fetched or if it is certain that
10140 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010141 * 1 if an HTTP message is ready
10142 */
James Rosewell91a41cb2015-09-18 17:11:16 +010010143int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010144 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +020010145{
Willy Tarreau192252e2015-04-04 01:47:55 +020010146 struct http_txn *txn;
10147 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010148
Willy Tarreaube508f12016-03-10 11:47:01 +010010149 /* Note: it is possible that <s> is NULL when called before stream
10150 * initialization (eg: tcp-request connection), so this function is the
10151 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010152 */
Willy Tarreau192252e2015-04-04 01:47:55 +020010153 if (!s)
10154 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +010010155
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +020010156 if (!s->txn) {
10157 if (unlikely(!http_alloc_txn(s)))
10158 return 0; /* not enough memory */
10159 http_init_txn(s);
10160 }
Willy Tarreau192252e2015-04-04 01:47:55 +020010161 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +020010162 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010163
10164 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010165 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010166
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010167 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010168 /* If the buffer does not leave enough free space at the end,
10169 * we must first realign it.
10170 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010171 if (s->req.buf->p > s->req.buf->data &&
10172 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
10173 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +010010174
Willy Tarreau14174bc2012-04-16 14:34:04 +020010175 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +020010176 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +020010177 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010178
10179 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010180 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +020010181 http_msg_analyzer(msg, &txn->hdr_idx);
10182
10183 /* Still no valid request ? */
10184 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +020010185 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010186 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +020010187 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010188 }
10189 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +020010190 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010191 return 0;
10192 }
10193
10194 /* OK we just got a valid HTTP request. We have some minor
10195 * preparation to perform so that further checks can rely
10196 * on HTTP tests.
10197 */
Willy Tarreauaae75e32013-03-29 12:31:49 +010010198
10199 /* If the request was parsed but was too large, we must absolutely
10200 * return an error so that it is not processed. At the moment this
10201 * cannot happen, but if the parsers are to change in the future,
10202 * we want this check to be maintained.
10203 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010204 if (unlikely(s->req.buf->i + s->req.buf->p >
10205 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +010010206 msg->err_state = msg->msg_state;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010207 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010208 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010209 return 1;
10210 }
10211
Willy Tarreau9b28e032012-10-12 23:49:43 +020010212 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +020010213 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +020010214 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010215
Willy Tarreau506d0502013-07-06 13:29:24 +020010216 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
10217 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010218 }
10219
Willy Tarreau506d0502013-07-06 13:29:24 +020010220 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +020010221 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +020010222 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010223
10224 /* otherwise everything's ready for the request */
10225 }
Willy Tarreau24e32d82012-04-23 23:55:44 +020010226 else {
10227 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +020010228 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
10229 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010230 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +020010231 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010232 }
10233
10234 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010235 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010236 return 1;
10237}
Willy Tarreau8797c062007-05-07 00:55:35 +020010238
Willy Tarreau8797c062007-05-07 00:55:35 +020010239/* 1. Check on METHOD
10240 * We use the pre-parsed method if it is known, and store its number as an
10241 * integer. If it is unknown, we use the pointer and the length.
10242 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010243static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +020010244{
10245 int len, meth;
10246
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010010247 len = strlen(text);
10248 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +020010249
10250 pattern->val.i = meth;
10251 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +020010252 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +020010253 pattern->len = len;
10254 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010255 else {
10256 pattern->ptr.str = NULL;
10257 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010258 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010259 return 1;
10260}
10261
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010262/* This function fetches the method of current HTTP request and stores
10263 * it in the global pattern struct as a chunk. There are two possibilities :
10264 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10265 * in <len> and <ptr> is NULL ;
10266 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10267 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010268 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010269 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010270static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010271smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010272{
10273 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +010010274 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010275
Willy Tarreau24e32d82012-04-23 23:55:44 +020010276 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010277
Willy Tarreaube508f12016-03-10 11:47:01 +010010278 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010279 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010280 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010281 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010282 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010283 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10284 /* ensure the indexes are not affected */
10285 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010286 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010287 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
10288 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010289 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010290 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010291 return 1;
10292}
10293
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010294/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010295static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010296{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010297 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010298 struct pattern_list *lst;
10299 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010300
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010301 list_for_each_entry(lst, &expr->patterns, list) {
10302 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010303
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010304 /* well-known method */
10305 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010306 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010307 return pattern;
10308 else
10309 continue;
10310 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010311
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010312 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010313 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010314 continue;
10315
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010316 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010317 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
10318 (!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 +010010319 return pattern;
10320 }
10321 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010322}
10323
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010324static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010325smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010326{
Willy Tarreaube508f12016-03-10 11:47:01 +010010327 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010328 char *ptr;
10329 int len;
10330
Willy Tarreauc0239e02012-04-16 14:42:55 +020010331 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010332
Willy Tarreaube508f12016-03-10 11:47:01 +010010333 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010334 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010335 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010336
10337 while ((len-- > 0) && (*ptr++ != '/'));
10338 if (len <= 0)
10339 return 0;
10340
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010341 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010342 smp->data.u.str.str = ptr;
10343 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010344
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010345 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010346 return 1;
10347}
10348
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010349static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010350smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010351{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010352 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010353 char *ptr;
10354 int len;
10355
Willy Tarreauc0239e02012-04-16 14:42:55 +020010356 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010357
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010358 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010359 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10360 return 0;
10361
Willy Tarreau8797c062007-05-07 00:55:35 +020010362 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010363 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010364
10365 while ((len-- > 0) && (*ptr++ != '/'));
10366 if (len <= 0)
10367 return 0;
10368
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010369 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010370 smp->data.u.str.str = ptr;
10371 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010372
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010373 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010374 return 1;
10375}
10376
10377/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010378static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010379smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010380{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010381 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010382 char *ptr;
10383 int len;
10384
Willy Tarreauc0239e02012-04-16 14:42:55 +020010385 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010386
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010387 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010388 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10389 return 0;
10390
Willy Tarreau8797c062007-05-07 00:55:35 +020010391 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010392 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010393
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010394 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010395 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010396 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010397 return 1;
10398}
10399
Thierry Fournierf4011dd2016-03-29 17:23:51 +020010400static int
10401smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
10402{
10403 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
10404 return 0;
10405
10406 if (!smp->strm->unique_id) {
10407 if ((smp->strm->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
10408 return 0;
10409 smp->strm->unique_id[0] = '\0';
10410 }
10411 smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id,
10412 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
10413
10414 smp->data.type = SMP_T_STR;
10415 smp->data.u.str.str = smp->strm->unique_id;
10416 smp->flags = SMP_F_CONST;
10417 return 1;
10418}
10419
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010420/* returns the longest available part of the body. This requires that the body
10421 * has been waited for using http-buffer-request.
10422 */
10423static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010424smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010425{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010426 struct http_msg *msg;
10427 unsigned long len;
10428 unsigned long block1;
10429 char *body;
10430 struct chunk *temp;
10431
10432 CHECK_HTTP_MESSAGE_FIRST();
10433
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010434 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010435 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010436 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010437 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010438
10439 len = http_body_bytes(msg);
10440 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
10441
10442 block1 = len;
10443 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
10444 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
10445
10446 if (block1 == len) {
10447 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010448 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010449 smp->data.u.str.str = body;
10450 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010451 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
10452 }
10453 else {
10454 /* buffer is wrapped, we need to defragment it */
10455 temp = get_trash_chunk();
10456 memcpy(temp->str, body, block1);
10457 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010458 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010459 smp->data.u.str.str = temp->str;
10460 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010461 smp->flags = SMP_F_VOL_TEST;
10462 }
10463 return 1;
10464}
10465
10466
10467/* returns the available length of the body. This requires that the body
10468 * has been waited for using http-buffer-request.
10469 */
10470static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010471smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010472{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010473 struct http_msg *msg;
10474
10475 CHECK_HTTP_MESSAGE_FIRST();
10476
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010477 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010478 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010479 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010480 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010481
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010482 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010483 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010484
10485 smp->flags = SMP_F_VOL_TEST;
10486 return 1;
10487}
10488
10489
10490/* returns the advertised length of the body, or the advertised size of the
10491 * chunks available in the buffer. This requires that the body has been waited
10492 * for using http-buffer-request.
10493 */
10494static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010495smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010496{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010497 struct http_msg *msg;
10498
10499 CHECK_HTTP_MESSAGE_FIRST();
10500
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010501 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010502 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010503 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010504 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010505
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010506 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010507 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010508
10509 smp->flags = SMP_F_VOL_TEST;
10510 return 1;
10511}
10512
10513
Willy Tarreau8797c062007-05-07 00:55:35 +020010514/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010515static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010516smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010517{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010518 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010519
Willy Tarreauc0239e02012-04-16 14:42:55 +020010520 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010521
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010522 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010523 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010524 smp->data.u.str.len = txn->req.sl.rq.u_l;
10525 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010526 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010527 return 1;
10528}
10529
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010530static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010531smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010532{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010533 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010534 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010535
Willy Tarreauc0239e02012-04-16 14:42:55 +020010536 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010537
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010538 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010539 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 +020010540 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010541 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010542
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010543 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010544 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010545 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010546 return 1;
10547}
10548
10549static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010550smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010551{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010552 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010553 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010554
Willy Tarreauc0239e02012-04-16 14:42:55 +020010555 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010556
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010557 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010558 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 +020010559 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10560 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010561
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010562 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010563 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010564 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010565 return 1;
10566}
10567
Willy Tarreau185b5c42012-04-26 15:11:51 +020010568/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10569 * Accepts an optional argument of type string containing the header field name,
10570 * and an optional argument of type signed or unsigned integer to request an
10571 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010572 * headers are considered from the first one. It does not stop on commas and
10573 * returns full lines instead (useful for User-Agent or Date for example).
10574 */
10575static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010576smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010577{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010578 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010579 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010580 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010581 int occ = 0;
10582 const char *name_str = NULL;
10583 int name_len = 0;
10584
10585 if (!ctx) {
10586 /* first call */
10587 ctx = &static_hdr_ctx;
10588 ctx->idx = 0;
10589 smp->ctx.a[0] = ctx;
10590 }
10591
10592 if (args) {
10593 if (args[0].type != ARGT_STR)
10594 return 0;
10595 name_str = args[0].data.str.str;
10596 name_len = args[0].data.str.len;
10597
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010598 if (args[1].type == ARGT_SINT)
10599 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010600 }
10601
10602 CHECK_HTTP_MESSAGE_FIRST();
10603
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010604 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010605 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 +020010606
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010607 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10608 /* search for header from the beginning */
10609 ctx->idx = 0;
10610
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010611 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010612 /* no explicit occurrence and single fetch => last header by default */
10613 occ = -1;
10614
10615 if (!occ)
10616 /* prepare to report multiple occurrences for ACL fetches */
10617 smp->flags |= SMP_F_NOT_LAST;
10618
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010619 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010620 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010621 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 +020010622 return 1;
10623
10624 smp->flags &= ~SMP_F_NOT_LAST;
10625 return 0;
10626}
10627
10628/* 6. Check on HTTP header count. The number of occurrences is returned.
10629 * Accepts exactly 1 argument of type string. It does not stop on commas and
10630 * returns full lines instead (useful for User-Agent or Date for example).
10631 */
10632static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010633smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010634{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010635 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010636 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010637 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010638 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010639 const char *name = NULL;
10640 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010641
Willy Tarreau601a4d12015-04-01 19:16:09 +020010642 if (args && args->type == ARGT_STR) {
10643 name = args->data.str.str;
10644 len = args->data.str.len;
10645 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010646
10647 CHECK_HTTP_MESSAGE_FIRST();
10648
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010649 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010650 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 +020010651
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010652 ctx.idx = 0;
10653 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010654 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010655 cnt++;
10656
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010657 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010658 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010659 smp->flags = SMP_F_VOL_HDR;
10660 return 1;
10661}
10662
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010663static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010664smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010665{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010666 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010667 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010668 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010669 struct chunk *temp;
10670 char del = ',';
10671
10672 if (args && args->type == ARGT_STR)
10673 del = *args[0].data.str.str;
10674
10675 CHECK_HTTP_MESSAGE_FIRST();
10676
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010677 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010678 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 +020010679
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010680 temp = get_trash_chunk();
10681
10682 ctx.idx = 0;
10683 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10684 if (temp->len)
10685 temp->str[temp->len++] = del;
10686 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10687 temp->len += ctx.del;
10688 }
10689
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010690 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010691 smp->data.u.str.str = temp->str;
10692 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010693 smp->flags = SMP_F_VOL_HDR;
10694 return 1;
10695}
10696
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010697/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10698 * Accepts an optional argument of type string containing the header field name,
10699 * and an optional argument of type signed or unsigned integer to request an
10700 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010701 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010702 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010703static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010704smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010705{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010706 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010707 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010708 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010709 int occ = 0;
10710 const char *name_str = NULL;
10711 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010712
Willy Tarreaua890d072013-04-02 12:01:06 +020010713 if (!ctx) {
10714 /* first call */
10715 ctx = &static_hdr_ctx;
10716 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010717 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010718 }
10719
Willy Tarreau185b5c42012-04-26 15:11:51 +020010720 if (args) {
10721 if (args[0].type != ARGT_STR)
10722 return 0;
10723 name_str = args[0].data.str.str;
10724 name_len = args[0].data.str.len;
10725
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010726 if (args[1].type == ARGT_SINT)
10727 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010728 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010729
Willy Tarreaue333ec92012-04-16 16:26:40 +020010730 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010731
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010732 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010733 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 +020010734
Willy Tarreau185b5c42012-04-26 15:11:51 +020010735 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010736 /* search for header from the beginning */
10737 ctx->idx = 0;
10738
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010739 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010740 /* no explicit occurrence and single fetch => last header by default */
10741 occ = -1;
10742
10743 if (!occ)
10744 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010745 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010746
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010747 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010748 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010749 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 +020010750 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010751
Willy Tarreau37406352012-04-23 16:16:37 +020010752 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010753 return 0;
10754}
10755
Willy Tarreauc11416f2007-06-17 16:58:38 +020010756/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010757 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010758 */
10759static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010760smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010761{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010762 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010763 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010764 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010765 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010766 const char *name = NULL;
10767 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010768
Willy Tarreau601a4d12015-04-01 19:16:09 +020010769 if (args && args->type == ARGT_STR) {
10770 name = args->data.str.str;
10771 len = args->data.str.len;
10772 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010773
Willy Tarreaue333ec92012-04-16 16:26:40 +020010774 CHECK_HTTP_MESSAGE_FIRST();
10775
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010776 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010777 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 +020010778
Willy Tarreau33a7e692007-06-10 19:45:56 +020010779 ctx.idx = 0;
10780 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010781 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010782 cnt++;
10783
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010784 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010785 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010786 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010787 return 1;
10788}
10789
Willy Tarreau185b5c42012-04-26 15:11:51 +020010790/* Fetch an HTTP header's integer value. The integer value is returned. It
10791 * takes a mandatory argument of type string and an optional one of type int
10792 * to designate a specific occurrence. It returns an unsigned integer, which
10793 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010794 */
10795static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010796smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010797{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010798 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010799
Willy Tarreauf853c462012-04-23 18:53:56 +020010800 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010801 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010802 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010803 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010804
Willy Tarreaud53e2422012-04-16 17:21:11 +020010805 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010806}
10807
Cyril Bonté69fa9922012-10-25 00:01:06 +020010808/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10809 * and an optional one of type int to designate a specific occurrence.
10810 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010811 */
10812static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010813smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010814{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010815 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010816
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010817 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010818 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010819 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010820 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010821 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010822 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010823 if (smp->data.u.str.len < temp->size - 1) {
10824 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10825 temp->str[smp->data.u.str.len] = '\0';
10826 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010827 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010828 break;
10829 }
10830 }
10831 }
10832
Willy Tarreaud53e2422012-04-16 17:21:11 +020010833 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010834 if (!(smp->flags & SMP_F_NOT_LAST))
10835 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010836 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010837 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010838}
10839
Willy Tarreau737b0c12007-06-10 21:28:46 +020010840/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10841 * the first '/' after the possible hostname, and ends before the possible '?'.
10842 */
10843static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010844smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010845{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010846 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010847 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010848
Willy Tarreauc0239e02012-04-16 14:42:55 +020010849 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010850
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010851 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010852 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010853 ptr = http_get_path(txn);
10854 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010855 return 0;
10856
10857 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010858 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010859 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010860
10861 while (ptr < end && *ptr != '?')
10862 ptr++;
10863
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010864 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010865 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010866 return 1;
10867}
10868
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010869/* This produces a concatenation of the first occurrence of the Host header
10870 * followed by the path component if it begins with a slash ('/'). This means
10871 * that '*' will not be added, resulting in exactly the first Host entry.
10872 * If no Host header is found, then the path is returned as-is. The returned
10873 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010874 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010875 */
10876static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010877smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010878{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010879 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010880 char *ptr, *end, *beg;
10881 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010882 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010883
10884 CHECK_HTTP_MESSAGE_FIRST();
10885
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010886 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010887 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010888 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010889 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010890
10891 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010892 temp = get_trash_chunk();
10893 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010894 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010895 smp->data.u.str.str = temp->str;
10896 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010897
10898 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010899 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010900 beg = http_get_path(txn);
10901 if (!beg)
10902 beg = end;
10903
10904 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10905
10906 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010907 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10908 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010909 }
10910
10911 smp->flags = SMP_F_VOL_1ST;
10912 return 1;
10913}
10914
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010915/* This produces a 32-bit hash of the concatenation of the first occurrence of
10916 * the Host header followed by the path component if it begins with a slash ('/').
10917 * This means that '*' will not be added, resulting in exactly the first Host
10918 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010919 * is hashed using the path hash followed by a full avalanche hash and provides a
10920 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010921 * high-traffic sites without having to store whole paths.
10922 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010923int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010924smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010925{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010926 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010927 struct hdr_ctx ctx;
10928 unsigned int hash = 0;
10929 char *ptr, *beg, *end;
10930 int len;
10931
10932 CHECK_HTTP_MESSAGE_FIRST();
10933
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010934 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010935 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010936 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010937 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10938 ptr = ctx.line + ctx.val;
10939 len = ctx.vlen;
10940 while (len--)
10941 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10942 }
10943
10944 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010945 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010946 beg = http_get_path(txn);
10947 if (!beg)
10948 beg = end;
10949
10950 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10951
10952 if (beg < ptr && *beg == '/') {
10953 while (beg < ptr)
10954 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10955 }
10956 hash = full_hash(hash);
10957
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010958 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010959 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010960 smp->flags = SMP_F_VOL_1ST;
10961 return 1;
10962}
10963
Willy Tarreau4a550602012-12-09 14:53:32 +010010964/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010965 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10966 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10967 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010968 * that in environments where IPv6 is insignificant, truncating the output to
10969 * 8 bytes would still work.
10970 */
10971static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010972smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010973{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010974 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010975 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010976
10977 if (!cli_conn)
10978 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010979
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010980 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010981 return 0;
10982
Willy Tarreau47ca5452012-12-23 20:22:19 +010010983 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010984 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010985 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010986
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010987 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010988 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010989 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010990 temp->len += 4;
10991 break;
10992 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010993 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010994 temp->len += 16;
10995 break;
10996 default:
10997 return 0;
10998 }
10999
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011000 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011001 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010011002 return 1;
11003}
11004
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011005/* Extracts the query string, which comes after the question mark '?'. If no
11006 * question mark is found, nothing is returned. Otherwise it returns a sample
11007 * of type string carrying the whole query string.
11008 */
11009static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011010smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011011{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011012 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011013 char *ptr, *end;
11014
11015 CHECK_HTTP_MESSAGE_FIRST();
11016
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011017 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011018 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
11019 end = ptr + txn->req.sl.rq.u_l;
11020
11021 /* look up the '?' */
11022 do {
11023 if (ptr == end)
11024 return 0;
11025 } while (*ptr++ != '?');
11026
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011027 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011028 smp->data.u.str.str = ptr;
11029 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011030 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11031 return 1;
11032}
11033
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011034static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011035smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011036{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011037 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
11038 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
11039 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011040
Willy Tarreau24e32d82012-04-23 23:55:44 +020011041 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011042
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011043 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011044 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011045 return 1;
11046}
11047
Willy Tarreau7f18e522010-10-22 20:04:13 +020011048/* return a valid test if the current request is the first one on the connection */
11049static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011050smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020011051{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011052 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011053 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020011054 return 1;
11055}
11056
Willy Tarreau34db1082012-04-19 17:16:54 +020011057/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011058static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011059smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011060{
11061
Willy Tarreau24e32d82012-04-23 23:55:44 +020011062 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011063 return 0;
11064
Willy Tarreauc0239e02012-04-16 14:42:55 +020011065 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011066
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011067 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011068 return 0;
11069
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011070 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011071 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011072 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011073 return 1;
11074}
Willy Tarreau8797c062007-05-07 00:55:35 +020011075
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011076/* Accepts exactly 1 argument of type userlist */
11077static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011078smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011079{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011080 if (!args || args->type != ARGT_USR)
11081 return 0;
11082
11083 CHECK_HTTP_MESSAGE_FIRST();
11084
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011085 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011086 return 0;
11087
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011088 /* if the user does not belong to the userlist or has a wrong password,
11089 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010011090 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011091 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011092 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
11093 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010011094 return 0;
11095
11096 /* pat_match_auth() will need the user list */
11097 smp->ctx.a[0] = args->data.usr;
11098
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011099 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011100 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011101 smp->data.u.str.str = smp->strm->txn->auth.user;
11102 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011103
11104 return 1;
11105}
11106
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011107/* Try to find the next occurrence of a cookie name in a cookie header value.
11108 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
11109 * the cookie value is returned into *value and *value_l, and the function
11110 * returns a pointer to the next pointer to search from if the value was found.
11111 * Otherwise if the cookie was not found, NULL is returned and neither value
11112 * nor value_l are touched. The input <hdr> string should first point to the
11113 * header's value, and the <hdr_end> pointer must point to the first character
11114 * not part of the value. <list> must be non-zero if value may represent a list
11115 * of values (cookie headers). This makes it faster to abort parsing when no
11116 * list is expected.
11117 */
David Carlier4686f792015-09-25 14:10:50 +010011118char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011119extract_cookie_value(char *hdr, const char *hdr_end,
11120 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020011121 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011122{
11123 char *equal, *att_end, *att_beg, *val_beg, *val_end;
11124 char *next;
11125
11126 /* we search at least a cookie name followed by an equal, and more
11127 * generally something like this :
11128 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
11129 */
11130 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
11131 /* Iterate through all cookies on this line */
11132
Willy Tarreau2235b262016-11-05 15:50:20 +010011133 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011134 att_beg++;
11135
11136 /* find att_end : this is the first character after the last non
11137 * space before the equal. It may be equal to hdr_end.
11138 */
11139 equal = att_end = att_beg;
11140
11141 while (equal < hdr_end) {
11142 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
11143 break;
Willy Tarreau2235b262016-11-05 15:50:20 +010011144 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011145 continue;
11146 att_end = equal;
11147 }
11148
11149 /* here, <equal> points to '=', a delimitor or the end. <att_end>
11150 * is between <att_beg> and <equal>, both may be identical.
11151 */
11152
11153 /* look for end of cookie if there is an equal sign */
11154 if (equal < hdr_end && *equal == '=') {
11155 /* look for the beginning of the value */
11156 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +010011157 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011158 val_beg++;
11159
11160 /* find the end of the value, respecting quotes */
11161 next = find_cookie_value_end(val_beg, hdr_end);
11162
11163 /* make val_end point to the first white space or delimitor after the value */
11164 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +010011165 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011166 val_end--;
11167 } else {
11168 val_beg = val_end = next = equal;
11169 }
11170
11171 /* We have nothing to do with attributes beginning with '$'. However,
11172 * they will automatically be removed if a header before them is removed,
11173 * since they're supposed to be linked together.
11174 */
11175 if (*att_beg == '$')
11176 continue;
11177
11178 /* Ignore cookies with no equal sign */
11179 if (equal == next)
11180 continue;
11181
11182 /* Now we have the cookie name between att_beg and att_end, and
11183 * its value between val_beg and val_end.
11184 */
11185
11186 if (att_end - att_beg == cookie_name_l &&
11187 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
11188 /* let's return this value and indicate where to go on from */
11189 *value = val_beg;
11190 *value_l = val_end - val_beg;
11191 return next + 1;
11192 }
11193
11194 /* Set-Cookie headers only have the name in the first attr=value part */
11195 if (!list)
11196 break;
11197 }
11198
11199 return NULL;
11200}
11201
William Lallemanda43ba4e2014-01-28 18:14:25 +010011202/* Fetch a captured HTTP request header. The index is the position of
11203 * the "capture" option in the configuration file
11204 */
11205static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011206smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011207{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011208 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011209 int idx;
11210
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011211 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011212 return 0;
11213
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011214 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011215
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011216 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 +010011217 return 0;
11218
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011219 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011220 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011221 smp->data.u.str.str = smp->strm->req_cap[idx];
11222 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011223
11224 return 1;
11225}
11226
11227/* Fetch a captured HTTP response header. The index is the position of
11228 * the "capture" option in the configuration file
11229 */
11230static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011231smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011232{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011233 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011234 int idx;
11235
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011236 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011237 return 0;
11238
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011239 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011240
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011241 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 +010011242 return 0;
11243
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011244 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011245 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011246 smp->data.u.str.str = smp->strm->res_cap[idx];
11247 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011248
11249 return 1;
11250}
11251
William Lallemand65ad6e12014-01-31 15:08:02 +010011252/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
11253static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011254smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011255{
11256 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011257 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010011258 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011259
Willy Tarreau15e91e12015-04-04 00:52:09 +020011260 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011261 return 0;
11262
William Lallemand96a77852014-02-05 00:30:02 +010011263 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011264
William Lallemand96a77852014-02-05 00:30:02 +010011265 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11266 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011267
William Lallemand96a77852014-02-05 00:30:02 +010011268 temp = get_trash_chunk();
11269 temp->str = txn->uri;
11270 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011271 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011272 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011273 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011274
11275 return 1;
11276
11277}
11278
11279/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
11280static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011281smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011282{
11283 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011284 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010011285 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011286
Willy Tarreau15e91e12015-04-04 00:52:09 +020011287 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011288 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010011289
William Lallemand65ad6e12014-01-31 15:08:02 +010011290 ptr = txn->uri;
11291
11292 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11293 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010011294
William Lallemand65ad6e12014-01-31 15:08:02 +010011295 if (!*ptr)
11296 return 0;
11297
11298 ptr++; /* skip the space */
11299
11300 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010011301 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010011302 if (!ptr)
11303 return 0;
11304 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
11305 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011306
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011307 smp->data.u.str = *temp;
11308 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011309 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011310 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011311
11312 return 1;
11313}
11314
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011315/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
11316 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11317 */
11318static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011319smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011320{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011321 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011322
Willy Tarreau15e91e12015-04-04 00:52:09 +020011323 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011324 return 0;
11325
11326 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011327 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011328 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011329 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011330
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011331 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011332 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011333 smp->flags = SMP_F_CONST;
11334 return 1;
11335
11336}
11337
11338/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
11339 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11340 */
11341static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011342smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011343{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011344 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011345
Willy Tarreau15e91e12015-04-04 00:52:09 +020011346 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011347 return 0;
11348
11349 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011350 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011351 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011352 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011353
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011354 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011355 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011356 smp->flags = SMP_F_CONST;
11357 return 1;
11358
11359}
11360
William Lallemand65ad6e12014-01-31 15:08:02 +010011361
Willy Tarreaue333ec92012-04-16 16:26:40 +020011362/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011363 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011364 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011365 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011366 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011367 * Accepts exactly 1 argument of type string. If the input options indicate
11368 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011369 * The returned sample is of type CSTR. Can be used to parse cookies in other
11370 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011371 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011372int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011373{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011374 struct http_txn *txn;
11375 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011376 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011377 const struct http_msg *msg;
11378 const char *hdr_name;
11379 int hdr_name_len;
11380 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011381 int occ = 0;
11382 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011383
Willy Tarreau24e32d82012-04-23 23:55:44 +020011384 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011385 return 0;
11386
Willy Tarreaua890d072013-04-02 12:01:06 +020011387 if (!ctx) {
11388 /* first call */
11389 ctx = &static_hdr_ctx;
11390 ctx->idx = 0;
11391 smp->ctx.a[2] = ctx;
11392 }
11393
Willy Tarreaue333ec92012-04-16 16:26:40 +020011394 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011395
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011396 txn = smp->strm->txn;
11397 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011398
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011399 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011400 msg = &txn->req;
11401 hdr_name = "Cookie";
11402 hdr_name_len = 6;
11403 } else {
11404 msg = &txn->rsp;
11405 hdr_name = "Set-Cookie";
11406 hdr_name_len = 10;
11407 }
11408
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011409 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020011410 /* no explicit occurrence and single fetch => last cookie by default */
11411 occ = -1;
11412
11413 /* OK so basically here, either we want only one value and it's the
11414 * last one, or we want to iterate over all of them and we fetch the
11415 * next one.
11416 */
11417
Willy Tarreau9b28e032012-10-12 23:49:43 +020011418 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011419 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011420 /* search for the header from the beginning, we must first initialize
11421 * the search parameters.
11422 */
Willy Tarreau37406352012-04-23 16:16:37 +020011423 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011424 ctx->idx = 0;
11425 }
11426
Willy Tarreau28376d62012-04-26 21:26:10 +020011427 smp->flags |= SMP_F_VOL_HDR;
11428
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011429 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011430 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11431 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011432 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11433 goto out;
11434
Willy Tarreau24e32d82012-04-23 23:55:44 +020011435 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011436 continue;
11437
Willy Tarreau37406352012-04-23 16:16:37 +020011438 smp->ctx.a[0] = ctx->line + ctx->val;
11439 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011440 }
11441
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011442 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011443 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011444 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011445 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011446 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011447 &smp->data.u.str.str,
11448 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011449 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011450 found = 1;
11451 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011452 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020011453 smp->flags |= SMP_F_NOT_LAST;
11454 return 1;
11455 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011456 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011457 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011458 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011459 /* all cookie headers and values were scanned. If we're looking for the
11460 * last occurrence, we may return it now.
11461 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011462 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011463 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011464 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011465}
11466
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011467/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011468 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011469 * multiple cookies may be parsed on the same line. The returned sample is of
11470 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011471 */
11472static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011473smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011474{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011475 struct http_txn *txn;
11476 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011477 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011478 const struct http_msg *msg;
11479 const char *hdr_name;
11480 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011481 int cnt;
11482 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011483 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011484
Willy Tarreau24e32d82012-04-23 23:55:44 +020011485 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011486 return 0;
11487
Willy Tarreaue333ec92012-04-16 16:26:40 +020011488 CHECK_HTTP_MESSAGE_FIRST();
11489
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011490 txn = smp->strm->txn;
11491 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011492
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011493 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011494 msg = &txn->req;
11495 hdr_name = "Cookie";
11496 hdr_name_len = 6;
11497 } else {
11498 msg = &txn->rsp;
11499 hdr_name = "Set-Cookie";
11500 hdr_name_len = 10;
11501 }
11502
Willy Tarreau9b28e032012-10-12 23:49:43 +020011503 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011504 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011505 ctx.idx = 0;
11506 cnt = 0;
11507
11508 while (1) {
11509 /* Note: val_beg == NULL every time we need to fetch a new header */
11510 if (!val_beg) {
11511 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11512 break;
11513
Willy Tarreau24e32d82012-04-23 23:55:44 +020011514 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011515 continue;
11516
11517 val_beg = ctx.line + ctx.val;
11518 val_end = val_beg + ctx.vlen;
11519 }
11520
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011521 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011522 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011523 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011524 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011525 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011526 &smp->data.u.str.str,
11527 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011528 cnt++;
11529 }
11530 }
11531
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011532 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011533 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011534 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011535 return 1;
11536}
11537
Willy Tarreau51539362012-05-08 12:46:28 +020011538/* Fetch an cookie's integer value. The integer value is returned. It
11539 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11540 */
11541static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011542smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011543{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011544 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011545
11546 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011547 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011548 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020011549 }
11550
11551 return ret;
11552}
11553
Willy Tarreau8797c062007-05-07 00:55:35 +020011554/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011555/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011556/************************************************************************/
11557
David Cournapeau16023ee2010-12-23 20:55:41 +090011558/*
11559 * Given a path string and its length, find the position of beginning of the
11560 * query string. Returns NULL if no query string is found in the path.
11561 *
11562 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11563 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011564 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011565 */
bedis4c75cca2012-10-05 08:38:24 +020011566static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011567{
11568 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011569
bedis4c75cca2012-10-05 08:38:24 +020011570 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011571 return p ? p + 1 : NULL;
11572}
11573
bedis4c75cca2012-10-05 08:38:24 +020011574static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011575{
bedis4c75cca2012-10-05 08:38:24 +020011576 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011577}
11578
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011579/* after increasing a pointer value, it can exceed the first buffer
11580 * size. This function transform the value of <ptr> according with
11581 * the expected position. <chunks> is an array of the one or two
11582 * avalaible chunks. The first value is the start of the first chunk,
11583 * the second value if the end+1 of the first chunks. The third value
11584 * is NULL or the start of the second chunk and the fourth value is
11585 * the end+1 of the second chunk. The function returns 1 if does a
11586 * wrap, else returns 0.
11587 */
11588static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11589{
11590 if (*ptr < chunks[1])
11591 return 0;
11592 if (!chunks[2])
11593 return 0;
11594 *ptr = chunks[2] + ( *ptr - chunks[1] );
11595 return 1;
11596}
11597
David Cournapeau16023ee2010-12-23 20:55:41 +090011598/*
11599 * Given a url parameter, find the starting position of the first occurence,
11600 * or NULL if the parameter is not found.
11601 *
11602 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11603 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011604 *
Willy Tarreauf6625822015-12-27 14:51:01 +010011605 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011606 * or the second chunk. The caller must be check the position before using the
11607 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011608 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011609static const char *
11610find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011611 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011612 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011613{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011614 const char *pos, *last, *equal;
11615 const char **bufs = chunks;
11616 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011617
David Cournapeau16023ee2010-12-23 20:55:41 +090011618
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011619 pos = bufs[0];
11620 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010011621 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011622 /* Check the equal. */
11623 equal = pos + url_param_name_l;
11624 if (fix_pointer_if_wrap(chunks, &equal)) {
11625 if (equal >= chunks[3])
11626 return NULL;
11627 } else {
11628 if (equal >= chunks[1])
11629 return NULL;
11630 }
11631 if (*equal == '=') {
11632 if (pos + url_param_name_l > last) {
11633 /* process wrap case, we detect a wrap. In this case, the
11634 * comparison is performed in two parts.
11635 */
11636
11637 /* This is the end, we dont have any other chunk. */
11638 if (bufs != chunks || !bufs[2])
11639 return NULL;
11640
11641 /* Compute the length of each part of the comparison. */
11642 l1 = last - pos;
11643 l2 = url_param_name_l - l1;
11644
11645 /* The second buffer is too short to contain the compared string. */
11646 if (bufs[2] + l2 > bufs[3])
11647 return NULL;
11648
11649 if (memcmp(pos, url_param_name, l1) == 0 &&
11650 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11651 return pos;
11652
11653 /* Perform wrapping and jump the string who fail the comparison. */
11654 bufs += 2;
11655 pos = bufs[0] + l2;
11656 last = bufs[1];
11657
11658 } else {
11659 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011660 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11661 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011662 pos += url_param_name_l + 1;
11663 if (fix_pointer_if_wrap(chunks, &pos))
11664 last = bufs[2];
11665 }
11666 }
11667
11668 while (1) {
11669 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011670 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011671 pos++;
11672 if (pos < last)
11673 break;
11674 /* process buffer wrapping. */
11675 if (bufs != chunks || !bufs[2])
11676 return NULL;
11677 bufs += 2;
11678 pos = bufs[0];
11679 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011680 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011681 pos++;
11682 }
11683 return NULL;
11684}
11685
11686/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011687 * Given a url parameter name and a query string, find the next value.
11688 * An empty url_param_name matches the first available parameter.
11689 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
11690 * respectively provide a pointer to the value and its end.
11691 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090011692 */
11693static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011694find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011695 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011696 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011697{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011698 const char *arg_start, *qs_end;
11699 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011700
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011701 arg_start = chunks[0];
11702 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011703 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011704 /* Looks for an argument name. */
11705 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011706 url_param_name, url_param_name_l,
11707 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011708 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011709 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011710 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011711 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011712 if (!arg_start)
11713 return 0;
11714
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011715 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011716 while (1) {
11717 /* looks for the first argument. */
11718 value_start = memchr(arg_start, '=', qs_end - arg_start);
11719 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011720 /* Check for wrapping. */
11721 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011722 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011723 chunks[2]) {
11724 arg_start = chunks[2];
11725 qs_end = chunks[3];
11726 continue;
11727 }
11728 return 0;
11729 }
11730 break;
11731 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011732 value_start++;
11733 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011734 else {
11735 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011736 value_start = arg_start + url_param_name_l + 1;
11737
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011738 /* Check for pointer wrapping. */
11739 if (fix_pointer_if_wrap(chunks, &value_start)) {
11740 /* Update the end pointer. */
11741 qs_end = chunks[3];
11742
11743 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011744 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011745 return 0;
11746 }
11747 }
11748
David Cournapeau16023ee2010-12-23 20:55:41 +090011749 value_end = value_start;
11750
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011751 while (1) {
11752 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11753 value_end++;
11754 if (value_end < qs_end)
11755 break;
11756 /* process buffer wrapping. */
11757 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011758 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011759 chunks[2]) {
11760 value_end = chunks[2];
11761 qs_end = chunks[3];
11762 continue;
11763 }
11764 break;
11765 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011766
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011767 *vstart = value_start;
11768 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011769 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011770}
11771
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011772/* This scans a URL-encoded query string. It takes an optionally wrapping
11773 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11774 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11775 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011776 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011777static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011778smp_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 +090011779{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011780 const char *vstart, *vend;
11781 struct chunk *temp;
11782 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011783
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011784 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011785 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011786 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011787 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011788 return 0;
11789
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011790 /* Create sample. If the value is contiguous, return the pointer as CONST,
11791 * if the value is wrapped, copy-it in a buffer.
11792 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011793 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011794 if (chunks[2] &&
11795 vstart >= chunks[0] && vstart <= chunks[1] &&
11796 vend >= chunks[2] && vend <= chunks[3]) {
11797 /* Wrapped case. */
11798 temp = get_trash_chunk();
11799 memcpy(temp->str, vstart, chunks[1] - vstart);
11800 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011801 smp->data.u.str.str = temp->str;
11802 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011803 } else {
11804 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011805 smp->data.u.str.str = (char *)vstart;
11806 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011807 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11808 }
11809
11810 /* Update context, check wrapping. */
11811 chunks[0] = vend;
11812 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11813 chunks[1] = chunks[3];
11814 chunks[2] = NULL;
11815 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011816
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011817 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011818 smp->flags |= SMP_F_NOT_LAST;
11819
David Cournapeau16023ee2010-12-23 20:55:41 +090011820 return 1;
11821}
11822
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011823/* This function iterates over each parameter of the query string. It uses
11824 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011825 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11826 * An optional parameter name is passed in args[0], otherwise any parameter is
11827 * considered. It supports an optional delimiter argument for the beginning of
11828 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011829 */
11830static int
11831smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11832{
11833 struct http_msg *msg;
11834 char delim = '?';
11835 const char *name;
11836 int name_len;
11837
Dragan Dosen26f77e52015-05-25 10:02:11 +020011838 if (!args ||
11839 (args[0].type && args[0].type != ARGT_STR) ||
11840 (args[1].type && args[1].type != ARGT_STR))
11841 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011842
Dragan Dosen26f77e52015-05-25 10:02:11 +020011843 name = "";
11844 name_len = 0;
11845 if (args->type == ARGT_STR) {
11846 name = args->data.str.str;
11847 name_len = args->data.str.len;
11848 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011849
Dragan Dosen26f77e52015-05-25 10:02:11 +020011850 if (args[1].type)
11851 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011852
Dragan Dosen26f77e52015-05-25 10:02:11 +020011853 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011854 CHECK_HTTP_MESSAGE_FIRST();
11855
11856 msg = &smp->strm->txn->req;
11857
11858 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11859 msg->sl.rq.u_l, delim);
11860 if (!smp->ctx.a[0])
11861 return 0;
11862
11863 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011864
11865 /* Assume that the context is filled with NULL pointer
11866 * before the first call.
11867 * smp->ctx.a[2] = NULL;
11868 * smp->ctx.a[3] = NULL;
11869 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011870 }
11871
11872 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11873}
11874
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011875/* This function iterates over each parameter of the body. This requires
11876 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011877 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11878 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11879 * optional second part if the body wraps at the end of the buffer. An optional
11880 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011881 */
11882static int
11883smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11884{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011885 struct http_msg *msg;
11886 unsigned long len;
11887 unsigned long block1;
11888 char *body;
11889 const char *name;
11890 int name_len;
11891
11892 if (!args || (args[0].type && args[0].type != ARGT_STR))
11893 return 0;
11894
11895 name = "";
11896 name_len = 0;
11897 if (args[0].type == ARGT_STR) {
11898 name = args[0].data.str.str;
11899 name_len = args[0].data.str.len;
11900 }
11901
11902 if (!smp->ctx.a[0]) { // first call, find the query string
11903 CHECK_HTTP_MESSAGE_FIRST();
11904
11905 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010011906 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011907 else
Willy Tarreaube508f12016-03-10 11:47:01 +010011908 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011909
11910 len = http_body_bytes(msg);
11911 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11912
11913 block1 = len;
11914 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11915 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11916
11917 if (block1 == len) {
11918 /* buffer is not wrapped (or empty) */
11919 smp->ctx.a[0] = body;
11920 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011921
11922 /* Assume that the context is filled with NULL pointer
11923 * before the first call.
11924 * smp->ctx.a[2] = NULL;
11925 * smp->ctx.a[3] = NULL;
11926 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011927 }
11928 else {
11929 /* buffer is wrapped, we need to defragment it */
11930 smp->ctx.a[0] = body;
11931 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011932 smp->ctx.a[2] = msg->chn->buf->data;
11933 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011934 }
11935 }
11936 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11937}
11938
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011939/* Return the signed integer value for the specified url parameter (see url_param
11940 * above).
11941 */
11942static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011943smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011944{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011945 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011946
11947 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011948 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011949 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011950 }
11951
11952 return ret;
11953}
11954
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011955/* This produces a 32-bit hash of the concatenation of the first occurrence of
11956 * the Host header followed by the path component if it begins with a slash ('/').
11957 * This means that '*' will not be added, resulting in exactly the first Host
11958 * entry. If no Host header is found, then the path is used. The resulting value
11959 * is hashed using the url hash followed by a full avalanche hash and provides a
11960 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11961 * high-traffic sites without having to store whole paths.
11962 * this differs from the base32 functions in that it includes the url parameters
11963 * as well as the path
11964 */
11965static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011966smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011967{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011968 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011969 struct hdr_ctx ctx;
11970 unsigned int hash = 0;
11971 char *ptr, *beg, *end;
11972 int len;
11973
11974 CHECK_HTTP_MESSAGE_FIRST();
11975
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011976 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011977 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011978 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011979 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11980 ptr = ctx.line + ctx.val;
11981 len = ctx.vlen;
11982 while (len--)
11983 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11984 }
11985
11986 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011987 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 +000011988 beg = http_get_path(txn);
11989 if (!beg)
11990 beg = end;
11991
11992 for (ptr = beg; ptr < end ; ptr++);
11993
11994 if (beg < ptr && *beg == '/') {
11995 while (beg < ptr)
11996 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11997 }
11998 hash = full_hash(hash);
11999
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012000 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012001 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012002 smp->flags = SMP_F_VOL_1ST;
12003 return 1;
12004}
12005
12006/* This concatenates the source address with the 32-bit hash of the Host and
12007 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
12008 * per-url counters. The result is a binary block from 8 to 20 bytes depending
12009 * on the source address length. The URL hash is stored before the address so
12010 * that in environments where IPv6 is insignificant, truncating the output to
12011 * 8 bytes would still work.
12012 */
12013static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020012014smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012015{
12016 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012017 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012018
Dragan Dosendb5af612016-06-16 11:23:01 +020012019 if (!cli_conn)
12020 return 0;
12021
Thierry FOURNIER0786d052015-05-11 15:42:45 +020012022 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012023 return 0;
12024
12025 temp = get_trash_chunk();
Dragan Dosene5f41332016-06-16 11:08:08 +020012026 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
12027 temp->len += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012028
Willy Tarreaub363a1f2013-10-01 10:45:07 +020012029 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012030 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020012031 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012032 temp->len += 4;
12033 break;
12034 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020012035 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012036 temp->len += 16;
12037 break;
12038 default:
12039 return 0;
12040 }
12041
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012042 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012043 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012044 return 1;
12045}
12046
Willy Tarreau185b5c42012-04-26 15:11:51 +020012047/* This function is used to validate the arguments passed to any "hdr" fetch
12048 * keyword. These keywords support an optional positive or negative occurrence
12049 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
12050 * is assumed that the types are already the correct ones. Returns 0 on error,
12051 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
12052 * error message in case of error, that the caller is responsible for freeing.
12053 * The initial location must either be freeable or NULL.
12054 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010012055int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020012056{
12057 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020012058 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020012059 return 0;
12060 }
12061 return 1;
12062}
12063
Willy Tarreau276fae92013-07-25 14:36:01 +020012064/* takes an UINT value on input supposed to represent the time since EPOCH,
12065 * adds an optional offset found in args[0] and emits a string representing
12066 * the date in RFC-1123/5322 format.
12067 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012068static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020012069{
Cyril Bontéf78d8962016-01-22 19:40:28 +010012070 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020012071 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
12072 struct chunk *temp;
12073 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012074 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012075 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020012076
12077 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012078 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020012079 curr_date += args[0].data.sint;
12080
12081 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020012082 if (!tm)
12083 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020012084
12085 temp = get_trash_chunk();
12086 temp->len = snprintf(temp->str, temp->size - temp->len,
12087 "%s, %02d %s %04d %02d:%02d:%02d GMT",
12088 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
12089 tm->tm_hour, tm->tm_min, tm->tm_sec);
12090
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012091 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012092 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020012093 return 1;
12094}
12095
Thierry FOURNIERad903512014-04-11 17:51:01 +020012096/* Match language range with language tag. RFC2616 14.4:
12097 *
12098 * A language-range matches a language-tag if it exactly equals
12099 * the tag, or if it exactly equals a prefix of the tag such
12100 * that the first tag character following the prefix is "-".
12101 *
12102 * Return 1 if the strings match, else return 0.
12103 */
12104static inline int language_range_match(const char *range, int range_len,
12105 const char *tag, int tag_len)
12106{
12107 const char *end = range + range_len;
12108 const char *tend = tag + tag_len;
12109 while (range < end) {
12110 if (*range == '-' && tag == tend)
12111 return 1;
12112 if (*range != *tag || tag == tend)
12113 return 0;
12114 range++;
12115 tag++;
12116 }
12117 /* Return true only if the last char of the tag is matched. */
12118 return tag == tend;
12119}
12120
12121/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012122static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020012123{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012124 const char *al = smp->data.u.str.str;
12125 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012126 const char *token;
12127 int toklen;
12128 int qvalue;
12129 const char *str;
12130 const char *w;
12131 int best_q = 0;
12132
12133 /* Set the constant to the sample, because the output of the
12134 * function will be peek in the constant configuration string.
12135 */
12136 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012137 smp->data.u.str.size = 0;
12138 smp->data.u.str.str = "";
12139 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012140
12141 /* Parse the accept language */
12142 while (1) {
12143
12144 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012145 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012146 al++;
12147 if (al >= end)
12148 break;
12149
12150 /* Start of the fisrt word. */
12151 token = al;
12152
12153 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012154 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012155 al++;
12156 if (al == token)
12157 goto expect_comma;
12158
12159 /* Length of the token. */
12160 toklen = al - token;
12161 qvalue = 1000;
12162
12163 /* Check if the token exists in the list. If the token not exists,
12164 * jump to the next token.
12165 */
12166 str = args[0].data.str.str;
12167 w = str;
12168 while (1) {
12169 if (*str == ';' || *str == '\0') {
12170 if (language_range_match(token, toklen, w, str-w))
12171 goto look_for_q;
12172 if (*str == '\0')
12173 goto expect_comma;
12174 w = str + 1;
12175 }
12176 str++;
12177 }
12178 goto expect_comma;
12179
12180look_for_q:
12181
12182 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012183 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012184 al++;
12185 if (al >= end)
12186 goto process_value;
12187
12188 /* If ',' is found, process the result */
12189 if (*al == ',')
12190 goto process_value;
12191
12192 /* If the character is different from ';', look
12193 * for the end of the header part in best effort.
12194 */
12195 if (*al != ';')
12196 goto expect_comma;
12197
12198 /* Assumes that the char is ';', now expect "q=". */
12199 al++;
12200
12201 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012202 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012203 al++;
12204 if (al >= end)
12205 goto process_value;
12206
12207 /* Expect 'q'. If no 'q', continue in best effort */
12208 if (*al != 'q')
12209 goto process_value;
12210 al++;
12211
12212 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012213 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012214 al++;
12215 if (al >= end)
12216 goto process_value;
12217
12218 /* Expect '='. If no '=', continue in best effort */
12219 if (*al != '=')
12220 goto process_value;
12221 al++;
12222
12223 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012224 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012225 al++;
12226 if (al >= end)
12227 goto process_value;
12228
12229 /* Parse the q value. */
12230 qvalue = parse_qvalue(al, &al);
12231
12232process_value:
12233
12234 /* If the new q value is the best q value, then store the associated
12235 * language in the response. If qvalue is the biggest value (1000),
12236 * break the process.
12237 */
12238 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012239 smp->data.u.str.str = (char *)w;
12240 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012241 if (qvalue >= 1000)
12242 break;
12243 best_q = qvalue;
12244 }
12245
12246expect_comma:
12247
12248 /* Expect comma or end. If the end is detected, quit the loop. */
12249 while (al < end && *al != ',')
12250 al++;
12251 if (al >= end)
12252 break;
12253
12254 /* Comma is found, jump it and restart the analyzer. */
12255 al++;
12256 }
12257
12258 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012259 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
12260 smp->data.u.str.str = args[1].data.str.str;
12261 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012262 }
12263
12264 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012265 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012266}
12267
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012268/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012269static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012270{
12271 /* If the constant flag is set or if not size is avalaible at
12272 * the end of the buffer, copy the string in other buffer
12273 * before decoding.
12274 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012275 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012276 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012277 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
12278 smp->data.u.str.str = str->str;
12279 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012280 smp->flags &= ~SMP_F_CONST;
12281 }
12282
12283 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012284 smp->data.u.str.str[smp->data.u.str.len] = '\0';
12285 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012286 return 1;
12287}
12288
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012289static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
12290{
12291 struct proxy *fe = strm_fe(smp->strm);
12292 int idx, i;
12293 struct cap_hdr *hdr;
12294 int len;
12295
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012296 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012297 return 0;
12298
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012299 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012300
12301 /* Check the availibity of the capture id. */
12302 if (idx > fe->nb_req_cap - 1)
12303 return 0;
12304
12305 /* Look for the original configuration. */
12306 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
12307 hdr != NULL && i != idx ;
12308 i--, hdr = hdr->next);
12309 if (!hdr)
12310 return 0;
12311
12312 /* check for the memory allocation */
12313 if (smp->strm->req_cap[hdr->index] == NULL)
12314 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
12315 if (smp->strm->req_cap[hdr->index] == NULL)
12316 return 0;
12317
12318 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012319 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012320 if (len > hdr->len)
12321 len = hdr->len;
12322
12323 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012324 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012325 smp->strm->req_cap[idx][len] = '\0';
12326
12327 return 1;
12328}
12329
12330static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
12331{
12332 struct proxy *fe = strm_fe(smp->strm);
12333 int idx, i;
12334 struct cap_hdr *hdr;
12335 int len;
12336
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012337 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012338 return 0;
12339
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012340 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012341
12342 /* Check the availibity of the capture id. */
12343 if (idx > fe->nb_rsp_cap - 1)
12344 return 0;
12345
12346 /* Look for the original configuration. */
12347 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
12348 hdr != NULL && i != idx ;
12349 i--, hdr = hdr->next);
12350 if (!hdr)
12351 return 0;
12352
12353 /* check for the memory allocation */
12354 if (smp->strm->res_cap[hdr->index] == NULL)
12355 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
12356 if (smp->strm->res_cap[hdr->index] == NULL)
12357 return 0;
12358
12359 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012360 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012361 if (len > hdr->len)
12362 len = hdr->len;
12363
12364 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012365 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012366 smp->strm->res_cap[idx][len] = '\0';
12367
12368 return 1;
12369}
12370
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012371/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012372 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012373 * the relevant part of the request line accordingly. Then it updates various
12374 * pointers to the next elements which were moved, and the total buffer length.
12375 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012376 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
12377 * error, though this can be revisited when this code is finally exploited.
12378 *
12379 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
12380 * query string and 3 to replace uri.
12381 *
12382 * In query string case, the mark question '?' must be set at the start of the
12383 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012384 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012385int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012386 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012387{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012388 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012389 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012390 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012391 int delta;
12392
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012393 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012394 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012395 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012396 cur_end = cur_ptr + txn->req.sl.rq.m_l;
12397
12398 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012399 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012400 txn->req.sl.rq.m_l += delta;
12401 txn->req.sl.rq.u += delta;
12402 txn->req.sl.rq.v += delta;
12403 break;
12404
12405 case 1: // path
12406 cur_ptr = http_get_path(txn);
12407 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012408 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012409
12410 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012411 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 +010012412 cur_end++;
12413
12414 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012415 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012416 txn->req.sl.rq.u_l += delta;
12417 txn->req.sl.rq.v += delta;
12418 break;
12419
12420 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012421 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012422 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012423 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12424 while (cur_ptr < cur_end && *cur_ptr != '?')
12425 cur_ptr++;
12426
12427 /* skip the question mark or indicate that we must insert it
12428 * (but only if the format string is not empty then).
12429 */
12430 if (cur_ptr < cur_end)
12431 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012432 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012433 offset = 0;
12434
12435 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012436 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012437 txn->req.sl.rq.u_l += delta;
12438 txn->req.sl.rq.v += delta;
12439 break;
12440
12441 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012442 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012443 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12444
12445 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012446 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012447 txn->req.sl.rq.u_l += delta;
12448 txn->req.sl.rq.v += delta;
12449 break;
12450
12451 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012452 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012453 }
12454
12455 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012456 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020012457 txn->req.sl.rq.l += delta;
12458 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012459 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012460 return 0;
12461}
12462
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012463/* This function replace the HTTP status code and the associated message. The
12464 * variable <status> contains the new status code. This function never fails.
12465 */
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012466void http_set_status(unsigned int status, const char *reason, struct stream *s)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012467{
12468 struct http_txn *txn = s->txn;
12469 char *cur_ptr, *cur_end;
12470 int delta;
12471 char *res;
12472 int c_l;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012473 const char *msg = reason;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012474 int msg_len;
12475
12476 chunk_reset(&trash);
12477
12478 res = ultoa_o(status, trash.str, trash.size);
12479 c_l = res - trash.str;
12480
12481 trash.str[c_l] = ' ';
12482 trash.len = c_l + 1;
12483
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012484 /* Do we have a custom reason format string? */
12485 if (msg == NULL)
12486 msg = get_reason(status);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012487 msg_len = strlen(msg);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012488 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
12489 trash.len += msg_len;
12490
12491 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
12492 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
12493
12494 /* commit changes and adjust message */
12495 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
12496
12497 /* adjust res line offsets and lengths */
12498 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
12499 txn->rsp.sl.st.c_l = c_l;
12500 txn->rsp.sl.st.r_l = msg_len;
12501
12502 delta = trash.len - (cur_end - cur_ptr);
12503 txn->rsp.sl.st.l += delta;
12504 txn->hdr_idx.v[0].len += delta;
12505 http_msg_move_end(&txn->rsp, delta);
12506}
12507
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012508/* This function executes one of the set-{method,path,query,uri} actions. It
12509 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012510 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012511 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012512 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
12513 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012514 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012515enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012516 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012517{
12518 chunk_reset(&trash);
12519
12520 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012521 if (rule->arg.http.action == 2)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012522 trash.str[trash.len++] = '?';
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012523 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012524
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012525 http_replace_req_line(rule->arg.http.action, trash.str, trash.len, px, s);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012526 return ACT_RET_CONT;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012527}
12528
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012529/* This function is just a compliant action wrapper for "set-status". */
12530enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012531 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012532{
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012533 http_set_status(rule->arg.status.code, rule->arg.status.reason, s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012534 return ACT_RET_CONT;
12535}
12536
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012537/* parse an http-request action among :
12538 * set-method
12539 * set-path
12540 * set-query
12541 * set-uri
12542 *
12543 * All of them accept a single argument of type string representing a log-format.
12544 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
12545 * 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 +020012546 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012547 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012548enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
12549 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012550{
12551 int cur_arg = *orig_arg;
12552
Thierry FOURNIER42148732015-09-02 17:17:33 +020012553 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012554
12555 switch (args[0][4]) {
12556 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012557 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012558 rule->action_ptr = http_action_set_req_line;
12559 break;
12560 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012561 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012562 rule->action_ptr = http_action_set_req_line;
12563 break;
12564 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012565 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012566 rule->action_ptr = http_action_set_req_line;
12567 break;
12568 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012569 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012570 rule->action_ptr = http_action_set_req_line;
12571 break;
12572 default:
12573 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012574 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012575 }
12576
12577 if (!*args[cur_arg] ||
12578 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12579 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012580 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012581 }
12582
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012583 LIST_INIT(&rule->arg.http.logfmt);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012584 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010012585 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +010012586 (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 +010012587 return ACT_RET_PRS_ERR;
12588 }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012589
12590 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012591 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012592}
12593
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012594/* parse set-status action:
12595 * This action accepts a single argument of type int representing
12596 * an http status code. It returns ACT_RET_PRS_OK on success,
12597 * ACT_RET_PRS_ERR on error.
12598 */
12599enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12600 struct act_rule *rule, char **err)
12601{
12602 char *error;
12603
Thierry FOURNIER42148732015-09-02 17:17:33 +020012604 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012605 rule->action_ptr = action_http_set_status;
12606
12607 /* Check if an argument is available */
12608 if (!*args[*orig_arg]) {
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012609 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012610 return ACT_RET_PRS_ERR;
12611 }
12612
12613 /* convert status code as integer */
12614 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12615 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12616 memprintf(err, "expects an integer status code between 100 and 999");
12617 return ACT_RET_PRS_ERR;
12618 }
12619
12620 (*orig_arg)++;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012621
12622 /* set custom reason string */
12623 rule->arg.status.reason = NULL; // If null, we use the default reason for the status code.
12624 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
12625 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
12626 (*orig_arg)++;
12627 rule->arg.status.reason = strdup(args[*orig_arg]);
12628 (*orig_arg)++;
12629 }
12630
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012631 return ACT_RET_PRS_OK;
12632}
12633
Willy Tarreaua9083d02015-05-08 15:27:59 +020012634/* This function executes the "capture" action. It executes a fetch expression,
12635 * turns the result into a string and puts it in a capture slot. It always
12636 * returns 1. If an error occurs the action is cancelled, but the rule
12637 * processing continues.
12638 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012639enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012640 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012641{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012642 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012643 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012644 char **cap = s->req_cap;
12645 int len;
12646
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012647 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 +020012648 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012649 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012650
12651 if (cap[h->index] == NULL)
12652 cap[h->index] = pool_alloc2(h->pool);
12653
12654 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012655 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012656
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012657 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012658 if (len > h->len)
12659 len = h->len;
12660
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012661 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012662 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012663 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012664}
12665
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012666/* This function executes the "capture" action and store the result in a
12667 * capture slot if exists. It executes a fetch expression, turns the result
12668 * into a string and puts it in a capture slot. It always returns 1. If an
12669 * error occurs the action is cancelled, but the rule processing continues.
12670 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012671enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012672 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012673{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012674 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012675 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012676 char **cap = s->req_cap;
12677 struct proxy *fe = strm_fe(s);
12678 int len;
12679 int i;
12680
12681 /* Look for the original configuration. */
12682 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012683 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012684 i--, h = h->next);
12685 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012686 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012687
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012688 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 +020012689 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012690 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012691
12692 if (cap[h->index] == NULL)
12693 cap[h->index] = pool_alloc2(h->pool);
12694
12695 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012696 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012697
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012698 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012699 if (len > h->len)
12700 len = h->len;
12701
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012702 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012703 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012704 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012705}
12706
Willy Tarreaua9083d02015-05-08 15:27:59 +020012707/* parse an "http-request capture" action. It takes a single argument which is
12708 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012709 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012710 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012711 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012712enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12713 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012714{
12715 struct sample_expr *expr;
12716 struct cap_hdr *hdr;
12717 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012718 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012719
12720 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12721 if (strcmp(args[cur_arg], "if") == 0 ||
12722 strcmp(args[cur_arg], "unless") == 0)
12723 break;
12724
12725 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012726 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012727 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012728 }
12729
Willy Tarreaua9083d02015-05-08 15:27:59 +020012730 cur_arg = *orig_arg;
12731 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12732 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012733 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012734
12735 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12736 memprintf(err,
12737 "fetch method '%s' extracts information from '%s', none of which is available here",
12738 args[cur_arg-1], sample_src_names(expr->fetch->use));
12739 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012740 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012741 }
12742
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012743 if (!args[cur_arg] || !*args[cur_arg]) {
12744 memprintf(err, "expects 'len or 'id'");
12745 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012746 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012747 }
12748
Willy Tarreaua9083d02015-05-08 15:27:59 +020012749 if (strcmp(args[cur_arg], "len") == 0) {
12750 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012751
12752 if (!(px->cap & PR_CAP_FE)) {
12753 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012754 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012755 }
12756
12757 proxy->conf.args.ctx = ARGC_CAP;
12758
Willy Tarreaua9083d02015-05-08 15:27:59 +020012759 if (!args[cur_arg]) {
12760 memprintf(err, "missing length value");
12761 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012762 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012763 }
12764 /* we copy the table name for now, it will be resolved later */
12765 len = atoi(args[cur_arg]);
12766 if (len <= 0) {
12767 memprintf(err, "length must be > 0");
12768 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012769 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012770 }
12771 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012772
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012773 if (!len) {
12774 memprintf(err, "a positive 'len' argument is mandatory");
12775 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012776 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012777 }
12778
Vincent Bernat02779b62016-04-03 13:48:43 +020012779 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012780 hdr->next = px->req_cap;
12781 hdr->name = NULL; /* not a header capture */
12782 hdr->namelen = 0;
12783 hdr->len = len;
12784 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12785 hdr->index = px->nb_req_cap++;
12786
12787 px->req_cap = hdr;
12788 px->to_log |= LW_REQHDR;
12789
Thierry FOURNIER42148732015-09-02 17:17:33 +020012790 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012791 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012792 rule->arg.cap.expr = expr;
12793 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012794 }
12795
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012796 else if (strcmp(args[cur_arg], "id") == 0) {
12797 int id;
12798 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012799
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012800 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012801
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012802 if (!args[cur_arg]) {
12803 memprintf(err, "missing id value");
12804 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012805 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012806 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012807
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012808 id = strtol(args[cur_arg], &error, 10);
12809 if (*error != '\0') {
12810 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12811 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012812 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012813 }
12814 cur_arg++;
12815
12816 proxy->conf.args.ctx = ARGC_CAP;
12817
Thierry FOURNIER42148732015-09-02 17:17:33 +020012818 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012819 rule->action_ptr = http_action_req_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012820 rule->arg.capid.expr = expr;
12821 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012822 }
12823
12824 else {
12825 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12826 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012827 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012828 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012829
12830 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012831 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012832}
12833
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012834/* This function executes the "capture" action and store the result in a
12835 * capture slot if exists. It executes a fetch expression, turns the result
12836 * into a string and puts it in a capture slot. It always returns 1. If an
12837 * error occurs the action is cancelled, but the rule processing continues.
12838 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012839enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012840 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012841{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012842 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012843 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012844 char **cap = s->res_cap;
12845 struct proxy *fe = strm_fe(s);
12846 int len;
12847 int i;
12848
12849 /* Look for the original configuration. */
12850 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012851 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012852 i--, h = h->next);
12853 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012854 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012855
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012856 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 +020012857 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012858 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012859
12860 if (cap[h->index] == NULL)
12861 cap[h->index] = pool_alloc2(h->pool);
12862
12863 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012864 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012865
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012866 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012867 if (len > h->len)
12868 len = h->len;
12869
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012870 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012871 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012872 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012873}
12874
12875/* parse an "http-response capture" action. It takes a single argument which is
12876 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12877 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012878 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012879 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012880enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12881 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012882{
12883 struct sample_expr *expr;
12884 int cur_arg;
12885 int id;
12886 char *error;
12887
12888 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12889 if (strcmp(args[cur_arg], "if") == 0 ||
12890 strcmp(args[cur_arg], "unless") == 0)
12891 break;
12892
12893 if (cur_arg < *orig_arg + 3) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012894 memprintf(err, "expects <expression> id <idx>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012895 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012896 }
12897
12898 cur_arg = *orig_arg;
12899 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12900 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012901 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012902
12903 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12904 memprintf(err,
12905 "fetch method '%s' extracts information from '%s', none of which is available here",
12906 args[cur_arg-1], sample_src_names(expr->fetch->use));
12907 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012908 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012909 }
12910
12911 if (!args[cur_arg] || !*args[cur_arg]) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012912 memprintf(err, "expects 'id'");
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012913 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012914 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012915 }
12916
12917 if (strcmp(args[cur_arg], "id") != 0) {
12918 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12919 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012920 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012921 }
12922
12923 cur_arg++;
12924
12925 if (!args[cur_arg]) {
12926 memprintf(err, "missing id value");
12927 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012928 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012929 }
12930
12931 id = strtol(args[cur_arg], &error, 10);
12932 if (*error != '\0') {
12933 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12934 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012935 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012936 }
12937 cur_arg++;
12938
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012939 proxy->conf.args.ctx = ARGC_CAP;
12940
Thierry FOURNIER42148732015-09-02 17:17:33 +020012941 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012942 rule->action_ptr = http_action_res_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012943 rule->arg.capid.expr = expr;
12944 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012945
12946 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012947 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012948}
12949
William Lallemand73025dd2014-04-24 14:38:37 +020012950/*
12951 * Return the struct http_req_action_kw associated to a keyword.
12952 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012953struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012954{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012955 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012956}
12957
12958/*
12959 * Return the struct http_res_action_kw associated to a keyword.
12960 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012961struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012962{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012963 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012964}
12965
Willy Tarreau12207b32016-11-22 19:48:51 +010012966
12967/* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop
12968 * now.
12969 */
12970static int cli_parse_show_errors(char **args, struct appctx *appctx, void *private)
12971{
12972 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
12973 return 1;
12974
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012975 if (*args[2]) {
12976 struct proxy *px;
12977
12978 px = proxy_find_by_name(args[2], 0, 0);
12979 if (px)
12980 appctx->ctx.errors.iid = px->uuid;
12981 else
12982 appctx->ctx.errors.iid = atoi(args[2]);
12983
12984 if (!appctx->ctx.errors.iid) {
12985 appctx->ctx.cli.msg = "No such proxy.\n";
12986 appctx->st0 = CLI_ST_PRINT;
12987 return 1;
12988 }
12989 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012990 else
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012991 appctx->ctx.errors.iid = -1; // dump all proxies
12992
Willy Tarreau35069f82016-11-25 09:16:37 +010012993 appctx->ctx.errors.flag = 0;
12994 if (strcmp(args[3], "request") == 0)
12995 appctx->ctx.errors.flag |= 4; // ignore response
12996 else if (strcmp(args[3], "response") == 0)
12997 appctx->ctx.errors.flag |= 2; // ignore request
Willy Tarreau12207b32016-11-22 19:48:51 +010012998 appctx->ctx.errors.px = NULL;
Willy Tarreau12207b32016-11-22 19:48:51 +010012999 return 0;
13000}
13001
13002/* This function dumps all captured errors onto the stream interface's
13003 * read buffer. It returns 0 if the output buffer is full and it needs
13004 * to be called again, otherwise non-zero.
13005 */
13006static int cli_io_handler_show_errors(struct appctx *appctx)
13007{
13008 struct stream_interface *si = appctx->owner;
13009 extern const char *monthname[12];
13010
13011 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
13012 return 1;
13013
13014 chunk_reset(&trash);
13015
13016 if (!appctx->ctx.errors.px) {
13017 /* the function had not been called yet, let's prepare the
13018 * buffer for a response.
13019 */
13020 struct tm tm;
13021
13022 get_localtime(date.tv_sec, &tm);
13023 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
13024 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
13025 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
13026 error_snapshot_id);
13027
13028 if (bi_putchk(si_ic(si), &trash) == -1) {
13029 /* Socket buffer full. Let's try again later from the same point */
13030 si_applet_cant_put(si);
13031 return 0;
13032 }
13033
13034 appctx->ctx.errors.px = proxy;
Willy Tarreau12207b32016-11-22 19:48:51 +010013035 appctx->ctx.errors.bol = 0;
13036 appctx->ctx.errors.ptr = -1;
13037 }
13038
13039 /* we have two inner loops here, one for the proxy, the other one for
13040 * the buffer.
13041 */
13042 while (appctx->ctx.errors.px) {
13043 struct error_snapshot *es;
13044
Willy Tarreau35069f82016-11-25 09:16:37 +010013045 if ((appctx->ctx.errors.flag & 1) == 0) {
Willy Tarreau12207b32016-11-22 19:48:51 +010013046 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau35069f82016-11-25 09:16:37 +010013047 if (appctx->ctx.errors.flag & 2) // skip req
13048 goto next;
13049 }
13050 else {
Willy Tarreau12207b32016-11-22 19:48:51 +010013051 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau35069f82016-11-25 09:16:37 +010013052 if (appctx->ctx.errors.flag & 4) // skip resp
13053 goto next;
13054 }
Willy Tarreau12207b32016-11-22 19:48:51 +010013055
13056 if (!es->when.tv_sec)
13057 goto next;
13058
13059 if (appctx->ctx.errors.iid >= 0 &&
13060 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
13061 es->oe->uuid != appctx->ctx.errors.iid)
13062 goto next;
13063
13064 if (appctx->ctx.errors.ptr < 0) {
13065 /* just print headers now */
13066
13067 char pn[INET6_ADDRSTRLEN];
13068 struct tm tm;
13069 int port;
13070
13071 get_localtime(es->when.tv_sec, &tm);
13072 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
13073 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
13074 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
13075
13076 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
13077 case AF_INET:
13078 case AF_INET6:
13079 port = get_host_port(&es->src);
13080 break;
13081 default:
13082 port = 0;
13083 }
13084
Willy Tarreau35069f82016-11-25 09:16:37 +010013085 switch (appctx->ctx.errors.flag & 1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010013086 case 0:
13087 chunk_appendf(&trash,
13088 " frontend %s (#%d): invalid request\n"
13089 " backend %s (#%d)",
13090 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
13091 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
13092 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
13093 break;
13094 case 1:
13095 chunk_appendf(&trash,
13096 " backend %s (#%d): invalid response\n"
13097 " frontend %s (#%d)",
13098 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
13099 es->oe->id, es->oe->uuid);
13100 break;
13101 }
13102
13103 chunk_appendf(&trash,
13104 ", server %s (#%d), event #%u\n"
13105 " src %s:%d, session #%d, session flags 0x%08x\n"
Willy Tarreau10e61cb2017-01-04 14:51:22 +010013106 " HTTP msg state %s(%d), msg flags 0x%08x, tx flags 0x%08x\n"
Willy Tarreau12207b32016-11-22 19:48:51 +010013107 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
13108 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
13109 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
13110 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
13111 es->ev_id,
13112 pn, port, es->sid, es->s_flags,
Willy Tarreau10e61cb2017-01-04 14:51:22 +010013113 http_msg_state_str(es->state), es->state, es->m_flags, es->t_flags,
Willy Tarreau12207b32016-11-22 19:48:51 +010013114 es->m_clen, es->m_blen,
13115 es->b_flags, es->b_out, es->b_tot,
13116 es->len, es->b_wrap, es->pos);
13117
13118 if (bi_putchk(si_ic(si), &trash) == -1) {
13119 /* Socket buffer full. Let's try again later from the same point */
13120 si_applet_cant_put(si);
13121 return 0;
13122 }
13123 appctx->ctx.errors.ptr = 0;
13124 appctx->ctx.errors.sid = es->sid;
13125 }
13126
13127 if (appctx->ctx.errors.sid != es->sid) {
13128 /* the snapshot changed while we were dumping it */
13129 chunk_appendf(&trash,
13130 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
13131 if (bi_putchk(si_ic(si), &trash) == -1) {
13132 si_applet_cant_put(si);
13133 return 0;
13134 }
13135 goto next;
13136 }
13137
13138 /* OK, ptr >= 0, so we have to dump the current line */
13139 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
13140 int newptr;
13141 int newline;
13142
13143 newline = appctx->ctx.errors.bol;
13144 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
13145 if (newptr == appctx->ctx.errors.ptr)
13146 return 0;
13147
13148 if (bi_putchk(si_ic(si), &trash) == -1) {
13149 /* Socket buffer full. Let's try again later from the same point */
13150 si_applet_cant_put(si);
13151 return 0;
13152 }
13153 appctx->ctx.errors.ptr = newptr;
13154 appctx->ctx.errors.bol = newline;
13155 };
13156 next:
13157 appctx->ctx.errors.bol = 0;
13158 appctx->ctx.errors.ptr = -1;
Willy Tarreau35069f82016-11-25 09:16:37 +010013159 appctx->ctx.errors.flag ^= 1;
13160 if (!(appctx->ctx.errors.flag & 1))
Willy Tarreau12207b32016-11-22 19:48:51 +010013161 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau12207b32016-11-22 19:48:51 +010013162 }
13163
13164 /* dump complete */
13165 return 1;
13166}
13167
13168/* register cli keywords */
13169static struct cli_kw_list cli_kws = {{ },{
13170 { { "show", "errors", NULL },
13171 "show errors : report last request and response errors for each proxy",
13172 cli_parse_show_errors, cli_io_handler_show_errors, NULL,
13173 },
13174 {{},}
13175}};
13176
Willy Tarreau4a568972010-05-12 08:08:50 +020013177/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013178/* All supported ACL keywords must be declared here. */
13179/************************************************************************/
13180
13181/* Note: must not be declared <const> as its list will be overwritten.
13182 * Please take care of keeping this list alphabetically sorted.
13183 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020013184static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013185 { "base", "base", PAT_MATCH_STR },
13186 { "base_beg", "base", PAT_MATCH_BEG },
13187 { "base_dir", "base", PAT_MATCH_DIR },
13188 { "base_dom", "base", PAT_MATCH_DOM },
13189 { "base_end", "base", PAT_MATCH_END },
13190 { "base_len", "base", PAT_MATCH_LEN },
13191 { "base_reg", "base", PAT_MATCH_REG },
13192 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020013193
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013194 { "cook", "req.cook", PAT_MATCH_STR },
13195 { "cook_beg", "req.cook", PAT_MATCH_BEG },
13196 { "cook_dir", "req.cook", PAT_MATCH_DIR },
13197 { "cook_dom", "req.cook", PAT_MATCH_DOM },
13198 { "cook_end", "req.cook", PAT_MATCH_END },
13199 { "cook_len", "req.cook", PAT_MATCH_LEN },
13200 { "cook_reg", "req.cook", PAT_MATCH_REG },
13201 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013202
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013203 { "hdr", "req.hdr", PAT_MATCH_STR },
13204 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
13205 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
13206 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
13207 { "hdr_end", "req.hdr", PAT_MATCH_END },
13208 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
13209 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
13210 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013211
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013212 /* these two declarations uses strings with list storage (in place
13213 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
13214 * and delete functions are relative to the list management. The parse
13215 * and match method are related to the corresponding fetch methods. This
13216 * is very particular ACL declaration mode.
13217 */
13218 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
13219 { "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 +020013220
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013221 { "path", "path", PAT_MATCH_STR },
13222 { "path_beg", "path", PAT_MATCH_BEG },
13223 { "path_dir", "path", PAT_MATCH_DIR },
13224 { "path_dom", "path", PAT_MATCH_DOM },
13225 { "path_end", "path", PAT_MATCH_END },
13226 { "path_len", "path", PAT_MATCH_LEN },
13227 { "path_reg", "path", PAT_MATCH_REG },
13228 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013229
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013230 { "req_ver", "req.ver", PAT_MATCH_STR },
13231 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013232
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013233 { "scook", "res.cook", PAT_MATCH_STR },
13234 { "scook_beg", "res.cook", PAT_MATCH_BEG },
13235 { "scook_dir", "res.cook", PAT_MATCH_DIR },
13236 { "scook_dom", "res.cook", PAT_MATCH_DOM },
13237 { "scook_end", "res.cook", PAT_MATCH_END },
13238 { "scook_len", "res.cook", PAT_MATCH_LEN },
13239 { "scook_reg", "res.cook", PAT_MATCH_REG },
13240 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013241
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013242 { "shdr", "res.hdr", PAT_MATCH_STR },
13243 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
13244 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
13245 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
13246 { "shdr_end", "res.hdr", PAT_MATCH_END },
13247 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
13248 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
13249 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013250
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013251 { "url", "url", PAT_MATCH_STR },
13252 { "url_beg", "url", PAT_MATCH_BEG },
13253 { "url_dir", "url", PAT_MATCH_DIR },
13254 { "url_dom", "url", PAT_MATCH_DOM },
13255 { "url_end", "url", PAT_MATCH_END },
13256 { "url_len", "url", PAT_MATCH_LEN },
13257 { "url_reg", "url", PAT_MATCH_REG },
13258 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013259
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013260 { "urlp", "urlp", PAT_MATCH_STR },
13261 { "urlp_beg", "urlp", PAT_MATCH_BEG },
13262 { "urlp_dir", "urlp", PAT_MATCH_DIR },
13263 { "urlp_dom", "urlp", PAT_MATCH_DOM },
13264 { "urlp_end", "urlp", PAT_MATCH_END },
13265 { "urlp_len", "urlp", PAT_MATCH_LEN },
13266 { "urlp_reg", "urlp", PAT_MATCH_REG },
13267 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013268
Willy Tarreau8ed669b2013-01-11 15:49:37 +010013269 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013270}};
13271
13272/************************************************************************/
13273/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020013274/************************************************************************/
13275/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020013276static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013277 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013278 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013279 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
13280
Willy Tarreau87b09662015-04-03 00:22:06 +020013281 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013282 { "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 +020013283
13284 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013285 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
13286 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
13287 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020013288
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013289 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
13290 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010013291
Willy Tarreau409bcde2013-01-08 00:31:00 +010013292 /* cookie is valid in both directions (eg: for "stick ...") but cook*
13293 * are only here to match the ACL's name, are request-only and are used
13294 * for ACL compatibility only.
13295 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013296 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13297 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013298 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
13299 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013300
13301 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
13302 * only here to match the ACL's name, are request-only and are used for
13303 * ACL compatibility only.
13304 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013305 { "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 +020013306 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013307 { "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 +020013308 { "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 +010013309
Willy Tarreau0a0daec2013-04-02 22:44:58 +020013310 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013311 { "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 +010013312 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010013313 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013314 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010013315 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013316
13317 /* HTTP protocol on the request path */
13318 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013319 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013320
13321 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013322 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
13323 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013324
Willy Tarreaua5910cc2015-05-02 00:46:08 +020013325 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013326 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
13327 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020013328 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020013329
Willy Tarreau18ed2562013-01-14 15:56:36 +010013330 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013331 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
13332 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013333
Willy Tarreau18ed2562013-01-14 15:56:36 +010013334 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013335 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013336 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
13337 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013338
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013339 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013340 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013341 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013342 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013343 { "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 +010013344 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013345 { "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 +010013346
13347 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013348 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013349 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13350 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013351
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013352 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013353 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013354 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013355 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013356 { "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 +010013357 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013358 { "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 +010013359
Willy Tarreau409bcde2013-01-08 00:31:00 +010013360 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013361 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013362 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13363 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013364 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010013365
13366 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013367 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013368 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013369 { "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 +020013370 { "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 +010013371
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013372 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020013373 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013374 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013375 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000013376 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013377 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013378 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020013379 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13380 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013381 { "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 +010013382 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020013383}};
13384
Willy Tarreau8797c062007-05-07 00:55:35 +020013385
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013386/************************************************************************/
13387/* All supported converter keywords must be declared here. */
13388/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020013389/* Note: must not be declared <const> as its list will be overwritten */
13390static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013391 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020013392 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013393 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
13394 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020013395 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020013396 { NULL, NULL, 0, 0, 0 },
13397}};
13398
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020013399
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013400/************************************************************************/
13401/* All supported http-request action keywords must be declared here. */
13402/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013403struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013404 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020013405 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013406 { "set-method", parse_set_req_line },
13407 { "set-path", parse_set_req_line },
13408 { "set-query", parse_set_req_line },
13409 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020013410 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013411 }
13412};
13413
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013414struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013415 .kw = {
13416 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020013417 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013418 { NULL, NULL }
13419 }
13420};
13421
Willy Tarreau8797c062007-05-07 00:55:35 +020013422__attribute__((constructor))
13423static void __http_protocol_init(void)
13424{
13425 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020013426 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020013427 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013428 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013429 http_res_keywords_register(&http_res_actions);
Willy Tarreau12207b32016-11-22 19:48:51 +010013430 cli_register_kw(&cli_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020013431}
13432
13433
Willy Tarreau58f10d72006-12-04 02:26:12 +010013434/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020013435 * Local variables:
13436 * c-indent-level: 8
13437 * c-basic-offset: 8
13438 * End:
13439 */