blob: 5ad295676ac23a48bfcfe9583c764fa3e41fd322 [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{
Christopher Faulet07a0fec2017-02-08 12:17:07 +01003422 struct chunk *replace;
3423 int ret = -1;
3424
3425 replace = alloc_trash_chunk();
3426 if (!replace)
3427 goto leave;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003428
3429 replace->len = build_logline(s, replace->str, replace->size, fmt);
3430 if (replace->len >= replace->size - 1)
Christopher Faulet07a0fec2017-02-08 12:17:07 +01003431 goto leave;
3432
3433 ret = http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003434
Christopher Faulet07a0fec2017-02-08 12:17:07 +01003435 leave:
3436 free_trash_chunk(replace);
3437 return ret;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003438}
3439
Willy Tarreau87b09662015-04-03 00:22:06 +02003440/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003441 * transaction <txn>. Returns the verdict of the first rule that prevents
3442 * further processing of the request (auth, deny, ...), and defaults to
3443 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3444 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02003445 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
3446 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
3447 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003448 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003449enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02003450http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003451{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003452 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003453 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003454 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003455 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003456 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003457 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02003458 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02003459 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003460
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003461 /* If "the current_rule_list" match the executed rule list, we are in
3462 * resume condition. If a resume is needed it is always in the action
3463 * and never in the ACL or converters. In this case, we initialise the
3464 * current rule, and go to the action execution point.
3465 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003466 if (s->current_rule) {
3467 rule = s->current_rule;
3468 s->current_rule = NULL;
3469 if (s->current_rule_list == rules)
3470 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003471 }
3472 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003473
Willy Tarreauff011f22011-01-06 17:51:27 +01003474 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003475
Willy Tarreau96257ec2012-12-27 10:46:37 +01003476 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003477 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003478 int ret;
3479
Willy Tarreau192252e2015-04-04 01:47:55 +02003480 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003481 ret = acl_pass(ret);
3482
Willy Tarreauff011f22011-01-06 17:51:27 +01003483 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003484 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003485
3486 if (!ret) /* condition not matched */
3487 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003488 }
3489
Willy Tarreauacc98002015-09-27 23:34:39 +02003490 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003491resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003492 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003493 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003494 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003495
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003496 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02003497 if (deny_status)
3498 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003499 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003500
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003501 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01003502 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003503 if (deny_status)
3504 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003505 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003506
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003507 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003508 /* Auth might be performed on regular http-req rules as well as on stats */
3509 auth_realm = rule->arg.auth.realm;
3510 if (!auth_realm) {
3511 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3512 auth_realm = STATS_DEFAULT_REALM;
3513 else
3514 auth_realm = px->id;
3515 }
3516 /* send 401/407 depending on whether we use a proxy or not. We still
3517 * count one error, because normal browsing won't significantly
3518 * increase the counter but brute force attempts will.
3519 */
3520 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3521 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01003522 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02003523 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003524 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003525
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003526 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003527 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3528 return HTTP_RULE_RES_BADREQ;
3529 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003530
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003531 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003532 s->task->nice = rule->arg.nice;
3533 break;
3534
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003535 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003536 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Vincent Bernat6e615892016-05-18 16:17:44 +02003537 inet_set_tos(cli_conn->t.sock.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003538 break;
3539
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003540 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003541#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003542 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003543 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003544#endif
3545 break;
3546
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003547 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003548 s->logs.level = rule->arg.loglevel;
3549 break;
3550
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003551 case ACT_HTTP_REPLACE_HDR:
3552 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003553 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3554 rule->arg.hdr_add.name_len,
3555 &rule->arg.hdr_add.fmt,
3556 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003557 return HTTP_RULE_RES_BADREQ;
3558 break;
3559
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003560 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003561 ctx.idx = 0;
3562 /* remove all occurrences of the header */
3563 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3564 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3565 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003566 }
Willy Tarreau85603282015-01-21 20:39:27 +01003567 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003568
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003569 case ACT_HTTP_SET_HDR:
3570 case ACT_HTTP_ADD_HDR:
Thierry Fournier4b788f72016-06-01 13:35:36 +02003571 /* The scope of the trash buffer must be limited to this function. The
3572 * build_logline() function can execute a lot of other function which
3573 * can use the trash buffer. So for limiting the scope of this global
3574 * buffer, we build first the header value using build_logline, and
3575 * after we store the header name.
3576 */
3577 len = rule->arg.hdr_add.name_len + 2,
3578 len += build_logline(s, trash.str + len, trash.size - len, &rule->arg.hdr_add.fmt);
Willy Tarreau96257ec2012-12-27 10:46:37 +01003579 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
Thierry Fournier4b788f72016-06-01 13:35:36 +02003580 trash.str[rule->arg.hdr_add.name_len] = ':';
3581 trash.str[rule->arg.hdr_add.name_len + 1] = ' ';
3582 trash.len = len;
Willy Tarreau85603282015-01-21 20:39:27 +01003583
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003584 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003585 /* remove all occurrences of the header */
3586 ctx.idx = 0;
3587 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3588 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3589 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3590 }
3591 }
3592
Willy Tarreau96257ec2012-12-27 10:46:37 +01003593 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3594 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003595
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003596 case ACT_HTTP_DEL_ACL:
3597 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003598 struct pat_ref *ref;
3599 char *key;
3600 int len;
3601
3602 /* collect reference */
3603 ref = pat_ref_lookup(rule->arg.map.ref);
3604 if (!ref)
3605 continue;
3606
3607 /* collect key */
3608 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3609 key = trash.str;
3610 key[len] = '\0';
3611
3612 /* perform update */
3613 /* returned code: 1=ok, 0=ko */
3614 pat_ref_delete(ref, key);
3615
3616 break;
3617 }
3618
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003619 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003620 struct pat_ref *ref;
3621 char *key;
3622 struct chunk *trash_key;
3623 int len;
3624
3625 trash_key = get_trash_chunk();
3626
3627 /* collect reference */
3628 ref = pat_ref_lookup(rule->arg.map.ref);
3629 if (!ref)
3630 continue;
3631
3632 /* collect key */
3633 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3634 key = trash_key->str;
3635 key[len] = '\0';
3636
3637 /* perform update */
3638 /* add entry only if it does not already exist */
3639 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003640 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003641
3642 break;
3643 }
3644
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003645 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003646 struct pat_ref *ref;
3647 char *key, *value;
3648 struct chunk *trash_key, *trash_value;
3649 int len;
3650
3651 trash_key = get_trash_chunk();
3652 trash_value = get_trash_chunk();
3653
3654 /* collect reference */
3655 ref = pat_ref_lookup(rule->arg.map.ref);
3656 if (!ref)
3657 continue;
3658
3659 /* collect key */
3660 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3661 key = trash_key->str;
3662 key[len] = '\0';
3663
3664 /* collect value */
3665 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3666 value = trash_value->str;
3667 value[len] = '\0';
3668
3669 /* perform update */
3670 if (pat_ref_find_elt(ref, key) != NULL)
3671 /* update entry if it exists */
3672 pat_ref_set(ref, key, value, NULL);
3673 else
3674 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003675 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003676
3677 break;
3678 }
William Lallemand73025dd2014-04-24 14:38:37 +02003679
Thierry FOURNIER42148732015-09-02 17:17:33 +02003680 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02003681 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
3682 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003683
Willy Tarreauacc98002015-09-27 23:34:39 +02003684 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003685 case ACT_RET_ERR:
3686 case ACT_RET_CONT:
3687 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003688 case ACT_RET_STOP:
3689 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003690 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003691 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003692 return HTTP_RULE_RES_YIELD;
3693 }
William Lallemand73025dd2014-04-24 14:38:37 +02003694 break;
3695
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003696 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02003697 /* Note: only the first valid tracking parameter of each
3698 * applies.
3699 */
3700
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003701 if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02003702 struct stktable *t;
3703 struct stksess *ts;
3704 struct stktable_key *key;
3705 void *ptr;
3706
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02003707 t = rule->arg.trk_ctr.table.t;
3708 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 +02003709
3710 if (key && (ts = stktable_get_entry(t, key))) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003711 stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02003712
3713 /* let's count a new HTTP request as it's the first time we do it */
3714 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3715 if (ptr)
3716 stktable_data_cast(ptr, http_req_cnt)++;
3717
3718 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3719 if (ptr)
3720 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3721 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3722
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003723 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003724 if (sess->fe != s->be)
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003725 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02003726 }
3727 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003728 break;
3729
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003730 /* other flags exists, but normaly, they never be matched. */
3731 default:
3732 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003733 }
3734 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003735
3736 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003737 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003738}
3739
Willy Tarreau71241ab2012-12-27 11:30:54 +01003740
Willy Tarreau51d861a2015-05-22 17:30:48 +02003741/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3742 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3743 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3744 * is returned, the process can continue the evaluation of next rule list. If
3745 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3746 * is returned, it means the operation could not be processed and a server error
3747 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3748 * deny rule. If *YIELD is returned, the caller must call again the function
3749 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003750 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003751static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003752http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003753{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003754 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003755 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003756 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003757 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003758 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02003759 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003760
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003761 /* If "the current_rule_list" match the executed rule list, we are in
3762 * resume condition. If a resume is needed it is always in the action
3763 * and never in the ACL or converters. In this case, we initialise the
3764 * current rule, and go to the action execution point.
3765 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003766 if (s->current_rule) {
3767 rule = s->current_rule;
3768 s->current_rule = NULL;
3769 if (s->current_rule_list == rules)
3770 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003771 }
3772 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003773
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003774 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003775
3776 /* check optional condition */
3777 if (rule->cond) {
3778 int ret;
3779
Willy Tarreau192252e2015-04-04 01:47:55 +02003780 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003781 ret = acl_pass(ret);
3782
3783 if (rule->cond->pol == ACL_COND_UNLESS)
3784 ret = !ret;
3785
3786 if (!ret) /* condition not matched */
3787 continue;
3788 }
3789
Willy Tarreauacc98002015-09-27 23:34:39 +02003790 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003791resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003792 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003793 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003794 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003795
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003796 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003797 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003798 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003799
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003800 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003801 s->task->nice = rule->arg.nice;
3802 break;
3803
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003804 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003805 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Vincent Bernat6e615892016-05-18 16:17:44 +02003806 inet_set_tos(cli_conn->t.sock.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003807 break;
3808
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003809 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003810#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003811 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003812 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003813#endif
3814 break;
3815
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003816 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003817 s->logs.level = rule->arg.loglevel;
3818 break;
3819
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003820 case ACT_HTTP_REPLACE_HDR:
3821 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003822 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3823 rule->arg.hdr_add.name_len,
3824 &rule->arg.hdr_add.fmt,
3825 &rule->arg.hdr_add.re, rule->action))
Christopher Fauletcdade942017-02-08 12:41:31 +01003826 return HTTP_RULE_RES_BADREQ;
Sasha Pachev218f0642014-06-16 12:05:59 -06003827 break;
3828
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003829 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003830 ctx.idx = 0;
3831 /* remove all occurrences of the header */
3832 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3833 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3834 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3835 }
Willy Tarreau85603282015-01-21 20:39:27 +01003836 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003837
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003838 case ACT_HTTP_SET_HDR:
3839 case ACT_HTTP_ADD_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003840 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3841 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3842 trash.len = rule->arg.hdr_add.name_len;
3843 trash.str[trash.len++] = ':';
3844 trash.str[trash.len++] = ' ';
3845 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 +01003846
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003847 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003848 /* remove all occurrences of the header */
3849 ctx.idx = 0;
3850 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3851 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3852 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3853 }
3854 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003855 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3856 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003857
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003858 case ACT_HTTP_DEL_ACL:
3859 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003860 struct pat_ref *ref;
3861 char *key;
3862 int len;
3863
3864 /* collect reference */
3865 ref = pat_ref_lookup(rule->arg.map.ref);
3866 if (!ref)
3867 continue;
3868
3869 /* collect key */
3870 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3871 key = trash.str;
3872 key[len] = '\0';
3873
3874 /* perform update */
3875 /* returned code: 1=ok, 0=ko */
3876 pat_ref_delete(ref, key);
3877
3878 break;
3879 }
3880
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003881 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003882 struct pat_ref *ref;
3883 char *key;
3884 struct chunk *trash_key;
3885 int len;
3886
3887 trash_key = get_trash_chunk();
3888
3889 /* collect reference */
3890 ref = pat_ref_lookup(rule->arg.map.ref);
3891 if (!ref)
3892 continue;
3893
3894 /* collect key */
3895 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3896 key = trash_key->str;
3897 key[len] = '\0';
3898
3899 /* perform update */
3900 /* check if the entry already exists */
3901 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003902 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003903
3904 break;
3905 }
3906
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003907 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003908 struct pat_ref *ref;
3909 char *key, *value;
3910 struct chunk *trash_key, *trash_value;
3911 int len;
3912
3913 trash_key = get_trash_chunk();
3914 trash_value = get_trash_chunk();
3915
3916 /* collect reference */
3917 ref = pat_ref_lookup(rule->arg.map.ref);
3918 if (!ref)
3919 continue;
3920
3921 /* collect key */
3922 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3923 key = trash_key->str;
3924 key[len] = '\0';
3925
3926 /* collect value */
3927 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3928 value = trash_value->str;
3929 value[len] = '\0';
3930
3931 /* perform update */
3932 if (pat_ref_find_elt(ref, key) != NULL)
3933 /* update entry if it exists */
3934 pat_ref_set(ref, key, value, NULL);
3935 else
3936 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003937 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003938
3939 break;
3940 }
William Lallemand73025dd2014-04-24 14:38:37 +02003941
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003942 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02003943 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3944 return HTTP_RULE_RES_BADREQ;
3945 return HTTP_RULE_RES_DONE;
3946
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003947 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3948 /* Note: only the first valid tracking parameter of each
3949 * applies.
3950 */
3951
3952 if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) {
3953 struct stktable *t;
3954 struct stksess *ts;
3955 struct stktable_key *key;
3956 void *ptr;
3957
3958 t = rule->arg.trk_ctr.table.t;
3959 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
3960
3961 if (key && (ts = stktable_get_entry(t, key))) {
3962 stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts);
3963
3964 /* let's count a new HTTP request as it's the first time we do it */
3965 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3966 if (ptr)
3967 stktable_data_cast(ptr, http_req_cnt)++;
3968
3969 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3970 if (ptr)
3971 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3972 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3973
3974 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3975 if (sess->fe != s->be)
3976 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3977
3978 /* When the client triggers a 4xx from the server, it's most often due
3979 * to a missing object or permission. These events should be tracked
3980 * because if they happen often, it may indicate a brute force or a
3981 * vulnerability scan. Normally this is done when receiving the response
3982 * but here we're tracking after this ought to have been done so we have
3983 * to do it on purpose.
3984 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02003985 if ((unsigned)(txn->status - 400) < 100) {
3986 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3987 if (ptr)
3988 stktable_data_cast(ptr, http_err_cnt)++;
3989
3990 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3991 if (ptr)
3992 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3993 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3994 }
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003995 }
3996 }
3997 break;
3998
Thierry FOURNIER42148732015-09-02 17:17:33 +02003999 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02004000 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
4001 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02004002
Willy Tarreauacc98002015-09-27 23:34:39 +02004003 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004004 case ACT_RET_ERR:
4005 case ACT_RET_CONT:
4006 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02004007 case ACT_RET_STOP:
4008 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02004009 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02004010 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004011 return HTTP_RULE_RES_YIELD;
4012 }
William Lallemand73025dd2014-04-24 14:38:37 +02004013 break;
4014
Thierry FOURNIER22e49012015-08-05 19:13:48 +02004015 /* other flags exists, but normaly, they never be matched. */
4016 default:
4017 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004018 }
4019 }
4020
4021 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01004022 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004023}
4024
4025
Willy Tarreau71241ab2012-12-27 11:30:54 +01004026/* Perform an HTTP redirect based on the information in <rule>. The function
4027 * returns non-zero on success, or zero in case of a, irrecoverable error such
4028 * as too large a request to build a valid response.
4029 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004030static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01004031{
Willy Tarreaub329a312015-05-22 16:27:37 +02004032 struct http_msg *req = &txn->req;
4033 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004034 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004035 const char *location;
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004036 struct chunk *chunk;
4037 int ret = 0;
4038
4039 chunk = alloc_trash_chunk();
4040 if (!chunk)
4041 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004042
4043 /* build redirect message */
4044 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04004045 case 308:
4046 msg_fmt = HTTP_308;
4047 break;
4048 case 307:
4049 msg_fmt = HTTP_307;
4050 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004051 case 303:
4052 msg_fmt = HTTP_303;
4053 break;
4054 case 301:
4055 msg_fmt = HTTP_301;
4056 break;
4057 case 302:
4058 default:
4059 msg_fmt = HTTP_302;
4060 break;
4061 }
4062
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004063 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
4064 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004065
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004066 location = chunk->str + chunk->len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004067
Willy Tarreau71241ab2012-12-27 11:30:54 +01004068 switch(rule->type) {
4069 case REDIRECT_TYPE_SCHEME: {
4070 const char *path;
4071 const char *host;
4072 struct hdr_ctx ctx;
4073 int pathlen;
4074 int hostlen;
4075
4076 host = "";
4077 hostlen = 0;
4078 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02004079 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004080 host = ctx.line + ctx.val;
4081 hostlen = ctx.vlen;
4082 }
4083
4084 path = http_get_path(txn);
4085 /* build message using path */
4086 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004087 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004088 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4089 int qs = 0;
4090 while (qs < pathlen) {
4091 if (path[qs] == '?') {
4092 pathlen = qs;
4093 break;
4094 }
4095 qs++;
4096 }
4097 }
4098 } else {
4099 path = "/";
4100 pathlen = 1;
4101 }
4102
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004103 if (rule->rdr_str) { /* this is an old "redirect" rule */
4104 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004105 if (chunk->len + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4)
4106 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004107
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004108 /* add scheme */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004109 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
4110 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004111 }
4112 else {
4113 /* add scheme with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004114 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004115
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004116 /* check if we can add scheme + "://" + host + path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004117 if (chunk->len + 3 + hostlen + pathlen > chunk->size - 4)
4118 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004119 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004120 /* add "://" */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004121 memcpy(chunk->str + chunk->len, "://", 3);
4122 chunk->len += 3;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004123
4124 /* add host */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004125 memcpy(chunk->str + chunk->len, host, hostlen);
4126 chunk->len += hostlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004127
4128 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004129 memcpy(chunk->str + chunk->len, path, pathlen);
4130 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004131
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004132 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004133 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004134 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004135 if (chunk->len > chunk->size - 5)
4136 goto leave;
4137 chunk->str[chunk->len] = '/';
4138 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004139 }
4140
4141 break;
4142 }
4143 case REDIRECT_TYPE_PREFIX: {
4144 const char *path;
4145 int pathlen;
4146
4147 path = http_get_path(txn);
4148 /* build message using path */
4149 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004150 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004151 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4152 int qs = 0;
4153 while (qs < pathlen) {
4154 if (path[qs] == '?') {
4155 pathlen = qs;
4156 break;
4157 }
4158 qs++;
4159 }
4160 }
4161 } else {
4162 path = "/";
4163 pathlen = 1;
4164 }
4165
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004166 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004167 if (chunk->len + rule->rdr_len + pathlen > chunk->size - 4)
4168 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004169
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004170 /* add prefix. Note that if prefix == "/", we don't want to
4171 * add anything, otherwise it makes it hard for the user to
4172 * configure a self-redirection.
4173 */
4174 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004175 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
4176 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004177 }
4178 }
4179 else {
4180 /* add prefix with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004181 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004182
4183 /* Check length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004184 if (chunk->len + pathlen > chunk->size - 4)
4185 goto leave;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004186 }
4187
4188 /* add path */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004189 memcpy(chunk->str + chunk->len, path, pathlen);
4190 chunk->len += pathlen;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004191
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004192 /* append a slash at the end of the location if needed and missing */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004193 if (chunk->len && chunk->str[chunk->len - 1] != '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004194 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004195 if (chunk->len > chunk->size - 5)
4196 goto leave;
4197 chunk->str[chunk->len] = '/';
4198 chunk->len++;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004199 }
4200
4201 break;
4202 }
4203 case REDIRECT_TYPE_LOCATION:
4204 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004205 if (rule->rdr_str) { /* this is an old "redirect" rule */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004206 if (chunk->len + rule->rdr_len > chunk->size - 4)
4207 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004208
4209 /* add location */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004210 memcpy(chunk->str + chunk->len, rule->rdr_str, rule->rdr_len);
4211 chunk->len += rule->rdr_len;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004212 }
4213 else {
4214 /* add location with executing log format */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004215 chunk->len += build_logline(s, chunk->str + chunk->len, chunk->size - chunk->len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004216
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004217 /* Check left length */
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004218 if (chunk->len > chunk->size - 4)
4219 goto leave;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004220 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004221 break;
4222 }
4223
4224 if (rule->cookie_len) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004225 memcpy(chunk->str + chunk->len, "\r\nSet-Cookie: ", 14);
4226 chunk->len += 14;
4227 memcpy(chunk->str + chunk->len, rule->cookie_str, rule->cookie_len);
4228 chunk->len += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004229 }
4230
4231 /* add end of headers and the keep-alive/close status.
4232 * We may choose to set keep-alive if the Location begins
4233 * with a slash, because the client will come back to the
4234 * same server.
4235 */
4236 txn->status = rule->code;
4237 /* let's log the request time */
4238 s->logs.tv_request = now;
4239
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004240 if (*location == '/' &&
Willy Tarreaub329a312015-05-22 16:27:37 +02004241 (req->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau2de8a502015-05-28 17:23:54 +02004242 ((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004243 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4244 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4245 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02004246 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004247 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004248 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: keep-alive", 30);
4249 chunk->len += 30;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004250 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004251 memcpy(chunk->str + chunk->len, "\r\nConnection: keep-alive", 24);
4252 chunk->len += 24;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004253 }
4254 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004255 memcpy(chunk->str + chunk->len, "\r\n\r\n", 4);
4256 chunk->len += 4;
4257 FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk));
4258 bo_inject(res->chn, chunk->str, chunk->len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004259 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02004260 bi_fast_delete(req->chn->buf, req->sov);
4261 req->next -= req->sov;
4262 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01004263 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
4264 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->req.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02004265 req->msg_state = HTTP_MSG_CLOSED;
4266 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02004267 /* Trim any possible response */
4268 res->chn->buf->i = 0;
4269 res->next = res->sov = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004270 } else {
4271 /* keep-alive not possible */
4272 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004273 memcpy(chunk->str + chunk->len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4274 chunk->len += 29;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004275 } else {
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004276 memcpy(chunk->str + chunk->len, "\r\nConnection: close\r\n\r\n", 23);
4277 chunk->len += 23;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004278 }
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004279 http_reply_and_close(s, txn->status, chunk);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004280 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004281 }
4282
Willy Tarreaue7dff022015-04-03 01:14:29 +02004283 if (!(s->flags & SF_ERR_MASK))
4284 s->flags |= SF_ERR_LOCAL;
4285 if (!(s->flags & SF_FINST_MASK))
4286 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004287
Thierry FOURNIER0d945762017-01-28 07:39:53 +01004288 ret = 1;
4289 leave:
4290 free_trash_chunk(chunk);
4291 return ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004292}
4293
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004294/* This stream analyser runs all HTTP request processing which is common to
4295 * frontends and backends, which means blocking ACLs, filters, connection-close,
4296 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004297 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004298 * either needs more data or wants to immediately abort the request (eg: deny,
4299 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004300 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004301int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004302{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004303 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004304 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02004305 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004306 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004307 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004308 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02004309 int deny_status = HTTP_ERR_403;
Willy Tarreaud787e662009-07-07 10:14:51 +02004310
Willy Tarreau655dce92009-11-08 13:10:58 +01004311 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004312 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004313 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004314 }
4315
Willy Tarreau87b09662015-04-03 00:22:06 +02004316 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 +02004317 now_ms, __FUNCTION__,
4318 s,
4319 req,
4320 req->rex, req->wex,
4321 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004322 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004323 req->analysers);
4324
Willy Tarreau65410832014-04-28 21:25:43 +02004325 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02004326 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02004327
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004328 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004329 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02004330 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01004331
Willy Tarreau0b748332014-04-29 00:13:29 +02004332 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004333 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4334 goto return_prx_yield;
4335
Willy Tarreau0b748332014-04-29 00:13:29 +02004336 case HTTP_RULE_RES_CONT:
4337 case HTTP_RULE_RES_STOP: /* nothing to do */
4338 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004339
Willy Tarreau0b748332014-04-29 00:13:29 +02004340 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4341 if (txn->flags & TX_CLTARPIT)
4342 goto tarpit;
4343 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004344
Willy Tarreau0b748332014-04-29 00:13:29 +02004345 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4346 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004347
Willy Tarreau0b748332014-04-29 00:13:29 +02004348 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004349 goto done;
4350
Willy Tarreau0b748332014-04-29 00:13:29 +02004351 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4352 goto return_bad_req;
4353 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004354 }
4355
Willy Tarreau52542592014-04-28 18:33:26 +02004356 /* OK at this stage, we know that the request was accepted according to
4357 * the http-request rules, we can check for the stats. Note that the
4358 * URI is detected *before* the req* rules in order not to be affected
4359 * by a possible reqrep, while they are processed *after* so that a
4360 * reqdeny can still block them. This clearly needs to change in 1.6!
4361 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004362 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004363 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004364 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004365 txn->status = 500;
4366 s->logs.tv_request = now;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004367 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004368
Willy Tarreaue7dff022015-04-03 01:14:29 +02004369 if (!(s->flags & SF_ERR_MASK))
4370 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02004371 goto return_prx_cond;
4372 }
4373
4374 /* parse the whole stats request and extract the relevant information */
4375 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02004376 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02004377 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004378
Willy Tarreau0b748332014-04-29 00:13:29 +02004379 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4380 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004381
Willy Tarreau0b748332014-04-29 00:13:29 +02004382 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4383 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004384 }
4385
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004386 /* evaluate the req* rules except reqadd */
4387 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004388 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004389 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004390
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004391 if (txn->flags & TX_CLDENY)
4392 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004393
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004394 if (txn->flags & TX_CLTARPIT)
4395 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004396 }
Willy Tarreau06619262006-12-17 08:37:22 +01004397
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004398 /* add request headers from the rule sets in the same order */
4399 list_for_each_entry(wl, &px->req_add, list) {
4400 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004401 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004402 ret = acl_pass(ret);
4403 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4404 ret = !ret;
4405 if (!ret)
4406 continue;
4407 }
4408
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004409 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004410 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004411 }
4412
Willy Tarreau52542592014-04-28 18:33:26 +02004413
4414 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004415 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004416 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004417 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
4418 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004419
Willy Tarreaue7dff022015-04-03 01:14:29 +02004420 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
4421 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
4422 if (!(s->flags & SF_FINST_MASK))
4423 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004424
Willy Tarreau70730dd2014-04-24 18:06:27 +02004425 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01004426 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
4427 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004428 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004429 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004430 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004431
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004432 /* check whether we have some ACLs set to redirect this request */
4433 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004434 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004435 int ret;
4436
Willy Tarreau192252e2015-04-04 01:47:55 +02004437 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004438 ret = acl_pass(ret);
4439 if (rule->cond->pol == ACL_COND_UNLESS)
4440 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004441 if (!ret)
4442 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004443 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004444 if (!http_apply_redirect_rule(rule, s, txn))
4445 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004446 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004447 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004448
Willy Tarreau2be39392010-01-03 17:24:51 +01004449 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4450 * If this happens, then the data will not come immediately, so we must
4451 * send all what we have without waiting. Note that due to the small gain
4452 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004453 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004454 * itself once used.
4455 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004456 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004457
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004458 done: /* done with this analyser, continue with next ones that the calling
4459 * points will have set, if any.
4460 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004461 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004462 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4463 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004464 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004465
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004466 tarpit:
4467 /* When a connection is tarpitted, we use the tarpit timeout,
4468 * which may be the same as the connect timeout if unspecified.
4469 * If unset, then set it to zero because we really want it to
4470 * eventually expire. We build the tarpit as an analyser.
4471 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004472 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004473
4474 /* wipe the request out so that we can drop the connection early
4475 * if the client closes first.
4476 */
4477 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004478
4479 /* Allow cookie logging
4480 */
4481 if (s->be->cookie_name || sess->fe->capture_name)
4482 manage_client_side_cookies(s, req);
4483
Christopher Faulet0184ea72017-01-05 14:06:34 +01004484 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004485 req->analysers |= AN_REQ_HTTP_TARPIT;
4486 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4487 if (!req->analyse_exp)
4488 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02004489 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004490 sess->fe->fe_counters.denied_req++;
4491 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004492 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004493 if (sess->listener->counters)
4494 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004495 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004496
4497 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004498
4499 /* Allow cookie logging
4500 */
4501 if (s->be->cookie_name || sess->fe->capture_name)
4502 manage_client_side_cookies(s, req);
4503
Willy Tarreau0b748332014-04-29 00:13:29 +02004504 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02004505 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004506 s->logs.tv_request = now;
Willy Tarreau58727ec2016-05-25 16:23:59 +02004507 http_reply_and_close(s, txn->status, http_error_message(s, deny_status));
Willy Tarreau87b09662015-04-03 00:22:06 +02004508 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004509 sess->fe->fe_counters.denied_req++;
4510 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004511 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004512 if (sess->listener->counters)
4513 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004514 goto return_prx_cond;
4515
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004516 return_bad_req:
4517 /* We centralize bad requests processing here */
4518 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4519 /* we detected a parsing error. We want to archive this request
4520 * in the dedicated proxy area for later troubleshooting.
4521 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004522 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004523 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004524
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004525 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004526 txn->req.msg_state = HTTP_MSG_ERROR;
4527 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004528 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004529
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004530 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004531 if (sess->listener->counters)
4532 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004533
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004534 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02004535 if (!(s->flags & SF_ERR_MASK))
4536 s->flags |= SF_ERR_PRXCOND;
4537 if (!(s->flags & SF_FINST_MASK))
4538 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004539
Christopher Faulet0184ea72017-01-05 14:06:34 +01004540 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004541 req->analyse_exp = TICK_ETERNITY;
4542 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004543
4544 return_prx_yield:
4545 channel_dont_connect(req);
4546 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004547}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004548
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004549/* This function performs all the processing enabled for the current request.
4550 * It returns 1 if the processing can continue on next analysers, or zero if it
4551 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004552 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004553 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004554int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004555{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004556 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004557 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004558 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02004559 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004560
Willy Tarreau655dce92009-11-08 13:10:58 +01004561 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004562 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004563 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004564 return 0;
4565 }
4566
Willy Tarreau87b09662015-04-03 00:22:06 +02004567 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 +02004568 now_ms, __FUNCTION__,
4569 s,
4570 req,
4571 req->rex, req->wex,
4572 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004573 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004574 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004575
Willy Tarreau59234e92008-11-30 23:51:27 +01004576 /*
4577 * Right now, we know that we have processed the entire headers
4578 * and that unwanted requests have been filtered out. We can do
4579 * whatever we want with the remaining request. Also, now we
4580 * may have separate values for ->fe, ->be.
4581 */
Willy Tarreau06619262006-12-17 08:37:22 +01004582
Willy Tarreau59234e92008-11-30 23:51:27 +01004583 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004584 * If HTTP PROXY is set we simply get remote server address parsing
4585 * incoming request. Note that this requires that a connection is
4586 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004587 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004588 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004589 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004590 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004591
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004592 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau973a5422015-08-05 21:47:23 +02004593 if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004594 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004595 txn->req.msg_state = HTTP_MSG_ERROR;
4596 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01004597 req->analysers &= AN_REQ_FLT_END;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004598 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004599
Willy Tarreaue7dff022015-04-03 01:14:29 +02004600 if (!(s->flags & SF_ERR_MASK))
4601 s->flags |= SF_ERR_RESOURCE;
4602 if (!(s->flags & SF_FINST_MASK))
4603 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004604
4605 return 0;
4606 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004607
4608 path = http_get_path(txn);
4609 url2sa(req->buf->p + msg->sl.rq.u,
4610 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004611 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004612 /* if the path was found, we have to remove everything between
4613 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4614 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4615 * u_l characters by a single "/".
4616 */
4617 if (path) {
4618 char *cur_ptr = req->buf->p;
4619 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4620 int delta;
4621
4622 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4623 http_msg_move_end(&txn->req, delta);
4624 cur_end += delta;
4625 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4626 goto return_bad_req;
4627 }
4628 else {
4629 char *cur_ptr = req->buf->p;
4630 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4631 int delta;
4632
4633 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4634 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4635 http_msg_move_end(&txn->req, delta);
4636 cur_end += delta;
4637 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4638 goto return_bad_req;
4639 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004640 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004641
Willy Tarreau59234e92008-11-30 23:51:27 +01004642 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004643 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004644 * Note that doing so might move headers in the request, but
4645 * the fields will stay coherent and the URI will not move.
4646 * This should only be performed in the backend.
4647 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004648 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004649 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004650
William Lallemanda73203e2012-03-12 12:48:57 +01004651 /* add unique-id if "header-unique-id" is specified */
4652
Thierry Fournierf4011dd2016-03-29 17:23:51 +02004653 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004654 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4655 goto return_bad_req;
4656 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004657 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004658 }
William Lallemanda73203e2012-03-12 12:48:57 +01004659
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004660 if (sess->fe->header_unique_id && s->unique_id) {
4661 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004662 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004663 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004664 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004665 goto return_bad_req;
4666 }
4667
Cyril Bontéb21570a2009-11-29 20:04:48 +01004668 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004669 * 9: add X-Forwarded-For if either the frontend or the backend
4670 * asks for it.
4671 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004672 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004673 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004674 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
4675 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
4676 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004677 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004678 /* The header is set to be added only if none is present
4679 * and we found it, so don't do anything.
4680 */
4681 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004682 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004683 /* Add an X-Forwarded-For header unless the source IP is
4684 * in the 'except' network range.
4685 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004686 if ((!sess->fe->except_mask.s_addr ||
4687 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
4688 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01004689 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004690 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004691 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004692 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004693 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004694 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004695
4696 /* Note: we rely on the backend to get the header name to be used for
4697 * x-forwarded-for, because the header is really meant for the backends.
4698 * However, if the backend did not specify any option, we have to rely
4699 * on the frontend's header name.
4700 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004701 if (s->be->fwdfor_hdr_len) {
4702 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004703 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004704 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004705 len = sess->fe->fwdfor_hdr_len;
4706 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004707 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004708 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 +01004709
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004710 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004711 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004712 }
4713 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004714 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004715 /* FIXME: for the sake of completeness, we should also support
4716 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004717 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004718 int len;
4719 char pn[INET6_ADDRSTRLEN];
4720 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004721 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004722 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004723
Willy Tarreau59234e92008-11-30 23:51:27 +01004724 /* Note: we rely on the backend to get the header name to be used for
4725 * x-forwarded-for, because the header is really meant for the backends.
4726 * However, if the backend did not specify any option, we have to rely
4727 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004728 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004729 if (s->be->fwdfor_hdr_len) {
4730 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004731 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004732 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004733 len = sess->fe->fwdfor_hdr_len;
4734 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004735 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004736 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004737
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004738 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004739 goto return_bad_req;
4740 }
4741 }
4742
4743 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004744 * 10: add X-Original-To if either the frontend or the backend
4745 * asks for it.
4746 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004747 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004748
4749 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004750 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004751 /* Add an X-Original-To header unless the destination IP is
4752 * in the 'except' network range.
4753 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004754 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004755
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004756 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004757 ((!sess->fe->except_mask_to.s_addr ||
4758 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
4759 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004760 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004761 (((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 +02004762 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004763 int len;
4764 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004765 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004766
4767 /* Note: we rely on the backend to get the header name to be used for
4768 * x-original-to, because the header is really meant for the backends.
4769 * However, if the backend did not specify any option, we have to rely
4770 * on the frontend's header name.
4771 */
4772 if (s->be->orgto_hdr_len) {
4773 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004774 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004775 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004776 len = sess->fe->orgto_hdr_len;
4777 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004778 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004779 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 +02004780
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004781 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004782 goto return_bad_req;
4783 }
4784 }
4785 }
4786
Willy Tarreau50fc7772012-11-11 22:19:57 +01004787 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4788 * If an "Upgrade" token is found, the header is left untouched in order not to have
4789 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4790 * "Upgrade" is present in the Connection header.
4791 */
4792 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4793 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004794 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004795 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004796 unsigned int want_flags = 0;
4797
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004798 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004799 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004800 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004801 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004802 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004803 want_flags |= TX_CON_CLO_SET;
4804 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004805 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004806 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004807 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004808 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004809 want_flags |= TX_CON_KAL_SET;
4810 }
4811
4812 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004813 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004814 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004815
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004816
Willy Tarreau522d6c02009-12-06 18:49:18 +01004817 /* If we have no server assigned yet and we're balancing on url_param
4818 * with a POST request, we may be interested in checking the body for
4819 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004820 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004821 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02004822 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004823 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004824 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004825 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004826 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004827
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004828 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004829 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004830 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004831#ifdef TCP_QUICKACK
4832 /* We expect some data from the client. Unless we know for sure
4833 * we already have a full request, we have to re-enable quick-ack
4834 * in case we previously disabled it, otherwise we might cause
4835 * the client to delay further data.
4836 */
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004837 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004838 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004839 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004840 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004841 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004842#endif
4843 }
Willy Tarreau03945942009-12-22 16:50:27 +01004844
Willy Tarreau59234e92008-11-30 23:51:27 +01004845 /*************************************************************
4846 * OK, that's finished for the headers. We have done what we *
4847 * could. Let's switch to the DATA state. *
4848 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004849 req->analyse_exp = TICK_ETERNITY;
4850 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004851
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004852 /* if the server closes the connection, we want to immediately react
4853 * and close the socket to save packets and syscalls.
4854 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004855 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
Willy Tarreau350f4872014-11-28 14:42:25 +01004856 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004857
Willy Tarreau59234e92008-11-30 23:51:27 +01004858 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004859 /* OK let's go on with the BODY now */
4860 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004861
Willy Tarreau59234e92008-11-30 23:51:27 +01004862 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004863 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004864 /* we detected a parsing error. We want to archive this request
4865 * in the dedicated proxy area for later troubleshooting.
4866 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004867 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004868 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004869
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004870 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01004871 txn->req.msg_state = HTTP_MSG_ERROR;
4872 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01004873 req->analysers &= AN_REQ_FLT_END;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004874 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004875
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004876 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004877 if (sess->listener->counters)
4878 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004879
Willy Tarreaue7dff022015-04-03 01:14:29 +02004880 if (!(s->flags & SF_ERR_MASK))
4881 s->flags |= SF_ERR_PRXCOND;
4882 if (!(s->flags & SF_FINST_MASK))
4883 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004884 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004885}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004886
Willy Tarreau60b85b02008-11-30 23:28:40 +01004887/* This function is an analyser which processes the HTTP tarpit. It always
4888 * returns zero, at the beginning because it prevents any other processing
4889 * from occurring, and at the end because it terminates the request.
4890 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004891int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004892{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004893 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004894
4895 /* This connection is being tarpitted. The CLIENT side has
4896 * already set the connect expiration date to the right
4897 * timeout. We just have to check that the client is still
4898 * there and that the timeout has not expired.
4899 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004900 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004901 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004902 !tick_is_expired(req->analyse_exp, now_ms))
4903 return 0;
4904
4905 /* We will set the queue timer to the time spent, just for
4906 * logging purposes. We fake a 500 server error, so that the
4907 * attacker will not suspect his connection has been tarpitted.
4908 * It will not cause trouble to the logs because we can exclude
4909 * the tarpitted connections by filtering on the 'PT' status flags.
4910 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004911 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4912
4913 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004914 if (!(req->flags & CF_READ_ERROR))
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004915 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004916
Christopher Faulet0184ea72017-01-05 14:06:34 +01004917 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004918 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004919
Willy Tarreaue7dff022015-04-03 01:14:29 +02004920 if (!(s->flags & SF_ERR_MASK))
4921 s->flags |= SF_ERR_PRXCOND;
4922 if (!(s->flags & SF_FINST_MASK))
4923 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004924 return 0;
4925}
4926
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004927/* This function is an analyser which waits for the HTTP request body. It waits
4928 * for either the buffer to be full, or the full advertised contents to have
4929 * reached the buffer. It must only be called after the standard HTTP request
4930 * processing has occurred, because it expects the request to be parsed and will
4931 * look for the Expect header. It may send a 100-Continue interim response. It
4932 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4933 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4934 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004935 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004936int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004937{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004938 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004939 struct http_txn *txn = s->txn;
4940 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004941
4942 /* We have to parse the HTTP request body to find any required data.
4943 * "balance url_param check_post" should have been the only way to get
4944 * into this. We were brought here after HTTP header analysis, so all
4945 * related structures are ready.
4946 */
4947
Willy Tarreau890988f2014-04-10 11:59:33 +02004948 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4949 /* This is the first call */
4950 if (msg->msg_state < HTTP_MSG_BODY)
4951 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004952
Willy Tarreau890988f2014-04-10 11:59:33 +02004953 if (msg->msg_state < HTTP_MSG_100_SENT) {
4954 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4955 * send an HTTP/1.1 100 Continue intermediate response.
4956 */
4957 if (msg->flags & HTTP_MSGF_VER_11) {
4958 struct hdr_ctx ctx;
4959 ctx.idx = 0;
4960 /* Expect is allowed in 1.1, look for it */
4961 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4962 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004963 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01004964 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02004965 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004966 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004967 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004968 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004969
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004970 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004971 * req->buf->p still points to the beginning of the message. We
4972 * must save the body in msg->next because it survives buffer
4973 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004974 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004975 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004976
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004977 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004978 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4979 else
4980 msg->msg_state = HTTP_MSG_DATA;
4981 }
4982
Willy Tarreau890988f2014-04-10 11:59:33 +02004983 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4984 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004985 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004986 goto missing_data;
4987
4988 /* OK we have everything we need now */
4989 goto http_end;
4990 }
4991
4992 /* OK here we're parsing a chunked-encoded message */
4993
Willy Tarreau522d6c02009-12-06 18:49:18 +01004994 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004995 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004996 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004997 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004998 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004999 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01005000
Willy Tarreau115acb92009-12-26 13:56:06 +01005001 if (!ret)
5002 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005003 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005004 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01005005 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005006 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01005007 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02005008 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01005009 }
5010
Willy Tarreaud98cf932009-12-27 22:54:55 +01005011 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02005012 * We have the first data byte is in msg->sov + msg->sol. We're waiting
5013 * for at least a whole chunk or the whole content length bytes after
5014 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01005015 */
Willy Tarreau890988f2014-04-10 11:59:33 +02005016 if (msg->msg_state == HTTP_MSG_TRAILERS)
5017 goto http_end;
5018
Willy Tarreaue115b492015-05-01 23:05:14 +02005019 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01005020 goto http_end;
5021
5022 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02005023 /* we get here if we need to wait for more data. If the buffer is full,
5024 * we have the maximum we can expect.
5025 */
5026 if (buffer_full(req->buf, global.tune.maxrewrite))
5027 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01005028
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005029 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01005030 txn->status = 408;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005031 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02005032
Willy Tarreaue7dff022015-04-03 01:14:29 +02005033 if (!(s->flags & SF_ERR_MASK))
5034 s->flags |= SF_ERR_CLITO;
5035 if (!(s->flags & SF_FINST_MASK))
5036 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005037 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01005038 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01005039
5040 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02005041 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01005042 /* Not enough data. We'll re-use the http-request
5043 * timeout here. Ideally, we should set the timeout
5044 * relative to the accept() date. We just set the
5045 * request timeout once at the beginning of the
5046 * request.
5047 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005048 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01005049 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02005050 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01005051 return 0;
5052 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01005053
5054 http_end:
5055 /* The situation will not evolve, so let's give up on the analysis. */
5056 s->logs.tv_request = now; /* update the request timer to reflect full request */
5057 req->analysers &= ~an_bit;
5058 req->analyse_exp = TICK_ETERNITY;
5059 return 1;
5060
5061 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005062 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005063 txn->req.msg_state = HTTP_MSG_ERROR;
5064 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005065 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01005066
Willy Tarreaue7dff022015-04-03 01:14:29 +02005067 if (!(s->flags & SF_ERR_MASK))
5068 s->flags |= SF_ERR_PRXCOND;
5069 if (!(s->flags & SF_FINST_MASK))
5070 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005071
Willy Tarreau522d6c02009-12-06 18:49:18 +01005072 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005073 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005074 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005075 if (sess->listener->counters)
5076 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005077 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01005078}
5079
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005080/* send a server's name with an outgoing request over an established connection.
5081 * Note: this function is designed to be called once the request has been scheduled
5082 * for being forwarded. This is the reason why it rewinds the buffer before
5083 * proceeding.
5084 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01005085int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05005086
5087 struct hdr_ctx ctx;
5088
Mark Lamourinec2247f02012-01-04 13:02:01 -05005089 char *hdr_name = be->server_id_hdr_name;
5090 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02005091 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005092 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005093 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005094
William Lallemandd9e90662012-01-30 17:27:17 +01005095 ctx.idx = 0;
5096
Willy Tarreau211cdec2014-04-17 20:18:08 +02005097 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005098 if (old_o) {
5099 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005100 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02005101 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02005102 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005103 }
5104
Willy Tarreau9b28e032012-10-12 23:49:43 +02005105 old_i = chn->buf->i;
5106 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 -05005107 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005108 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005109 }
5110
5111 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005112 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005113 memcpy(hdr_val, hdr_name, hdr_name_len);
5114 hdr_val += hdr_name_len;
5115 *hdr_val++ = ':';
5116 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005117 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
5118 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005119
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005120 if (old_o) {
5121 /* If this was a forwarded request, we must readjust the amount of
5122 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02005123 * variations. Note that the current state is >= HTTP_MSG_BODY,
5124 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005125 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02005126 old_o += chn->buf->i - old_i;
5127 b_adv(chn->buf, old_o);
5128 txn->req.next -= old_o;
5129 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005130 }
5131
Mark Lamourinec2247f02012-01-04 13:02:01 -05005132 return 0;
5133}
5134
Willy Tarreau610ecce2010-01-04 21:15:02 +01005135/* Terminate current transaction and prepare a new one. This is very tricky
5136 * right now but it works.
5137 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005138void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005139{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005140 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005141 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01005142 struct proxy *be = s->be;
Willy Tarreau323a2d92015-08-04 19:00:17 +02005143 struct connection *srv_conn;
5144 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02005145 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01005146
Willy Tarreau610ecce2010-01-04 21:15:02 +01005147 /* FIXME: We need a more portable way of releasing a backend's and a
5148 * server's connections. We need a safer way to reinitialize buffer
5149 * flags. We also need a more accurate method for computing per-request
5150 * data.
5151 */
Willy Tarreau323a2d92015-08-04 19:00:17 +02005152 srv_conn = objt_conn(s->si[1].end);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005153
Willy Tarreau4213a112013-12-15 10:25:42 +01005154 /* unless we're doing keep-alive, we want to quickly close the connection
5155 * to the server.
5156 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005157 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005158 !si_conn_ready(&s->si[1])) {
5159 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
5160 si_shutr(&s->si[1]);
5161 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01005162 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005163
Willy Tarreaue7dff022015-04-03 01:14:29 +02005164 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau858b1032015-12-07 17:04:59 +01005165 be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01005166 if (unlikely(s->srv_conn))
5167 sess_change_server(s, NULL);
5168 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005169
5170 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02005171 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005172
Willy Tarreaueee5b512015-04-03 23:46:31 +02005173 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005174 int n;
5175
Willy Tarreaueee5b512015-04-03 23:46:31 +02005176 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005177 if (n < 1 || n > 5)
5178 n = 0;
5179
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005180 if (fe->mode == PR_MODE_HTTP) {
5181 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005182 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02005183 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01005184 (be->mode == PR_MODE_HTTP)) {
5185 be->be_counters.p.http.rsp[n]++;
5186 be->be_counters.p.http.cum_req++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005187 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005188 }
5189
5190 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005191 s->logs.bytes_in -= s->req.buf->i;
5192 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005193
5194 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005195 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005196 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005197 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005198 s->do_log(s);
5199 }
5200
Willy Tarreaud713bcc2014-06-25 15:36:04 +02005201 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02005202 stream_stop_content_counters(s);
5203 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02005204
Willy Tarreau610ecce2010-01-04 21:15:02 +01005205 s->logs.accept_date = date; /* user-visible date for logging */
5206 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02005207 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
5208 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005209 tv_zero(&s->logs.tv_request);
5210 s->logs.t_queue = -1;
5211 s->logs.t_connect = -1;
5212 s->logs.t_data = -1;
5213 s->logs.t_close = 0;
5214 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
5215 s->logs.srv_queue_size = 0; /* we will get this number soon */
5216
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005217 s->logs.bytes_in = s->req.total = s->req.buf->i;
5218 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005219
5220 if (s->pend_pos)
5221 pendconn_free(s->pend_pos);
5222
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005223 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005224 if (s->flags & SF_CURR_SESS) {
5225 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005226 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005227 }
Willy Tarreau858b1032015-12-07 17:04:59 +01005228 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005229 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01005230 }
5231
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005232 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005233
Willy Tarreau4213a112013-12-15 10:25:42 +01005234 /* only release our endpoint if we don't intend to reuse the
5235 * connection.
5236 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005237 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005238 !si_conn_ready(&s->si[1])) {
5239 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02005240 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005241 }
5242
Willy Tarreau350f4872014-11-28 14:42:25 +01005243 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
5244 s->si[1].err_type = SI_ET_NONE;
5245 s->si[1].conn_retries = 0; /* used for logging too */
5246 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02005247 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 +01005248 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);
5249 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 +02005250 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
5251 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
5252 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005253
Willy Tarreaueee5b512015-04-03 23:46:31 +02005254 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005255 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02005256 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005257
5258 if (prev_status == 401 || prev_status == 407) {
5259 /* In HTTP keep-alive mode, if we receive a 401, we still have
5260 * a chance of being able to send the visitor again to the same
5261 * server over the same connection. This is required by some
5262 * broken protocols such as NTLM, and anyway whenever there is
5263 * an opportunity for sending the challenge to the proper place,
5264 * it's better to do it (at least it helps with debugging).
5265 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005266 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02005267 if (srv_conn)
5268 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01005269 }
5270
Willy Tarreau53f96852016-02-02 18:50:47 +01005271 /* Never ever allow to reuse a connection from a non-reuse backend */
5272 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
5273 srv_conn->flags |= CO_FL_PRIVATE;
5274
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005275 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005276 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005277
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005278 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005279 s->req.flags |= CF_NEVER_WAIT;
5280 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005281 }
5282
Willy Tarreau610ecce2010-01-04 21:15:02 +01005283 /* if the request buffer is not empty, it means we're
5284 * about to process another request, so send pending
5285 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005286 * Just don't do this if the buffer is close to be full,
5287 * because the request will wait for it to flush a little
5288 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005289 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005290 if (s->req.buf->i) {
5291 if (s->res.buf->o &&
5292 !buffer_full(s->res.buf, global.tune.maxrewrite) &&
5293 bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)
5294 s->res.flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005295 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005296
Willy Tarreau714ea782015-11-25 20:11:11 +01005297 /* we're removing the analysers, we MUST re-enable events detection.
5298 * We don't enable close on the response channel since it's either
5299 * already closed, or in keep-alive with an idle connection handler.
5300 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005301 channel_auto_read(&s->req);
5302 channel_auto_close(&s->req);
5303 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005304
Willy Tarreau1c59bd52015-11-02 20:20:11 +01005305 /* we're in keep-alive with an idle connection, monitor it if not already done */
5306 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02005307 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005308 if (!srv)
5309 si_idle_conn(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01005310 else if (srv_conn->flags & CO_FL_PRIVATE)
Willy Tarreau8dff9982015-08-04 20:45:52 +02005311 si_idle_conn(&s->si[1], &srv->priv_conns);
Willy Tarreau449d74a2015-08-05 17:16:33 +02005312 else if (prev_flags & TX_NOT_FIRST)
5313 /* note: we check the request, not the connection, but
5314 * this is valid for strategies SAFE and AGGR, and in
5315 * case of ALWS, we don't care anyway.
5316 */
5317 si_idle_conn(&s->si[1], &srv->safe_conns);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005318 else
5319 si_idle_conn(&s->si[1], &srv->idle_conns);
Willy Tarreau4320eaa2015-08-05 11:08:30 +02005320 }
Willy Tarreau27375622013-12-17 00:00:28 +01005321
Christopher Faulet3e344292015-11-24 16:24:13 +01005322 if (HAS_FILTERS(s)) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01005323 s->req.analysers &= AN_REQ_FLT_END;
5324 s->res.analysers &= AN_RES_FLT_END;
Christopher Fauletd7c91962015-04-30 11:48:27 +02005325 }
Christopher Faulet3e344292015-11-24 16:24:13 +01005326 else {
5327 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
5328 s->res.analysers = 0;
5329 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005330}
5331
5332
5333/* This function updates the request state machine according to the response
5334 * state machine and buffer flags. It returns 1 if it changes anything (flag
5335 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5336 * it is only used to find when a request/response couple is complete. Both
5337 * this function and its equivalent should loop until both return zero. It
5338 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5339 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005340int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005341{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005342 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005343 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005344 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005345 unsigned int old_state = txn->req.msg_state;
5346
Willy Tarreau610ecce2010-01-04 21:15:02 +01005347 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5348 return 0;
5349
5350 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005351 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005352 * We can shut the read side unless we want to abort_on_close,
5353 * or we have a POST request. The issue with POST requests is
5354 * that some browsers still send a CRLF after the request, and
5355 * this CRLF must be read so that it does not remain in the kernel
5356 * buffers, otherwise a close could cause an RST on some systems
5357 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005358 * Note that if we're using keep-alive on the client side, we'd
5359 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005360 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005361 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005362 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005363 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5364 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5365 !(s->be->options & PR_O_ABRT_CLOSE) &&
5366 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005367 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005368
Willy Tarreau40f151a2012-12-20 12:10:09 +01005369 /* if the server closes the connection, we want to immediately react
5370 * and close the socket to save packets and syscalls.
5371 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005372 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005373
Willy Tarreau7f876a12015-11-18 11:59:55 +01005374 /* In any case we've finished parsing the request so we must
5375 * disable Nagle when sending data because 1) we're not going
5376 * to shut this side, and 2) the server is waiting for us to
5377 * send pending data.
5378 */
5379 chn->flags |= CF_NEVER_WAIT;
5380
Willy Tarreau610ecce2010-01-04 21:15:02 +01005381 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5382 goto wait_other_side;
5383
5384 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5385 /* The server has not finished to respond, so we
5386 * don't want to move in order not to upset it.
5387 */
5388 goto wait_other_side;
5389 }
5390
5391 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5392 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005393 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005394 txn->req.msg_state = HTTP_MSG_TUNNEL;
5395 goto wait_other_side;
5396 }
5397
5398 /* When we get here, it means that both the request and the
5399 * response have finished receiving. Depending on the connection
5400 * mode, we'll have to wait for the last bytes to leave in either
5401 * direction, and sometimes for a close to be effective.
5402 */
5403
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005404 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5405 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005406 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5407 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005408 }
5409 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5410 /* Option forceclose is set, or either side wants to close,
5411 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005412 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005413 * once both states are CLOSED.
5414 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005415 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5416 channel_shutr_now(chn);
5417 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005418 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005419 }
5420 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005421 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5422 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005423 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005424 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5425 channel_auto_read(chn);
5426 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005427 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005428 }
5429
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005430 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005431 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005432 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005433
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005434 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005435 txn->req.msg_state = HTTP_MSG_CLOSING;
5436 goto http_msg_closing;
5437 }
5438 else {
5439 txn->req.msg_state = HTTP_MSG_CLOSED;
5440 goto http_msg_closed;
5441 }
5442 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005443 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005444 }
5445
5446 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5447 http_msg_closing:
5448 /* nothing else to forward, just waiting for the output buffer
5449 * to be empty and for the shutw_now to take effect.
5450 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005451 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005452 txn->req.msg_state = HTTP_MSG_CLOSED;
5453 goto http_msg_closed;
5454 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005455 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005456 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005457 txn->req.msg_state = HTTP_MSG_ERROR;
5458 goto wait_other_side;
5459 }
5460 }
5461
5462 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5463 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005464 /* see above in MSG_DONE why we only do this in these states */
5465 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5466 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5467 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005468 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005469 goto wait_other_side;
5470 }
5471
5472 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005473 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005474}
5475
5476
5477/* This function updates the response state machine according to the request
5478 * state machine and buffer flags. It returns 1 if it changes anything (flag
5479 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5480 * it is only used to find when a request/response couple is complete. Both
5481 * this function and its equivalent should loop until both return zero. It
5482 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5483 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005484int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005485{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005486 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005487 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005488 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005489 unsigned int old_state = txn->rsp.msg_state;
5490
Willy Tarreau610ecce2010-01-04 21:15:02 +01005491 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5492 return 0;
5493
5494 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5495 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005496 * still monitor the server connection for a possible close
5497 * while the request is being uploaded, so we don't disable
5498 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005499 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005500 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005501
5502 if (txn->req.msg_state == HTTP_MSG_ERROR)
5503 goto wait_other_side;
5504
5505 if (txn->req.msg_state < HTTP_MSG_DONE) {
5506 /* The client seems to still be sending data, probably
5507 * because we got an error response during an upload.
5508 * We have the choice of either breaking the connection
5509 * or letting it pass through. Let's do the later.
5510 */
5511 goto wait_other_side;
5512 }
5513
5514 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5515 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005516 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005517 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005518 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005519 goto wait_other_side;
5520 }
5521
5522 /* When we get here, it means that both the request and the
5523 * response have finished receiving. Depending on the connection
5524 * mode, we'll have to wait for the last bytes to leave in either
5525 * direction, and sometimes for a close to be effective.
5526 */
5527
5528 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5529 /* Server-close mode : shut read and wait for the request
5530 * side to close its output buffer. The caller will detect
5531 * when we're in DONE and the other is in CLOSED and will
5532 * catch that for the final cleanup.
5533 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005534 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5535 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005536 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005537 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5538 /* Option forceclose is set, or either side wants to close,
5539 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005540 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005541 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005542 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005543 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5544 channel_shutr_now(chn);
5545 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005546 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005547 }
5548 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005549 /* The last possible modes are keep-alive and tunnel. Tunnel will
5550 * need to forward remaining data. Keep-alive will need to monitor
5551 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005552 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005553 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005554 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005555 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5556 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005557 }
5558
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005559 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005560 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005561 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005562 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5563 goto http_msg_closing;
5564 }
5565 else {
5566 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5567 goto http_msg_closed;
5568 }
5569 }
5570 goto wait_other_side;
5571 }
5572
5573 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5574 http_msg_closing:
5575 /* nothing else to forward, just waiting for the output buffer
5576 * to be empty and for the shutw_now to take effect.
5577 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005578 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005579 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5580 goto http_msg_closed;
5581 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005582 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005583 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005584 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005585 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005586 if (objt_server(s->target))
5587 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005588 goto wait_other_side;
5589 }
5590 }
5591
5592 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5593 http_msg_closed:
5594 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005595 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005596 channel_auto_close(chn);
5597 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005598 goto wait_other_side;
5599 }
5600
5601 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005602 /* We force the response to leave immediately if we're waiting for the
5603 * other side, since there is no pending shutdown to push it out.
5604 */
5605 if (!channel_is_empty(chn))
5606 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005607 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005608}
5609
5610
5611/* Resync the request and response state machines. Return 1 if either state
5612 * changes.
5613 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005614int http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005615{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005616 struct http_txn *txn = s->txn;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005617 int old_req_state = txn->req.msg_state;
5618 int old_res_state = txn->rsp.msg_state;
5619
Willy Tarreau610ecce2010-01-04 21:15:02 +01005620 http_sync_req_state(s);
5621 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005622 if (!http_sync_res_state(s))
5623 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005624 if (!http_sync_req_state(s))
5625 break;
5626 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005627
Willy Tarreau610ecce2010-01-04 21:15:02 +01005628 /* OK, both state machines agree on a compatible state.
5629 * There are a few cases we're interested in :
5630 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5631 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5632 * directions, so let's simply disable both analysers.
5633 * - HTTP_MSG_CLOSED on the response only means we must abort the
5634 * request.
5635 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5636 * with server-close mode means we've completed one request and we
5637 * must re-initialize the server connection.
5638 */
5639
5640 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5641 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5642 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5643 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01005644 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005645 channel_auto_close(&s->req);
5646 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005647 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005648 channel_auto_close(&s->res);
5649 channel_auto_read(&s->res);
Christopher Faulet6962f4e2016-11-28 14:21:27 +01005650 if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s))
Christopher Faulet0184ea72017-01-05 14:06:34 +01005651 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet6962f4e2016-11-28 14:21:27 +01005652 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s))
Christopher Faulet0184ea72017-01-05 14:06:34 +01005653 s->res.analysers |= AN_RES_FLT_XFER_DATA;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005654 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005655 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005656 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005657 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005658 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01005659 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005660 channel_auto_close(&s->res);
5661 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005662 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005663 channel_abort(&s->req);
5664 channel_auto_close(&s->req);
5665 channel_auto_read(&s->req);
5666 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005667 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005668 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5669 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005670 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005671 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5672 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5673 /* server-close/keep-alive: terminate this transaction,
5674 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02005675 * a fresh-new transaction, but only once we're sure there's
5676 * enough room in the request and response buffer to process
5677 * another request. The request buffer must not hold any
5678 * pending output data and the request buffer must not have
5679 * output data occupying the reserve.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005680 */
Willy Tarreau3de5bd62016-05-02 16:07:07 +02005681 if (s->req.buf->o)
5682 s->req.flags |= CF_WAKE_WRITE;
5683 else if (channel_congested(&s->res))
5684 s->res.flags |= CF_WAKE_WRITE;
5685 else
5686 http_end_txn_clean_session(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005687 }
5688
Willy Tarreau610ecce2010-01-04 21:15:02 +01005689 return txn->req.msg_state != old_req_state ||
5690 txn->rsp.msg_state != old_res_state;
5691}
5692
Willy Tarreaud98cf932009-12-27 22:54:55 +01005693/* This function is an analyser which forwards request body (including chunk
5694 * sizes if any). It is called as soon as we must forward, even if we forward
5695 * zero byte. The only situation where it must not be called is when we're in
5696 * tunnel mode and we want to forward till the close. It's used both to forward
5697 * remaining data and to resync after end of body. It expects the msg_state to
5698 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005699 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005700 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005701 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005702 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005703int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005704{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005705 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005706 struct http_txn *txn = s->txn;
5707 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01005708 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005709
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005710 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5711 return 0;
5712
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005713 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005714 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005715 /* Output closed while we were sending data. We must abort and
5716 * wake the other side up.
5717 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005718 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005719 msg->msg_state = HTTP_MSG_ERROR;
5720 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005721 return 1;
5722 }
5723
Willy Tarreaud98cf932009-12-27 22:54:55 +01005724 /* Note that we don't have to send 100-continue back because we don't
5725 * need the data to complete our job, and it's up to the server to
5726 * decide whether to return 100, 417 or anything else in return of
5727 * an "Expect: 100-continue" header.
5728 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02005729 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005730 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5731 ? HTTP_MSG_CHUNK_SIZE
5732 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005733
5734 /* TODO/filters: when http-buffer-request option is set or if a
5735 * rule on url_param exists, the first chunk size could be
5736 * already parsed. In that case, msg->next is after the chunk
5737 * size (including the CRLF after the size). So this case should
5738 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005739 }
5740
Willy Tarreau7ba23542014-04-17 21:50:00 +02005741 /* Some post-connect processing might want us to refrain from starting to
5742 * forward data. Currently, the only reason for this is "balance url_param"
5743 * whichs need to parse/process the request after we've enabled forwarding.
5744 */
5745 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005746 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005747 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005748 req->flags |= CF_WAKE_CONNECT;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005749 goto missing_data_or_waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005750 }
5751 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5752 }
5753
Willy Tarreau80a92c02014-03-12 10:41:13 +01005754 /* in most states, we should abort in case of early close */
5755 channel_auto_close(req);
5756
Willy Tarreauefdf0942014-04-24 20:08:57 +02005757 if (req->to_forward) {
5758 /* We can't process the buffer's contents yet */
5759 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005760 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005761 }
5762
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005763 if (msg->msg_state < HTTP_MSG_DONE) {
5764 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5765 ? http_msg_forward_chunked_body(s, msg)
5766 : http_msg_forward_body(s, msg));
5767 if (!ret)
5768 goto missing_data_or_waiting;
5769 if (ret < 0)
5770 goto return_bad_req;
5771 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005772
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005773 /* other states, DONE...TUNNEL */
5774 /* we don't want to forward closes on DONE except in tunnel mode. */
5775 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5776 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005777
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005778 if (http_resync_states(s)) {
5779 /* some state changes occurred, maybe the analyser
5780 * was disabled too. */
5781 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5782 if (req->flags & CF_SHUTW) {
5783 /* request errors are most likely due to the
5784 * server aborting the transfer. */
5785 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005786 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005787 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005788 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005789 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005790 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005791 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005792 }
5793
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005794 /* If "option abortonclose" is set on the backend, we want to monitor
5795 * the client's connection and forward any shutdown notification to the
5796 * server, which will decide whether to close or to go on processing the
5797 * request. We only do that in tunnel mode, and not in other modes since
5798 * it can be abused to exhaust source ports. */
5799 if (s->be->options & PR_O_ABRT_CLOSE) {
5800 channel_auto_read(req);
5801 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
5802 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
5803 s->si[1].flags |= SI_FL_NOLINGER;
5804 channel_auto_close(req);
5805 }
5806 else if (s->txn->meth == HTTP_METH_POST) {
5807 /* POST requests may require to read extra CRLF sent by broken
5808 * browsers and which could cause an RST to be sent upon close
5809 * on some systems (eg: Linux). */
5810 channel_auto_read(req);
5811 }
5812 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005813
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005814 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005815 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005816 if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005817 if (!(s->flags & SF_ERR_MASK))
5818 s->flags |= SF_ERR_CLICL;
5819 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005820 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005821 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005822 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005823 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005824 }
5825
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005826 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005827 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005828 if (objt_server(s->target))
5829 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005830
5831 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005832 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005833
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005834 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005835 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005836 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005837
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005838 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005839 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005840 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005841 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005842 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005843
Willy Tarreau5c620922011-05-11 19:56:11 +02005844 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005845 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005846 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005847 * modes are already handled by the stream sock layer. We must not do
5848 * this in content-length mode because it could present the MSG_MORE
5849 * flag with the last block of forwarded data, which would cause an
5850 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005851 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005852 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005853 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005854
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005855 return 0;
5856
Willy Tarreaud98cf932009-12-27 22:54:55 +01005857 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005858 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005859 if (sess->listener->counters)
5860 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005861
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005862 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005863 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005864 txn->req.msg_state = HTTP_MSG_ERROR;
5865 if (txn->status) {
5866 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005867 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005868 } else {
5869 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005870 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005871 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01005872 req->analysers &= AN_REQ_FLT_END;
5873 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 +01005874
Willy Tarreaue7dff022015-04-03 01:14:29 +02005875 if (!(s->flags & SF_ERR_MASK))
5876 s->flags |= SF_ERR_PRXCOND;
5877 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005878 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005879 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005880 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005881 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005882 }
5883 return 0;
5884
5885 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005886 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005887 txn->req.msg_state = HTTP_MSG_ERROR;
5888 if (txn->status) {
5889 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005890 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005891 } else {
5892 txn->status = 502;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005893 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005894 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01005895 req->analysers &= AN_REQ_FLT_END;
5896 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 +01005897
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005898 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005899 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005900 if (objt_server(s->target))
5901 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005902
Willy Tarreaue7dff022015-04-03 01:14:29 +02005903 if (!(s->flags & SF_ERR_MASK))
5904 s->flags |= SF_ERR_SRVCL;
5905 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005906 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005907 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005908 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005909 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005910 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005911 return 0;
5912}
5913
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005914/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5915 * processing can continue on next analysers, or zero if it either needs more
5916 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005917 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005918 * when it has nothing left to do, and may remove any analyser when it wants to
5919 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005920 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005921int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005922{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005923 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005924 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005925 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005926 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005927 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005928 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005929 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005930
Willy Tarreau87b09662015-04-03 00:22:06 +02005931 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 +02005932 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005933 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005934 rep,
5935 rep->rex, rep->wex,
5936 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005937 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005938 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005939
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005940 /*
5941 * Now parse the partial (or complete) lines.
5942 * We will check the response syntax, and also join multi-line
5943 * headers. An index of all the lines will be elaborated while
5944 * parsing.
5945 *
5946 * For the parsing, we use a 28 states FSM.
5947 *
5948 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005949 * rep->buf->p = beginning of response
5950 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5951 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005952 * msg->eol = end of current header or line (LF or CRLF)
5953 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005954 */
5955
Willy Tarreau628c40c2014-04-24 19:11:26 +02005956 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005957 /* There's a protected area at the end of the buffer for rewriting
5958 * purposes. We don't want to start to parse the request if the
5959 * protected area is affected, because we may have to move processed
5960 * data later, which is much more complicated.
5961 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005962 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005963 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005964 /* some data has still not left the buffer, wake us once that's done */
5965 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5966 goto abort_response;
5967 channel_dont_close(rep);
5968 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005969 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005970 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005971 }
5972
Willy Tarreau379357a2013-06-08 12:55:46 +02005973 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5974 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5975 buffer_slow_realign(rep->buf);
5976
Willy Tarreau9b28e032012-10-12 23:49:43 +02005977 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005978 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005979 }
5980
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005981 /* 1: we might have to print this header in debug mode */
5982 if (unlikely((global.mode & MODE_DEBUG) &&
5983 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005984 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005985 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005986
Willy Tarreau9b28e032012-10-12 23:49:43 +02005987 sol = rep->buf->p;
5988 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005989 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005990
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005991 sol += hdr_idx_first_pos(&txn->hdr_idx);
5992 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005993
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005994 while (cur_idx) {
5995 eol = sol + txn->hdr_idx.v[cur_idx].len;
5996 debug_hdr("srvhdr", s, sol, eol);
5997 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5998 cur_idx = txn->hdr_idx.v[cur_idx].next;
5999 }
6000 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006001
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006002 /*
6003 * Now we quickly check if we have found a full valid response.
6004 * If not so, we check the FD and buffer states before leaving.
6005 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01006006 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006007 * responses are checked first.
6008 *
6009 * Depending on whether the client is still there or not, we
6010 * may send an error response back or not. Note that normally
6011 * we should only check for HTTP status there, and check I/O
6012 * errors somewhere else.
6013 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006014
Willy Tarreau655dce92009-11-08 13:10:58 +01006015 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006016 /* Invalid response */
6017 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6018 /* we detected a parsing error. We want to archive this response
6019 * in the dedicated proxy area for later troubleshooting.
6020 */
6021 hdr_response_bad:
6022 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006023 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006024
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006025 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006026 if (objt_server(s->target)) {
6027 objt_server(s->target)->counters.failed_resp++;
6028 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006029 }
Willy Tarreau64648412010-03-05 10:41:54 +01006030 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006031 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006032 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006033 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006034 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006035 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006036 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006037
Willy Tarreaue7dff022015-04-03 01:14:29 +02006038 if (!(s->flags & SF_ERR_MASK))
6039 s->flags |= SF_ERR_PRXCOND;
6040 if (!(s->flags & SF_FINST_MASK))
6041 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006042
6043 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006044 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006045
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006046 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006047 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02006048 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02006049 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006050 goto hdr_response_bad;
6051 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006052
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006053 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006054 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006055 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006056 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006057 else if (txn->flags & TX_NOT_FIRST)
6058 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02006059
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006060 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006061 if (objt_server(s->target)) {
6062 objt_server(s->target)->counters.failed_resp++;
6063 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006064 }
Willy Tarreau461f6622008-08-15 23:43:19 +02006065
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006066 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006067 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006068 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006069 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006070 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006071 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02006072
Willy Tarreaue7dff022015-04-03 01:14:29 +02006073 if (!(s->flags & SF_ERR_MASK))
6074 s->flags |= SF_ERR_SRVCL;
6075 if (!(s->flags & SF_FINST_MASK))
6076 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02006077 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006078 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006079
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02006080 /* read timeout : return a 504 to the client. */
6081 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006082 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006083 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006084
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006085 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006086 if (objt_server(s->target)) {
6087 objt_server(s->target)->counters.failed_resp++;
6088 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006089 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006090
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006091 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006092 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006093 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01006094 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006095 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006096 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02006097
Willy Tarreaue7dff022015-04-03 01:14:29 +02006098 if (!(s->flags & SF_ERR_MASK))
6099 s->flags |= SF_ERR_SRVTO;
6100 if (!(s->flags & SF_FINST_MASK))
6101 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006102 return 0;
6103 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02006104
Willy Tarreauf003d372012-11-26 13:35:37 +01006105 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006106 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006107 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01006108 s->be->be_counters.cli_aborts++;
6109 if (objt_server(s->target))
6110 objt_server(s->target)->counters.cli_aborts++;
6111
Christopher Faulet0184ea72017-01-05 14:06:34 +01006112 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01006113 channel_auto_close(rep);
6114
6115 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01006116 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006117 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreauf003d372012-11-26 13:35:37 +01006118
Willy Tarreaue7dff022015-04-03 01:14:29 +02006119 if (!(s->flags & SF_ERR_MASK))
6120 s->flags |= SF_ERR_CLICL;
6121 if (!(s->flags & SF_FINST_MASK))
6122 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01006123
Willy Tarreau87b09662015-04-03 00:22:06 +02006124 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01006125 return 0;
6126 }
6127
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006128 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006129 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006130 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006131 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006132 else if (txn->flags & TX_NOT_FIRST)
6133 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006134
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006135 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006136 if (objt_server(s->target)) {
6137 objt_server(s->target)->counters.failed_resp++;
6138 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006139 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006140
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006141 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006142 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006143 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006144 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006145 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006146 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01006147
Willy Tarreaue7dff022015-04-03 01:14:29 +02006148 if (!(s->flags & SF_ERR_MASK))
6149 s->flags |= SF_ERR_SRVCL;
6150 if (!(s->flags & SF_FINST_MASK))
6151 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006152 return 0;
6153 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006154
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006155 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006156 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006157 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006158 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006159 else if (txn->flags & TX_NOT_FIRST)
6160 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006161
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006162 s->be->be_counters.failed_resp++;
Christopher Faulet0184ea72017-01-05 14:06:34 +01006163 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006164 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006165
Willy Tarreaue7dff022015-04-03 01:14:29 +02006166 if (!(s->flags & SF_ERR_MASK))
6167 s->flags |= SF_ERR_CLICL;
6168 if (!(s->flags & SF_FINST_MASK))
6169 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006170
Willy Tarreau87b09662015-04-03 00:22:06 +02006171 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006172 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006173 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006174
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006175 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01006176 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006177 return 0;
6178 }
6179
6180 /* More interesting part now : we know that we have a complete
6181 * response which at least looks like HTTP. We have an indicator
6182 * of each header's length, so we can parse them quickly.
6183 */
6184
6185 if (unlikely(msg->err_pos >= 0))
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006186 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006187
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006188 /*
6189 * 1: get the status code
6190 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006191 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006192 if (n < 1 || n > 5)
6193 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02006194 /* when the client triggers a 4xx from the server, it's most often due
6195 * to a missing object or permission. These events should be tracked
6196 * because if they happen often, it may indicate a brute force or a
6197 * vulnerability scan.
6198 */
6199 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02006200 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02006201
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006202 if (objt_server(s->target))
6203 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006204
Willy Tarreau91852eb2015-05-01 13:26:00 +02006205 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
6206 * exactly one digit "." one digit. This check may be disabled using
6207 * option accept-invalid-http-response.
6208 */
6209 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
6210 if (msg->sl.st.v_l != 8) {
6211 msg->err_pos = 0;
6212 goto hdr_response_bad;
6213 }
6214
6215 if (rep->buf->p[4] != '/' ||
6216 !isdigit((unsigned char)rep->buf->p[5]) ||
6217 rep->buf->p[6] != '.' ||
6218 !isdigit((unsigned char)rep->buf->p[7])) {
6219 msg->err_pos = 4;
6220 goto hdr_response_bad;
6221 }
6222 }
6223
Willy Tarreau5b154472009-12-21 20:11:07 +01006224 /* check if the response is HTTP/1.1 or above */
6225 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006226 ((rep->buf->p[5] > '1') ||
6227 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006228 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006229
6230 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006231 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 +01006232
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006233 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006234 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006235
Willy Tarreau9b28e032012-10-12 23:49:43 +02006236 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006237
Willy Tarreau39650402010-03-15 19:44:39 +01006238 /* Adjust server's health based on status code. Note: status codes 501
6239 * and 505 are triggered on demand by client request, so we must not
6240 * count them as server failures.
6241 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006242 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006243 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006244 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006245 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006246 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006247 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006248
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006249 /*
6250 * 2: check for cacheability.
6251 */
6252
6253 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006254 case 100:
6255 /*
6256 * We may be facing a 100-continue response, in which case this
6257 * is not the right response, and we're waiting for the next one.
6258 * Let's allow this response to go to the client and wait for the
6259 * next one.
6260 */
6261 hdr_idx_init(&txn->hdr_idx);
6262 msg->next -= channel_forward(rep, msg->next);
6263 msg->msg_state = HTTP_MSG_RPBEFORE;
6264 txn->status = 0;
6265 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01006266 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02006267 goto next_one;
6268
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006269 case 200:
6270 case 203:
6271 case 206:
6272 case 300:
6273 case 301:
6274 case 410:
6275 /* RFC2616 @13.4:
6276 * "A response received with a status code of
6277 * 200, 203, 206, 300, 301 or 410 MAY be stored
6278 * by a cache (...) unless a cache-control
6279 * directive prohibits caching."
6280 *
6281 * RFC2616 @9.5: POST method :
6282 * "Responses to this method are not cacheable,
6283 * unless the response includes appropriate
6284 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006285 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006286 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006287 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006288 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6289 break;
6290 default:
6291 break;
6292 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006293
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006294 /*
6295 * 3: we may need to capture headers
6296 */
6297 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006298 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006299 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006300 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006301
Willy Tarreau557f1992015-05-01 10:05:17 +02006302 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6303 * by RFC7230#3.3.3 :
6304 *
6305 * The length of a message body is determined by one of the following
6306 * (in order of precedence):
6307 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006308 * 1. Any 2xx (Successful) response to a CONNECT request implies that
6309 * the connection will become a tunnel immediately after the empty
6310 * line that concludes the header fields. A client MUST ignore
6311 * any Content-Length or Transfer-Encoding header fields received
6312 * in such a message. Any 101 response (Switching Protocols) is
6313 * managed in the same manner.
6314 *
6315 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02006316 * (Informational), 204 (No Content), or 304 (Not Modified) status
6317 * code is always terminated by the first empty line after the
6318 * header fields, regardless of the header fields present in the
6319 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006320 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006321 * 3. If a Transfer-Encoding header field is present and the chunked
6322 * transfer coding (Section 4.1) is the final encoding, the message
6323 * body length is determined by reading and decoding the chunked
6324 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006325 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006326 * If a Transfer-Encoding header field is present in a response and
6327 * the chunked transfer coding is not the final encoding, the
6328 * message body length is determined by reading the connection until
6329 * it is closed by the server. If a Transfer-Encoding header field
6330 * is present in a request and the chunked transfer coding is not
6331 * the final encoding, the message body length cannot be determined
6332 * reliably; the server MUST respond with the 400 (Bad Request)
6333 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006334 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006335 * If a message is received with both a Transfer-Encoding and a
6336 * Content-Length header field, the Transfer-Encoding overrides the
6337 * Content-Length. Such a message might indicate an attempt to
6338 * perform request smuggling (Section 9.5) or response splitting
6339 * (Section 9.4) and ought to be handled as an error. A sender MUST
6340 * remove the received Content-Length field prior to forwarding such
6341 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006342 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006343 * 4. If a message is received without Transfer-Encoding and with
6344 * either multiple Content-Length header fields having differing
6345 * field-values or a single Content-Length header field having an
6346 * invalid value, then the message framing is invalid and the
6347 * recipient MUST treat it as an unrecoverable error. If this is a
6348 * request message, the server MUST respond with a 400 (Bad Request)
6349 * status code and then close the connection. If this is a response
6350 * message received by a proxy, the proxy MUST close the connection
6351 * to the server, discard the received response, and send a 502 (Bad
6352 * Gateway) response to the client. If this is a response message
6353 * received by a user agent, the user agent MUST close the
6354 * connection to the server and discard the received response.
6355 *
6356 * 5. If a valid Content-Length header field is present without
6357 * Transfer-Encoding, its decimal value defines the expected message
6358 * body length in octets. If the sender closes the connection or
6359 * the recipient times out before the indicated number of octets are
6360 * received, the recipient MUST consider the message to be
6361 * incomplete and close the connection.
6362 *
6363 * 6. If this is a request message and none of the above are true, then
6364 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006365 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006366 * 7. Otherwise, this is a response message without a declared message
6367 * body length, so the message body length is determined by the
6368 * number of octets received prior to the server closing the
6369 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006370 */
6371
6372 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006373 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006374 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006375 * FIXME: should we parse anyway and return an error on chunked encoding ?
6376 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006377 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6378 txn->status == 101)) {
6379 /* Either we've established an explicit tunnel, or we're
6380 * switching the protocol. In both cases, we're very unlikely
6381 * to understand the next protocols. We have to switch to tunnel
6382 * mode, so that we transfer the request and responses then let
6383 * this protocol pass unmodified. When we later implement specific
6384 * parsers for such protocols, we'll want to check the Upgrade
6385 * header which contains information about that protocol for
6386 * responses with status 101 (eg: see RFC2817 about TLS).
6387 */
6388 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6389 msg->flags |= HTTP_MSGF_XFER_LEN;
6390 goto end;
6391 }
6392
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006393 if (txn->meth == HTTP_METH_HEAD ||
6394 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006395 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006396 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006397 goto skip_content_length;
6398 }
6399
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006400 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006401 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02006402 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006403 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006404 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6405 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006406 /* bad transfer-encoding (chunked followed by something else) */
6407 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006408 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006409 break;
6410 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006411 }
6412
Willy Tarreau1c913912015-04-30 10:57:51 +02006413 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006414 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006415 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02006416 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6417 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6418 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006419 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006420 signed long long cl;
6421
Willy Tarreauad14f752011-09-02 20:33:27 +02006422 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006423 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006424 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006425 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006426
Willy Tarreauad14f752011-09-02 20:33:27 +02006427 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006428 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006429 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006430 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006431
Willy Tarreauad14f752011-09-02 20:33:27 +02006432 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006433 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006434 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006435 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006436
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006437 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006438 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006439 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006440 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006441
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006442 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006443 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006444 }
6445
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006446 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006447 /* Now we have to check if we need to modify the Connection header.
6448 * This is more difficult on the response than it is on the request,
6449 * because we can have two different HTTP versions and we don't know
6450 * how the client will interprete a response. For instance, let's say
6451 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6452 * HTTP/1.1 response without any header. Maybe it will bound itself to
6453 * HTTP/1.0 because it only knows about it, and will consider the lack
6454 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6455 * the lack of header as a keep-alive. Thus we will use two flags
6456 * indicating how a request MAY be understood by the client. In case
6457 * of multiple possibilities, we'll fix the header to be explicit. If
6458 * ambiguous cases such as both close and keepalive are seen, then we
6459 * will fall back to explicit close. Note that we won't take risks with
6460 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006461 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006462 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006463 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6464 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
6465 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6466 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006467 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006468
Willy Tarreau70dffda2014-01-30 03:07:23 +01006469 /* this situation happens when combining pretend-keepalive with httpclose. */
6470 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006471 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006472 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006473 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6474
Willy Tarreau60466522010-01-18 19:08:45 +01006475 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006476 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006477 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6478 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006479
Willy Tarreau60466522010-01-18 19:08:45 +01006480 /* now adjust header transformations depending on current state */
6481 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6482 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6483 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006484 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006485 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006486 }
Willy Tarreau60466522010-01-18 19:08:45 +01006487 else { /* SCL / KAL */
6488 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006489 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006490 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006491 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006492
Willy Tarreau60466522010-01-18 19:08:45 +01006493 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006494 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006495
Willy Tarreau60466522010-01-18 19:08:45 +01006496 /* Some keep-alive responses are converted to Server-close if
6497 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006498 */
Willy Tarreau60466522010-01-18 19:08:45 +01006499 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6500 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006501 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006502 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006503 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006504 }
6505
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006506 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02006507 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006508 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006509
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006510 /* end of job, return OK */
6511 rep->analysers &= ~an_bit;
6512 rep->analyse_exp = TICK_ETERNITY;
6513 channel_auto_close(rep);
6514 return 1;
6515
6516 abort_keep_alive:
6517 /* A keep-alive request to the server failed on a network error.
6518 * The client is required to retry. We need to close without returning
6519 * any other information so that the client retries.
6520 */
6521 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01006522 rep->analysers &= AN_RES_FLT_END;
6523 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006524 channel_auto_close(rep);
6525 s->logs.logwait = 0;
6526 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006527 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006528 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006529 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006530 return 0;
6531}
6532
6533/* This function performs all the processing enabled for the current response.
6534 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006535 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006536 * other functions. It works like process_request (see indications above).
6537 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006538int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006539{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006540 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006541 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006542 struct http_msg *msg = &txn->rsp;
6543 struct proxy *cur_proxy;
6544 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006545 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006546
Willy Tarreau87b09662015-04-03 00:22:06 +02006547 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 +02006548 now_ms, __FUNCTION__,
6549 s,
6550 rep,
6551 rep->rex, rep->wex,
6552 rep->flags,
6553 rep->buf->i,
6554 rep->analysers);
6555
6556 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6557 return 0;
6558
Willy Tarreau70730dd2014-04-24 18:06:27 +02006559 /* The stats applet needs to adjust the Connection header but we don't
6560 * apply any filter there.
6561 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006562 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6563 rep->analysers &= ~an_bit;
6564 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006565 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006566 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006567
Willy Tarreau58975672014-04-24 21:13:57 +02006568 /*
6569 * We will have to evaluate the filters.
6570 * As opposed to version 1.2, now they will be evaluated in the
6571 * filters order and not in the header order. This means that
6572 * each filter has to be validated among all headers.
6573 *
6574 * Filters are tried with ->be first, then with ->fe if it is
6575 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006576 *
6577 * Maybe we are in resume condiion. In this case I choose the
6578 * "struct proxy" which contains the rule list matching the resume
6579 * pointer. If none of theses "struct proxy" match, I initialise
6580 * the process with the first one.
6581 *
6582 * In fact, I check only correspondance betwwen the current list
6583 * pointer and the ->fe rule list. If it doesn't match, I initialize
6584 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006585 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006586 if (s->current_rule_list == &sess->fe->http_res_rules)
6587 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006588 else
6589 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006590 while (1) {
6591 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006592
Willy Tarreau58975672014-04-24 21:13:57 +02006593 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02006594 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02006595 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006596
Willy Tarreau51d861a2015-05-22 17:30:48 +02006597 if (ret == HTTP_RULE_RES_BADREQ)
6598 goto return_srv_prx_502;
6599
6600 if (ret == HTTP_RULE_RES_DONE) {
6601 rep->analysers &= ~an_bit;
6602 rep->analyse_exp = TICK_ETERNITY;
6603 return 1;
6604 }
6605 }
6606
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006607 /* we need to be called again. */
6608 if (ret == HTTP_RULE_RES_YIELD) {
6609 channel_dont_close(rep);
6610 return 0;
6611 }
6612
Willy Tarreau58975672014-04-24 21:13:57 +02006613 /* try headers filters */
6614 if (rule_set->rsp_exp != NULL) {
6615 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6616 return_bad_resp:
6617 if (objt_server(s->target)) {
6618 objt_server(s->target)->counters.failed_resp++;
6619 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006620 }
Willy Tarreau58975672014-04-24 21:13:57 +02006621 s->be->be_counters.failed_resp++;
6622 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01006623 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02006624 txn->status = 502;
6625 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006626 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006627 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006628 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006629 if (!(s->flags & SF_ERR_MASK))
6630 s->flags |= SF_ERR_PRXCOND;
6631 if (!(s->flags & SF_FINST_MASK))
6632 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006633 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006634 }
Willy Tarreau58975672014-04-24 21:13:57 +02006635 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006636
Willy Tarreau58975672014-04-24 21:13:57 +02006637 /* has the response been denied ? */
6638 if (txn->flags & TX_SVDENY) {
6639 if (objt_server(s->target))
6640 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006641
Willy Tarreau58975672014-04-24 21:13:57 +02006642 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006643 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006644 if (sess->listener->counters)
6645 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006646
Willy Tarreau58975672014-04-24 21:13:57 +02006647 goto return_srv_prx_502;
6648 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006649
Willy Tarreau58975672014-04-24 21:13:57 +02006650 /* add response headers from the rule sets in the same order */
6651 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006652 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006653 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006654 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006655 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02006656 ret = acl_pass(ret);
6657 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6658 ret = !ret;
6659 if (!ret)
6660 continue;
6661 }
6662 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6663 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006664 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006665
Willy Tarreau58975672014-04-24 21:13:57 +02006666 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006667 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006668 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006669 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006670 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006671
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006672 /* After this point, this anayzer can't return yield, so we can
6673 * remove the bit corresponding to this analyzer from the list.
6674 *
6675 * Note that the intermediate returns and goto found previously
6676 * reset the analyzers.
6677 */
6678 rep->analysers &= ~an_bit;
6679 rep->analyse_exp = TICK_ETERNITY;
6680
Willy Tarreau58975672014-04-24 21:13:57 +02006681 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006682 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006683 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006684
Willy Tarreau58975672014-04-24 21:13:57 +02006685 /*
6686 * Now check for a server cookie.
6687 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006688 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02006689 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006690
Willy Tarreau58975672014-04-24 21:13:57 +02006691 /*
6692 * Check for cache-control or pragma headers if required.
6693 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006694 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 +02006695 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006696
Willy Tarreau58975672014-04-24 21:13:57 +02006697 /*
6698 * Add server cookie in the response if needed
6699 */
6700 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6701 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006702 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006703 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6704 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6705 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6706 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6707 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006708 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006709 /* the server is known, it's not the one the client requested, or the
6710 * cookie's last seen date needs to be refreshed. We have to
6711 * insert a set-cookie here, except if we want to insert only on POST
6712 * requests and this one isn't. Note that servers which don't have cookies
6713 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006714 */
Willy Tarreau58975672014-04-24 21:13:57 +02006715 if (!objt_server(s->target)->cookie) {
6716 chunk_printf(&trash,
6717 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6718 s->be->cookie_name);
6719 }
6720 else {
6721 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006722
Willy Tarreau58975672014-04-24 21:13:57 +02006723 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6724 /* emit last_date, which is mandatory */
6725 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6726 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6727 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006728
Willy Tarreau58975672014-04-24 21:13:57 +02006729 if (s->be->cookie_maxlife) {
6730 /* emit first_date, which is either the original one or
6731 * the current date.
6732 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006733 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006734 s30tob64(txn->cookie_first_date ?
6735 txn->cookie_first_date >> 2 :
6736 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006737 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006738 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006739 }
Willy Tarreau58975672014-04-24 21:13:57 +02006740 chunk_appendf(&trash, "; path=/");
6741 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006742
Willy Tarreau58975672014-04-24 21:13:57 +02006743 if (s->be->cookie_domain)
6744 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006745
Willy Tarreau58975672014-04-24 21:13:57 +02006746 if (s->be->ck_opts & PR_CK_HTTPONLY)
6747 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006748
Willy Tarreau58975672014-04-24 21:13:57 +02006749 if (s->be->ck_opts & PR_CK_SECURE)
6750 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006751
Willy Tarreau58975672014-04-24 21:13:57 +02006752 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6753 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006754
Willy Tarreau58975672014-04-24 21:13:57 +02006755 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006756 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006757 /* the server did not change, only the date was updated */
6758 txn->flags |= TX_SCK_UPDATED;
6759 else
6760 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006761
Willy Tarreau58975672014-04-24 21:13:57 +02006762 /* Here, we will tell an eventual cache on the client side that we don't
6763 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6764 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6765 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006766 */
Willy Tarreau58975672014-04-24 21:13:57 +02006767 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006768
Willy Tarreau58975672014-04-24 21:13:57 +02006769 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006770
Willy Tarreau58975672014-04-24 21:13:57 +02006771 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6772 "Cache-control: private", 22) < 0))
6773 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006774 }
Willy Tarreau58975672014-04-24 21:13:57 +02006775 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006776
Willy Tarreau58975672014-04-24 21:13:57 +02006777 /*
6778 * Check if result will be cacheable with a cookie.
6779 * We'll block the response if security checks have caught
6780 * nasty things such as a cacheable cookie.
6781 */
6782 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6783 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6784 (s->be->options & PR_O_CHK_CACHE)) {
6785 /* we're in presence of a cacheable response containing
6786 * a set-cookie header. We'll block it as requested by
6787 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006788 */
Willy Tarreau58975672014-04-24 21:13:57 +02006789 if (objt_server(s->target))
6790 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006791
Willy Tarreau58975672014-04-24 21:13:57 +02006792 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006793 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006794 if (sess->listener->counters)
6795 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006796
Willy Tarreau58975672014-04-24 21:13:57 +02006797 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6798 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6799 send_log(s->be, LOG_ALERT,
6800 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6801 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6802 goto return_srv_prx_502;
6803 }
Willy Tarreau03945942009-12-22 16:50:27 +01006804
Willy Tarreau70730dd2014-04-24 18:06:27 +02006805 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006806 /*
6807 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6808 * If an "Upgrade" token is found, the header is left untouched in order
6809 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006810 * if anything but "Upgrade" is present in the Connection header. We don't
6811 * want to touch any 101 response either since it's switching to another
6812 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006813 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006814 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006815 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006816 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006817 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6818 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006819
Willy Tarreau58975672014-04-24 21:13:57 +02006820 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6821 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6822 /* we want a keep-alive response here. Keep-alive header
6823 * required if either side is not 1.1.
6824 */
6825 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6826 want_flags |= TX_CON_KAL_SET;
6827 }
6828 else {
6829 /* we want a close response here. Close header required if
6830 * the server is 1.1, regardless of the client.
6831 */
6832 if (msg->flags & HTTP_MSGF_VER_11)
6833 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006834 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006835
Willy Tarreau58975672014-04-24 21:13:57 +02006836 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6837 http_change_connection_header(txn, msg, want_flags);
6838 }
6839
6840 skip_header_mangling:
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006841 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_DATA_FILTERS(s, rep) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006842 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01006843 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
Willy Tarreau58975672014-04-24 21:13:57 +02006844 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Christopher Fauletd7c91962015-04-30 11:48:27 +02006845 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006846
Willy Tarreau58975672014-04-24 21:13:57 +02006847 /* if the user wants to log as soon as possible, without counting
6848 * bytes from the server, then this is the right moment. We have
6849 * to temporarily assign bytes_out to log what we currently have.
6850 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006851 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006852 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6853 s->logs.bytes_out = txn->rsp.eoh;
6854 s->do_log(s);
6855 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006856 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006857 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006858}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006859
Willy Tarreaud98cf932009-12-27 22:54:55 +01006860/* This function is an analyser which forwards response body (including chunk
6861 * sizes if any). It is called as soon as we must forward, even if we forward
6862 * zero byte. The only situation where it must not be called is when we're in
6863 * tunnel mode and we want to forward till the close. It's used both to forward
6864 * remaining data and to resync after end of body. It expects the msg_state to
6865 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006866 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006867 *
6868 * It is capable of compressing response data both in content-length mode and
6869 * in chunked mode. The state machines follows different flows depending on
6870 * whether content-length and chunked modes are used, since there are no
6871 * trailers in content-length :
6872 *
6873 * chk-mode cl-mode
6874 * ,----- BODY -----.
6875 * / \
6876 * V size > 0 V chk-mode
6877 * .--> SIZE -------------> DATA -------------> CRLF
6878 * | | size == 0 | last byte |
6879 * | v final crlf v inspected |
6880 * | TRAILERS -----------> DONE |
6881 * | |
6882 * `----------------------------------------------'
6883 *
6884 * Compression only happens in the DATA state, and must be flushed in final
6885 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6886 * is performed at once on final states for all bytes parsed, or when leaving
6887 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006888 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006889int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006890{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006891 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006892 struct http_txn *txn = s->txn;
6893 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01006894 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006895
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006896 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6897 return 0;
6898
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006899 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006900 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006901 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006902 /* Output closed while we were sending data. We must abort and
6903 * wake the other side up.
6904 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006905 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006906 msg->msg_state = HTTP_MSG_ERROR;
6907 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006908 return 1;
6909 }
6910
Willy Tarreau4fe41902010-06-07 22:27:41 +02006911 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006912 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006913
Christopher Fauletd7c91962015-04-30 11:48:27 +02006914 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006915 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
6916 ? HTTP_MSG_CHUNK_SIZE
6917 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006918 }
6919
Willy Tarreauefdf0942014-04-24 20:08:57 +02006920 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006921 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02006922 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006923 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02006924 }
6925
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006926 if (msg->msg_state < HTTP_MSG_DONE) {
6927 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
6928 ? http_msg_forward_chunked_body(s, msg)
6929 : http_msg_forward_body(s, msg));
6930 if (!ret)
6931 goto missing_data_or_waiting;
6932 if (ret < 0)
6933 goto return_bad_res;
6934 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02006935
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006936 /* other states, DONE...TUNNEL */
6937 /* for keep-alive we don't want to forward closes on DONE */
6938 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6939 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
6940 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006941
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006942 if (http_resync_states(s)) {
6943 /* some state changes occurred, maybe the analyser was disabled
6944 * too. */
6945 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6946 if (res->flags & CF_SHUTW) {
6947 /* response errors are most likely due to the
6948 * client aborting the transfer. */
6949 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01006950 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006951 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006952 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006953 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006954 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006955 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006956 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02006957 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006958
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006959 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01006960 if (res->flags & CF_SHUTW)
6961 goto aborted_xfer;
6962
6963 /* stop waiting for data if the input is closed before the end. If the
6964 * client side was already closed, it means that the client has aborted,
6965 * so we don't want to count this as a server abort. Otherwise it's a
6966 * server abort.
6967 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006968 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006969 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006970 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006971 /* If we have some pending data, we continue the processing */
6972 if (!buffer_pending(res->buf)) {
6973 if (!(s->flags & SF_ERR_MASK))
6974 s->flags |= SF_ERR_SRVCL;
6975 s->be->be_counters.srv_aborts++;
6976 if (objt_server(s->target))
6977 objt_server(s->target)->counters.srv_aborts++;
6978 goto return_bad_res_stats_ok;
6979 }
Willy Tarreau40dba092010-03-04 18:14:51 +01006980 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006981
Willy Tarreau40dba092010-03-04 18:14:51 +01006982 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006983 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006984 goto return_bad_res;
6985
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006986 /* When TE: chunked is used, we need to get there again to parse
6987 * remaining chunks even if the server has closed, so we don't want to
6988 * set CF_DONTCLOSE. Similarly, if the body length is undefined, if
6989 * keep-alive is set on the client side or if there are filters
6990 * registered on the stream, we don't want to forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006991 */
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01006992 if ((msg->flags & HTTP_MSGF_TE_CHNK) || !(msg->flags & HTTP_MSGF_XFER_LEN) ||
6993 HAS_DATA_FILTERS(s, res) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006994 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6995 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006996 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006997
Willy Tarreau5c620922011-05-11 19:56:11 +02006998 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006999 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02007000 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01007001 * modes are already handled by the stream sock layer. We must not do
7002 * this in content-length mode because it could present the MSG_MORE
7003 * flag with the last block of forwarded data, which would cause an
7004 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02007005 */
Christopher Faulet92d36382015-11-05 13:35:03 +01007006 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007007 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02007008
Willy Tarreau87b09662015-04-03 00:22:06 +02007009 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01007010 return 0;
7011
Willy Tarreau40dba092010-03-04 18:14:51 +01007012 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007013 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007014 if (objt_server(s->target))
7015 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007016
7017 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01007018 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007019 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01007020 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01007021 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01007022 res->analysers &= AN_RES_FLT_END;
7023 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 +01007024 if (objt_server(s->target))
7025 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007026
Willy Tarreaue7dff022015-04-03 01:14:29 +02007027 if (!(s->flags & SF_ERR_MASK))
7028 s->flags |= SF_ERR_PRXCOND;
7029 if (!(s->flags & SF_FINST_MASK))
7030 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007031 return 0;
7032
7033 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01007034 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007035 txn->rsp.msg_state = HTTP_MSG_ERROR;
7036 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01007037 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01007038 res->analysers &= AN_RES_FLT_END;
7039 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 +01007040
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007041 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007042 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007043 if (objt_server(s->target))
7044 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007045
Willy Tarreaue7dff022015-04-03 01:14:29 +02007046 if (!(s->flags & SF_ERR_MASK))
7047 s->flags |= SF_ERR_CLICL;
7048 if (!(s->flags & SF_FINST_MASK))
7049 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007050 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007051}
7052
7053
7054static inline int
7055http_msg_forward_body(struct stream *s, struct http_msg *msg)
7056{
7057 struct channel *chn = msg->chn;
7058 int ret;
7059
7060 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
7061
7062 if (msg->msg_state == HTTP_MSG_ENDING)
7063 goto ending;
7064
7065 /* Neither content-length, nor transfer-encoding was found, so we must
7066 * read the body until the server connection is closed. In that case, we
7067 * eat data as they come. Of course, this happens for response only. */
7068 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
7069 unsigned long long len = (chn->buf->i - msg->next);
7070 msg->chunk_len += len;
7071 msg->body_len += len;
7072 }
Christopher Fauletda02e172015-12-04 09:25:05 +01007073 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
7074 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
7075 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007076 msg->next += ret;
7077 msg->chunk_len -= ret;
7078 if (msg->chunk_len) {
7079 /* input empty or output full */
7080 if (chn->buf->i > msg->next)
7081 chn->flags |= CF_WAKE_WRITE;
7082 goto missing_data_or_waiting;
7083 }
7084
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01007085 /* The server still sending data that should be filtered */
7086 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && !(chn->flags & CF_SHUTR))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007087 goto missing_data_or_waiting;
Christopher Fauletf1cc5d02017-02-08 09:45:13 +01007088
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007089 msg->msg_state = HTTP_MSG_ENDING;
7090
7091 ending:
7092 /* we may have some pending data starting at res->buf->p such as a last
7093 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01007094 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
7095 /* default_ret */ msg->next,
7096 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007097 b_adv(chn->buf, ret);
7098 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007099 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
7100 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02007101 if (msg->next)
7102 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007103
Christopher Fauletda02e172015-12-04 09:25:05 +01007104 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
7105 /* default_ret */ 1,
7106 /* on_error */ goto error,
7107 /* on_wait */ goto waiting);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007108 msg->msg_state = HTTP_MSG_DONE;
7109 return 1;
7110
7111 missing_data_or_waiting:
7112 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01007113 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
7114 /* default_ret */ msg->next,
7115 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007116 b_adv(chn->buf, ret);
7117 msg->next -= ret;
7118 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
7119 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01007120 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007121 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02007122 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007123 return 0;
7124 error:
7125 return -1;
7126}
7127
7128static inline int
7129http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
7130{
7131 struct channel *chn = msg->chn;
7132 int ret;
7133
7134 /* Here we have the guarantee to be in one of the following state:
7135 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
7136 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
7137
7138 switch_states:
7139 switch (msg->msg_state) {
7140 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01007141 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
7142 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
7143 /* on_error */ goto error);
7144 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007145 msg->chunk_len -= ret;
7146 if (msg->chunk_len) {
7147 /* input empty or output full */
7148 if (chn->buf->i > msg->next)
7149 chn->flags |= CF_WAKE_WRITE;
7150 goto missing_data_or_waiting;
7151 }
7152
7153 /* nothing left to forward for this chunk*/
7154 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
7155 /* fall through for HTTP_MSG_CHUNK_CRLF */
7156
7157 case HTTP_MSG_CHUNK_CRLF:
7158 /* we want the CRLF after the data */
7159 ret = http_skip_chunk_crlf(msg);
7160 if (ret == 0)
7161 goto missing_data_or_waiting;
7162 if (ret < 0)
7163 goto chunk_parsing_error;
Christopher Faulet113f7de2015-12-14 14:52:13 +01007164 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007165 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
7166 /* fall through for HTTP_MSG_CHUNK_SIZE */
7167
7168 case HTTP_MSG_CHUNK_SIZE:
7169 /* read the chunk size and assign it to ->chunk_len,
7170 * then set ->next to point to the body and switch to
7171 * DATA or TRAILERS state.
7172 */
7173 ret = http_parse_chunk_size(msg);
7174 if (ret == 0)
7175 goto missing_data_or_waiting;
7176 if (ret < 0)
7177 goto chunk_parsing_error;
Christopher Faulet113f7de2015-12-14 14:52:13 +01007178 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007179 if (msg->chunk_len) {
7180 msg->msg_state = HTTP_MSG_DATA;
7181 goto switch_states;
7182 }
7183 msg->msg_state = HTTP_MSG_TRAILERS;
7184 /* fall through for HTTP_MSG_TRAILERS */
7185
7186 case HTTP_MSG_TRAILERS:
7187 ret = http_forward_trailers(msg);
7188 if (ret < 0)
7189 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01007190 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
7191 /* default_ret */ 1,
7192 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007193 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007194 if (!ret)
7195 goto missing_data_or_waiting;
7196 break;
7197
7198 case HTTP_MSG_ENDING:
7199 goto ending;
7200
7201 default:
7202 /* This should no happen in this function */
7203 goto error;
7204 }
7205
7206 msg->msg_state = HTTP_MSG_ENDING;
7207 ending:
7208 /* we may have some pending data starting at res->buf->p such as a last
7209 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01007210 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007211 /* default_ret */ msg->next,
7212 /* on_error */ goto error);
7213 b_adv(chn->buf, ret);
7214 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007215 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
7216 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02007217 if (msg->next)
7218 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007219
Christopher Fauletda02e172015-12-04 09:25:05 +01007220 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007221 /* default_ret */ 1,
7222 /* on_error */ goto error,
7223 /* on_wait */ goto waiting);
7224 msg->msg_state = HTTP_MSG_DONE;
7225 return 1;
7226
7227 missing_data_or_waiting:
7228 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01007229 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007230 /* default_ret */ msg->next,
7231 /* on_error */ goto error);
7232 b_adv(chn->buf, ret);
7233 msg->next -= ret;
7234 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
7235 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01007236 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007237 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02007238 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007239 return 0;
7240
7241 chunk_parsing_error:
7242 if (msg->err_pos >= 0) {
7243 if (chn->flags & CF_ISRESP)
7244 http_capture_bad_message(&s->be->invalid_rep, s, msg,
7245 msg->msg_state, strm_fe(s));
7246 else
7247 http_capture_bad_message(&strm_fe(s)->invalid_req, s,
7248 msg, msg->msg_state, s->be);
7249 }
7250 error:
7251 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007252}
7253
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007254
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007255/* Iterate the same filter through all request headers.
7256 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007257 * Since it can manage the switch to another backend, it updates the per-proxy
7258 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007259 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007260int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007261{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007262 char *cur_ptr, *cur_end, *cur_next;
7263 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007264 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007265 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007266 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01007267
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007268 last_hdr = 0;
7269
Willy Tarreau9b28e032012-10-12 23:49:43 +02007270 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007271 old_idx = 0;
7272
7273 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007274 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007275 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007276 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007277 (exp->action == ACT_ALLOW ||
7278 exp->action == ACT_DENY ||
7279 exp->action == ACT_TARPIT))
7280 return 0;
7281
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007282 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007283 if (!cur_idx)
7284 break;
7285
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007286 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007287 cur_ptr = cur_next;
7288 cur_end = cur_ptr + cur_hdr->len;
7289 cur_next = cur_end + cur_hdr->cr + 1;
7290
7291 /* Now we have one header between cur_ptr and cur_end,
7292 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007293 */
7294
Willy Tarreau15a53a42015-01-21 13:39:42 +01007295 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007296 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007297 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007298 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007299 last_hdr = 1;
7300 break;
7301
7302 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007303 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007304 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007305 break;
7306
7307 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007308 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007309 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007310 break;
7311
7312 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007313 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7314 if (trash.len < 0)
7315 return -1;
7316
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007317 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007318 /* FIXME: if the user adds a newline in the replacement, the
7319 * index will not be recalculated for now, and the new line
7320 * will not be counted as a new header.
7321 */
7322
7323 cur_end += delta;
7324 cur_next += delta;
7325 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007326 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007327 break;
7328
7329 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007330 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007331 cur_next += delta;
7332
Willy Tarreaufa355d42009-11-29 18:12:29 +01007333 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007334 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7335 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007336 cur_hdr->len = 0;
7337 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007338 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007339 break;
7340
7341 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007342 }
7343
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007344 /* keep the link from this header to next one in case of later
7345 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007346 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007347 old_idx = cur_idx;
7348 }
7349 return 0;
7350}
7351
7352
7353/* Apply the filter to the request line.
7354 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7355 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007356 * Since it can manage the switch to another backend, it updates the per-proxy
7357 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007358 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007359int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007360{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007361 char *cur_ptr, *cur_end;
7362 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007363 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007364 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007365
Willy Tarreau3d300592007-03-18 18:34:41 +01007366 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007367 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007368 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007369 (exp->action == ACT_ALLOW ||
7370 exp->action == ACT_DENY ||
7371 exp->action == ACT_TARPIT))
7372 return 0;
7373 else if (exp->action == ACT_REMOVE)
7374 return 0;
7375
7376 done = 0;
7377
Willy Tarreau9b28e032012-10-12 23:49:43 +02007378 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007379 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007380
7381 /* Now we have the request line between cur_ptr and cur_end */
7382
Willy Tarreau15a53a42015-01-21 13:39:42 +01007383 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007384 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007385 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007386 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007387 done = 1;
7388 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007389
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007390 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007391 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007392 done = 1;
7393 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007394
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007395 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007396 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007397 done = 1;
7398 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007399
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007400 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007401 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7402 if (trash.len < 0)
7403 return -1;
7404
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007405 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007406 /* FIXME: if the user adds a newline in the replacement, the
7407 * index will not be recalculated for now, and the new line
7408 * will not be counted as a new header.
7409 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007410
Willy Tarreaufa355d42009-11-29 18:12:29 +01007411 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007412 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007413 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007414 HTTP_MSG_RQMETH,
7415 cur_ptr, cur_end + 1,
7416 NULL, NULL);
7417 if (unlikely(!cur_end))
7418 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007419
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007420 /* we have a full request and we know that we have either a CR
7421 * or an LF at <ptr>.
7422 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007423 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7424 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007425 /* there is no point trying this regex on headers */
7426 return 1;
7427 }
7428 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007429 return done;
7430}
Willy Tarreau97de6242006-12-27 17:18:38 +01007431
Willy Tarreau58f10d72006-12-04 02:26:12 +01007432
Willy Tarreau58f10d72006-12-04 02:26:12 +01007433
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007434/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007435 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007436 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007437 * unparsable request. Since it can manage the switch to another backend, it
7438 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007439 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007440int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007441{
Willy Tarreau192252e2015-04-04 01:47:55 +02007442 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007443 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007444 struct hdr_exp *exp;
7445
7446 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007447 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007448
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007449 /*
7450 * The interleaving of transformations and verdicts
7451 * makes it difficult to decide to continue or stop
7452 * the evaluation.
7453 */
7454
Willy Tarreau6c123b12010-01-28 20:22:06 +01007455 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7456 break;
7457
Willy Tarreau3d300592007-03-18 18:34:41 +01007458 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007459 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007460 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007461 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007462
7463 /* if this filter had a condition, evaluate it now and skip to
7464 * next filter if the condition does not match.
7465 */
7466 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007467 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007468 ret = acl_pass(ret);
7469 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7470 ret = !ret;
7471
7472 if (!ret)
7473 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007474 }
7475
7476 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007477 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007478 if (unlikely(ret < 0))
7479 return -1;
7480
7481 if (likely(ret == 0)) {
7482 /* The filter did not match the request, it can be
7483 * iterated through all headers.
7484 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007485 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7486 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007487 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007488 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007489 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007490}
7491
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007492
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007493/* Find the end of a cookie value contained between <s> and <e>. It works the
7494 * same way as with headers above except that the semi-colon also ends a token.
7495 * See RFC2965 for more information. Note that it requires a valid header to
7496 * return a valid result.
7497 */
7498char *find_cookie_value_end(char *s, const char *e)
7499{
7500 int quoted, qdpair;
7501
7502 quoted = qdpair = 0;
7503 for (; s < e; s++) {
7504 if (qdpair) qdpair = 0;
7505 else if (quoted) {
7506 if (*s == '\\') qdpair = 1;
7507 else if (*s == '"') quoted = 0;
7508 }
7509 else if (*s == '"') quoted = 1;
7510 else if (*s == ',' || *s == ';') return s;
7511 }
7512 return s;
7513}
7514
7515/* Delete a value in a header between delimiters <from> and <next> in buffer
7516 * <buf>. The number of characters displaced is returned, and the pointer to
7517 * the first delimiter is updated if required. The function tries as much as
7518 * possible to respect the following principles :
7519 * - replace <from> delimiter by the <next> one unless <from> points to a
7520 * colon, in which case <next> is simply removed
7521 * - set exactly one space character after the new first delimiter, unless
7522 * there are not enough characters in the block being moved to do so.
7523 * - remove unneeded spaces before the previous delimiter and after the new
7524 * one.
7525 *
7526 * It is the caller's responsibility to ensure that :
7527 * - <from> points to a valid delimiter or the colon ;
7528 * - <next> points to a valid delimiter or the final CR/LF ;
7529 * - there are non-space chars before <from> ;
7530 * - there is a CR/LF at or after <next>.
7531 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007532int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007533{
7534 char *prev = *from;
7535
7536 if (*prev == ':') {
7537 /* We're removing the first value, preserve the colon and add a
7538 * space if possible.
7539 */
Willy Tarreau2235b262016-11-05 15:50:20 +01007540 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007541 next++;
7542 prev++;
7543 if (prev < next)
7544 *prev++ = ' ';
7545
Willy Tarreau2235b262016-11-05 15:50:20 +01007546 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007547 next++;
7548 } else {
7549 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01007550 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007551 prev--;
7552 *from = prev;
7553
7554 /* copy the delimiter and if possible a space if we're
7555 * not at the end of the line.
7556 */
Willy Tarreau2235b262016-11-05 15:50:20 +01007557 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007558 *prev++ = *next++;
7559 if (prev + 1 < next)
7560 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01007561 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007562 next++;
7563 }
7564 }
7565 return buffer_replace2(buf, prev, next, NULL, 0);
7566}
7567
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007568/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007569 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007570 * desirable to call it only when needed. This code is quite complex because
7571 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7572 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007573 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007574void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007575{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007576 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007577 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007578 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007579 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007580 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7581 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007582
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007583 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007584 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007585 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007586
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007587 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007588 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007589 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007590
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007591 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007592 hdr_beg = hdr_next;
7593 hdr_end = hdr_beg + cur_hdr->len;
7594 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007595
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007596 /* We have one full header between hdr_beg and hdr_end, and the
7597 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007598 * "Cookie:" headers.
7599 */
7600
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007601 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007602 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007603 old_idx = cur_idx;
7604 continue;
7605 }
7606
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007607 del_from = NULL; /* nothing to be deleted */
7608 preserve_hdr = 0; /* assume we may kill the whole header */
7609
Willy Tarreau58f10d72006-12-04 02:26:12 +01007610 /* Now look for cookies. Conforming to RFC2109, we have to support
7611 * attributes whose name begin with a '$', and associate them with
7612 * the right cookie, if we want to delete this cookie.
7613 * So there are 3 cases for each cookie read :
7614 * 1) it's a special attribute, beginning with a '$' : ignore it.
7615 * 2) it's a server id cookie that we *MAY* want to delete : save
7616 * some pointers on it (last semi-colon, beginning of cookie...)
7617 * 3) it's an application cookie : we *MAY* have to delete a previous
7618 * "special" cookie.
7619 * At the end of loop, if a "special" cookie remains, we may have to
7620 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007621 * *MUST* delete it.
7622 *
7623 * Note: RFC2965 is unclear about the processing of spaces around
7624 * the equal sign in the ATTR=VALUE form. A careful inspection of
7625 * the RFC explicitly allows spaces before it, and not within the
7626 * tokens (attrs or values). An inspection of RFC2109 allows that
7627 * too but section 10.1.3 lets one think that spaces may be allowed
7628 * after the equal sign too, resulting in some (rare) buggy
7629 * implementations trying to do that. So let's do what servers do.
7630 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7631 * allowed quoted strings in values, with any possible character
7632 * after a backslash, including control chars and delimitors, which
7633 * causes parsing to become ambiguous. Browsers also allow spaces
7634 * within values even without quotes.
7635 *
7636 * We have to keep multiple pointers in order to support cookie
7637 * removal at the beginning, middle or end of header without
7638 * corrupting the header. All of these headers are valid :
7639 *
7640 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7641 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7642 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7643 * | | | | | | | | |
7644 * | | | | | | | | hdr_end <--+
7645 * | | | | | | | +--> next
7646 * | | | | | | +----> val_end
7647 * | | | | | +-----------> val_beg
7648 * | | | | +--------------> equal
7649 * | | | +----------------> att_end
7650 * | | +---------------------> att_beg
7651 * | +--------------------------> prev
7652 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007653 */
7654
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007655 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7656 /* Iterate through all cookies on this line */
7657
7658 /* find att_beg */
7659 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007660 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007661 att_beg++;
7662
7663 /* find att_end : this is the first character after the last non
7664 * space before the equal. It may be equal to hdr_end.
7665 */
7666 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007667
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007668 while (equal < hdr_end) {
7669 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007670 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007671 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007672 continue;
7673 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007674 }
7675
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007676 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7677 * is between <att_beg> and <equal>, both may be identical.
7678 */
7679
7680 /* look for end of cookie if there is an equal sign */
7681 if (equal < hdr_end && *equal == '=') {
7682 /* look for the beginning of the value */
7683 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007684 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007685 val_beg++;
7686
7687 /* find the end of the value, respecting quotes */
7688 next = find_cookie_value_end(val_beg, hdr_end);
7689
7690 /* make val_end point to the first white space or delimitor after the value */
7691 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007692 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007693 val_end--;
7694 } else {
7695 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007696 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007697
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007698 /* We have nothing to do with attributes beginning with '$'. However,
7699 * they will automatically be removed if a header before them is removed,
7700 * since they're supposed to be linked together.
7701 */
7702 if (*att_beg == '$')
7703 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007704
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007705 /* Ignore cookies with no equal sign */
7706 if (equal == next) {
7707 /* This is not our cookie, so we must preserve it. But if we already
7708 * scheduled another cookie for removal, we cannot remove the
7709 * complete header, but we can remove the previous block itself.
7710 */
7711 preserve_hdr = 1;
7712 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007713 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007714 val_end += delta;
7715 next += delta;
7716 hdr_end += delta;
7717 hdr_next += delta;
7718 cur_hdr->len += delta;
7719 http_msg_move_end(&txn->req, delta);
7720 prev = del_from;
7721 del_from = NULL;
7722 }
7723 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007724 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007725
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007726 /* if there are spaces around the equal sign, we need to
7727 * strip them otherwise we'll get trouble for cookie captures,
7728 * or even for rewrites. Since this happens extremely rarely,
7729 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007730 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007731 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7732 int stripped_before = 0;
7733 int stripped_after = 0;
7734
7735 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007736 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007737 equal += stripped_before;
7738 val_beg += stripped_before;
7739 }
7740
7741 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007742 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007743 val_beg += stripped_after;
7744 stripped_before += stripped_after;
7745 }
7746
7747 val_end += stripped_before;
7748 next += stripped_before;
7749 hdr_end += stripped_before;
7750 hdr_next += stripped_before;
7751 cur_hdr->len += stripped_before;
7752 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007753 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007754 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007755
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007756 /* First, let's see if we want to capture this cookie. We check
7757 * that we don't already have a client side cookie, because we
7758 * can only capture one. Also as an optimisation, we ignore
7759 * cookies shorter than the declared name.
7760 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007761 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7762 (val_end - att_beg >= sess->fe->capture_namelen) &&
7763 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007764 int log_len = val_end - att_beg;
7765
7766 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7767 Alert("HTTP logging : out of memory.\n");
7768 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007769 if (log_len > sess->fe->capture_len)
7770 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007771 memcpy(txn->cli_cookie, att_beg, log_len);
7772 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007773 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007774 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007775
Willy Tarreaubca99692010-10-06 19:25:55 +02007776 /* Persistence cookies in passive, rewrite or insert mode have the
7777 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007778 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007779 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007780 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007781 * For cookies in prefix mode, the form is :
7782 *
7783 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007784 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007785 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7786 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7787 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007788 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007789
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007790 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7791 * have the server ID between val_beg and delim, and the original cookie between
7792 * delim+1 and val_end. Otherwise, delim==val_end :
7793 *
7794 * Cookie: NAME=SRV; # in all but prefix modes
7795 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7796 * | || || | |+-> next
7797 * | || || | +--> val_end
7798 * | || || +---------> delim
7799 * | || |+------------> val_beg
7800 * | || +-------------> att_end = equal
7801 * | |+-----------------> att_beg
7802 * | +------------------> prev
7803 * +-------------------------> hdr_beg
7804 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007805
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007806 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007807 for (delim = val_beg; delim < val_end; delim++)
7808 if (*delim == COOKIE_DELIM)
7809 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007810 } else {
7811 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007812 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007813 /* Now check if the cookie contains a date field, which would
7814 * appear after a vertical bar ('|') just after the server name
7815 * and before the delimiter.
7816 */
7817 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7818 if (vbar1) {
7819 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007820 * right is the last seen date. It is a base64 encoded
7821 * 30-bit value representing the UNIX date since the
7822 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007823 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007824 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007825 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007826 if (val_end - vbar1 >= 5) {
7827 val = b64tos30(vbar1);
7828 if (val > 0)
7829 txn->cookie_last_date = val << 2;
7830 }
7831 /* look for a second vertical bar */
7832 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7833 if (vbar1 && (val_end - vbar1 > 5)) {
7834 val = b64tos30(vbar1 + 1);
7835 if (val > 0)
7836 txn->cookie_first_date = val << 2;
7837 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007838 }
7839 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007840
Willy Tarreauf64d1412010-10-07 20:06:11 +02007841 /* if the cookie has an expiration date and the proxy wants to check
7842 * it, then we do that now. We first check if the cookie is too old,
7843 * then only if it has expired. We detect strict overflow because the
7844 * time resolution here is not great (4 seconds). Cookies with dates
7845 * in the future are ignored if their offset is beyond one day. This
7846 * allows an admin to fix timezone issues without expiring everyone
7847 * and at the same time avoids keeping unwanted side effects for too
7848 * long.
7849 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007850 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7851 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007852 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007853 txn->flags &= ~TX_CK_MASK;
7854 txn->flags |= TX_CK_OLD;
7855 delim = val_beg; // let's pretend we have not found the cookie
7856 txn->cookie_first_date = 0;
7857 txn->cookie_last_date = 0;
7858 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007859 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7860 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007861 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007862 txn->flags &= ~TX_CK_MASK;
7863 txn->flags |= TX_CK_EXPIRED;
7864 delim = val_beg; // let's pretend we have not found the cookie
7865 txn->cookie_first_date = 0;
7866 txn->cookie_last_date = 0;
7867 }
7868
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007869 /* Here, we'll look for the first running server which supports the cookie.
7870 * This allows to share a same cookie between several servers, for example
7871 * to dedicate backup servers to specific servers only.
7872 * However, to prevent clients from sticking to cookie-less backup server
7873 * when they have incidentely learned an empty cookie, we simply ignore
7874 * empty cookies and mark them as invalid.
7875 * The same behaviour is applied when persistence must be ignored.
7876 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007877 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007878 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007879
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007880 while (srv) {
7881 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7882 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007883 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007884 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007885 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007886 /* we found the server and we can use it */
7887 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007888 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007889 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007890 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007891 break;
7892 } else {
7893 /* we found a server, but it's down,
7894 * mark it as such and go on in case
7895 * another one is available.
7896 */
7897 txn->flags &= ~TX_CK_MASK;
7898 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007899 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007900 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007901 srv = srv->next;
7902 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007903
Willy Tarreauf64d1412010-10-07 20:06:11 +02007904 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007905 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007906 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007907 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007908 txn->flags |= TX_CK_UNUSED;
7909 else
7910 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007911 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007912
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007913 /* depending on the cookie mode, we may have to either :
7914 * - delete the complete cookie if we're in insert+indirect mode, so that
7915 * the server never sees it ;
7916 * - remove the server id from the cookie value, and tag the cookie as an
7917 * application cookie so that it does not get accidentely removed later,
7918 * if we're in cookie prefix mode
7919 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007920 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007921 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007922
Willy Tarreau9b28e032012-10-12 23:49:43 +02007923 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007924 val_end += delta;
7925 next += delta;
7926 hdr_end += delta;
7927 hdr_next += delta;
7928 cur_hdr->len += delta;
7929 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007930
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007931 del_from = NULL;
7932 preserve_hdr = 1; /* we want to keep this cookie */
7933 }
7934 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007935 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007936 del_from = prev;
7937 }
7938 } else {
7939 /* This is not our cookie, so we must preserve it. But if we already
7940 * scheduled another cookie for removal, we cannot remove the
7941 * complete header, but we can remove the previous block itself.
7942 */
7943 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007944
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007945 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007946 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007947 if (att_beg >= del_from)
7948 att_beg += delta;
7949 if (att_end >= del_from)
7950 att_end += delta;
7951 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007952 val_end += delta;
7953 next += delta;
7954 hdr_end += delta;
7955 hdr_next += delta;
7956 cur_hdr->len += delta;
7957 http_msg_move_end(&txn->req, delta);
7958 prev = del_from;
7959 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007960 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007961 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007962
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007963 /* continue with next cookie on this header line */
7964 att_beg = next;
7965 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007966
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007967 /* There are no more cookies on this line.
7968 * We may still have one (or several) marked for deletion at the
7969 * end of the line. We must do this now in two ways :
7970 * - if some cookies must be preserved, we only delete from the
7971 * mark to the end of line ;
7972 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007973 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007974 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007975 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007976 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007977 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007978 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007979 cur_hdr->len += delta;
7980 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007981 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007982
7983 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007984 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7985 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007986 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007987 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007988 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007989 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007990 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007991 }
7992
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007993 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007994 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007995 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007996}
7997
7998
Willy Tarreaua15645d2007-03-18 16:22:39 +01007999/* Iterate the same filter through all response headers contained in <rtr>.
8000 * Returns 1 if this filter can be stopped upon return, otherwise 0.
8001 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008002int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008003{
Willy Tarreaua15645d2007-03-18 16:22:39 +01008004 char *cur_ptr, *cur_end, *cur_next;
8005 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008006 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008007 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008008 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008009
8010 last_hdr = 0;
8011
Willy Tarreau9b28e032012-10-12 23:49:43 +02008012 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008013 old_idx = 0;
8014
8015 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008016 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008017 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008018 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008019 (exp->action == ACT_ALLOW ||
8020 exp->action == ACT_DENY))
8021 return 0;
8022
8023 cur_idx = txn->hdr_idx.v[old_idx].next;
8024 if (!cur_idx)
8025 break;
8026
8027 cur_hdr = &txn->hdr_idx.v[cur_idx];
8028 cur_ptr = cur_next;
8029 cur_end = cur_ptr + cur_hdr->len;
8030 cur_next = cur_end + cur_hdr->cr + 1;
8031
8032 /* Now we have one header between cur_ptr and cur_end,
8033 * and the next header starts at cur_next.
8034 */
8035
Willy Tarreau15a53a42015-01-21 13:39:42 +01008036 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008037 switch (exp->action) {
8038 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008039 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008040 last_hdr = 1;
8041 break;
8042
8043 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008044 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008045 last_hdr = 1;
8046 break;
8047
8048 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008049 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8050 if (trash.len < 0)
8051 return -1;
8052
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008053 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008054 /* FIXME: if the user adds a newline in the replacement, the
8055 * index will not be recalculated for now, and the new line
8056 * will not be counted as a new header.
8057 */
8058
8059 cur_end += delta;
8060 cur_next += delta;
8061 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008062 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008063 break;
8064
8065 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02008066 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008067 cur_next += delta;
8068
Willy Tarreaufa355d42009-11-29 18:12:29 +01008069 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008070 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8071 txn->hdr_idx.used--;
8072 cur_hdr->len = 0;
8073 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01008074 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008075 break;
8076
8077 }
8078 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008079
8080 /* keep the link from this header to next one in case of later
8081 * removal of next header.
8082 */
8083 old_idx = cur_idx;
8084 }
8085 return 0;
8086}
8087
8088
8089/* Apply the filter to the status line in the response buffer <rtr>.
8090 * Returns 0 if nothing has been done, 1 if the filter has been applied,
8091 * or -1 if a replacement resulted in an invalid status line.
8092 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008093int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008094{
Willy Tarreaua15645d2007-03-18 16:22:39 +01008095 char *cur_ptr, *cur_end;
8096 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008097 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008098 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008099
8100
Willy Tarreau3d300592007-03-18 18:34:41 +01008101 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008102 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008103 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008104 (exp->action == ACT_ALLOW ||
8105 exp->action == ACT_DENY))
8106 return 0;
8107 else if (exp->action == ACT_REMOVE)
8108 return 0;
8109
8110 done = 0;
8111
Willy Tarreau9b28e032012-10-12 23:49:43 +02008112 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008113 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008114
8115 /* Now we have the status line between cur_ptr and cur_end */
8116
Willy Tarreau15a53a42015-01-21 13:39:42 +01008117 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008118 switch (exp->action) {
8119 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008120 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008121 done = 1;
8122 break;
8123
8124 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008125 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008126 done = 1;
8127 break;
8128
8129 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008130 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8131 if (trash.len < 0)
8132 return -1;
8133
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008134 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008135 /* FIXME: if the user adds a newline in the replacement, the
8136 * index will not be recalculated for now, and the new line
8137 * will not be counted as a new header.
8138 */
8139
Willy Tarreaufa355d42009-11-29 18:12:29 +01008140 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008141 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008142 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02008143 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01008144 cur_ptr, cur_end + 1,
8145 NULL, NULL);
8146 if (unlikely(!cur_end))
8147 return -1;
8148
8149 /* we have a full respnse and we know that we have either a CR
8150 * or an LF at <ptr>.
8151 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008152 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008153 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01008154 /* there is no point trying this regex on headers */
8155 return 1;
8156 }
8157 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008158 return done;
8159}
8160
8161
8162
8163/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008164 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008165 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
8166 * unparsable response.
8167 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008168int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008169{
Willy Tarreau192252e2015-04-04 01:47:55 +02008170 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008171 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008172 struct hdr_exp *exp;
8173
8174 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008175 int ret;
8176
8177 /*
8178 * The interleaving of transformations and verdicts
8179 * makes it difficult to decide to continue or stop
8180 * the evaluation.
8181 */
8182
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008183 if (txn->flags & TX_SVDENY)
8184 break;
8185
Willy Tarreau3d300592007-03-18 18:34:41 +01008186 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008187 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
8188 exp->action == ACT_PASS)) {
8189 exp = exp->next;
8190 continue;
8191 }
8192
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008193 /* if this filter had a condition, evaluate it now and skip to
8194 * next filter if the condition does not match.
8195 */
8196 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02008197 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008198 ret = acl_pass(ret);
8199 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8200 ret = !ret;
8201 if (!ret)
8202 continue;
8203 }
8204
Willy Tarreaua15645d2007-03-18 16:22:39 +01008205 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008206 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008207 if (unlikely(ret < 0))
8208 return -1;
8209
8210 if (likely(ret == 0)) {
8211 /* The filter did not match the response, it can be
8212 * iterated through all headers.
8213 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008214 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8215 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008216 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008217 }
8218 return 0;
8219}
8220
8221
Willy Tarreaua15645d2007-03-18 16:22:39 +01008222/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008223 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008224 * desirable to call it only when needed. This function is also used when we
8225 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008226 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008227void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008228{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008229 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008230 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01008231 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008232 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008233 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008234 char *hdr_beg, *hdr_end, *hdr_next;
8235 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008236
Willy Tarreaua15645d2007-03-18 16:22:39 +01008237 /* Iterate through the headers.
8238 * we start with the start line.
8239 */
8240 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008241 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008242
8243 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8244 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008245 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008246
8247 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008248 hdr_beg = hdr_next;
8249 hdr_end = hdr_beg + cur_hdr->len;
8250 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008251
Willy Tarreau24581ba2010-08-31 22:39:35 +02008252 /* We have one full header between hdr_beg and hdr_end, and the
8253 * next header starts at hdr_next. We're only interested in
8254 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008255 */
8256
Willy Tarreau24581ba2010-08-31 22:39:35 +02008257 is_cookie2 = 0;
8258 prev = hdr_beg + 10;
8259 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008260 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008261 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8262 if (!val) {
8263 old_idx = cur_idx;
8264 continue;
8265 }
8266 is_cookie2 = 1;
8267 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008268 }
8269
Willy Tarreau24581ba2010-08-31 22:39:35 +02008270 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8271 * <prev> points to the colon.
8272 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008273 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008274
Willy Tarreau24581ba2010-08-31 22:39:35 +02008275 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8276 * check-cache is enabled) and we are not interested in checking
8277 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008278 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02008279 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008280 return;
8281
Willy Tarreau24581ba2010-08-31 22:39:35 +02008282 /* OK so now we know we have to process this response cookie.
8283 * The format of the Set-Cookie header is slightly different
8284 * from the format of the Cookie header in that it does not
8285 * support the comma as a cookie delimiter (thus the header
8286 * cannot be folded) because the Expires attribute described in
8287 * the original Netscape's spec may contain an unquoted date
8288 * with a comma inside. We have to live with this because
8289 * many browsers don't support Max-Age and some browsers don't
8290 * support quoted strings. However the Set-Cookie2 header is
8291 * clean.
8292 *
8293 * We have to keep multiple pointers in order to support cookie
8294 * removal at the beginning, middle or end of header without
8295 * corrupting the header (in case of set-cookie2). A special
8296 * pointer, <scav> points to the beginning of the set-cookie-av
8297 * fields after the first semi-colon. The <next> pointer points
8298 * either to the end of line (set-cookie) or next unquoted comma
8299 * (set-cookie2). All of these headers are valid :
8300 *
8301 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8302 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8303 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8304 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8305 * | | | | | | | | | |
8306 * | | | | | | | | +-> next hdr_end <--+
8307 * | | | | | | | +------------> scav
8308 * | | | | | | +--------------> val_end
8309 * | | | | | +--------------------> val_beg
8310 * | | | | +----------------------> equal
8311 * | | | +------------------------> att_end
8312 * | | +----------------------------> att_beg
8313 * | +------------------------------> prev
8314 * +-----------------------------------------> hdr_beg
8315 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008316
Willy Tarreau24581ba2010-08-31 22:39:35 +02008317 for (; prev < hdr_end; prev = next) {
8318 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008319
Willy Tarreau24581ba2010-08-31 22:39:35 +02008320 /* find att_beg */
8321 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01008322 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008323 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008324
Willy Tarreau24581ba2010-08-31 22:39:35 +02008325 /* find att_end : this is the first character after the last non
8326 * space before the equal. It may be equal to hdr_end.
8327 */
8328 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008329
Willy Tarreau24581ba2010-08-31 22:39:35 +02008330 while (equal < hdr_end) {
8331 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8332 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01008333 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008334 continue;
8335 att_end = equal;
8336 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008337
Willy Tarreau24581ba2010-08-31 22:39:35 +02008338 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8339 * is between <att_beg> and <equal>, both may be identical.
8340 */
8341
8342 /* look for end of cookie if there is an equal sign */
8343 if (equal < hdr_end && *equal == '=') {
8344 /* look for the beginning of the value */
8345 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01008346 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008347 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008348
Willy Tarreau24581ba2010-08-31 22:39:35 +02008349 /* find the end of the value, respecting quotes */
8350 next = find_cookie_value_end(val_beg, hdr_end);
8351
8352 /* make val_end point to the first white space or delimitor after the value */
8353 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01008354 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008355 val_end--;
8356 } else {
8357 /* <equal> points to next comma, semi-colon or EOL */
8358 val_beg = val_end = next = equal;
8359 }
8360
8361 if (next < hdr_end) {
8362 /* Set-Cookie2 supports multiple cookies, and <next> points to
8363 * a colon or semi-colon before the end. So skip all attr-value
8364 * pairs and look for the next comma. For Set-Cookie, since
8365 * commas are permitted in values, skip to the end.
8366 */
8367 if (is_cookie2)
8368 next = find_hdr_value_end(next, hdr_end);
8369 else
8370 next = hdr_end;
8371 }
8372
8373 /* Now everything is as on the diagram above */
8374
8375 /* Ignore cookies with no equal sign */
8376 if (equal == val_end)
8377 continue;
8378
8379 /* If there are spaces around the equal sign, we need to
8380 * strip them otherwise we'll get trouble for cookie captures,
8381 * or even for rewrites. Since this happens extremely rarely,
8382 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008383 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008384 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8385 int stripped_before = 0;
8386 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008387
Willy Tarreau24581ba2010-08-31 22:39:35 +02008388 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008389 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008390 equal += stripped_before;
8391 val_beg += stripped_before;
8392 }
8393
8394 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008395 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008396 val_beg += stripped_after;
8397 stripped_before += stripped_after;
8398 }
8399
8400 val_end += stripped_before;
8401 next += stripped_before;
8402 hdr_end += stripped_before;
8403 hdr_next += stripped_before;
8404 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008405 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008406 }
8407
8408 /* First, let's see if we want to capture this cookie. We check
8409 * that we don't already have a server side cookie, because we
8410 * can only capture one. Also as an optimisation, we ignore
8411 * cookies shorter than the declared name.
8412 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008413 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008414 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008415 (val_end - att_beg >= sess->fe->capture_namelen) &&
8416 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008417 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008418 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008419 Alert("HTTP logging : out of memory.\n");
8420 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008421 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008422 if (log_len > sess->fe->capture_len)
8423 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008424 memcpy(txn->srv_cookie, att_beg, log_len);
8425 txn->srv_cookie[log_len] = 0;
8426 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008427 }
8428
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008429 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008430 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008431 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008432 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8433 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008434 /* assume passive cookie by default */
8435 txn->flags &= ~TX_SCK_MASK;
8436 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008437
8438 /* If the cookie is in insert mode on a known server, we'll delete
8439 * this occurrence because we'll insert another one later.
8440 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008441 * a direct access.
8442 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008443 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008444 /* The "preserve" flag was set, we don't want to touch the
8445 * server's cookie.
8446 */
8447 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008448 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008449 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008450 /* this cookie must be deleted */
8451 if (*prev == ':' && next == hdr_end) {
8452 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008453 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008454 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8455 txn->hdr_idx.used--;
8456 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008457 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008458 hdr_next += delta;
8459 http_msg_move_end(&txn->rsp, delta);
8460 /* note: while both invalid now, <next> and <hdr_end>
8461 * are still equal, so the for() will stop as expected.
8462 */
8463 } else {
8464 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008465 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008466 next = prev;
8467 hdr_end += delta;
8468 hdr_next += delta;
8469 cur_hdr->len += delta;
8470 http_msg_move_end(&txn->rsp, delta);
8471 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008472 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008473 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008474 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008475 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008476 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008477 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008478 * with this server since we know it.
8479 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008480 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008481 next += delta;
8482 hdr_end += delta;
8483 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008484 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008485 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008486
Willy Tarreauf1348312010-10-07 15:54:11 +02008487 txn->flags &= ~TX_SCK_MASK;
8488 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008489 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008490 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008491 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008492 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008493 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008494 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008495 next += delta;
8496 hdr_end += delta;
8497 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008498 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008499 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008500
Willy Tarreau827aee92011-03-10 16:55:02 +01008501 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008502 txn->flags &= ~TX_SCK_MASK;
8503 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008504 }
8505 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008506 /* that's done for this cookie, check the next one on the same
8507 * line when next != hdr_end (only if is_cookie2).
8508 */
8509 }
8510 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008511 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008512 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008513}
8514
8515
Willy Tarreaua15645d2007-03-18 16:22:39 +01008516/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008517 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008518 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008519void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008520{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008521 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008522 char *p1, *p2;
8523
8524 char *cur_ptr, *cur_end, *cur_next;
8525 int cur_idx;
8526
Willy Tarreau5df51872007-11-25 16:20:08 +01008527 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008528 return;
8529
8530 /* Iterate through the headers.
8531 * we start with the start line.
8532 */
8533 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008534 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008535
8536 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8537 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008538 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008539
8540 cur_hdr = &txn->hdr_idx.v[cur_idx];
8541 cur_ptr = cur_next;
8542 cur_end = cur_ptr + cur_hdr->len;
8543 cur_next = cur_end + cur_hdr->cr + 1;
8544
8545 /* We have one full header between cur_ptr and cur_end, and the
8546 * next header starts at cur_next. We're only interested in
8547 * "Cookie:" headers.
8548 */
8549
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008550 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8551 if (val) {
8552 if ((cur_end - (cur_ptr + val) >= 8) &&
8553 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8554 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8555 return;
8556 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008557 }
8558
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008559 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8560 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008561 continue;
8562
8563 /* OK, right now we know we have a cache-control header at cur_ptr */
8564
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008565 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008566
8567 if (p1 >= cur_end) /* no more info */
8568 continue;
8569
8570 /* p1 is at the beginning of the value */
8571 p2 = p1;
8572
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008573 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008574 p2++;
8575
8576 /* we have a complete value between p1 and p2 */
8577 if (p2 < cur_end && *p2 == '=') {
8578 /* we have something of the form no-cache="set-cookie" */
8579 if ((cur_end - p1 >= 21) &&
8580 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8581 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008582 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008583 continue;
8584 }
8585
8586 /* OK, so we know that either p2 points to the end of string or to a comma */
8587 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008588 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008589 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8590 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8591 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008592 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008593 return;
8594 }
8595
8596 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008597 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008598 continue;
8599 }
8600 }
8601}
8602
Willy Tarreau58f10d72006-12-04 02:26:12 +01008603
Willy Tarreaub2513902006-12-17 14:52:38 +01008604/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008605 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008606 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008607 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008608 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008609 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008610 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008611 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008612 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008613int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008614{
8615 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008616 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008617 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008618
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008619 if (!uri_auth)
8620 return 0;
8621
Cyril Bonté70be45d2010-10-12 00:14:35 +02008622 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008623 return 0;
8624
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008625 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008626 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008627 return 0;
8628
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008629 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008630 return 0;
8631
Willy Tarreaub2513902006-12-17 14:52:38 +01008632 return 1;
8633}
8634
Willy Tarreau4076a152009-04-02 15:18:36 +02008635/*
8636 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008637 * By default it tries to report the error position as msg->err_pos. However if
8638 * this one is not set, it will then report msg->next, which is the last known
8639 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008640 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008641 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008642void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008643 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008644 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008645{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008646 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008647 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008648 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008649
Willy Tarreauf3764b72016-03-31 13:45:10 +02008650 es->len = MIN(chn->buf->i, global.tune.bufsize);
Willy Tarreau9b28e032012-10-12 23:49:43 +02008651 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008652 len1 = MIN(len1, es->len);
8653 len2 = es->len - len1; /* remaining data if buffer wraps */
8654
Willy Tarreauf3764b72016-03-31 13:45:10 +02008655 if (!es->buf)
8656 es->buf = malloc(global.tune.bufsize);
8657
8658 if (es->buf) {
8659 memcpy(es->buf, chn->buf->p, len1);
8660 if (len2)
8661 memcpy(es->buf + len1, chn->buf->data, len2);
8662 }
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008663
Willy Tarreau4076a152009-04-02 15:18:36 +02008664 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008665 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008666 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008667 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008668
Willy Tarreau4076a152009-04-02 15:18:36 +02008669 es->when = date; // user-visible date
8670 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008671 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008672 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008673 if (objt_conn(sess->origin))
8674 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008675 else
8676 memset(&es->src, 0, sizeof(es->src));
8677
Willy Tarreau078272e2010-12-12 12:46:33 +01008678 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008679 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008680 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008681 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008682 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008683 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008684 es->b_out = chn->buf->o;
8685 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008686 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008687 es->m_clen = msg->chunk_len;
8688 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008689}
Willy Tarreaub2513902006-12-17 14:52:38 +01008690
Willy Tarreau294c4732011-12-16 21:35:50 +01008691/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8692 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8693 * performed over the whole headers. Otherwise it must contain a valid header
8694 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8695 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8696 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8697 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008698 * -1. The value fetch stops at commas, so this function is suited for use with
8699 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008700 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008701 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008702unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008703 struct hdr_idx *idx, int occ,
8704 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008705{
Willy Tarreau294c4732011-12-16 21:35:50 +01008706 struct hdr_ctx local_ctx;
8707 char *ptr_hist[MAX_HDR_HISTORY];
8708 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008709 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008710 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008711
Willy Tarreau294c4732011-12-16 21:35:50 +01008712 if (!ctx) {
8713 local_ctx.idx = 0;
8714 ctx = &local_ctx;
8715 }
8716
Willy Tarreaubce70882009-09-07 11:51:47 +02008717 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008718 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008719 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008720 occ--;
8721 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008722 *vptr = ctx->line + ctx->val;
8723 *vlen = ctx->vlen;
8724 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008725 }
8726 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008727 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008728 }
8729
8730 /* negative occurrence, we scan all the list then walk back */
8731 if (-occ > MAX_HDR_HISTORY)
8732 return 0;
8733
Willy Tarreau294c4732011-12-16 21:35:50 +01008734 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008735 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008736 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8737 len_hist[hist_ptr] = ctx->vlen;
8738 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008739 hist_ptr = 0;
8740 found++;
8741 }
8742 if (-occ > found)
8743 return 0;
8744 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008745 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8746 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8747 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008748 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008749 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008750 if (hist_ptr >= MAX_HDR_HISTORY)
8751 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008752 *vptr = ptr_hist[hist_ptr];
8753 *vlen = len_hist[hist_ptr];
8754 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008755}
8756
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008757/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8758 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8759 * performed over the whole headers. Otherwise it must contain a valid header
8760 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8761 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8762 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8763 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8764 * -1. This function differs from http_get_hdr() in that it only returns full
8765 * line header values and does not stop at commas.
8766 * The return value is 0 if nothing was found, or non-zero otherwise.
8767 */
8768unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8769 struct hdr_idx *idx, int occ,
8770 struct hdr_ctx *ctx, char **vptr, int *vlen)
8771{
8772 struct hdr_ctx local_ctx;
8773 char *ptr_hist[MAX_HDR_HISTORY];
8774 int len_hist[MAX_HDR_HISTORY];
8775 unsigned int hist_ptr;
8776 int found;
8777
8778 if (!ctx) {
8779 local_ctx.idx = 0;
8780 ctx = &local_ctx;
8781 }
8782
8783 if (occ >= 0) {
8784 /* search from the beginning */
8785 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8786 occ--;
8787 if (occ <= 0) {
8788 *vptr = ctx->line + ctx->val;
8789 *vlen = ctx->vlen;
8790 return 1;
8791 }
8792 }
8793 return 0;
8794 }
8795
8796 /* negative occurrence, we scan all the list then walk back */
8797 if (-occ > MAX_HDR_HISTORY)
8798 return 0;
8799
8800 found = hist_ptr = 0;
8801 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8802 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8803 len_hist[hist_ptr] = ctx->vlen;
8804 if (++hist_ptr >= MAX_HDR_HISTORY)
8805 hist_ptr = 0;
8806 found++;
8807 }
8808 if (-occ > found)
8809 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008810
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008811 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008812 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8813 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8814 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008815 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008816 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008817 if (hist_ptr >= MAX_HDR_HISTORY)
8818 hist_ptr -= MAX_HDR_HISTORY;
8819 *vptr = ptr_hist[hist_ptr];
8820 *vlen = len_hist[hist_ptr];
8821 return 1;
8822}
8823
Willy Tarreaubaaee002006-06-26 02:48:02 +02008824/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008825 * Print a debug line with a header. Always stop at the first CR or LF char,
8826 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8827 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008828 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008829void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008830{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008831 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008832 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008833
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008834 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008835 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008836 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008837 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 +02008838
8839 for (max = 0; start + max < end; max++)
8840 if (start[max] == '\r' || start[max] == '\n')
8841 break;
8842
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008843 UBOUND(max, trash.size - trash.len - 3);
8844 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8845 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008846 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008847}
8848
Willy Tarreaueee5b512015-04-03 23:46:31 +02008849
8850/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8851 * The hdr_idx is allocated as well. In case of allocation failure, everything
8852 * allocated is freed and NULL is returned. Otherwise the new transaction is
8853 * assigned to the stream and returned.
8854 */
8855struct http_txn *http_alloc_txn(struct stream *s)
8856{
8857 struct http_txn *txn = s->txn;
8858
8859 if (txn)
8860 return txn;
8861
8862 txn = pool_alloc2(pool2_http_txn);
8863 if (!txn)
8864 return txn;
8865
8866 txn->hdr_idx.size = global.tune.max_http_hdr;
8867 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8868 if (!txn->hdr_idx.v) {
8869 pool_free2(pool2_http_txn, txn);
8870 return NULL;
8871 }
8872
8873 s->txn = txn;
8874 return txn;
8875}
8876
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008877void http_txn_reset_req(struct http_txn *txn)
8878{
8879 txn->req.flags = 0;
8880 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8881 txn->req.next = 0;
8882 txn->req.chunk_len = 0LL;
8883 txn->req.body_len = 0LL;
8884 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8885}
8886
8887void http_txn_reset_res(struct http_txn *txn)
8888{
8889 txn->rsp.flags = 0;
8890 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8891 txn->rsp.next = 0;
8892 txn->rsp.chunk_len = 0LL;
8893 txn->rsp.body_len = 0LL;
8894 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8895}
8896
Willy Tarreau0937bc42009-12-22 15:03:09 +01008897/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008898 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008899 * the required fields are properly allocated and that we only need to (re)init
8900 * them. This should be used before processing any new request.
8901 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008902void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008903{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008904 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008905 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008906
8907 txn->flags = 0;
8908 txn->status = -1;
8909
Willy Tarreauf64d1412010-10-07 20:06:11 +02008910 txn->cookie_first_date = 0;
8911 txn->cookie_last_date = 0;
8912
Willy Tarreaueee5b512015-04-03 23:46:31 +02008913 txn->srv_cookie = NULL;
8914 txn->cli_cookie = NULL;
8915 txn->uri = NULL;
8916
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008917 http_txn_reset_req(txn);
8918 http_txn_reset_res(txn);
8919
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008920 txn->req.chn = &s->req;
8921 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008922
8923 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008924
8925 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8926 if (fe->options2 & PR_O2_REQBUG_OK)
8927 txn->req.err_pos = -1; /* let buggy requests pass */
8928
Willy Tarreau0937bc42009-12-22 15:03:09 +01008929 if (txn->hdr_idx.v)
8930 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008931
8932 vars_init(&s->vars_txn, SCOPE_TXN);
8933 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008934}
8935
8936/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008937void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008938{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008939 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008940 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008941
8942 /* these ones will have been dynamically allocated */
8943 pool_free2(pool2_requri, txn->uri);
8944 pool_free2(pool2_capture, txn->cli_cookie);
8945 pool_free2(pool2_capture, txn->srv_cookie);
William Lallemanda73203e2012-03-12 12:48:57 +01008946 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008947
William Lallemanda73203e2012-03-12 12:48:57 +01008948 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008949 txn->uri = NULL;
8950 txn->srv_cookie = NULL;
8951 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008952
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008953 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008954 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008955 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008956 pool_free2(h->pool, s->req_cap[h->index]);
8957 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008958 }
8959
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008960 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008961 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008962 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008963 pool_free2(h->pool, s->res_cap[h->index]);
8964 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008965 }
8966
Willy Tarreau6204cd92016-03-10 16:33:04 +01008967 vars_prune(&s->vars_txn, s->sess, s);
8968 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008969}
8970
8971/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008972void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008973{
8974 http_end_txn(s);
8975 http_init_txn(s);
8976
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008977 /* reinitialise the current rule list pointer to NULL. We are sure that
8978 * any rulelist match the NULL pointer.
8979 */
8980 s->current_rule_list = NULL;
8981
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008982 s->be = strm_fe(s);
8983 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008984 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008985 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008986 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008987 /* re-init store persistence */
8988 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008989 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008990
Willy Tarreau0937bc42009-12-22 15:03:09 +01008991 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008992
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008993 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008994
Willy Tarreau739cfba2010-01-25 23:11:14 +01008995 /* We must trim any excess data from the response buffer, because we
8996 * may have blocked an invalid response from a server that we don't
8997 * want to accidentely forward once we disable the analysers, nor do
8998 * we want those data to come along with next response. A typical
8999 * example of such data would be from a buggy server responding to
9000 * a HEAD with some data, or sending more than the advertised
9001 * content-length.
9002 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009003 if (unlikely(s->res.buf->i))
9004 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01009005
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009006 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009007 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009008
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009009 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02009010 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009011
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01009012 s->req.rex = TICK_ETERNITY;
9013 s->req.wex = TICK_ETERNITY;
9014 s->req.analyse_exp = TICK_ETERNITY;
9015 s->res.rex = TICK_ETERNITY;
9016 s->res.wex = TICK_ETERNITY;
9017 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01009018}
Willy Tarreau58f10d72006-12-04 02:26:12 +01009019
Sasha Pachev218f0642014-06-16 12:05:59 -06009020void free_http_res_rules(struct list *r)
9021{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009022 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06009023
9024 list_for_each_entry_safe(pr, tr, r, list) {
9025 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009026 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06009027 free(pr);
9028 }
9029}
9030
9031void free_http_req_rules(struct list *r)
9032{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009033 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01009034
9035 list_for_each_entry_safe(pr, tr, r, list) {
9036 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009037 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009038 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01009039
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009040 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01009041 free(pr);
9042 }
9043}
9044
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009045/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009046struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01009047{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009048 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009049 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009050 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009051 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01009052
Vincent Bernat02779b62016-04-03 13:48:43 +02009053 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01009054 if (!rule) {
9055 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009056 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009057 }
9058
CJ Ess108b1dd2015-04-07 12:03:37 -04009059 rule->deny_status = HTTP_ERR_403;
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009060 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009061 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01009062 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02009063 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04009064 int code;
9065 int hc;
9066
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009067 rule->action = ACT_ACTION_DENY;
Willy Tarreauff011f22011-01-06 17:51:27 +01009068 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04009069 if (strcmp(args[cur_arg], "deny_status") == 0) {
9070 cur_arg++;
9071 if (!args[cur_arg]) {
9072 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
9073 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9074 goto out_err;
9075 }
9076
9077 code = atol(args[cur_arg]);
9078 cur_arg++;
9079 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
9080 if (http_err_codes[hc] == code) {
9081 rule->deny_status = hc;
9082 break;
9083 }
9084 }
9085
9086 if (hc >= HTTP_ERR_SIZE) {
9087 Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n",
9088 file, linenum, code);
9089 }
9090 }
Willy Tarreauccbcc372012-12-27 12:37:57 +01009091 } else if (!strcmp(args[0], "tarpit")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009092 rule->action = ACT_HTTP_REQ_TARPIT;
Willy Tarreauccbcc372012-12-27 12:37:57 +01009093 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01009094 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009095 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01009096 cur_arg = 1;
9097
9098 while(*args[cur_arg]) {
9099 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009100 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01009101 cur_arg+=2;
9102 continue;
9103 } else
9104 break;
9105 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009106 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009107 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009108 cur_arg = 1;
9109
9110 if (!*args[cur_arg] ||
9111 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9112 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
9113 file, linenum, args[0]);
9114 goto out_err;
9115 }
9116 rule->arg.nice = atoi(args[cur_arg]);
9117 if (rule->arg.nice < -1024)
9118 rule->arg.nice = -1024;
9119 else if (rule->arg.nice > 1024)
9120 rule->arg.nice = 1024;
9121 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009122 } else if (!strcmp(args[0], "set-tos")) {
9123#ifdef IP_TOS
9124 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009125 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009126 cur_arg = 1;
9127
9128 if (!*args[cur_arg] ||
9129 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9130 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9131 file, linenum, args[0]);
9132 goto out_err;
9133 }
9134
9135 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9136 if (err && *err != '\0') {
9137 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9138 file, linenum, err, args[0]);
9139 goto out_err;
9140 }
9141 cur_arg++;
9142#else
9143 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9144 goto out_err;
9145#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009146 } else if (!strcmp(args[0], "set-mark")) {
9147#ifdef SO_MARK
9148 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009149 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009150 cur_arg = 1;
9151
9152 if (!*args[cur_arg] ||
9153 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9154 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9155 file, linenum, args[0]);
9156 goto out_err;
9157 }
9158
9159 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9160 if (err && *err != '\0') {
9161 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9162 file, linenum, err, args[0]);
9163 goto out_err;
9164 }
9165 cur_arg++;
9166 global.last_checks |= LSTCHK_NETADM;
9167#else
9168 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9169 goto out_err;
9170#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009171 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009172 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009173 cur_arg = 1;
9174
9175 if (!*args[cur_arg] ||
9176 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9177 bad_log_level:
9178 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9179 file, linenum, args[0]);
9180 goto out_err;
9181 }
9182 if (strcmp(args[cur_arg], "silent") == 0)
9183 rule->arg.loglevel = -1;
9184 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9185 goto bad_log_level;
9186 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009187 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009188 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009189 cur_arg = 1;
9190
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009191 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9192 (*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 +01009193 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9194 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009195 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009196 }
9197
9198 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9199 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9200 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009201
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009202 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009203 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009204 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 +01009205 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9206 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9207 file, linenum, args[0], error);
9208 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009209 goto out_err;
9210 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009211 free(proxy->conf.lfs_file);
9212 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9213 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009214 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009215 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009216 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009217 cur_arg = 1;
9218
9219 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009220 (*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 -06009221 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9222 file, linenum, args[0]);
9223 goto out_err;
9224 }
9225
9226 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9227 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9228 LIST_INIT(&rule->arg.hdr_add.fmt);
9229
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009230 error = NULL;
9231 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9232 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9233 args[cur_arg + 1], error);
9234 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009235 goto out_err;
9236 }
9237
9238 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009239 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009240 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 +01009241 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9242 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9243 file, linenum, args[0], error);
9244 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009245 goto out_err;
9246 }
Sasha Pachev218f0642014-06-16 12:05:59 -06009247
9248 free(proxy->conf.lfs_file);
9249 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9250 proxy->conf.lfs_line = proxy->conf.args.line;
9251 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009252 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009253 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009254 cur_arg = 1;
9255
9256 if (!*args[cur_arg] ||
9257 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9258 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9259 file, linenum, args[0]);
9260 goto out_err;
9261 }
9262
9263 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9264 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9265
9266 proxy->conf.args.ctx = ARGC_HRQ;
9267 free(proxy->conf.lfs_file);
9268 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9269 proxy->conf.lfs_line = proxy->conf.args.line;
9270 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009271 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9272 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009273 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009274 struct sample_expr *expr;
9275 unsigned int where;
9276 char *err = NULL;
9277
9278 cur_arg = 1;
9279 proxy->conf.args.ctx = ARGC_TRK;
9280
9281 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9282 if (!expr) {
9283 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9284 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9285 free(err);
9286 goto out_err;
9287 }
9288
9289 where = 0;
9290 if (proxy->cap & PR_CAP_FE)
9291 where |= SMP_VAL_FE_HRQ_HDR;
9292 if (proxy->cap & PR_CAP_BE)
9293 where |= SMP_VAL_BE_HRQ_HDR;
9294
9295 if (!(expr->fetch->val & where)) {
9296 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9297 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9298 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9299 args[cur_arg-1], sample_src_names(expr->fetch->use));
9300 free(expr);
9301 goto out_err;
9302 }
9303
9304 if (strcmp(args[cur_arg], "table") == 0) {
9305 cur_arg++;
9306 if (!args[cur_arg]) {
9307 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9308 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9309 free(expr);
9310 goto out_err;
9311 }
9312 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009313 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02009314 cur_arg++;
9315 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009316 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009317 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009318 } else if (strcmp(args[0], "redirect") == 0) {
9319 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009320 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009321
Willy Tarreaube4653b2015-05-28 15:26:58 +02009322 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009323 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9324 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9325 goto out_err;
9326 }
9327
9328 /* this redirect rule might already contain a parsed condition which
9329 * we'll pass to the http-request rule.
9330 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009331 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009332 rule->arg.redir = redir;
9333 rule->cond = redir->cond;
9334 redir->cond = NULL;
9335 cur_arg = 2;
9336 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009337 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9338 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009339 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009340 /*
9341 * '+ 8' for 'add-acl('
9342 * '- 9' for 'add-acl(' + trailing ')'
9343 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009344 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009345
9346 cur_arg = 1;
9347
9348 if (!*args[cur_arg] ||
9349 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9350 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9351 file, linenum, args[0]);
9352 goto out_err;
9353 }
9354
9355 LIST_INIT(&rule->arg.map.key);
9356 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009357 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009358 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009359 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9360 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9361 file, linenum, args[0], error);
9362 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009363 goto out_err;
9364 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009365 free(proxy->conf.lfs_file);
9366 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9367 proxy->conf.lfs_line = proxy->conf.args.line;
9368 cur_arg += 1;
9369 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9370 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009371 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009372 /*
9373 * '+ 8' for 'del-acl('
9374 * '- 9' for 'del-acl(' + trailing ')'
9375 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009376 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009377
9378 cur_arg = 1;
9379
9380 if (!*args[cur_arg] ||
9381 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9382 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9383 file, linenum, args[0]);
9384 goto out_err;
9385 }
9386
9387 LIST_INIT(&rule->arg.map.key);
9388 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009389 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009390 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009391 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9392 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9393 file, linenum, args[0], error);
9394 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009395 goto out_err;
9396 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009397 free(proxy->conf.lfs_file);
9398 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9399 proxy->conf.lfs_line = proxy->conf.args.line;
9400 cur_arg += 1;
9401 } else if (strncmp(args[0], "del-map", 7) == 0) {
9402 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009403 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009404 /*
9405 * '+ 8' for 'del-map('
9406 * '- 9' for 'del-map(' + trailing ')'
9407 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009408 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009409
9410 cur_arg = 1;
9411
9412 if (!*args[cur_arg] ||
9413 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9414 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9415 file, linenum, args[0]);
9416 goto out_err;
9417 }
9418
9419 LIST_INIT(&rule->arg.map.key);
9420 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009421 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009422 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009423 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9424 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9425 file, linenum, args[0], error);
9426 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009427 goto out_err;
9428 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009429 free(proxy->conf.lfs_file);
9430 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9431 proxy->conf.lfs_line = proxy->conf.args.line;
9432 cur_arg += 1;
9433 } else if (strncmp(args[0], "set-map", 7) == 0) {
9434 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009435 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009436 /*
9437 * '+ 8' for 'set-map('
9438 * '- 9' for 'set-map(' + trailing ')'
9439 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009440 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009441
9442 cur_arg = 1;
9443
9444 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9445 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9446 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9447 file, linenum, args[0]);
9448 goto out_err;
9449 }
9450
9451 LIST_INIT(&rule->arg.map.key);
9452 LIST_INIT(&rule->arg.map.value);
9453 proxy->conf.args.ctx = ARGC_HRQ;
9454
9455 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009456 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009457 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009458 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9459 Alert("parsing [%s:%d]: 'http-request %s' key: %s.\n",
9460 file, linenum, args[0], error);
9461 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009462 goto out_err;
9463 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009464
9465 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009466 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009467 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 +01009468 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9469 Alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n",
9470 file, linenum, args[0], error);
9471 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009472 goto out_err;
9473 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009474 free(proxy->conf.lfs_file);
9475 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9476 proxy->conf.lfs_line = proxy->conf.args.line;
9477
9478 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009479 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9480 char *errmsg = NULL;
9481 cur_arg = 1;
9482 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009483 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009484 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009485 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009486 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9487 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9488 free(errmsg);
9489 goto out_err;
9490 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009491 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009492 action_build_list(&http_req_keywords.list, &trash);
9493 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
9494 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009495 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02009496 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009497 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009498 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009499 }
9500
9501 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9502 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009503 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009504
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009505 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9506 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9507 file, linenum, args[0], errmsg);
9508 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009509 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009510 }
9511 rule->cond = cond;
9512 }
9513 else if (*args[cur_arg]) {
9514 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9515 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9516 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009517 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009518 }
9519
9520 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009521 out_err:
9522 free(rule);
9523 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009524}
9525
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009526/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009527struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009528{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009529 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009530 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009531 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009532 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009533
9534 rule = calloc(1, sizeof(*rule));
9535 if (!rule) {
9536 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9537 goto out_err;
9538 }
9539
9540 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009541 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009542 cur_arg = 1;
9543 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009544 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009545 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009546 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009547 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009548 cur_arg = 1;
9549
9550 if (!*args[cur_arg] ||
9551 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9552 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9553 file, linenum, args[0]);
9554 goto out_err;
9555 }
9556 rule->arg.nice = atoi(args[cur_arg]);
9557 if (rule->arg.nice < -1024)
9558 rule->arg.nice = -1024;
9559 else if (rule->arg.nice > 1024)
9560 rule->arg.nice = 1024;
9561 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009562 } else if (!strcmp(args[0], "set-tos")) {
9563#ifdef IP_TOS
9564 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009565 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009566 cur_arg = 1;
9567
9568 if (!*args[cur_arg] ||
9569 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9570 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9571 file, linenum, args[0]);
9572 goto out_err;
9573 }
9574
9575 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9576 if (err && *err != '\0') {
9577 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9578 file, linenum, err, args[0]);
9579 goto out_err;
9580 }
9581 cur_arg++;
9582#else
9583 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9584 goto out_err;
9585#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009586 } else if (!strcmp(args[0], "set-mark")) {
9587#ifdef SO_MARK
9588 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009589 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009590 cur_arg = 1;
9591
9592 if (!*args[cur_arg] ||
9593 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9594 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9595 file, linenum, args[0]);
9596 goto out_err;
9597 }
9598
9599 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9600 if (err && *err != '\0') {
9601 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9602 file, linenum, err, args[0]);
9603 goto out_err;
9604 }
9605 cur_arg++;
9606 global.last_checks |= LSTCHK_NETADM;
9607#else
9608 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9609 goto out_err;
9610#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009611 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009612 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009613 cur_arg = 1;
9614
9615 if (!*args[cur_arg] ||
9616 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9617 bad_log_level:
9618 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9619 file, linenum, args[0]);
9620 goto out_err;
9621 }
9622 if (strcmp(args[cur_arg], "silent") == 0)
9623 rule->arg.loglevel = -1;
Ruoshan Huangdd016782016-06-15 22:16:03 +08009624 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009625 goto bad_log_level;
9626 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009627 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009628 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009629 cur_arg = 1;
9630
9631 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9632 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9633 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9634 file, linenum, args[0]);
9635 goto out_err;
9636 }
9637
9638 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9639 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9640 LIST_INIT(&rule->arg.hdr_add.fmt);
9641
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009642 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009643 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009644 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 +01009645 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9646 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9647 file, linenum, args[0], error);
9648 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009649 goto out_err;
9650 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009651 free(proxy->conf.lfs_file);
9652 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9653 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009654 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009655 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009656 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009657 cur_arg = 1;
9658
9659 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009660 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9661 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009662 file, linenum, args[0]);
9663 goto out_err;
9664 }
9665
9666 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9667 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9668 LIST_INIT(&rule->arg.hdr_add.fmt);
9669
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009670 error = NULL;
9671 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9672 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9673 args[cur_arg + 1], error);
9674 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009675 goto out_err;
9676 }
9677
9678 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009679 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009680 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 +01009681 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9682 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9683 file, linenum, args[0], error);
9684 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009685 goto out_err;
9686 }
Sasha Pachev218f0642014-06-16 12:05:59 -06009687
9688 free(proxy->conf.lfs_file);
9689 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9690 proxy->conf.lfs_line = proxy->conf.args.line;
9691 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009692 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009693 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009694 cur_arg = 1;
9695
9696 if (!*args[cur_arg] ||
9697 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9698 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9699 file, linenum, args[0]);
9700 goto out_err;
9701 }
9702
9703 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9704 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9705
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009706 proxy->conf.args.ctx = ARGC_HRS;
9707 free(proxy->conf.lfs_file);
9708 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9709 proxy->conf.lfs_line = proxy->conf.args.line;
9710 cur_arg += 1;
9711 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9712 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009713 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009714 /*
9715 * '+ 8' for 'add-acl('
9716 * '- 9' for 'add-acl(' + trailing ')'
9717 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009718 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009719
9720 cur_arg = 1;
9721
9722 if (!*args[cur_arg] ||
9723 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9724 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9725 file, linenum, args[0]);
9726 goto out_err;
9727 }
9728
9729 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009730 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009731 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009732 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009733 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9734 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9735 file, linenum, args[0], error);
9736 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009737 goto out_err;
9738 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009739 free(proxy->conf.lfs_file);
9740 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9741 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009742
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009743 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009744 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9745 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009746 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009747 /*
9748 * '+ 8' for 'del-acl('
9749 * '- 9' for 'del-acl(' + trailing ')'
9750 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009751 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009752
9753 cur_arg = 1;
9754
9755 if (!*args[cur_arg] ||
9756 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9757 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9758 file, linenum, args[0]);
9759 goto out_err;
9760 }
9761
9762 LIST_INIT(&rule->arg.map.key);
9763 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009764 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009765 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009766 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9767 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9768 file, linenum, args[0], error);
9769 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009770 goto out_err;
9771 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009772 free(proxy->conf.lfs_file);
9773 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9774 proxy->conf.lfs_line = proxy->conf.args.line;
9775 cur_arg += 1;
9776 } else if (strncmp(args[0], "del-map", 7) == 0) {
9777 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009778 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009779 /*
9780 * '+ 8' for 'del-map('
9781 * '- 9' for 'del-map(' + trailing ')'
9782 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009783 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009784
9785 cur_arg = 1;
9786
9787 if (!*args[cur_arg] ||
9788 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9789 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9790 file, linenum, args[0]);
9791 goto out_err;
9792 }
9793
9794 LIST_INIT(&rule->arg.map.key);
9795 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009796 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009797 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009798 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9799 Alert("parsing [%s:%d]: 'http-response %s' %s.\n",
9800 file, linenum, args[0], error);
9801 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009802 goto out_err;
9803 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009804 free(proxy->conf.lfs_file);
9805 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9806 proxy->conf.lfs_line = proxy->conf.args.line;
9807 cur_arg += 1;
9808 } else if (strncmp(args[0], "set-map", 7) == 0) {
9809 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009810 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009811 /*
9812 * '+ 8' for 'set-map('
9813 * '- 9' for 'set-map(' + trailing ')'
9814 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009815 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009816
9817 cur_arg = 1;
9818
9819 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9820 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9821 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9822 file, linenum, args[0]);
9823 goto out_err;
9824 }
9825
9826 LIST_INIT(&rule->arg.map.key);
9827 LIST_INIT(&rule->arg.map.value);
9828
9829 proxy->conf.args.ctx = ARGC_HRS;
9830
9831 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009832 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009833 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009834 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9835 Alert("parsing [%s:%d]: 'http-response %s' name: %s.\n",
9836 file, linenum, args[0], error);
9837 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009838 goto out_err;
9839 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009840
9841 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009842 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009843 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 +01009844 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9845 Alert("parsing [%s:%d]: 'http-response %s' value: %s.\n",
9846 file, linenum, args[0], error);
9847 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009848 goto out_err;
9849 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009850
9851 free(proxy->conf.lfs_file);
9852 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9853 proxy->conf.lfs_line = proxy->conf.args.line;
9854
9855 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009856 } else if (strcmp(args[0], "redirect") == 0) {
9857 struct redirect_rule *redir;
9858 char *errmsg = NULL;
9859
9860 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
9861 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9862 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9863 goto out_err;
9864 }
9865
9866 /* this redirect rule might already contain a parsed condition which
9867 * we'll pass to the http-request rule.
9868 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009869 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009870 rule->arg.redir = redir;
9871 rule->cond = redir->cond;
9872 redir->cond = NULL;
9873 cur_arg = 2;
9874 return rule;
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009875 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9876 args[0][9] == '\0' && args[0][8] >= '0' &&
9877 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
9878 struct sample_expr *expr;
9879 unsigned int where;
9880 char *err = NULL;
9881
9882 cur_arg = 1;
9883 proxy->conf.args.ctx = ARGC_TRK;
9884
9885 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9886 if (!expr) {
9887 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9888 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9889 free(err);
9890 goto out_err;
9891 }
9892
9893 where = 0;
9894 if (proxy->cap & PR_CAP_FE)
9895 where |= SMP_VAL_FE_HRS_HDR;
9896 if (proxy->cap & PR_CAP_BE)
9897 where |= SMP_VAL_BE_HRS_HDR;
9898
9899 if (!(expr->fetch->val & where)) {
9900 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :"
9901 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9902 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9903 args[cur_arg-1], sample_src_names(expr->fetch->use));
9904 free(expr);
9905 goto out_err;
9906 }
9907
9908 if (strcmp(args[cur_arg], "table") == 0) {
9909 cur_arg++;
9910 if (!args[cur_arg]) {
9911 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n",
9912 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9913 free(expr);
9914 goto out_err;
9915 }
9916 /* we copy the table name for now, it will be resolved later */
9917 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
9918 cur_arg++;
9919 }
9920 rule->arg.trk_ctr.expr = expr;
9921 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
William Lallemand73025dd2014-04-24 14:38:37 +02009922 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9923 char *errmsg = NULL;
9924 cur_arg = 1;
9925 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009926 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009927 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009928 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009929 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9930 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9931 free(errmsg);
9932 goto out_err;
9933 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009934 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009935 action_build_list(&http_res_keywords.list, &trash);
9936 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9937 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009938 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02009939 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009940 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009941 goto out_err;
9942 }
9943
9944 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9945 struct acl_cond *cond;
9946 char *errmsg = NULL;
9947
9948 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9949 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9950 file, linenum, args[0], errmsg);
9951 free(errmsg);
9952 goto out_err;
9953 }
9954 rule->cond = cond;
9955 }
9956 else if (*args[cur_arg]) {
9957 Alert("parsing [%s:%d]: 'http-response %s' expects"
9958 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9959 file, linenum, args[0], args[cur_arg]);
9960 goto out_err;
9961 }
9962
9963 return rule;
9964 out_err:
9965 free(rule);
9966 return NULL;
9967}
9968
Willy Tarreau4baae242012-12-27 12:00:31 +01009969/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009970 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009971 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9972 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009973 */
9974struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009975 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009976{
9977 struct redirect_rule *rule;
9978 int cur_arg;
9979 int type = REDIRECT_TYPE_NONE;
9980 int code = 302;
9981 const char *destination = NULL;
9982 const char *cookie = NULL;
9983 int cookie_set = 0;
9984 unsigned int flags = REDIRECT_FLAG_NONE;
9985 struct acl_cond *cond = NULL;
9986
9987 cur_arg = 0;
9988 while (*(args[cur_arg])) {
9989 if (strcmp(args[cur_arg], "location") == 0) {
9990 if (!*args[cur_arg + 1])
9991 goto missing_arg;
9992
9993 type = REDIRECT_TYPE_LOCATION;
9994 cur_arg++;
9995 destination = args[cur_arg];
9996 }
9997 else if (strcmp(args[cur_arg], "prefix") == 0) {
9998 if (!*args[cur_arg + 1])
9999 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +010010000 type = REDIRECT_TYPE_PREFIX;
10001 cur_arg++;
10002 destination = args[cur_arg];
10003 }
10004 else if (strcmp(args[cur_arg], "scheme") == 0) {
10005 if (!*args[cur_arg + 1])
10006 goto missing_arg;
10007
10008 type = REDIRECT_TYPE_SCHEME;
10009 cur_arg++;
10010 destination = args[cur_arg];
10011 }
10012 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
10013 if (!*args[cur_arg + 1])
10014 goto missing_arg;
10015
10016 cur_arg++;
10017 cookie = args[cur_arg];
10018 cookie_set = 1;
10019 }
10020 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
10021 if (!*args[cur_arg + 1])
10022 goto missing_arg;
10023
10024 cur_arg++;
10025 cookie = args[cur_arg];
10026 cookie_set = 0;
10027 }
10028 else if (strcmp(args[cur_arg], "code") == 0) {
10029 if (!*args[cur_arg + 1])
10030 goto missing_arg;
10031
10032 cur_arg++;
10033 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040010034 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +010010035 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040010036 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +010010037 args[cur_arg - 1], args[cur_arg]);
10038 return NULL;
10039 }
10040 }
10041 else if (!strcmp(args[cur_arg],"drop-query")) {
10042 flags |= REDIRECT_FLAG_DROP_QS;
10043 }
10044 else if (!strcmp(args[cur_arg],"append-slash")) {
10045 flags |= REDIRECT_FLAG_APPEND_SLASH;
10046 }
10047 else if (strcmp(args[cur_arg], "if") == 0 ||
10048 strcmp(args[cur_arg], "unless") == 0) {
10049 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
10050 if (!cond) {
10051 memprintf(errmsg, "error in condition: %s", *errmsg);
10052 return NULL;
10053 }
10054 break;
10055 }
10056 else {
10057 memprintf(errmsg,
10058 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
10059 args[cur_arg]);
10060 return NULL;
10061 }
10062 cur_arg++;
10063 }
10064
10065 if (type == REDIRECT_TYPE_NONE) {
10066 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
10067 return NULL;
10068 }
10069
Willy Tarreaube4653b2015-05-28 15:26:58 +020010070 if (dir && type != REDIRECT_TYPE_LOCATION) {
10071 memprintf(errmsg, "response only supports redirect type 'location'");
10072 return NULL;
10073 }
10074
Vincent Bernat3c2f2f22016-04-03 13:48:42 +020010075 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +010010076 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +010010077 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010078
10079 if (!use_fmt) {
10080 /* old-style static redirect rule */
10081 rule->rdr_str = strdup(destination);
10082 rule->rdr_len = strlen(destination);
10083 }
10084 else {
10085 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010086
10087 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
10088 * if prefix == "/", we don't want to add anything, otherwise it
10089 * makes it hard for the user to configure a self-redirection.
10090 */
Godbachd9722032014-12-18 15:44:58 +080010091 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010092 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010010093 if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
10094 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 +010010095 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
10096 errmsg)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010010097 return NULL;
10098 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +010010099 free(curproxy->conf.lfs_file);
10100 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
10101 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010102 }
10103 }
10104
Willy Tarreau4baae242012-12-27 12:00:31 +010010105 if (cookie) {
10106 /* depending on cookie_set, either we want to set the cookie, or to clear it.
10107 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
10108 */
10109 rule->cookie_len = strlen(cookie);
10110 if (cookie_set) {
10111 rule->cookie_str = malloc(rule->cookie_len + 10);
10112 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10113 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
10114 rule->cookie_len += 9;
10115 } else {
10116 rule->cookie_str = malloc(rule->cookie_len + 21);
10117 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10118 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
10119 rule->cookie_len += 20;
10120 }
10121 }
10122 rule->type = type;
10123 rule->code = code;
10124 rule->flags = flags;
10125 LIST_INIT(&rule->list);
10126 return rule;
10127
10128 missing_arg:
10129 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
10130 return NULL;
10131}
10132
Willy Tarreau8797c062007-05-07 00:55:35 +020010133/************************************************************************/
10134/* The code below is dedicated to ACL parsing and matching */
10135/************************************************************************/
10136
10137
Willy Tarreau14174bc2012-04-16 14:34:04 +020010138/* This function ensures that the prerequisites for an L7 fetch are ready,
10139 * which means that a request or response is ready. If some data is missing,
10140 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +020010141 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
10142 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010143 *
10144 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +020010145 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
10146 * decide whether or not an HTTP message is present ;
10147 * 0 if the requested data cannot be fetched or if it is certain that
10148 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010149 * 1 if an HTTP message is ready
10150 */
James Rosewell91a41cb2015-09-18 17:11:16 +010010151int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010152 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +020010153{
Willy Tarreau192252e2015-04-04 01:47:55 +020010154 struct http_txn *txn;
10155 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010156
Willy Tarreaube508f12016-03-10 11:47:01 +010010157 /* Note: it is possible that <s> is NULL when called before stream
10158 * initialization (eg: tcp-request connection), so this function is the
10159 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010160 */
Willy Tarreau192252e2015-04-04 01:47:55 +020010161 if (!s)
10162 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +010010163
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +020010164 if (!s->txn) {
10165 if (unlikely(!http_alloc_txn(s)))
10166 return 0; /* not enough memory */
10167 http_init_txn(s);
10168 }
Willy Tarreau192252e2015-04-04 01:47:55 +020010169 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +020010170 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010171
10172 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010173 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010174
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010175 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010176 /* If the buffer does not leave enough free space at the end,
10177 * we must first realign it.
10178 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010179 if (s->req.buf->p > s->req.buf->data &&
10180 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
10181 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +010010182
Willy Tarreau14174bc2012-04-16 14:34:04 +020010183 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +020010184 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +020010185 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010186
10187 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010188 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +020010189 http_msg_analyzer(msg, &txn->hdr_idx);
10190
10191 /* Still no valid request ? */
10192 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +020010193 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010194 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +020010195 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010196 }
10197 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +020010198 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010199 return 0;
10200 }
10201
10202 /* OK we just got a valid HTTP request. We have some minor
10203 * preparation to perform so that further checks can rely
10204 * on HTTP tests.
10205 */
Willy Tarreauaae75e32013-03-29 12:31:49 +010010206
10207 /* If the request was parsed but was too large, we must absolutely
10208 * return an error so that it is not processed. At the moment this
10209 * cannot happen, but if the parsers are to change in the future,
10210 * we want this check to be maintained.
10211 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010212 if (unlikely(s->req.buf->i + s->req.buf->p >
10213 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +010010214 msg->err_state = msg->msg_state;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010215 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010216 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010217 return 1;
10218 }
10219
Willy Tarreau9b28e032012-10-12 23:49:43 +020010220 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +020010221 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +020010222 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010223
Willy Tarreau506d0502013-07-06 13:29:24 +020010224 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
10225 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010226 }
10227
Willy Tarreau506d0502013-07-06 13:29:24 +020010228 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +020010229 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +020010230 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010231
10232 /* otherwise everything's ready for the request */
10233 }
Willy Tarreau24e32d82012-04-23 23:55:44 +020010234 else {
10235 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +020010236 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
10237 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010238 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +020010239 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010240 }
10241
10242 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010243 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010244 return 1;
10245}
Willy Tarreau8797c062007-05-07 00:55:35 +020010246
Willy Tarreau8797c062007-05-07 00:55:35 +020010247/* 1. Check on METHOD
10248 * We use the pre-parsed method if it is known, and store its number as an
10249 * integer. If it is unknown, we use the pointer and the length.
10250 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010251static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +020010252{
10253 int len, meth;
10254
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010010255 len = strlen(text);
10256 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +020010257
10258 pattern->val.i = meth;
10259 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +020010260 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +020010261 pattern->len = len;
10262 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010263 else {
10264 pattern->ptr.str = NULL;
10265 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010266 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010267 return 1;
10268}
10269
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010270/* This function fetches the method of current HTTP request and stores
10271 * it in the global pattern struct as a chunk. There are two possibilities :
10272 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10273 * in <len> and <ptr> is NULL ;
10274 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10275 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010276 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010277 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010278static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010279smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010280{
10281 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +010010282 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010283
Willy Tarreau24e32d82012-04-23 23:55:44 +020010284 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010285
Willy Tarreaube508f12016-03-10 11:47:01 +010010286 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010287 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010288 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010289 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010290 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010291 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10292 /* ensure the indexes are not affected */
10293 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010294 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010295 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
10296 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010297 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010298 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010299 return 1;
10300}
10301
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010302/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010303static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010304{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010305 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010306 struct pattern_list *lst;
10307 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010308
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010309 list_for_each_entry(lst, &expr->patterns, list) {
10310 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010311
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010312 /* well-known method */
10313 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010314 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010315 return pattern;
10316 else
10317 continue;
10318 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010319
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010320 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010321 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010322 continue;
10323
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010324 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010325 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
10326 (!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 +010010327 return pattern;
10328 }
10329 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010330}
10331
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010332static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010333smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010334{
Willy Tarreaube508f12016-03-10 11:47:01 +010010335 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010336 char *ptr;
10337 int len;
10338
Willy Tarreauc0239e02012-04-16 14:42:55 +020010339 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010340
Willy Tarreaube508f12016-03-10 11:47:01 +010010341 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010342 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010343 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010344
10345 while ((len-- > 0) && (*ptr++ != '/'));
10346 if (len <= 0)
10347 return 0;
10348
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010349 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010350 smp->data.u.str.str = ptr;
10351 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010352
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010353 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010354 return 1;
10355}
10356
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010357static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010358smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010359{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010360 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010361 char *ptr;
10362 int len;
10363
Willy Tarreauc0239e02012-04-16 14:42:55 +020010364 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010365
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010366 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010367 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10368 return 0;
10369
Willy Tarreau8797c062007-05-07 00:55:35 +020010370 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010371 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010372
10373 while ((len-- > 0) && (*ptr++ != '/'));
10374 if (len <= 0)
10375 return 0;
10376
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010377 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010378 smp->data.u.str.str = ptr;
10379 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010380
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010381 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010382 return 1;
10383}
10384
10385/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010386static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010387smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010388{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010389 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010390 char *ptr;
10391 int len;
10392
Willy Tarreauc0239e02012-04-16 14:42:55 +020010393 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010394
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010395 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010396 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10397 return 0;
10398
Willy Tarreau8797c062007-05-07 00:55:35 +020010399 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010400 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010401
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010402 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010403 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010404 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010405 return 1;
10406}
10407
Thierry Fournierf4011dd2016-03-29 17:23:51 +020010408static int
10409smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
10410{
10411 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
10412 return 0;
10413
10414 if (!smp->strm->unique_id) {
10415 if ((smp->strm->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
10416 return 0;
10417 smp->strm->unique_id[0] = '\0';
10418 }
10419 smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id,
10420 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
10421
10422 smp->data.type = SMP_T_STR;
10423 smp->data.u.str.str = smp->strm->unique_id;
10424 smp->flags = SMP_F_CONST;
10425 return 1;
10426}
10427
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010428/* returns the longest available part of the body. This requires that the body
10429 * has been waited for using http-buffer-request.
10430 */
10431static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010432smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010433{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010434 struct http_msg *msg;
10435 unsigned long len;
10436 unsigned long block1;
10437 char *body;
10438 struct chunk *temp;
10439
10440 CHECK_HTTP_MESSAGE_FIRST();
10441
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010442 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010443 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010444 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010445 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010446
10447 len = http_body_bytes(msg);
10448 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
10449
10450 block1 = len;
10451 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
10452 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
10453
10454 if (block1 == len) {
10455 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010456 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010457 smp->data.u.str.str = body;
10458 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010459 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
10460 }
10461 else {
10462 /* buffer is wrapped, we need to defragment it */
10463 temp = get_trash_chunk();
10464 memcpy(temp->str, body, block1);
10465 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010466 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010467 smp->data.u.str.str = temp->str;
10468 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010469 smp->flags = SMP_F_VOL_TEST;
10470 }
10471 return 1;
10472}
10473
10474
10475/* returns the available length of the body. This requires that the body
10476 * has been waited for using http-buffer-request.
10477 */
10478static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010479smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010480{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010481 struct http_msg *msg;
10482
10483 CHECK_HTTP_MESSAGE_FIRST();
10484
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010485 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010486 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010487 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010488 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010489
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010490 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010491 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010492
10493 smp->flags = SMP_F_VOL_TEST;
10494 return 1;
10495}
10496
10497
10498/* returns the advertised length of the body, or the advertised size of the
10499 * chunks available in the buffer. This requires that the body has been waited
10500 * for using http-buffer-request.
10501 */
10502static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010503smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010504{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010505 struct http_msg *msg;
10506
10507 CHECK_HTTP_MESSAGE_FIRST();
10508
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010509 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010510 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010511 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010512 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010513
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010514 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010515 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010516
10517 smp->flags = SMP_F_VOL_TEST;
10518 return 1;
10519}
10520
10521
Willy Tarreau8797c062007-05-07 00:55:35 +020010522/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010523static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010524smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010525{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010526 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010527
Willy Tarreauc0239e02012-04-16 14:42:55 +020010528 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010529
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010530 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010531 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010532 smp->data.u.str.len = txn->req.sl.rq.u_l;
10533 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010534 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010535 return 1;
10536}
10537
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010538static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010539smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010540{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010541 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010542 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010543
Willy Tarreauc0239e02012-04-16 14:42:55 +020010544 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010545
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010546 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010547 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 +020010548 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010549 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010550
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010551 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010552 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010553 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010554 return 1;
10555}
10556
10557static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010558smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010559{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010560 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010561 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010562
Willy Tarreauc0239e02012-04-16 14:42:55 +020010563 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010564
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010565 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010566 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 +020010567 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10568 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010569
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010570 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010571 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010572 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010573 return 1;
10574}
10575
Willy Tarreau185b5c42012-04-26 15:11:51 +020010576/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10577 * Accepts an optional argument of type string containing the header field name,
10578 * and an optional argument of type signed or unsigned integer to request an
10579 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010580 * headers are considered from the first one. It does not stop on commas and
10581 * returns full lines instead (useful for User-Agent or Date for example).
10582 */
10583static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010584smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010585{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010586 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010587 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010588 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010589 int occ = 0;
10590 const char *name_str = NULL;
10591 int name_len = 0;
10592
10593 if (!ctx) {
10594 /* first call */
10595 ctx = &static_hdr_ctx;
10596 ctx->idx = 0;
10597 smp->ctx.a[0] = ctx;
10598 }
10599
10600 if (args) {
10601 if (args[0].type != ARGT_STR)
10602 return 0;
10603 name_str = args[0].data.str.str;
10604 name_len = args[0].data.str.len;
10605
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010606 if (args[1].type == ARGT_SINT)
10607 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010608 }
10609
10610 CHECK_HTTP_MESSAGE_FIRST();
10611
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010612 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010613 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 +020010614
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010615 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10616 /* search for header from the beginning */
10617 ctx->idx = 0;
10618
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010619 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010620 /* no explicit occurrence and single fetch => last header by default */
10621 occ = -1;
10622
10623 if (!occ)
10624 /* prepare to report multiple occurrences for ACL fetches */
10625 smp->flags |= SMP_F_NOT_LAST;
10626
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010627 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010628 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010629 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 +020010630 return 1;
10631
10632 smp->flags &= ~SMP_F_NOT_LAST;
10633 return 0;
10634}
10635
10636/* 6. Check on HTTP header count. The number of occurrences is returned.
10637 * Accepts exactly 1 argument of type string. It does not stop on commas and
10638 * returns full lines instead (useful for User-Agent or Date for example).
10639 */
10640static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010641smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010642{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010643 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010644 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010645 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010646 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010647 const char *name = NULL;
10648 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010649
Willy Tarreau601a4d12015-04-01 19:16:09 +020010650 if (args && args->type == ARGT_STR) {
10651 name = args->data.str.str;
10652 len = args->data.str.len;
10653 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010654
10655 CHECK_HTTP_MESSAGE_FIRST();
10656
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010657 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010658 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 +020010659
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010660 ctx.idx = 0;
10661 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010662 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010663 cnt++;
10664
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010665 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010666 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010667 smp->flags = SMP_F_VOL_HDR;
10668 return 1;
10669}
10670
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010671static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010672smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010673{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010674 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010675 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010676 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010677 struct chunk *temp;
10678 char del = ',';
10679
10680 if (args && args->type == ARGT_STR)
10681 del = *args[0].data.str.str;
10682
10683 CHECK_HTTP_MESSAGE_FIRST();
10684
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010685 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010686 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 +020010687
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010688 temp = get_trash_chunk();
10689
10690 ctx.idx = 0;
10691 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10692 if (temp->len)
10693 temp->str[temp->len++] = del;
10694 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10695 temp->len += ctx.del;
10696 }
10697
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010698 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010699 smp->data.u.str.str = temp->str;
10700 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010701 smp->flags = SMP_F_VOL_HDR;
10702 return 1;
10703}
10704
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010705/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10706 * Accepts an optional argument of type string containing the header field name,
10707 * and an optional argument of type signed or unsigned integer to request an
10708 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010709 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010710 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010711static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010712smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010713{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010714 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010715 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010716 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010717 int occ = 0;
10718 const char *name_str = NULL;
10719 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010720
Willy Tarreaua890d072013-04-02 12:01:06 +020010721 if (!ctx) {
10722 /* first call */
10723 ctx = &static_hdr_ctx;
10724 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010725 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010726 }
10727
Willy Tarreau185b5c42012-04-26 15:11:51 +020010728 if (args) {
10729 if (args[0].type != ARGT_STR)
10730 return 0;
10731 name_str = args[0].data.str.str;
10732 name_len = args[0].data.str.len;
10733
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010734 if (args[1].type == ARGT_SINT)
10735 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010736 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010737
Willy Tarreaue333ec92012-04-16 16:26:40 +020010738 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010739
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010740 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010741 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 +020010742
Willy Tarreau185b5c42012-04-26 15:11:51 +020010743 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010744 /* search for header from the beginning */
10745 ctx->idx = 0;
10746
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010747 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010748 /* no explicit occurrence and single fetch => last header by default */
10749 occ = -1;
10750
10751 if (!occ)
10752 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010753 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010754
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010755 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010756 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010757 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 +020010758 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010759
Willy Tarreau37406352012-04-23 16:16:37 +020010760 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010761 return 0;
10762}
10763
Willy Tarreauc11416f2007-06-17 16:58:38 +020010764/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010765 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010766 */
10767static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010768smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010769{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010770 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010771 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010772 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010773 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010774 const char *name = NULL;
10775 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010776
Willy Tarreau601a4d12015-04-01 19:16:09 +020010777 if (args && args->type == ARGT_STR) {
10778 name = args->data.str.str;
10779 len = args->data.str.len;
10780 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010781
Willy Tarreaue333ec92012-04-16 16:26:40 +020010782 CHECK_HTTP_MESSAGE_FIRST();
10783
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010784 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010785 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 +020010786
Willy Tarreau33a7e692007-06-10 19:45:56 +020010787 ctx.idx = 0;
10788 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010789 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010790 cnt++;
10791
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010792 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010793 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010794 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010795 return 1;
10796}
10797
Willy Tarreau185b5c42012-04-26 15:11:51 +020010798/* Fetch an HTTP header's integer value. The integer value is returned. It
10799 * takes a mandatory argument of type string and an optional one of type int
10800 * to designate a specific occurrence. It returns an unsigned integer, which
10801 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010802 */
10803static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010804smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010805{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010806 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010807
Willy Tarreauf853c462012-04-23 18:53:56 +020010808 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010809 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010810 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010811 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010812
Willy Tarreaud53e2422012-04-16 17:21:11 +020010813 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010814}
10815
Cyril Bonté69fa9922012-10-25 00:01:06 +020010816/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10817 * and an optional one of type int to designate a specific occurrence.
10818 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010819 */
10820static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010821smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010822{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010823 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010824
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010825 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010826 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010827 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010828 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010829 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010830 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010831 if (smp->data.u.str.len < temp->size - 1) {
10832 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10833 temp->str[smp->data.u.str.len] = '\0';
10834 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010835 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010836 break;
10837 }
10838 }
10839 }
10840
Willy Tarreaud53e2422012-04-16 17:21:11 +020010841 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010842 if (!(smp->flags & SMP_F_NOT_LAST))
10843 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010844 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010845 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010846}
10847
Willy Tarreau737b0c12007-06-10 21:28:46 +020010848/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10849 * the first '/' after the possible hostname, and ends before the possible '?'.
10850 */
10851static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010852smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010853{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010854 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010855 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010856
Willy Tarreauc0239e02012-04-16 14:42:55 +020010857 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010858
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010859 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010860 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010861 ptr = http_get_path(txn);
10862 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010863 return 0;
10864
10865 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010866 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010867 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010868
10869 while (ptr < end && *ptr != '?')
10870 ptr++;
10871
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010872 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010873 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010874 return 1;
10875}
10876
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010877/* This produces a concatenation of the first occurrence of the Host header
10878 * followed by the path component if it begins with a slash ('/'). This means
10879 * that '*' will not be added, resulting in exactly the first Host entry.
10880 * If no Host header is found, then the path is returned as-is. The returned
10881 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010882 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010883 */
10884static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010885smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010886{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010887 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010888 char *ptr, *end, *beg;
10889 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010890 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010891
10892 CHECK_HTTP_MESSAGE_FIRST();
10893
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010894 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010895 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010896 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010897 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010898
10899 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010900 temp = get_trash_chunk();
10901 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010902 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010903 smp->data.u.str.str = temp->str;
10904 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010905
10906 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010907 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010908 beg = http_get_path(txn);
10909 if (!beg)
10910 beg = end;
10911
10912 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10913
10914 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010915 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10916 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010917 }
10918
10919 smp->flags = SMP_F_VOL_1ST;
10920 return 1;
10921}
10922
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010923/* This produces a 32-bit hash of the concatenation of the first occurrence of
10924 * the Host header followed by the path component if it begins with a slash ('/').
10925 * This means that '*' will not be added, resulting in exactly the first Host
10926 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010927 * is hashed using the path hash followed by a full avalanche hash and provides a
10928 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010929 * high-traffic sites without having to store whole paths.
10930 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010931int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010932smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010933{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010934 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010935 struct hdr_ctx ctx;
10936 unsigned int hash = 0;
10937 char *ptr, *beg, *end;
10938 int len;
10939
10940 CHECK_HTTP_MESSAGE_FIRST();
10941
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010942 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010943 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010944 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010945 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10946 ptr = ctx.line + ctx.val;
10947 len = ctx.vlen;
10948 while (len--)
10949 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10950 }
10951
10952 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010953 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010954 beg = http_get_path(txn);
10955 if (!beg)
10956 beg = end;
10957
10958 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10959
10960 if (beg < ptr && *beg == '/') {
10961 while (beg < ptr)
10962 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10963 }
10964 hash = full_hash(hash);
10965
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010966 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010967 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010968 smp->flags = SMP_F_VOL_1ST;
10969 return 1;
10970}
10971
Willy Tarreau4a550602012-12-09 14:53:32 +010010972/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010973 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10974 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10975 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010976 * that in environments where IPv6 is insignificant, truncating the output to
10977 * 8 bytes would still work.
10978 */
10979static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010980smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010981{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010982 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010983 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010984
10985 if (!cli_conn)
10986 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010987
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010988 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010989 return 0;
10990
Willy Tarreau47ca5452012-12-23 20:22:19 +010010991 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010992 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010993 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010994
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010995 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010996 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010997 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010998 temp->len += 4;
10999 break;
11000 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011001 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010011002 temp->len += 16;
11003 break;
11004 default:
11005 return 0;
11006 }
11007
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011008 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011009 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010011010 return 1;
11011}
11012
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011013/* Extracts the query string, which comes after the question mark '?'. If no
11014 * question mark is found, nothing is returned. Otherwise it returns a sample
11015 * of type string carrying the whole query string.
11016 */
11017static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011018smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011019{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011020 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011021 char *ptr, *end;
11022
11023 CHECK_HTTP_MESSAGE_FIRST();
11024
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011025 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011026 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
11027 end = ptr + txn->req.sl.rq.u_l;
11028
11029 /* look up the '?' */
11030 do {
11031 if (ptr == end)
11032 return 0;
11033 } while (*ptr++ != '?');
11034
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011035 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011036 smp->data.u.str.str = ptr;
11037 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011038 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11039 return 1;
11040}
11041
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011042static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011043smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011044{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011045 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
11046 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
11047 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011048
Willy Tarreau24e32d82012-04-23 23:55:44 +020011049 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011050
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011051 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011052 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011053 return 1;
11054}
11055
Willy Tarreau7f18e522010-10-22 20:04:13 +020011056/* return a valid test if the current request is the first one on the connection */
11057static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011058smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020011059{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011060 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011061 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020011062 return 1;
11063}
11064
Willy Tarreau34db1082012-04-19 17:16:54 +020011065/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011066static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011067smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011068{
11069
Willy Tarreau24e32d82012-04-23 23:55:44 +020011070 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011071 return 0;
11072
Willy Tarreauc0239e02012-04-16 14:42:55 +020011073 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011074
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011075 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011076 return 0;
11077
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011078 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011079 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011080 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011081 return 1;
11082}
Willy Tarreau8797c062007-05-07 00:55:35 +020011083
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011084/* Accepts exactly 1 argument of type userlist */
11085static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011086smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011087{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011088 if (!args || args->type != ARGT_USR)
11089 return 0;
11090
11091 CHECK_HTTP_MESSAGE_FIRST();
11092
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011093 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011094 return 0;
11095
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011096 /* if the user does not belong to the userlist or has a wrong password,
11097 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010011098 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011099 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011100 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
11101 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010011102 return 0;
11103
11104 /* pat_match_auth() will need the user list */
11105 smp->ctx.a[0] = args->data.usr;
11106
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011107 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011108 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011109 smp->data.u.str.str = smp->strm->txn->auth.user;
11110 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011111
11112 return 1;
11113}
11114
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011115/* Try to find the next occurrence of a cookie name in a cookie header value.
11116 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
11117 * the cookie value is returned into *value and *value_l, and the function
11118 * returns a pointer to the next pointer to search from if the value was found.
11119 * Otherwise if the cookie was not found, NULL is returned and neither value
11120 * nor value_l are touched. The input <hdr> string should first point to the
11121 * header's value, and the <hdr_end> pointer must point to the first character
11122 * not part of the value. <list> must be non-zero if value may represent a list
11123 * of values (cookie headers). This makes it faster to abort parsing when no
11124 * list is expected.
11125 */
David Carlier4686f792015-09-25 14:10:50 +010011126char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011127extract_cookie_value(char *hdr, const char *hdr_end,
11128 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020011129 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011130{
11131 char *equal, *att_end, *att_beg, *val_beg, *val_end;
11132 char *next;
11133
11134 /* we search at least a cookie name followed by an equal, and more
11135 * generally something like this :
11136 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
11137 */
11138 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
11139 /* Iterate through all cookies on this line */
11140
Willy Tarreau2235b262016-11-05 15:50:20 +010011141 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011142 att_beg++;
11143
11144 /* find att_end : this is the first character after the last non
11145 * space before the equal. It may be equal to hdr_end.
11146 */
11147 equal = att_end = att_beg;
11148
11149 while (equal < hdr_end) {
11150 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
11151 break;
Willy Tarreau2235b262016-11-05 15:50:20 +010011152 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011153 continue;
11154 att_end = equal;
11155 }
11156
11157 /* here, <equal> points to '=', a delimitor or the end. <att_end>
11158 * is between <att_beg> and <equal>, both may be identical.
11159 */
11160
11161 /* look for end of cookie if there is an equal sign */
11162 if (equal < hdr_end && *equal == '=') {
11163 /* look for the beginning of the value */
11164 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +010011165 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011166 val_beg++;
11167
11168 /* find the end of the value, respecting quotes */
11169 next = find_cookie_value_end(val_beg, hdr_end);
11170
11171 /* make val_end point to the first white space or delimitor after the value */
11172 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +010011173 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011174 val_end--;
11175 } else {
11176 val_beg = val_end = next = equal;
11177 }
11178
11179 /* We have nothing to do with attributes beginning with '$'. However,
11180 * they will automatically be removed if a header before them is removed,
11181 * since they're supposed to be linked together.
11182 */
11183 if (*att_beg == '$')
11184 continue;
11185
11186 /* Ignore cookies with no equal sign */
11187 if (equal == next)
11188 continue;
11189
11190 /* Now we have the cookie name between att_beg and att_end, and
11191 * its value between val_beg and val_end.
11192 */
11193
11194 if (att_end - att_beg == cookie_name_l &&
11195 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
11196 /* let's return this value and indicate where to go on from */
11197 *value = val_beg;
11198 *value_l = val_end - val_beg;
11199 return next + 1;
11200 }
11201
11202 /* Set-Cookie headers only have the name in the first attr=value part */
11203 if (!list)
11204 break;
11205 }
11206
11207 return NULL;
11208}
11209
William Lallemanda43ba4e2014-01-28 18:14:25 +010011210/* Fetch a captured HTTP request header. The index is the position of
11211 * the "capture" option in the configuration file
11212 */
11213static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011214smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011215{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011216 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011217 int idx;
11218
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011219 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011220 return 0;
11221
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011222 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011223
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011224 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 +010011225 return 0;
11226
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011227 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011228 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011229 smp->data.u.str.str = smp->strm->req_cap[idx];
11230 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011231
11232 return 1;
11233}
11234
11235/* Fetch a captured HTTP response header. The index is the position of
11236 * the "capture" option in the configuration file
11237 */
11238static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011239smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011240{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011241 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011242 int idx;
11243
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011244 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011245 return 0;
11246
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011247 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011248
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011249 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 +010011250 return 0;
11251
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011252 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011253 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011254 smp->data.u.str.str = smp->strm->res_cap[idx];
11255 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011256
11257 return 1;
11258}
11259
William Lallemand65ad6e12014-01-31 15:08:02 +010011260/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
11261static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011262smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011263{
11264 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011265 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010011266 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011267
Willy Tarreau15e91e12015-04-04 00:52:09 +020011268 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011269 return 0;
11270
William Lallemand96a77852014-02-05 00:30:02 +010011271 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011272
William Lallemand96a77852014-02-05 00:30:02 +010011273 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11274 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011275
William Lallemand96a77852014-02-05 00:30:02 +010011276 temp = get_trash_chunk();
11277 temp->str = txn->uri;
11278 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011279 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011280 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011281 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011282
11283 return 1;
11284
11285}
11286
11287/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
11288static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011289smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011290{
11291 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011292 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010011293 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011294
Willy Tarreau15e91e12015-04-04 00:52:09 +020011295 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011296 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010011297
William Lallemand65ad6e12014-01-31 15:08:02 +010011298 ptr = txn->uri;
11299
11300 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11301 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010011302
William Lallemand65ad6e12014-01-31 15:08:02 +010011303 if (!*ptr)
11304 return 0;
11305
11306 ptr++; /* skip the space */
11307
11308 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010011309 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010011310 if (!ptr)
11311 return 0;
11312 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
11313 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011314
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011315 smp->data.u.str = *temp;
11316 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011317 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011318 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011319
11320 return 1;
11321}
11322
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011323/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
11324 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11325 */
11326static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011327smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011328{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011329 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011330
Willy Tarreau15e91e12015-04-04 00:52:09 +020011331 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011332 return 0;
11333
11334 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011335 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011336 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011337 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011338
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011339 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011340 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011341 smp->flags = SMP_F_CONST;
11342 return 1;
11343
11344}
11345
11346/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
11347 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11348 */
11349static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011350smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011351{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011352 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011353
Willy Tarreau15e91e12015-04-04 00:52:09 +020011354 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011355 return 0;
11356
11357 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011358 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011359 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011360 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011361
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011362 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011363 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011364 smp->flags = SMP_F_CONST;
11365 return 1;
11366
11367}
11368
William Lallemand65ad6e12014-01-31 15:08:02 +010011369
Willy Tarreaue333ec92012-04-16 16:26:40 +020011370/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011371 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011372 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011373 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011374 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011375 * Accepts exactly 1 argument of type string. If the input options indicate
11376 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011377 * The returned sample is of type CSTR. Can be used to parse cookies in other
11378 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011379 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011380int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011381{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011382 struct http_txn *txn;
11383 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011384 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011385 const struct http_msg *msg;
11386 const char *hdr_name;
11387 int hdr_name_len;
11388 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011389 int occ = 0;
11390 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011391
Willy Tarreau24e32d82012-04-23 23:55:44 +020011392 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011393 return 0;
11394
Willy Tarreaua890d072013-04-02 12:01:06 +020011395 if (!ctx) {
11396 /* first call */
11397 ctx = &static_hdr_ctx;
11398 ctx->idx = 0;
11399 smp->ctx.a[2] = ctx;
11400 }
11401
Willy Tarreaue333ec92012-04-16 16:26:40 +020011402 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011403
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011404 txn = smp->strm->txn;
11405 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011406
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011407 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011408 msg = &txn->req;
11409 hdr_name = "Cookie";
11410 hdr_name_len = 6;
11411 } else {
11412 msg = &txn->rsp;
11413 hdr_name = "Set-Cookie";
11414 hdr_name_len = 10;
11415 }
11416
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011417 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020011418 /* no explicit occurrence and single fetch => last cookie by default */
11419 occ = -1;
11420
11421 /* OK so basically here, either we want only one value and it's the
11422 * last one, or we want to iterate over all of them and we fetch the
11423 * next one.
11424 */
11425
Willy Tarreau9b28e032012-10-12 23:49:43 +020011426 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011427 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011428 /* search for the header from the beginning, we must first initialize
11429 * the search parameters.
11430 */
Willy Tarreau37406352012-04-23 16:16:37 +020011431 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011432 ctx->idx = 0;
11433 }
11434
Willy Tarreau28376d62012-04-26 21:26:10 +020011435 smp->flags |= SMP_F_VOL_HDR;
11436
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011437 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011438 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11439 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011440 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11441 goto out;
11442
Willy Tarreau24e32d82012-04-23 23:55:44 +020011443 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011444 continue;
11445
Willy Tarreau37406352012-04-23 16:16:37 +020011446 smp->ctx.a[0] = ctx->line + ctx->val;
11447 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011448 }
11449
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011450 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011451 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011452 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011453 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011454 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011455 &smp->data.u.str.str,
11456 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011457 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011458 found = 1;
11459 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011460 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020011461 smp->flags |= SMP_F_NOT_LAST;
11462 return 1;
11463 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011464 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011465 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011466 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011467 /* all cookie headers and values were scanned. If we're looking for the
11468 * last occurrence, we may return it now.
11469 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011470 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011471 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011472 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011473}
11474
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011475/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011476 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011477 * multiple cookies may be parsed on the same line. The returned sample is of
11478 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011479 */
11480static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011481smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011482{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011483 struct http_txn *txn;
11484 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011485 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011486 const struct http_msg *msg;
11487 const char *hdr_name;
11488 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011489 int cnt;
11490 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011491 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011492
Willy Tarreau24e32d82012-04-23 23:55:44 +020011493 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011494 return 0;
11495
Willy Tarreaue333ec92012-04-16 16:26:40 +020011496 CHECK_HTTP_MESSAGE_FIRST();
11497
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011498 txn = smp->strm->txn;
11499 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011500
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011501 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011502 msg = &txn->req;
11503 hdr_name = "Cookie";
11504 hdr_name_len = 6;
11505 } else {
11506 msg = &txn->rsp;
11507 hdr_name = "Set-Cookie";
11508 hdr_name_len = 10;
11509 }
11510
Willy Tarreau9b28e032012-10-12 23:49:43 +020011511 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011512 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011513 ctx.idx = 0;
11514 cnt = 0;
11515
11516 while (1) {
11517 /* Note: val_beg == NULL every time we need to fetch a new header */
11518 if (!val_beg) {
11519 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11520 break;
11521
Willy Tarreau24e32d82012-04-23 23:55:44 +020011522 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011523 continue;
11524
11525 val_beg = ctx.line + ctx.val;
11526 val_end = val_beg + ctx.vlen;
11527 }
11528
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011529 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011530 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011531 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011532 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011533 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011534 &smp->data.u.str.str,
11535 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011536 cnt++;
11537 }
11538 }
11539
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011540 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011541 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011542 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011543 return 1;
11544}
11545
Willy Tarreau51539362012-05-08 12:46:28 +020011546/* Fetch an cookie's integer value. The integer value is returned. It
11547 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11548 */
11549static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011550smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011551{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011552 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011553
11554 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011555 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011556 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020011557 }
11558
11559 return ret;
11560}
11561
Willy Tarreau8797c062007-05-07 00:55:35 +020011562/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011563/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011564/************************************************************************/
11565
David Cournapeau16023ee2010-12-23 20:55:41 +090011566/*
11567 * Given a path string and its length, find the position of beginning of the
11568 * query string. Returns NULL if no query string is found in the path.
11569 *
11570 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11571 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011572 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011573 */
bedis4c75cca2012-10-05 08:38:24 +020011574static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011575{
11576 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011577
bedis4c75cca2012-10-05 08:38:24 +020011578 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011579 return p ? p + 1 : NULL;
11580}
11581
bedis4c75cca2012-10-05 08:38:24 +020011582static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011583{
bedis4c75cca2012-10-05 08:38:24 +020011584 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011585}
11586
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011587/* after increasing a pointer value, it can exceed the first buffer
11588 * size. This function transform the value of <ptr> according with
11589 * the expected position. <chunks> is an array of the one or two
11590 * avalaible chunks. The first value is the start of the first chunk,
11591 * the second value if the end+1 of the first chunks. The third value
11592 * is NULL or the start of the second chunk and the fourth value is
11593 * the end+1 of the second chunk. The function returns 1 if does a
11594 * wrap, else returns 0.
11595 */
11596static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11597{
11598 if (*ptr < chunks[1])
11599 return 0;
11600 if (!chunks[2])
11601 return 0;
11602 *ptr = chunks[2] + ( *ptr - chunks[1] );
11603 return 1;
11604}
11605
David Cournapeau16023ee2010-12-23 20:55:41 +090011606/*
11607 * Given a url parameter, find the starting position of the first occurence,
11608 * or NULL if the parameter is not found.
11609 *
11610 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11611 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011612 *
Willy Tarreauf6625822015-12-27 14:51:01 +010011613 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011614 * or the second chunk. The caller must be check the position before using the
11615 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011616 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011617static const char *
11618find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011619 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011620 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011621{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011622 const char *pos, *last, *equal;
11623 const char **bufs = chunks;
11624 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011625
David Cournapeau16023ee2010-12-23 20:55:41 +090011626
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011627 pos = bufs[0];
11628 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010011629 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011630 /* Check the equal. */
11631 equal = pos + url_param_name_l;
11632 if (fix_pointer_if_wrap(chunks, &equal)) {
11633 if (equal >= chunks[3])
11634 return NULL;
11635 } else {
11636 if (equal >= chunks[1])
11637 return NULL;
11638 }
11639 if (*equal == '=') {
11640 if (pos + url_param_name_l > last) {
11641 /* process wrap case, we detect a wrap. In this case, the
11642 * comparison is performed in two parts.
11643 */
11644
11645 /* This is the end, we dont have any other chunk. */
11646 if (bufs != chunks || !bufs[2])
11647 return NULL;
11648
11649 /* Compute the length of each part of the comparison. */
11650 l1 = last - pos;
11651 l2 = url_param_name_l - l1;
11652
11653 /* The second buffer is too short to contain the compared string. */
11654 if (bufs[2] + l2 > bufs[3])
11655 return NULL;
11656
11657 if (memcmp(pos, url_param_name, l1) == 0 &&
11658 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11659 return pos;
11660
11661 /* Perform wrapping and jump the string who fail the comparison. */
11662 bufs += 2;
11663 pos = bufs[0] + l2;
11664 last = bufs[1];
11665
11666 } else {
11667 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011668 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11669 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011670 pos += url_param_name_l + 1;
11671 if (fix_pointer_if_wrap(chunks, &pos))
11672 last = bufs[2];
11673 }
11674 }
11675
11676 while (1) {
11677 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011678 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011679 pos++;
11680 if (pos < last)
11681 break;
11682 /* process buffer wrapping. */
11683 if (bufs != chunks || !bufs[2])
11684 return NULL;
11685 bufs += 2;
11686 pos = bufs[0];
11687 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011688 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011689 pos++;
11690 }
11691 return NULL;
11692}
11693
11694/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011695 * Given a url parameter name and a query string, find the next value.
11696 * An empty url_param_name matches the first available parameter.
11697 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
11698 * respectively provide a pointer to the value and its end.
11699 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090011700 */
11701static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011702find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011703 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011704 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011705{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011706 const char *arg_start, *qs_end;
11707 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011708
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011709 arg_start = chunks[0];
11710 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011711 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011712 /* Looks for an argument name. */
11713 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011714 url_param_name, url_param_name_l,
11715 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011716 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011717 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011718 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011719 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011720 if (!arg_start)
11721 return 0;
11722
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011723 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011724 while (1) {
11725 /* looks for the first argument. */
11726 value_start = memchr(arg_start, '=', qs_end - arg_start);
11727 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011728 /* Check for wrapping. */
11729 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011730 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011731 chunks[2]) {
11732 arg_start = chunks[2];
11733 qs_end = chunks[3];
11734 continue;
11735 }
11736 return 0;
11737 }
11738 break;
11739 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011740 value_start++;
11741 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011742 else {
11743 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011744 value_start = arg_start + url_param_name_l + 1;
11745
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011746 /* Check for pointer wrapping. */
11747 if (fix_pointer_if_wrap(chunks, &value_start)) {
11748 /* Update the end pointer. */
11749 qs_end = chunks[3];
11750
11751 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011752 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011753 return 0;
11754 }
11755 }
11756
David Cournapeau16023ee2010-12-23 20:55:41 +090011757 value_end = value_start;
11758
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011759 while (1) {
11760 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11761 value_end++;
11762 if (value_end < qs_end)
11763 break;
11764 /* process buffer wrapping. */
11765 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011766 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011767 chunks[2]) {
11768 value_end = chunks[2];
11769 qs_end = chunks[3];
11770 continue;
11771 }
11772 break;
11773 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011774
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011775 *vstart = value_start;
11776 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011777 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011778}
11779
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011780/* This scans a URL-encoded query string. It takes an optionally wrapping
11781 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11782 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11783 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011784 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011785static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011786smp_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 +090011787{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011788 const char *vstart, *vend;
11789 struct chunk *temp;
11790 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011791
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011792 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011793 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011794 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011795 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011796 return 0;
11797
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011798 /* Create sample. If the value is contiguous, return the pointer as CONST,
11799 * if the value is wrapped, copy-it in a buffer.
11800 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011801 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011802 if (chunks[2] &&
11803 vstart >= chunks[0] && vstart <= chunks[1] &&
11804 vend >= chunks[2] && vend <= chunks[3]) {
11805 /* Wrapped case. */
11806 temp = get_trash_chunk();
11807 memcpy(temp->str, vstart, chunks[1] - vstart);
11808 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011809 smp->data.u.str.str = temp->str;
11810 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011811 } else {
11812 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011813 smp->data.u.str.str = (char *)vstart;
11814 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011815 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11816 }
11817
11818 /* Update context, check wrapping. */
11819 chunks[0] = vend;
11820 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11821 chunks[1] = chunks[3];
11822 chunks[2] = NULL;
11823 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011824
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011825 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011826 smp->flags |= SMP_F_NOT_LAST;
11827
David Cournapeau16023ee2010-12-23 20:55:41 +090011828 return 1;
11829}
11830
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011831/* This function iterates over each parameter of the query string. It uses
11832 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011833 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11834 * An optional parameter name is passed in args[0], otherwise any parameter is
11835 * considered. It supports an optional delimiter argument for the beginning of
11836 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011837 */
11838static int
11839smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11840{
11841 struct http_msg *msg;
11842 char delim = '?';
11843 const char *name;
11844 int name_len;
11845
Dragan Dosen26f77e52015-05-25 10:02:11 +020011846 if (!args ||
11847 (args[0].type && args[0].type != ARGT_STR) ||
11848 (args[1].type && args[1].type != ARGT_STR))
11849 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011850
Dragan Dosen26f77e52015-05-25 10:02:11 +020011851 name = "";
11852 name_len = 0;
11853 if (args->type == ARGT_STR) {
11854 name = args->data.str.str;
11855 name_len = args->data.str.len;
11856 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011857
Dragan Dosen26f77e52015-05-25 10:02:11 +020011858 if (args[1].type)
11859 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011860
Dragan Dosen26f77e52015-05-25 10:02:11 +020011861 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011862 CHECK_HTTP_MESSAGE_FIRST();
11863
11864 msg = &smp->strm->txn->req;
11865
11866 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11867 msg->sl.rq.u_l, delim);
11868 if (!smp->ctx.a[0])
11869 return 0;
11870
11871 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011872
11873 /* Assume that the context is filled with NULL pointer
11874 * before the first call.
11875 * smp->ctx.a[2] = NULL;
11876 * smp->ctx.a[3] = NULL;
11877 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011878 }
11879
11880 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11881}
11882
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011883/* This function iterates over each parameter of the body. This requires
11884 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011885 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11886 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11887 * optional second part if the body wraps at the end of the buffer. An optional
11888 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011889 */
11890static int
11891smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11892{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011893 struct http_msg *msg;
11894 unsigned long len;
11895 unsigned long block1;
11896 char *body;
11897 const char *name;
11898 int name_len;
11899
11900 if (!args || (args[0].type && args[0].type != ARGT_STR))
11901 return 0;
11902
11903 name = "";
11904 name_len = 0;
11905 if (args[0].type == ARGT_STR) {
11906 name = args[0].data.str.str;
11907 name_len = args[0].data.str.len;
11908 }
11909
11910 if (!smp->ctx.a[0]) { // first call, find the query string
11911 CHECK_HTTP_MESSAGE_FIRST();
11912
11913 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010011914 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011915 else
Willy Tarreaube508f12016-03-10 11:47:01 +010011916 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011917
11918 len = http_body_bytes(msg);
11919 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11920
11921 block1 = len;
11922 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11923 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11924
11925 if (block1 == len) {
11926 /* buffer is not wrapped (or empty) */
11927 smp->ctx.a[0] = body;
11928 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011929
11930 /* Assume that the context is filled with NULL pointer
11931 * before the first call.
11932 * smp->ctx.a[2] = NULL;
11933 * smp->ctx.a[3] = NULL;
11934 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011935 }
11936 else {
11937 /* buffer is wrapped, we need to defragment it */
11938 smp->ctx.a[0] = body;
11939 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011940 smp->ctx.a[2] = msg->chn->buf->data;
11941 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011942 }
11943 }
11944 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11945}
11946
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011947/* Return the signed integer value for the specified url parameter (see url_param
11948 * above).
11949 */
11950static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011951smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011952{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011953 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011954
11955 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011956 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011957 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011958 }
11959
11960 return ret;
11961}
11962
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011963/* This produces a 32-bit hash of the concatenation of the first occurrence of
11964 * the Host header followed by the path component if it begins with a slash ('/').
11965 * This means that '*' will not be added, resulting in exactly the first Host
11966 * entry. If no Host header is found, then the path is used. The resulting value
11967 * is hashed using the url hash followed by a full avalanche hash and provides a
11968 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11969 * high-traffic sites without having to store whole paths.
11970 * this differs from the base32 functions in that it includes the url parameters
11971 * as well as the path
11972 */
11973static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011974smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011975{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011976 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011977 struct hdr_ctx ctx;
11978 unsigned int hash = 0;
11979 char *ptr, *beg, *end;
11980 int len;
11981
11982 CHECK_HTTP_MESSAGE_FIRST();
11983
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011984 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011985 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011986 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011987 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11988 ptr = ctx.line + ctx.val;
11989 len = ctx.vlen;
11990 while (len--)
11991 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11992 }
11993
11994 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011995 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 +000011996 beg = http_get_path(txn);
11997 if (!beg)
11998 beg = end;
11999
12000 for (ptr = beg; ptr < end ; ptr++);
12001
12002 if (beg < ptr && *beg == '/') {
12003 while (beg < ptr)
12004 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
12005 }
12006 hash = full_hash(hash);
12007
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012008 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012009 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012010 smp->flags = SMP_F_VOL_1ST;
12011 return 1;
12012}
12013
12014/* This concatenates the source address with the 32-bit hash of the Host and
12015 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
12016 * per-url counters. The result is a binary block from 8 to 20 bytes depending
12017 * on the source address length. The URL hash is stored before the address so
12018 * that in environments where IPv6 is insignificant, truncating the output to
12019 * 8 bytes would still work.
12020 */
12021static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020012022smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012023{
12024 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012025 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012026
Dragan Dosendb5af612016-06-16 11:23:01 +020012027 if (!cli_conn)
12028 return 0;
12029
Thierry FOURNIER0786d052015-05-11 15:42:45 +020012030 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012031 return 0;
12032
12033 temp = get_trash_chunk();
Dragan Dosene5f41332016-06-16 11:08:08 +020012034 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
12035 temp->len += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012036
Willy Tarreaub363a1f2013-10-01 10:45:07 +020012037 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012038 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020012039 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012040 temp->len += 4;
12041 break;
12042 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020012043 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012044 temp->len += 16;
12045 break;
12046 default:
12047 return 0;
12048 }
12049
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012050 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012051 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012052 return 1;
12053}
12054
Willy Tarreau185b5c42012-04-26 15:11:51 +020012055/* This function is used to validate the arguments passed to any "hdr" fetch
12056 * keyword. These keywords support an optional positive or negative occurrence
12057 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
12058 * is assumed that the types are already the correct ones. Returns 0 on error,
12059 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
12060 * error message in case of error, that the caller is responsible for freeing.
12061 * The initial location must either be freeable or NULL.
12062 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010012063int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020012064{
12065 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020012066 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020012067 return 0;
12068 }
12069 return 1;
12070}
12071
Willy Tarreau276fae92013-07-25 14:36:01 +020012072/* takes an UINT value on input supposed to represent the time since EPOCH,
12073 * adds an optional offset found in args[0] and emits a string representing
12074 * the date in RFC-1123/5322 format.
12075 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012076static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020012077{
Cyril Bontéf78d8962016-01-22 19:40:28 +010012078 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020012079 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
12080 struct chunk *temp;
12081 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012082 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012083 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020012084
12085 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012086 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020012087 curr_date += args[0].data.sint;
12088
12089 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020012090 if (!tm)
12091 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020012092
12093 temp = get_trash_chunk();
12094 temp->len = snprintf(temp->str, temp->size - temp->len,
12095 "%s, %02d %s %04d %02d:%02d:%02d GMT",
12096 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
12097 tm->tm_hour, tm->tm_min, tm->tm_sec);
12098
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012099 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012100 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020012101 return 1;
12102}
12103
Thierry FOURNIERad903512014-04-11 17:51:01 +020012104/* Match language range with language tag. RFC2616 14.4:
12105 *
12106 * A language-range matches a language-tag if it exactly equals
12107 * the tag, or if it exactly equals a prefix of the tag such
12108 * that the first tag character following the prefix is "-".
12109 *
12110 * Return 1 if the strings match, else return 0.
12111 */
12112static inline int language_range_match(const char *range, int range_len,
12113 const char *tag, int tag_len)
12114{
12115 const char *end = range + range_len;
12116 const char *tend = tag + tag_len;
12117 while (range < end) {
12118 if (*range == '-' && tag == tend)
12119 return 1;
12120 if (*range != *tag || tag == tend)
12121 return 0;
12122 range++;
12123 tag++;
12124 }
12125 /* Return true only if the last char of the tag is matched. */
12126 return tag == tend;
12127}
12128
12129/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012130static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020012131{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012132 const char *al = smp->data.u.str.str;
12133 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012134 const char *token;
12135 int toklen;
12136 int qvalue;
12137 const char *str;
12138 const char *w;
12139 int best_q = 0;
12140
12141 /* Set the constant to the sample, because the output of the
12142 * function will be peek in the constant configuration string.
12143 */
12144 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012145 smp->data.u.str.size = 0;
12146 smp->data.u.str.str = "";
12147 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012148
12149 /* Parse the accept language */
12150 while (1) {
12151
12152 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012153 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012154 al++;
12155 if (al >= end)
12156 break;
12157
12158 /* Start of the fisrt word. */
12159 token = al;
12160
12161 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012162 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012163 al++;
12164 if (al == token)
12165 goto expect_comma;
12166
12167 /* Length of the token. */
12168 toklen = al - token;
12169 qvalue = 1000;
12170
12171 /* Check if the token exists in the list. If the token not exists,
12172 * jump to the next token.
12173 */
12174 str = args[0].data.str.str;
12175 w = str;
12176 while (1) {
12177 if (*str == ';' || *str == '\0') {
12178 if (language_range_match(token, toklen, w, str-w))
12179 goto look_for_q;
12180 if (*str == '\0')
12181 goto expect_comma;
12182 w = str + 1;
12183 }
12184 str++;
12185 }
12186 goto expect_comma;
12187
12188look_for_q:
12189
12190 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012191 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012192 al++;
12193 if (al >= end)
12194 goto process_value;
12195
12196 /* If ',' is found, process the result */
12197 if (*al == ',')
12198 goto process_value;
12199
12200 /* If the character is different from ';', look
12201 * for the end of the header part in best effort.
12202 */
12203 if (*al != ';')
12204 goto expect_comma;
12205
12206 /* Assumes that the char is ';', now expect "q=". */
12207 al++;
12208
12209 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012210 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012211 al++;
12212 if (al >= end)
12213 goto process_value;
12214
12215 /* Expect 'q'. If no 'q', continue in best effort */
12216 if (*al != 'q')
12217 goto process_value;
12218 al++;
12219
12220 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012221 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012222 al++;
12223 if (al >= end)
12224 goto process_value;
12225
12226 /* Expect '='. If no '=', continue in best effort */
12227 if (*al != '=')
12228 goto process_value;
12229 al++;
12230
12231 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012232 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012233 al++;
12234 if (al >= end)
12235 goto process_value;
12236
12237 /* Parse the q value. */
12238 qvalue = parse_qvalue(al, &al);
12239
12240process_value:
12241
12242 /* If the new q value is the best q value, then store the associated
12243 * language in the response. If qvalue is the biggest value (1000),
12244 * break the process.
12245 */
12246 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012247 smp->data.u.str.str = (char *)w;
12248 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012249 if (qvalue >= 1000)
12250 break;
12251 best_q = qvalue;
12252 }
12253
12254expect_comma:
12255
12256 /* Expect comma or end. If the end is detected, quit the loop. */
12257 while (al < end && *al != ',')
12258 al++;
12259 if (al >= end)
12260 break;
12261
12262 /* Comma is found, jump it and restart the analyzer. */
12263 al++;
12264 }
12265
12266 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012267 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
12268 smp->data.u.str.str = args[1].data.str.str;
12269 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012270 }
12271
12272 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012273 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012274}
12275
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012276/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012277static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012278{
12279 /* If the constant flag is set or if not size is avalaible at
12280 * the end of the buffer, copy the string in other buffer
12281 * before decoding.
12282 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012283 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012284 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012285 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
12286 smp->data.u.str.str = str->str;
12287 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012288 smp->flags &= ~SMP_F_CONST;
12289 }
12290
12291 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012292 smp->data.u.str.str[smp->data.u.str.len] = '\0';
12293 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012294 return 1;
12295}
12296
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012297static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
12298{
12299 struct proxy *fe = strm_fe(smp->strm);
12300 int idx, i;
12301 struct cap_hdr *hdr;
12302 int len;
12303
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012304 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012305 return 0;
12306
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012307 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012308
12309 /* Check the availibity of the capture id. */
12310 if (idx > fe->nb_req_cap - 1)
12311 return 0;
12312
12313 /* Look for the original configuration. */
12314 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
12315 hdr != NULL && i != idx ;
12316 i--, hdr = hdr->next);
12317 if (!hdr)
12318 return 0;
12319
12320 /* check for the memory allocation */
12321 if (smp->strm->req_cap[hdr->index] == NULL)
12322 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
12323 if (smp->strm->req_cap[hdr->index] == NULL)
12324 return 0;
12325
12326 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012327 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012328 if (len > hdr->len)
12329 len = hdr->len;
12330
12331 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012332 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012333 smp->strm->req_cap[idx][len] = '\0';
12334
12335 return 1;
12336}
12337
12338static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
12339{
12340 struct proxy *fe = strm_fe(smp->strm);
12341 int idx, i;
12342 struct cap_hdr *hdr;
12343 int len;
12344
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012345 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012346 return 0;
12347
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012348 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012349
12350 /* Check the availibity of the capture id. */
12351 if (idx > fe->nb_rsp_cap - 1)
12352 return 0;
12353
12354 /* Look for the original configuration. */
12355 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
12356 hdr != NULL && i != idx ;
12357 i--, hdr = hdr->next);
12358 if (!hdr)
12359 return 0;
12360
12361 /* check for the memory allocation */
12362 if (smp->strm->res_cap[hdr->index] == NULL)
12363 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
12364 if (smp->strm->res_cap[hdr->index] == NULL)
12365 return 0;
12366
12367 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012368 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012369 if (len > hdr->len)
12370 len = hdr->len;
12371
12372 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012373 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012374 smp->strm->res_cap[idx][len] = '\0';
12375
12376 return 1;
12377}
12378
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012379/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012380 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012381 * the relevant part of the request line accordingly. Then it updates various
12382 * pointers to the next elements which were moved, and the total buffer length.
12383 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012384 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
12385 * error, though this can be revisited when this code is finally exploited.
12386 *
12387 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
12388 * query string and 3 to replace uri.
12389 *
12390 * In query string case, the mark question '?' must be set at the start of the
12391 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012392 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012393int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012394 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012395{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012396 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012397 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012398 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012399 int delta;
12400
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012401 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012402 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012403 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012404 cur_end = cur_ptr + txn->req.sl.rq.m_l;
12405
12406 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012407 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012408 txn->req.sl.rq.m_l += delta;
12409 txn->req.sl.rq.u += delta;
12410 txn->req.sl.rq.v += delta;
12411 break;
12412
12413 case 1: // path
12414 cur_ptr = http_get_path(txn);
12415 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012416 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012417
12418 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012419 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 +010012420 cur_end++;
12421
12422 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012423 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012424 txn->req.sl.rq.u_l += delta;
12425 txn->req.sl.rq.v += delta;
12426 break;
12427
12428 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012429 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012430 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012431 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12432 while (cur_ptr < cur_end && *cur_ptr != '?')
12433 cur_ptr++;
12434
12435 /* skip the question mark or indicate that we must insert it
12436 * (but only if the format string is not empty then).
12437 */
12438 if (cur_ptr < cur_end)
12439 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012440 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012441 offset = 0;
12442
12443 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012444 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012445 txn->req.sl.rq.u_l += delta;
12446 txn->req.sl.rq.v += delta;
12447 break;
12448
12449 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012450 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012451 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12452
12453 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012454 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012455 txn->req.sl.rq.u_l += delta;
12456 txn->req.sl.rq.v += delta;
12457 break;
12458
12459 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012460 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012461 }
12462
12463 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012464 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020012465 txn->req.sl.rq.l += delta;
12466 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012467 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012468 return 0;
12469}
12470
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012471/* This function replace the HTTP status code and the associated message. The
12472 * variable <status> contains the new status code. This function never fails.
12473 */
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012474void http_set_status(unsigned int status, const char *reason, struct stream *s)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012475{
12476 struct http_txn *txn = s->txn;
12477 char *cur_ptr, *cur_end;
12478 int delta;
12479 char *res;
12480 int c_l;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012481 const char *msg = reason;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012482 int msg_len;
12483
12484 chunk_reset(&trash);
12485
12486 res = ultoa_o(status, trash.str, trash.size);
12487 c_l = res - trash.str;
12488
12489 trash.str[c_l] = ' ';
12490 trash.len = c_l + 1;
12491
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012492 /* Do we have a custom reason format string? */
12493 if (msg == NULL)
12494 msg = get_reason(status);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012495 msg_len = strlen(msg);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012496 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
12497 trash.len += msg_len;
12498
12499 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
12500 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
12501
12502 /* commit changes and adjust message */
12503 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
12504
12505 /* adjust res line offsets and lengths */
12506 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
12507 txn->rsp.sl.st.c_l = c_l;
12508 txn->rsp.sl.st.r_l = msg_len;
12509
12510 delta = trash.len - (cur_end - cur_ptr);
12511 txn->rsp.sl.st.l += delta;
12512 txn->hdr_idx.v[0].len += delta;
12513 http_msg_move_end(&txn->rsp, delta);
12514}
12515
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012516/* This function executes one of the set-{method,path,query,uri} actions. It
12517 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012518 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012519 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012520 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
12521 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012522 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012523enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012524 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012525{
12526 chunk_reset(&trash);
12527
12528 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012529 if (rule->arg.http.action == 2)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012530 trash.str[trash.len++] = '?';
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012531 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012532
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012533 http_replace_req_line(rule->arg.http.action, trash.str, trash.len, px, s);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012534 return ACT_RET_CONT;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012535}
12536
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012537/* This function is just a compliant action wrapper for "set-status". */
12538enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012539 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012540{
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012541 http_set_status(rule->arg.status.code, rule->arg.status.reason, s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012542 return ACT_RET_CONT;
12543}
12544
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012545/* parse an http-request action among :
12546 * set-method
12547 * set-path
12548 * set-query
12549 * set-uri
12550 *
12551 * All of them accept a single argument of type string representing a log-format.
12552 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
12553 * 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 +020012554 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012555 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012556enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
12557 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012558{
12559 int cur_arg = *orig_arg;
12560
Thierry FOURNIER42148732015-09-02 17:17:33 +020012561 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012562
12563 switch (args[0][4]) {
12564 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012565 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012566 rule->action_ptr = http_action_set_req_line;
12567 break;
12568 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012569 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012570 rule->action_ptr = http_action_set_req_line;
12571 break;
12572 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012573 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012574 rule->action_ptr = http_action_set_req_line;
12575 break;
12576 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012577 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012578 rule->action_ptr = http_action_set_req_line;
12579 break;
12580 default:
12581 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012582 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012583 }
12584
12585 if (!*args[cur_arg] ||
12586 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12587 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012588 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012589 }
12590
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012591 LIST_INIT(&rule->arg.http.logfmt);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012592 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010012593 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +010012594 (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 +010012595 return ACT_RET_PRS_ERR;
12596 }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012597
12598 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012599 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012600}
12601
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012602/* parse set-status action:
12603 * This action accepts a single argument of type int representing
12604 * an http status code. It returns ACT_RET_PRS_OK on success,
12605 * ACT_RET_PRS_ERR on error.
12606 */
12607enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12608 struct act_rule *rule, char **err)
12609{
12610 char *error;
12611
Thierry FOURNIER42148732015-09-02 17:17:33 +020012612 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012613 rule->action_ptr = action_http_set_status;
12614
12615 /* Check if an argument is available */
12616 if (!*args[*orig_arg]) {
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012617 memprintf(err, "expects 1 argument: <status>; or 3 arguments: <status> reason <fmt>");
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012618 return ACT_RET_PRS_ERR;
12619 }
12620
12621 /* convert status code as integer */
12622 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12623 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12624 memprintf(err, "expects an integer status code between 100 and 999");
12625 return ACT_RET_PRS_ERR;
12626 }
12627
12628 (*orig_arg)++;
Robin H. Johnson52f5db22017-01-01 13:10:52 -080012629
12630 /* set custom reason string */
12631 rule->arg.status.reason = NULL; // If null, we use the default reason for the status code.
12632 if (*args[*orig_arg] && strcmp(args[*orig_arg], "reason") == 0 &&
12633 (*args[*orig_arg + 1] && strcmp(args[*orig_arg + 1], "if") != 0 && strcmp(args[*orig_arg + 1], "unless") != 0)) {
12634 (*orig_arg)++;
12635 rule->arg.status.reason = strdup(args[*orig_arg]);
12636 (*orig_arg)++;
12637 }
12638
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012639 return ACT_RET_PRS_OK;
12640}
12641
Willy Tarreaua9083d02015-05-08 15:27:59 +020012642/* This function executes the "capture" action. It executes a fetch expression,
12643 * turns the result into a string and puts it in a capture slot. It always
12644 * returns 1. If an error occurs the action is cancelled, but the rule
12645 * processing continues.
12646 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012647enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012648 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012649{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012650 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012651 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012652 char **cap = s->req_cap;
12653 int len;
12654
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012655 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 +020012656 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012657 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012658
12659 if (cap[h->index] == NULL)
12660 cap[h->index] = pool_alloc2(h->pool);
12661
12662 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012663 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012664
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012665 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012666 if (len > h->len)
12667 len = h->len;
12668
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012669 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012670 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012671 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012672}
12673
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012674/* This function executes the "capture" action and store the result in a
12675 * capture slot if exists. It executes a fetch expression, turns the result
12676 * into a string and puts it in a capture slot. It always returns 1. If an
12677 * error occurs the action is cancelled, but the rule processing continues.
12678 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012679enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012680 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012681{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012682 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012683 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012684 char **cap = s->req_cap;
12685 struct proxy *fe = strm_fe(s);
12686 int len;
12687 int i;
12688
12689 /* Look for the original configuration. */
12690 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012691 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012692 i--, h = h->next);
12693 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012694 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012695
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012696 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 +020012697 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012698 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012699
12700 if (cap[h->index] == NULL)
12701 cap[h->index] = pool_alloc2(h->pool);
12702
12703 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012704 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012705
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012706 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012707 if (len > h->len)
12708 len = h->len;
12709
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012710 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012711 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012712 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012713}
12714
Willy Tarreaua9083d02015-05-08 15:27:59 +020012715/* parse an "http-request capture" action. It takes a single argument which is
12716 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012717 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012718 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012719 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012720enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12721 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012722{
12723 struct sample_expr *expr;
12724 struct cap_hdr *hdr;
12725 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012726 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012727
12728 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12729 if (strcmp(args[cur_arg], "if") == 0 ||
12730 strcmp(args[cur_arg], "unless") == 0)
12731 break;
12732
12733 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012734 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012735 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012736 }
12737
Willy Tarreaua9083d02015-05-08 15:27:59 +020012738 cur_arg = *orig_arg;
12739 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12740 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012741 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012742
12743 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12744 memprintf(err,
12745 "fetch method '%s' extracts information from '%s', none of which is available here",
12746 args[cur_arg-1], sample_src_names(expr->fetch->use));
12747 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012748 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012749 }
12750
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012751 if (!args[cur_arg] || !*args[cur_arg]) {
12752 memprintf(err, "expects 'len or 'id'");
12753 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012754 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012755 }
12756
Willy Tarreaua9083d02015-05-08 15:27:59 +020012757 if (strcmp(args[cur_arg], "len") == 0) {
12758 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012759
12760 if (!(px->cap & PR_CAP_FE)) {
12761 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012762 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012763 }
12764
12765 proxy->conf.args.ctx = ARGC_CAP;
12766
Willy Tarreaua9083d02015-05-08 15:27:59 +020012767 if (!args[cur_arg]) {
12768 memprintf(err, "missing length value");
12769 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012770 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012771 }
12772 /* we copy the table name for now, it will be resolved later */
12773 len = atoi(args[cur_arg]);
12774 if (len <= 0) {
12775 memprintf(err, "length must be > 0");
12776 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012777 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012778 }
12779 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012780
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012781 if (!len) {
12782 memprintf(err, "a positive 'len' argument is mandatory");
12783 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012784 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012785 }
12786
Vincent Bernat02779b62016-04-03 13:48:43 +020012787 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012788 hdr->next = px->req_cap;
12789 hdr->name = NULL; /* not a header capture */
12790 hdr->namelen = 0;
12791 hdr->len = len;
12792 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12793 hdr->index = px->nb_req_cap++;
12794
12795 px->req_cap = hdr;
12796 px->to_log |= LW_REQHDR;
12797
Thierry FOURNIER42148732015-09-02 17:17:33 +020012798 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012799 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012800 rule->arg.cap.expr = expr;
12801 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012802 }
12803
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012804 else if (strcmp(args[cur_arg], "id") == 0) {
12805 int id;
12806 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012807
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012808 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012809
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012810 if (!args[cur_arg]) {
12811 memprintf(err, "missing id value");
12812 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012813 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012814 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012815
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012816 id = strtol(args[cur_arg], &error, 10);
12817 if (*error != '\0') {
12818 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12819 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012820 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012821 }
12822 cur_arg++;
12823
12824 proxy->conf.args.ctx = ARGC_CAP;
12825
Thierry FOURNIER42148732015-09-02 17:17:33 +020012826 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012827 rule->action_ptr = http_action_req_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012828 rule->arg.capid.expr = expr;
12829 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012830 }
12831
12832 else {
12833 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12834 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012835 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012836 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012837
12838 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012839 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012840}
12841
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012842/* This function executes the "capture" action and store the result in a
12843 * capture slot if exists. It executes a fetch expression, turns the result
12844 * into a string and puts it in a capture slot. It always returns 1. If an
12845 * error occurs the action is cancelled, but the rule processing continues.
12846 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012847enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012848 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012849{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012850 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012851 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012852 char **cap = s->res_cap;
12853 struct proxy *fe = strm_fe(s);
12854 int len;
12855 int i;
12856
12857 /* Look for the original configuration. */
12858 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012859 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012860 i--, h = h->next);
12861 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012862 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012863
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012864 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 +020012865 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012866 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012867
12868 if (cap[h->index] == NULL)
12869 cap[h->index] = pool_alloc2(h->pool);
12870
12871 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012872 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012873
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012874 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012875 if (len > h->len)
12876 len = h->len;
12877
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012878 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012879 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012880 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012881}
12882
12883/* parse an "http-response capture" action. It takes a single argument which is
12884 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12885 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012886 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012887 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012888enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12889 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012890{
12891 struct sample_expr *expr;
12892 int cur_arg;
12893 int id;
12894 char *error;
12895
12896 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12897 if (strcmp(args[cur_arg], "if") == 0 ||
12898 strcmp(args[cur_arg], "unless") == 0)
12899 break;
12900
12901 if (cur_arg < *orig_arg + 3) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012902 memprintf(err, "expects <expression> id <idx>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012903 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012904 }
12905
12906 cur_arg = *orig_arg;
12907 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12908 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012909 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012910
12911 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12912 memprintf(err,
12913 "fetch method '%s' extracts information from '%s', none of which is available here",
12914 args[cur_arg-1], sample_src_names(expr->fetch->use));
12915 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012916 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012917 }
12918
12919 if (!args[cur_arg] || !*args[cur_arg]) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012920 memprintf(err, "expects 'id'");
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012921 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012922 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012923 }
12924
12925 if (strcmp(args[cur_arg], "id") != 0) {
12926 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
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 cur_arg++;
12932
12933 if (!args[cur_arg]) {
12934 memprintf(err, "missing id value");
12935 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012936 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012937 }
12938
12939 id = strtol(args[cur_arg], &error, 10);
12940 if (*error != '\0') {
12941 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12942 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012943 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012944 }
12945 cur_arg++;
12946
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012947 proxy->conf.args.ctx = ARGC_CAP;
12948
Thierry FOURNIER42148732015-09-02 17:17:33 +020012949 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012950 rule->action_ptr = http_action_res_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012951 rule->arg.capid.expr = expr;
12952 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012953
12954 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012955 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012956}
12957
William Lallemand73025dd2014-04-24 14:38:37 +020012958/*
12959 * Return the struct http_req_action_kw associated to a keyword.
12960 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012961struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012962{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012963 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012964}
12965
12966/*
12967 * Return the struct http_res_action_kw associated to a keyword.
12968 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012969struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012970{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012971 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012972}
12973
Willy Tarreau12207b32016-11-22 19:48:51 +010012974
12975/* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop
12976 * now.
12977 */
12978static int cli_parse_show_errors(char **args, struct appctx *appctx, void *private)
12979{
12980 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
12981 return 1;
12982
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012983 if (*args[2]) {
12984 struct proxy *px;
12985
12986 px = proxy_find_by_name(args[2], 0, 0);
12987 if (px)
12988 appctx->ctx.errors.iid = px->uuid;
12989 else
12990 appctx->ctx.errors.iid = atoi(args[2]);
12991
12992 if (!appctx->ctx.errors.iid) {
12993 appctx->ctx.cli.msg = "No such proxy.\n";
12994 appctx->st0 = CLI_ST_PRINT;
12995 return 1;
12996 }
12997 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012998 else
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012999 appctx->ctx.errors.iid = -1; // dump all proxies
13000
Willy Tarreau35069f82016-11-25 09:16:37 +010013001 appctx->ctx.errors.flag = 0;
13002 if (strcmp(args[3], "request") == 0)
13003 appctx->ctx.errors.flag |= 4; // ignore response
13004 else if (strcmp(args[3], "response") == 0)
13005 appctx->ctx.errors.flag |= 2; // ignore request
Willy Tarreau12207b32016-11-22 19:48:51 +010013006 appctx->ctx.errors.px = NULL;
Willy Tarreau12207b32016-11-22 19:48:51 +010013007 return 0;
13008}
13009
13010/* This function dumps all captured errors onto the stream interface's
13011 * read buffer. It returns 0 if the output buffer is full and it needs
13012 * to be called again, otherwise non-zero.
13013 */
13014static int cli_io_handler_show_errors(struct appctx *appctx)
13015{
13016 struct stream_interface *si = appctx->owner;
13017 extern const char *monthname[12];
13018
13019 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
13020 return 1;
13021
13022 chunk_reset(&trash);
13023
13024 if (!appctx->ctx.errors.px) {
13025 /* the function had not been called yet, let's prepare the
13026 * buffer for a response.
13027 */
13028 struct tm tm;
13029
13030 get_localtime(date.tv_sec, &tm);
13031 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
13032 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
13033 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
13034 error_snapshot_id);
13035
13036 if (bi_putchk(si_ic(si), &trash) == -1) {
13037 /* Socket buffer full. Let's try again later from the same point */
13038 si_applet_cant_put(si);
13039 return 0;
13040 }
13041
13042 appctx->ctx.errors.px = proxy;
Willy Tarreau12207b32016-11-22 19:48:51 +010013043 appctx->ctx.errors.bol = 0;
13044 appctx->ctx.errors.ptr = -1;
13045 }
13046
13047 /* we have two inner loops here, one for the proxy, the other one for
13048 * the buffer.
13049 */
13050 while (appctx->ctx.errors.px) {
13051 struct error_snapshot *es;
13052
Willy Tarreau35069f82016-11-25 09:16:37 +010013053 if ((appctx->ctx.errors.flag & 1) == 0) {
Willy Tarreau12207b32016-11-22 19:48:51 +010013054 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau35069f82016-11-25 09:16:37 +010013055 if (appctx->ctx.errors.flag & 2) // skip req
13056 goto next;
13057 }
13058 else {
Willy Tarreau12207b32016-11-22 19:48:51 +010013059 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau35069f82016-11-25 09:16:37 +010013060 if (appctx->ctx.errors.flag & 4) // skip resp
13061 goto next;
13062 }
Willy Tarreau12207b32016-11-22 19:48:51 +010013063
13064 if (!es->when.tv_sec)
13065 goto next;
13066
13067 if (appctx->ctx.errors.iid >= 0 &&
13068 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
13069 es->oe->uuid != appctx->ctx.errors.iid)
13070 goto next;
13071
13072 if (appctx->ctx.errors.ptr < 0) {
13073 /* just print headers now */
13074
13075 char pn[INET6_ADDRSTRLEN];
13076 struct tm tm;
13077 int port;
13078
13079 get_localtime(es->when.tv_sec, &tm);
13080 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
13081 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
13082 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
13083
13084 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
13085 case AF_INET:
13086 case AF_INET6:
13087 port = get_host_port(&es->src);
13088 break;
13089 default:
13090 port = 0;
13091 }
13092
Willy Tarreau35069f82016-11-25 09:16:37 +010013093 switch (appctx->ctx.errors.flag & 1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010013094 case 0:
13095 chunk_appendf(&trash,
13096 " frontend %s (#%d): invalid request\n"
13097 " backend %s (#%d)",
13098 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
13099 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
13100 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
13101 break;
13102 case 1:
13103 chunk_appendf(&trash,
13104 " backend %s (#%d): invalid response\n"
13105 " frontend %s (#%d)",
13106 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
13107 es->oe->id, es->oe->uuid);
13108 break;
13109 }
13110
13111 chunk_appendf(&trash,
13112 ", server %s (#%d), event #%u\n"
13113 " src %s:%d, session #%d, session flags 0x%08x\n"
Willy Tarreau10e61cb2017-01-04 14:51:22 +010013114 " HTTP msg state %s(%d), msg flags 0x%08x, tx flags 0x%08x\n"
Willy Tarreau12207b32016-11-22 19:48:51 +010013115 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
13116 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
13117 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
13118 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
13119 es->ev_id,
13120 pn, port, es->sid, es->s_flags,
Willy Tarreau10e61cb2017-01-04 14:51:22 +010013121 http_msg_state_str(es->state), es->state, es->m_flags, es->t_flags,
Willy Tarreau12207b32016-11-22 19:48:51 +010013122 es->m_clen, es->m_blen,
13123 es->b_flags, es->b_out, es->b_tot,
13124 es->len, es->b_wrap, es->pos);
13125
13126 if (bi_putchk(si_ic(si), &trash) == -1) {
13127 /* Socket buffer full. Let's try again later from the same point */
13128 si_applet_cant_put(si);
13129 return 0;
13130 }
13131 appctx->ctx.errors.ptr = 0;
13132 appctx->ctx.errors.sid = es->sid;
13133 }
13134
13135 if (appctx->ctx.errors.sid != es->sid) {
13136 /* the snapshot changed while we were dumping it */
13137 chunk_appendf(&trash,
13138 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
13139 if (bi_putchk(si_ic(si), &trash) == -1) {
13140 si_applet_cant_put(si);
13141 return 0;
13142 }
13143 goto next;
13144 }
13145
13146 /* OK, ptr >= 0, so we have to dump the current line */
13147 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
13148 int newptr;
13149 int newline;
13150
13151 newline = appctx->ctx.errors.bol;
13152 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
13153 if (newptr == appctx->ctx.errors.ptr)
13154 return 0;
13155
13156 if (bi_putchk(si_ic(si), &trash) == -1) {
13157 /* Socket buffer full. Let's try again later from the same point */
13158 si_applet_cant_put(si);
13159 return 0;
13160 }
13161 appctx->ctx.errors.ptr = newptr;
13162 appctx->ctx.errors.bol = newline;
13163 };
13164 next:
13165 appctx->ctx.errors.bol = 0;
13166 appctx->ctx.errors.ptr = -1;
Willy Tarreau35069f82016-11-25 09:16:37 +010013167 appctx->ctx.errors.flag ^= 1;
13168 if (!(appctx->ctx.errors.flag & 1))
Willy Tarreau12207b32016-11-22 19:48:51 +010013169 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau12207b32016-11-22 19:48:51 +010013170 }
13171
13172 /* dump complete */
13173 return 1;
13174}
13175
13176/* register cli keywords */
13177static struct cli_kw_list cli_kws = {{ },{
13178 { { "show", "errors", NULL },
13179 "show errors : report last request and response errors for each proxy",
13180 cli_parse_show_errors, cli_io_handler_show_errors, NULL,
13181 },
13182 {{},}
13183}};
13184
Willy Tarreau4a568972010-05-12 08:08:50 +020013185/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013186/* All supported ACL keywords must be declared here. */
13187/************************************************************************/
13188
13189/* Note: must not be declared <const> as its list will be overwritten.
13190 * Please take care of keeping this list alphabetically sorted.
13191 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020013192static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013193 { "base", "base", PAT_MATCH_STR },
13194 { "base_beg", "base", PAT_MATCH_BEG },
13195 { "base_dir", "base", PAT_MATCH_DIR },
13196 { "base_dom", "base", PAT_MATCH_DOM },
13197 { "base_end", "base", PAT_MATCH_END },
13198 { "base_len", "base", PAT_MATCH_LEN },
13199 { "base_reg", "base", PAT_MATCH_REG },
13200 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020013201
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013202 { "cook", "req.cook", PAT_MATCH_STR },
13203 { "cook_beg", "req.cook", PAT_MATCH_BEG },
13204 { "cook_dir", "req.cook", PAT_MATCH_DIR },
13205 { "cook_dom", "req.cook", PAT_MATCH_DOM },
13206 { "cook_end", "req.cook", PAT_MATCH_END },
13207 { "cook_len", "req.cook", PAT_MATCH_LEN },
13208 { "cook_reg", "req.cook", PAT_MATCH_REG },
13209 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013210
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013211 { "hdr", "req.hdr", PAT_MATCH_STR },
13212 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
13213 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
13214 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
13215 { "hdr_end", "req.hdr", PAT_MATCH_END },
13216 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
13217 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
13218 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013219
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013220 /* these two declarations uses strings with list storage (in place
13221 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
13222 * and delete functions are relative to the list management. The parse
13223 * and match method are related to the corresponding fetch methods. This
13224 * is very particular ACL declaration mode.
13225 */
13226 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
13227 { "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 +020013228
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013229 { "path", "path", PAT_MATCH_STR },
13230 { "path_beg", "path", PAT_MATCH_BEG },
13231 { "path_dir", "path", PAT_MATCH_DIR },
13232 { "path_dom", "path", PAT_MATCH_DOM },
13233 { "path_end", "path", PAT_MATCH_END },
13234 { "path_len", "path", PAT_MATCH_LEN },
13235 { "path_reg", "path", PAT_MATCH_REG },
13236 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013237
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013238 { "req_ver", "req.ver", PAT_MATCH_STR },
13239 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013240
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013241 { "scook", "res.cook", PAT_MATCH_STR },
13242 { "scook_beg", "res.cook", PAT_MATCH_BEG },
13243 { "scook_dir", "res.cook", PAT_MATCH_DIR },
13244 { "scook_dom", "res.cook", PAT_MATCH_DOM },
13245 { "scook_end", "res.cook", PAT_MATCH_END },
13246 { "scook_len", "res.cook", PAT_MATCH_LEN },
13247 { "scook_reg", "res.cook", PAT_MATCH_REG },
13248 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013249
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013250 { "shdr", "res.hdr", PAT_MATCH_STR },
13251 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
13252 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
13253 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
13254 { "shdr_end", "res.hdr", PAT_MATCH_END },
13255 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
13256 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
13257 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013258
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013259 { "url", "url", PAT_MATCH_STR },
13260 { "url_beg", "url", PAT_MATCH_BEG },
13261 { "url_dir", "url", PAT_MATCH_DIR },
13262 { "url_dom", "url", PAT_MATCH_DOM },
13263 { "url_end", "url", PAT_MATCH_END },
13264 { "url_len", "url", PAT_MATCH_LEN },
13265 { "url_reg", "url", PAT_MATCH_REG },
13266 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013267
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013268 { "urlp", "urlp", PAT_MATCH_STR },
13269 { "urlp_beg", "urlp", PAT_MATCH_BEG },
13270 { "urlp_dir", "urlp", PAT_MATCH_DIR },
13271 { "urlp_dom", "urlp", PAT_MATCH_DOM },
13272 { "urlp_end", "urlp", PAT_MATCH_END },
13273 { "urlp_len", "urlp", PAT_MATCH_LEN },
13274 { "urlp_reg", "urlp", PAT_MATCH_REG },
13275 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013276
Willy Tarreau8ed669b2013-01-11 15:49:37 +010013277 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013278}};
13279
13280/************************************************************************/
13281/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020013282/************************************************************************/
13283/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020013284static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013285 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013286 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013287 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
13288
Willy Tarreau87b09662015-04-03 00:22:06 +020013289 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013290 { "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 +020013291
13292 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013293 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
13294 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
13295 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020013296
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013297 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
13298 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010013299
Willy Tarreau409bcde2013-01-08 00:31:00 +010013300 /* cookie is valid in both directions (eg: for "stick ...") but cook*
13301 * are only here to match the ACL's name, are request-only and are used
13302 * for ACL compatibility only.
13303 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013304 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13305 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013306 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
13307 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013308
13309 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
13310 * only here to match the ACL's name, are request-only and are used for
13311 * ACL compatibility only.
13312 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013313 { "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 +020013314 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013315 { "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 +020013316 { "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 +010013317
Willy Tarreau0a0daec2013-04-02 22:44:58 +020013318 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013319 { "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 +010013320 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010013321 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013322 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010013323 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013324
13325 /* HTTP protocol on the request path */
13326 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013327 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013328
13329 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013330 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
13331 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013332
Willy Tarreaua5910cc2015-05-02 00:46:08 +020013333 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013334 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
13335 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020013336 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020013337
Willy Tarreau18ed2562013-01-14 15:56:36 +010013338 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013339 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
13340 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013341
Willy Tarreau18ed2562013-01-14 15:56:36 +010013342 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013343 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013344 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
13345 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013346
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013347 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013348 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013349 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013350 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013351 { "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 +010013352 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013353 { "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 +010013354
13355 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013356 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013357 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13358 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013359
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013360 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013361 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013362 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013363 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013364 { "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 +010013365 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013366 { "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 +010013367
Willy Tarreau409bcde2013-01-08 00:31:00 +010013368 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013369 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013370 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13371 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013372 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010013373
13374 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013375 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013376 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013377 { "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 +020013378 { "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 +010013379
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013380 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020013381 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013382 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013383 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000013384 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013385 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013386 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020013387 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13388 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013389 { "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 +010013390 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020013391}};
13392
Willy Tarreau8797c062007-05-07 00:55:35 +020013393
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013394/************************************************************************/
13395/* All supported converter keywords must be declared here. */
13396/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020013397/* Note: must not be declared <const> as its list will be overwritten */
13398static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013399 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020013400 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013401 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
13402 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020013403 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020013404 { NULL, NULL, 0, 0, 0 },
13405}};
13406
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020013407
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013408/************************************************************************/
13409/* All supported http-request action keywords must be declared here. */
13410/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013411struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013412 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020013413 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013414 { "set-method", parse_set_req_line },
13415 { "set-path", parse_set_req_line },
13416 { "set-query", parse_set_req_line },
13417 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020013418 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013419 }
13420};
13421
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013422struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013423 .kw = {
13424 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020013425 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013426 { NULL, NULL }
13427 }
13428};
13429
Willy Tarreau8797c062007-05-07 00:55:35 +020013430__attribute__((constructor))
13431static void __http_protocol_init(void)
13432{
13433 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020013434 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020013435 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013436 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013437 http_res_keywords_register(&http_res_actions);
Willy Tarreau12207b32016-11-22 19:48:51 +010013438 cli_register_kw(&cli_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020013439}
13440
13441
Willy Tarreau58f10d72006-12-04 02:26:12 +010013442/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020013443 * Local variables:
13444 * c-indent-level: 8
13445 * c-basic-offset: 8
13446 * End:
13447 */