blob: a92e716c7a1b7365a866cbae05a260813bdcaa28 [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] =
199 "HTTP/1.0 500 Server Error\r\n"
200 "Cache-Control: no-cache\r\n"
201 "Connection: close\r\n"
202 "Content-Type: text/html\r\n"
203 "\r\n"
204 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
205
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";
346 case 502: return "Bad Gateway ou Proxy Error";
347 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
1563 http_msg_rquri2:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001564 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau5f10ea32016-11-05 17:52:06 +01001565 EAT_AND_JUMP_OR_RETURN(http_msg_rquri2, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001566
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001567 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001568 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001569 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1570 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001571
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001572 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001573 /* non-ASCII chars are forbidden unless option
1574 * accept-invalid-http-request is enabled in the frontend.
1575 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001576 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001577 if (msg->err_pos < -1)
1578 goto invalid_char;
1579 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001580 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001581 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1582 }
1583
1584 if (likely(HTTP_IS_CRLF(*ptr))) {
1585 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1586 goto http_msg_req09_uri_e;
1587 }
1588
1589 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001590 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001591 msg->err_pos = ptr - msg_start;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001592 msg->err_state = HTTP_MSG_RQURI;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001593 state = HTTP_MSG_ERROR;
1594 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001595
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001596 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001597 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001598 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001599 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001600 goto http_msg_rqver;
1601 }
1602 if (likely(HTTP_IS_SPHT(*ptr)))
1603 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1604 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1605 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001606
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001607 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001608 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001609 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001610 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001611
1612 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001613 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001614 http_msg_rqline_eol:
1615 /* We have seen the end of line. Note that we do not
1616 * necessarily have the \n yet, but at least we know that we
1617 * have EITHER \r OR \n, otherwise the request would not be
1618 * complete. We can then record the request length and return
1619 * to the caller which will be able to register it.
1620 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001621 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001622 return ptr;
1623 }
1624
1625 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001626 msg->err_state = HTTP_MSG_RQVER;
Willy Tarreau7552c032009-03-01 11:10:40 +01001627 state = HTTP_MSG_ERROR;
1628 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001629
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001630 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001631#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001632 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1633 exit(1);
1634#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001635 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001636 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001637
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001638 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001639 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001640 if (ret_state)
1641 *ret_state = state;
1642 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001643 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001644 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001645}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001646
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001647/*
1648 * Returns the data from Authorization header. Function may be called more
1649 * than once so data is stored in txn->auth_data. When no header is found
1650 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001651 * searching again for something we are unable to find anyway. However, if
1652 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +02001653 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001654 */
1655
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001656/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1657 * set according to global.tune.bufsize.
1658 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001659char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001660
1661int
Willy Tarreau87b09662015-04-03 00:22:06 +02001662get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001663{
1664
Willy Tarreaueee5b512015-04-03 23:46:31 +02001665 struct http_txn *txn = s->txn;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001666 struct chunk auth_method;
1667 struct hdr_ctx ctx;
1668 char *h, *p;
1669 int len;
1670
1671#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +02001672 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001673#endif
1674
1675 if (txn->auth.method == HTTP_AUTH_WRONG)
1676 return 0;
1677
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001678 txn->auth.method = HTTP_AUTH_WRONG;
1679
1680 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001681
1682 if (txn->flags & TX_USE_PX_CONN) {
1683 h = "Proxy-Authorization";
1684 len = strlen(h);
1685 } else {
1686 h = "Authorization";
1687 len = strlen(h);
1688 }
1689
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001690 if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001691 return 0;
1692
1693 h = ctx.line + ctx.val;
1694
1695 p = memchr(h, ' ', ctx.vlen);
Willy Tarreau5c557d12016-03-13 08:17:02 +01001696 len = p - h;
1697 if (!p || len <= 0)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001698 return 0;
1699
David Carlier7365f7d2016-04-04 11:54:42 +01001700 if (chunk_initlen(&auth_method, h, 0, len) != 1)
1701 return 0;
1702
Willy Tarreau5c557d12016-03-13 08:17:02 +01001703 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001704
1705 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1706
1707 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001708 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001709
1710 if (len < 0)
1711 return 0;
1712
1713
1714 get_http_auth_buff[len] = '\0';
1715
1716 p = strchr(get_http_auth_buff, ':');
1717
1718 if (!p)
1719 return 0;
1720
1721 txn->auth.user = get_http_auth_buff;
1722 *p = '\0';
1723 txn->auth.pass = p+1;
1724
1725 txn->auth.method = HTTP_AUTH_BASIC;
1726 return 1;
1727 }
1728
1729 return 0;
1730}
1731
Willy Tarreau58f10d72006-12-04 02:26:12 +01001732
Willy Tarreau8973c702007-01-21 23:58:29 +01001733/*
1734 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001735 * depending on the initial msg->msg_state. The caller is responsible for
1736 * ensuring that the message does not wrap. The function can be preempted
1737 * everywhere when data are missing and recalled at the exact same location
1738 * with no information loss. The message may even be realigned between two
1739 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001740 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001741 * fields. Note that msg->sol will be initialized after completing the first
1742 * state, so that none of the msg pointers has to be initialized prior to the
1743 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001744 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001745void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001746{
Willy Tarreau3770f232013-12-07 00:01:53 +01001747 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001748 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001749 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001750
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001751 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001752 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001753 ptr = buf->p + msg->next;
1754 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001755
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001756 if (unlikely(ptr >= end))
1757 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001758
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001759 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001760 /*
1761 * First, states that are specific to the response only.
1762 * We check them first so that request and headers are
1763 * closer to each other (accessed more often).
1764 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001765 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001766 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001767 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001768 /* we have a start of message, but we have to check
1769 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001770 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001771 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001772 if (unlikely(ptr != buf->p)) {
1773 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001774 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001775 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001776 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001777 }
Willy Tarreau26927362012-05-18 23:22:52 +02001778 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001779 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001780 hdr_idx_init(idx);
1781 state = HTTP_MSG_RPVER;
1782 goto http_msg_rpver;
1783 }
1784
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001785 if (unlikely(!HTTP_IS_CRLF(*ptr))) {
1786 state = HTTP_MSG_RPBEFORE;
Willy Tarreau8973c702007-01-21 23:58:29 +01001787 goto http_msg_invalid;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001788 }
Willy Tarreau8973c702007-01-21 23:58:29 +01001789
1790 if (unlikely(*ptr == '\n'))
1791 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1792 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1793 /* stop here */
1794
Willy Tarreau8973c702007-01-21 23:58:29 +01001795 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001796 http_msg_rpbefore_cr:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001797 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_RPBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001798 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1799 /* stop here */
1800
Willy Tarreau8973c702007-01-21 23:58:29 +01001801 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001802 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001803 case HTTP_MSG_RPVER_SP:
1804 case HTTP_MSG_RPCODE:
1805 case HTTP_MSG_RPCODE_SP:
1806 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001807 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001808 state, ptr, end,
1809 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001810 if (unlikely(!ptr))
1811 return;
1812
1813 /* we have a full response and we know that we have either a CR
1814 * or an LF at <ptr>.
1815 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001816 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1817
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001818 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001819 if (likely(*ptr == '\r'))
1820 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1821 goto http_msg_rpline_end;
1822
Willy Tarreau8973c702007-01-21 23:58:29 +01001823 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001824 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001825 /* msg->sol must point to the first of CR or LF. */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001826 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_RPLINE_END);
Willy Tarreau8973c702007-01-21 23:58:29 +01001827 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1828 /* stop here */
1829
1830 /*
1831 * Second, states that are specific to the request only
1832 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001833 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001834 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001835 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001836 /* we have a start of message, but we have to check
1837 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001838 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001839 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001840 if (likely(ptr != buf->p)) {
1841 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001842 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001843 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001844 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001845 }
Willy Tarreau26927362012-05-18 23:22:52 +02001846 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001847 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001848 state = HTTP_MSG_RQMETH;
1849 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001850 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001851
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001852 if (unlikely(!HTTP_IS_CRLF(*ptr))) {
1853 state = HTTP_MSG_RQBEFORE;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001854 goto http_msg_invalid;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001855 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001856
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001857 if (unlikely(*ptr == '\n'))
1858 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1859 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001860 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001861
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001862 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001863 http_msg_rqbefore_cr:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001864 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001865 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001866 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001867
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001868 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001869 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001870 case HTTP_MSG_RQMETH_SP:
1871 case HTTP_MSG_RQURI:
1872 case HTTP_MSG_RQURI_SP:
1873 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001874 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001875 state, ptr, end,
1876 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001877 if (unlikely(!ptr))
1878 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001879
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001880 /* we have a full request and we know that we have either a CR
1881 * or an LF at <ptr>.
1882 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001883 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001884
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001885 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001886 if (likely(*ptr == '\r'))
1887 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001888 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001889
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001890 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001891 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001892 /* check for HTTP/0.9 request : no version information available.
1893 * msg->sol must point to the first of CR or LF.
1894 */
1895 if (unlikely(msg->sl.rq.v_l == 0))
1896 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001897
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001898 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001899 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001900 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001901
Willy Tarreau8973c702007-01-21 23:58:29 +01001902 /*
1903 * Common states below
1904 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001905 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001906 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001907 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001908 if (likely(!HTTP_IS_CRLF(*ptr))) {
1909 goto http_msg_hdr_name;
1910 }
1911
1912 if (likely(*ptr == '\r'))
1913 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1914 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001915
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001916 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001917 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001918 /* assumes msg->sol points to the first char */
1919 if (likely(HTTP_IS_TOKEN(*ptr)))
1920 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001921
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001922 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001923 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001924
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001925 if (likely(msg->err_pos < -1) || *ptr == '\n') {
1926 state = HTTP_MSG_HDR_NAME;
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001927 goto http_msg_invalid;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001928 }
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001929
1930 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001931 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001932
1933 /* and we still accept this non-token character */
1934 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001935
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001936 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001937 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001938 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001939 if (likely(HTTP_IS_SPHT(*ptr)))
1940 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001941
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001942 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001943 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001944
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001945 if (likely(!HTTP_IS_CRLF(*ptr))) {
1946 goto http_msg_hdr_val;
1947 }
1948
1949 if (likely(*ptr == '\r'))
1950 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1951 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001952
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001953 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001954 http_msg_hdr_l1_lf:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01001955 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_HDR_L1_LF);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001956 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001957
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001958 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001959 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001960 if (likely(HTTP_IS_SPHT(*ptr))) {
1961 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001962 for (; buf->p + msg->sov < ptr; msg->sov++)
1963 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001964 goto http_msg_hdr_l1_sp;
1965 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001966 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001967 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001968 goto http_msg_complete_header;
1969
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001970 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001971 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001972 /* assumes msg->sol points to the first char, and msg->sov
1973 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001974 */
Willy Tarreau0431f9d2016-11-05 17:35:40 +01001975
1976 /* speedup: we'll skip packs of 4 or 8 bytes not containing bytes 0x0D
1977 * and lower. In fact since most of the time is spent in the loop, we
1978 * also remove the sign bit test so that bytes 0x8e..0x0d break the
1979 * loop, but we don't care since they're very rare in header values.
1980 */
1981#if defined(__x86_64__)
1982 while (ptr <= end - sizeof(long)) {
1983 if ((*(long *)ptr - 0x0e0e0e0e0e0e0e0eULL) & 0x8080808080808080ULL)
1984 goto http_msg_hdr_val2;
1985 ptr += sizeof(long);
1986 }
1987#endif
1988#if defined(__x86_64__) || \
1989 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
1990 defined(__ARM_ARCH_7A__)
1991 while (ptr <= end - sizeof(int)) {
1992 if ((*(int*)ptr - 0x0e0e0e0e) & 0x80808080)
1993 goto http_msg_hdr_val2;
1994 ptr += sizeof(int);
1995 }
1996#endif
1997 http_msg_hdr_val2:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001998 if (likely(!HTTP_IS_CRLF(*ptr)))
Willy Tarreau0431f9d2016-11-05 17:35:40 +01001999 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val2, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002000
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002001 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002002 /* Note: we could also copy eol into ->eoh so that we have the
2003 * real header end in case it ends with lots of LWS, but is this
2004 * really needed ?
2005 */
2006 if (likely(*ptr == '\r'))
2007 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
2008 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002009
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002010 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02002011 http_msg_hdr_l2_lf:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002012 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_HDR_L2_LF);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002013 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002014
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002015 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02002016 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002017 if (unlikely(HTTP_IS_SPHT(*ptr))) {
2018 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002019 for (; buf->p + msg->eol < ptr; msg->eol++)
2020 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002021 goto http_msg_hdr_val;
2022 }
2023 http_msg_complete_header:
2024 /*
2025 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002026 * Assumes msg->sol points to the first char, msg->sov points
2027 * to the first character of the value and msg->eol to the
2028 * first CR or LF so we know how the line ends. We insert last
2029 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002030 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002031 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002032 idx, idx->tail) < 0)) {
2033 state = HTTP_MSG_HDR_L2_LWS;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002034 goto http_msg_invalid;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002035 }
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002036
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002037 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002038 if (likely(!HTTP_IS_CRLF(*ptr))) {
2039 goto http_msg_hdr_name;
2040 }
2041
2042 if (likely(*ptr == '\r'))
2043 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
2044 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002045
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002046 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02002047 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01002048 /* Assumes msg->sol points to the first of either CR or LF.
2049 * Sets ->sov and ->next to the total header length, ->eoh to
2050 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
2051 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002052 EXPECT_LF_HERE(ptr, http_msg_invalid, HTTP_MSG_LAST_LF);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002053 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002054 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01002055 msg->eoh = msg->sol;
2056 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01002057 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002058 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002059 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02002060
2061 case HTTP_MSG_ERROR:
2062 /* this may only happen if we call http_msg_analyser() twice with an error */
2063 break;
2064
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002065 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01002066#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002067 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
2068 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002069#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01002070 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002071 }
2072 http_msg_ood:
2073 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002074 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002075 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002076 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002077
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002078 http_msg_invalid:
2079 /* invalid message */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002080 msg->err_state = state;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002081 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002082 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002083 return;
2084}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01002085
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002086/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
2087 * conversion succeeded, 0 in case of error. If the request was already 1.X,
2088 * nothing is done and 1 is returned.
2089 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002090static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002091{
2092 int delta;
2093 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002094 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002095
2096 if (msg->sl.rq.v_l != 0)
2097 return 1;
2098
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03002099 /* RFC 1945 allows only GET for HTTP/0.9 requests */
2100 if (txn->meth != HTTP_METH_GET)
2101 return 0;
2102
Willy Tarreau9b28e032012-10-12 23:49:43 +02002103 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002104
2105 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03002106 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
2107 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002108 }
2109 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002110 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01002111 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002112 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02002113 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002114 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002115 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002116 NULL, NULL);
2117 if (unlikely(!cur_end))
2118 return 0;
2119
2120 /* we have a full HTTP/1.0 request now and we know that
2121 * we have either a CR or an LF at <ptr>.
2122 */
2123 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
2124 return 1;
2125}
2126
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002127/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002128 * and "keep-alive" values. If we already know that some headers may safely
2129 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002130 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
2131 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01002132 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002133 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
2134 * found, and TX_CON_*_SET is adjusted depending on what is left so only
2135 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002136 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01002137 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002138void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01002139{
Willy Tarreau5b154472009-12-21 20:11:07 +01002140 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002141 const char *hdr_val = "Connection";
2142 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01002143
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002144 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01002145 return;
2146
Willy Tarreau88d349d2010-01-25 12:15:43 +01002147 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2148 hdr_val = "Proxy-Connection";
2149 hdr_len = 16;
2150 }
2151
Willy Tarreau5b154472009-12-21 20:11:07 +01002152 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002153 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002154 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002155 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2156 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002157 if (to_del & 2)
2158 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002159 else
2160 txn->flags |= TX_CON_KAL_SET;
2161 }
2162 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2163 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002164 if (to_del & 1)
2165 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002166 else
2167 txn->flags |= TX_CON_CLO_SET;
2168 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01002169 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
2170 txn->flags |= TX_HDR_CONN_UPG;
2171 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002172 }
2173
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002174 txn->flags |= TX_HDR_CONN_PRS;
2175 return;
2176}
Willy Tarreau5b154472009-12-21 20:11:07 +01002177
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002178/* Apply desired changes on the Connection: header. Values may be removed and/or
2179 * added depending on the <wanted> flags, which are exclusively composed of
2180 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
2181 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
2182 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002183void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002184{
2185 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002186 const char *hdr_val = "Connection";
2187 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002188
2189 ctx.idx = 0;
2190
Willy Tarreau88d349d2010-01-25 12:15:43 +01002191
2192 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2193 hdr_val = "Proxy-Connection";
2194 hdr_len = 16;
2195 }
2196
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002197 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002198 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002199 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2200 if (wanted & TX_CON_KAL_SET)
2201 txn->flags |= TX_CON_KAL_SET;
2202 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002203 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01002204 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002205 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2206 if (wanted & TX_CON_CLO_SET)
2207 txn->flags |= TX_CON_CLO_SET;
2208 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002209 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002210 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002211 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002212
2213 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
2214 return;
2215
2216 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
2217 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002218 hdr_val = "Connection: close";
2219 hdr_len = 17;
2220 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2221 hdr_val = "Proxy-Connection: close";
2222 hdr_len = 23;
2223 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002224 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002225 }
2226
2227 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
2228 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002229 hdr_val = "Connection: keep-alive";
2230 hdr_len = 22;
2231 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2232 hdr_val = "Proxy-Connection: keep-alive";
2233 hdr_len = 28;
2234 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002235 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002236 }
2237 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01002238}
2239
Christopher Faulet113f7de2015-12-14 14:52:13 +01002240/* Parse the chunk size at msg->next. Once done, caller should adjust ->next to
2241 * point to the first byte of data after the chunk size, so that we know we can
2242 * forward exactly msg->next bytes. msg->sol contains the exact number of bytes
2243 * forming the chunk size. That way it is always possible to differentiate
2244 * between the start of the body and the start of the data. Return the number
2245 * of byte parsed on success, 0 when some data is missing, <0 on error. Note:
2246 * this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002247 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002248static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002249{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002250 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002251 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002252 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002253 const char *end = buf->data + buf->size;
2254 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01002255 unsigned int chunk = 0;
2256
2257 /* The chunk size is in the following form, though we are only
2258 * interested in the size and CRLF :
2259 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2260 */
2261 while (1) {
2262 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002263 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002264 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002265 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002266 if (c < 0) /* not a hex digit anymore */
2267 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02002268 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002269 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01002270 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002271 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002272 chunk = (chunk << 4) + c;
2273 }
2274
Willy Tarreaud98cf932009-12-27 22:54:55 +01002275 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02002276 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002277 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002278
Willy Tarreau2235b262016-11-05 15:50:20 +01002279 while (HTTP_IS_SPHT(*ptr)) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01002280 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002281 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02002282 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01002283 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002284 }
2285
Willy Tarreaud98cf932009-12-27 22:54:55 +01002286 /* Up to there, we know that at least one byte is present at *ptr. Check
2287 * for the end of chunk size.
2288 */
2289 while (1) {
2290 if (likely(HTTP_IS_CRLF(*ptr))) {
2291 /* we now have a CR or an LF at ptr */
2292 if (likely(*ptr == '\r')) {
2293 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002294 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002295 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002296 return 0;
2297 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002298
Willy Tarreaud98cf932009-12-27 22:54:55 +01002299 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002300 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002301 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002302 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002303 /* done */
2304 break;
2305 }
2306 else if (*ptr == ';') {
2307 /* chunk extension, ends at next CRLF */
2308 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002309 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002310 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002311 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002312
2313 while (!HTTP_IS_CRLF(*ptr)) {
2314 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002315 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002316 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002317 return 0;
2318 }
2319 /* we have a CRLF now, loop above */
2320 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002321 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002322 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002323 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002324 }
2325
Christopher Faulet113f7de2015-12-14 14:52:13 +01002326 /* OK we found our CRLF and now <ptr> points to the next byte, which may
2327 * or may not be present. We save the number of bytes parsed into
2328 * msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002329 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002330 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002331 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002332 msg->sol += buf->size;
Willy Tarreau124d9912011-03-01 20:30:48 +01002333 msg->chunk_len = chunk;
2334 msg->body_len += chunk;
Christopher Faulet113f7de2015-12-14 14:52:13 +01002335 return msg->sol;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002336 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002337 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002338 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002339}
2340
Christopher Faulet113f7de2015-12-14 14:52:13 +01002341/* This function skips trailers in the buffer associated with HTTP message
2342 * <msg>. The first visited position is msg->next. If the end of the trailers is
2343 * found, the function returns >0. So, the caller can automatically schedul it
2344 * to be forwarded, and switch msg->msg_state to HTTP_MSG_DONE. If not enough
2345 * data are available, the function does not change anything except maybe
2346 * msg->sol if it could parse some lines, and returns zero. If a parse error
2347 * is encountered, the function returns < 0 and does not change anything except
2348 * maybe msg->sol. Note that the message must already be in HTTP_MSG_TRAILERS
2349 * state before calling this function, which implies that all non-trailers data
2350 * have already been scheduled for forwarding, and that msg->next exactly
2351 * matches the length of trailers already parsed and not forwarded. It is also
2352 * important to note that this function is designed to be able to parse wrapped
2353 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002354 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002355static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002356{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002357 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002358
Christopher Faulet113f7de2015-12-14 14:52:13 +01002359 /* we have msg->next which points to next line. Look for CRLF. But
2360 * first, we reset msg->sol */
2361 msg->sol = 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002362 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002363 const char *p1 = NULL, *p2 = NULL;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002364 const char *start = b_ptr(buf, msg->next + msg->sol);
2365 const char *stop = bi_end(buf);
2366 const char *ptr = start;
2367 int bytes = 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002368
2369 /* scan current line and stop at LF or CRLF */
2370 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002371 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002372 return 0;
2373
2374 if (*ptr == '\n') {
2375 if (!p1)
2376 p1 = ptr;
2377 p2 = ptr;
2378 break;
2379 }
2380
2381 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002382 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002383 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002384 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002385 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002386 p1 = ptr;
2387 }
2388
2389 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002390 if (ptr >= buf->data + buf->size)
2391 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002392 }
2393
2394 /* after LF; point to beginning of next line */
2395 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002396 if (p2 >= buf->data + buf->size)
2397 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002398
Christopher Faulet2fb28802015-12-01 10:40:57 +01002399 bytes = p2 - start;
Willy Tarreau638cd022010-01-03 07:42:04 +01002400 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002401 bytes += buf->size;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002402 msg->sol += bytes;
Willy Tarreau638cd022010-01-03 07:42:04 +01002403
Christopher Fauletd7c91962015-04-30 11:48:27 +02002404 /* LF/CRLF at beginning of line => end of trailers at p2.
2405 * Everything was scheduled for forwarding, there's nothing left
Christopher Faulet2fb28802015-12-01 10:40:57 +01002406 * from this message. */
2407 if (p1 == start)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002408 return 1;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002409
Willy Tarreaud98cf932009-12-27 22:54:55 +01002410 /* OK, next line then */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002411 }
2412}
2413
Christopher Faulet113f7de2015-12-14 14:52:13 +01002414/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
2415 * a possible LF alone at the end of a chunk. The caller should adjust msg->next
2416 * in order to include this part into the next forwarding phase. Note that the
2417 * caller must ensure that ->p points to the first byte to parse. It returns
2418 * the number of bytes parsed on success, so the caller can set msg_state to
2419 * HTTP_MSG_CHUNK_SIZE. If not enough data are available, the function does not
2420 * change anything and returns zero. If a parse error is encountered, the
2421 * function returns < 0. Note: this function is designed to parse wrapped CRLF
2422 * at the end of the buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002423 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002424static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002425{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002426 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002427 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002428 int bytes;
2429
2430 /* NB: we'll check data availabilty at the end. It's not a
2431 * problem because whatever we match first will be checked
2432 * against the correct length.
2433 */
2434 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002435 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002436 if (*ptr == '\r') {
2437 bytes++;
2438 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002439 if (ptr >= buf->data + buf->size)
2440 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002441 }
2442
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002443 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002444 return 0;
2445
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002446 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002447 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002448 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002449 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01002450 return bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002451}
Willy Tarreau5b154472009-12-21 20:11:07 +01002452
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002453/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2454 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2455 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2456 * Unparsable qvalues return 1000 as "q=1.000".
2457 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002458int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002459{
2460 int q = 1000;
2461
Willy Tarreau506c69a2014-07-08 00:59:48 +02002462 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002463 goto out;
2464 q = (*qvalue++ - '0') * 1000;
2465
2466 if (*qvalue++ != '.')
2467 goto out;
2468
Willy Tarreau506c69a2014-07-08 00:59:48 +02002469 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002470 goto out;
2471 q += (*qvalue++ - '0') * 100;
2472
Willy Tarreau506c69a2014-07-08 00:59:48 +02002473 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002474 goto out;
2475 q += (*qvalue++ - '0') * 10;
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') * 1;
2480 out:
2481 if (q > 1000)
2482 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002483 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002484 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002485 return q;
2486}
William Lallemand82fe75c2012-10-23 10:25:10 +02002487
Willy Tarreau87b09662015-04-03 00:22:06 +02002488void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002489{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002490 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002491 int tmp = TX_CON_WANT_KAL;
2492
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002493 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2494 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002495 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2496 tmp = TX_CON_WANT_TUN;
2497
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002498 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002499 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2500 tmp = TX_CON_WANT_TUN;
2501 }
2502
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002503 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002504 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2505 /* option httpclose + server_close => forceclose */
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_CLO;
2509 else
2510 tmp = TX_CON_WANT_SCL;
2511 }
2512
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002513 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002514 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2515 tmp = TX_CON_WANT_CLO;
2516
2517 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2518 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2519
2520 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2521 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2522 /* parse the Connection header and possibly clean it */
2523 int to_del = 0;
2524 if ((msg->flags & HTTP_MSGF_VER_11) ||
2525 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002526 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002527 to_del |= 2; /* remove "keep-alive" */
2528 if (!(msg->flags & HTTP_MSGF_VER_11))
2529 to_del |= 1; /* remove "close" */
2530 http_parse_connection_header(txn, msg, to_del);
2531 }
2532
2533 /* check if client or config asks for explicit close in KAL/SCL */
2534 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2535 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2536 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2537 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2538 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002539 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002540 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2541}
William Lallemand82fe75c2012-10-23 10:25:10 +02002542
Willy Tarreaud787e662009-07-07 10:14:51 +02002543/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2544 * processing can continue on next analysers, or zero if it either needs more
2545 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002546 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02002547 * when it has nothing left to do, and may remove any analyser when it wants to
2548 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002549 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002550int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002551{
Willy Tarreau59234e92008-11-30 23:51:27 +01002552 /*
2553 * We will parse the partial (or complete) lines.
2554 * We will check the request syntax, and also join multi-line
2555 * headers. An index of all the lines will be elaborated while
2556 * parsing.
2557 *
2558 * For the parsing, we use a 28 states FSM.
2559 *
2560 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002561 * req->buf->p = beginning of request
2562 * req->buf->p + msg->eoh = end of processed headers / start of current one
2563 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002564 * msg->eol = end of current header or line (LF or CRLF)
2565 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002566 *
2567 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02002568 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002569 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2570 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002571 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002572
Willy Tarreau59234e92008-11-30 23:51:27 +01002573 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002574 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002575 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01002576 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002577 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002578
Willy Tarreau87b09662015-04-03 00:22:06 +02002579 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 +01002580 now_ms, __FUNCTION__,
2581 s,
2582 req,
2583 req->rex, req->wex,
2584 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002585 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002586 req->analysers);
2587
Willy Tarreau52a0c602009-08-16 22:45:38 +02002588 /* we're speaking HTTP here, so let's speak HTTP to the client */
2589 s->srv_error = http_return_srv_error;
2590
Willy Tarreau83e3af02009-12-28 17:39:57 +01002591 /* There's a protected area at the end of the buffer for rewriting
2592 * purposes. We don't want to start to parse the request if the
2593 * protected area is affected, because we may have to move processed
2594 * data later, which is much more complicated.
2595 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002596 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002597
2598 /* This point is executed when some data is avalaible for analysis,
2599 * so we log the end of the idle time. */
2600 if (s->logs.t_idle == -1)
2601 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
2602
Willy Tarreau379357a2013-06-08 12:55:46 +02002603 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01002604 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002605 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002606 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002607 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002608 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002609 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002610 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002611 return 0;
2612 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002613 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2614 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2615 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002616 }
2617
Willy Tarreau065e8332010-01-08 00:30:20 +01002618 /* Note that we have the same problem with the response ; we
2619 * may want to send a redirect, error or anything which requires
2620 * some spare space. So we'll ensure that we have at least
2621 * maxrewrite bytes available in the response buffer before
2622 * processing that one. This will only affect pipelined
2623 * keep-alive requests.
2624 */
2625 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002626 unlikely(!channel_is_rewritable(&s->res) ||
2627 bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) ||
2628 bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) {
2629 if (s->res.buf->o) {
2630 if (s->res.flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002631 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002632 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002633 channel_dont_connect(req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002634 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
2635 s->res.flags |= CF_WAKE_WRITE;
2636 s->res.analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002637 return 0;
2638 }
2639 }
2640
Willy Tarreau9b28e032012-10-12 23:49:43 +02002641 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002642 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002643 }
2644
Willy Tarreau59234e92008-11-30 23:51:27 +01002645 /* 1: we might have to print this header in debug mode */
2646 if (unlikely((global.mode & MODE_DEBUG) &&
2647 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02002648 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002649 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002650
Willy Tarreau9b28e032012-10-12 23:49:43 +02002651 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002652 /* this is a bit complex : in case of error on the request line,
2653 * we know that rq.l is still zero, so we display only the part
2654 * up to the end of the line (truncated by debug_hdr).
2655 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002656 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002657 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002658
Willy Tarreau59234e92008-11-30 23:51:27 +01002659 sol += hdr_idx_first_pos(&txn->hdr_idx);
2660 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002661
Willy Tarreau59234e92008-11-30 23:51:27 +01002662 while (cur_idx) {
2663 eol = sol + txn->hdr_idx.v[cur_idx].len;
2664 debug_hdr("clihdr", s, sol, eol);
2665 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2666 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002667 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002668 }
2669
Willy Tarreau58f10d72006-12-04 02:26:12 +01002670
Willy Tarreau59234e92008-11-30 23:51:27 +01002671 /*
2672 * Now we quickly check if we have found a full valid request.
2673 * If not so, we check the FD and buffer states before leaving.
2674 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002675 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002676 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02002677 * on a keep-alive stream, if we encounter and error, close, t/o,
2678 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002679 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02002680 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002681 * Last, we may increase some tracked counters' http request errors on
2682 * the cases that are deliberately the client's fault. For instance,
2683 * a timeout or connection reset is not counted as an error. However
2684 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002685 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002686
Willy Tarreau655dce92009-11-08 13:10:58 +01002687 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002688 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002689 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002690 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002691 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002692 stream_inc_http_req_ctr(s);
2693 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002694 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002695 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002696 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002697
Willy Tarreau59234e92008-11-30 23:51:27 +01002698 /* 1: Since we are in header mode, if there's no space
2699 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02002700 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01002701 * must terminate it now.
2702 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002703 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002704 /* FIXME: check if URI is set and return Status
2705 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002706 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002707 stream_inc_http_req_ctr(s);
2708 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002709 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002710 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002711 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002712 goto return_bad_req;
2713 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002714
Willy Tarreau59234e92008-11-30 23:51:27 +01002715 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002716 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002717 if (!(s->flags & SF_ERR_MASK))
2718 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002719
Willy Tarreaufcffa692010-01-10 14:21:19 +01002720 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002721 goto failed_keep_alive;
2722
Willy Tarreau0f228a02015-05-01 15:37:53 +02002723 if (sess->fe->options & PR_O_IGNORE_PRB)
2724 goto failed_keep_alive;
2725
Willy Tarreau59234e92008-11-30 23:51:27 +01002726 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002727 if (msg->err_pos >= 0) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002728 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002729 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002730 }
2731
Willy Tarreaudc979f22012-12-04 10:39:01 +01002732 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002733 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01002734 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002735 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002736 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02002737 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002738 proxy_inc_fe_req_ctr(sess->fe);
2739 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002740 if (sess->listener->counters)
2741 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002742
Willy Tarreaue7dff022015-04-03 01:14:29 +02002743 if (!(s->flags & SF_FINST_MASK))
2744 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002745 return 0;
2746 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002747
Willy Tarreau59234e92008-11-30 23:51:27 +01002748 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002749 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002750 if (!(s->flags & SF_ERR_MASK))
2751 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002752
Willy Tarreaufcffa692010-01-10 14:21:19 +01002753 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002754 goto failed_keep_alive;
2755
Willy Tarreau0f228a02015-05-01 15:37:53 +02002756 if (sess->fe->options & PR_O_IGNORE_PRB)
2757 goto failed_keep_alive;
2758
Willy Tarreau59234e92008-11-30 23:51:27 +01002759 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002760 if (msg->err_pos >= 0) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002761 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002762 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002763 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002764 txn->status = 408;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002765 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01002766 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002767 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_408));
Christopher Faulet0184ea72017-01-05 14:06:34 +01002768 req->analysers &= AN_REQ_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002769
Willy Tarreau87b09662015-04-03 00:22:06 +02002770 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002771 proxy_inc_fe_req_ctr(sess->fe);
2772 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002773 if (sess->listener->counters)
2774 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002775
Willy Tarreaue7dff022015-04-03 01:14:29 +02002776 if (!(s->flags & SF_FINST_MASK))
2777 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002778 return 0;
2779 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002780
Willy Tarreau59234e92008-11-30 23:51:27 +01002781 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002782 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002783 if (!(s->flags & SF_ERR_MASK))
2784 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002785
Willy Tarreaufcffa692010-01-10 14:21:19 +01002786 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002787 goto failed_keep_alive;
2788
Willy Tarreau0f228a02015-05-01 15:37:53 +02002789 if (sess->fe->options & PR_O_IGNORE_PRB)
2790 goto failed_keep_alive;
2791
Willy Tarreau4076a152009-04-02 15:18:36 +02002792 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002793 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002794 txn->status = 400;
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002795 msg->err_state = msg->msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01002796 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002797 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Christopher Faulet0184ea72017-01-05 14:06:34 +01002798 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02002799 stream_inc_http_err_ctr(s);
2800 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002801 proxy_inc_fe_req_ctr(sess->fe);
2802 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002803 if (sess->listener->counters)
2804 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002805
Willy Tarreaue7dff022015-04-03 01:14:29 +02002806 if (!(s->flags & SF_FINST_MASK))
2807 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002808 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002809 }
2810
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002811 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002812 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002813 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002814#ifdef TCP_QUICKACK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002815 if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i &&
2816 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002817 /* We need more data, we have to re-enable quick-ack in case we
2818 * previously disabled it, otherwise we might cause the client
2819 * to delay next data.
2820 */
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002821 setsockopt(__objt_conn(sess->origin)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002822 }
2823#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002824
Willy Tarreaufcffa692010-01-10 14:21:19 +01002825 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2826 /* If the client starts to talk, let's fall back to
2827 * request timeout processing.
2828 */
2829 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002830 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002831 }
2832
Willy Tarreau59234e92008-11-30 23:51:27 +01002833 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002834 if (!tick_isset(req->analyse_exp)) {
2835 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2836 (txn->flags & TX_WAIT_NEXT_RQ) &&
2837 tick_isset(s->be->timeout.httpka))
2838 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2839 else
2840 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2841 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002842
Willy Tarreau59234e92008-11-30 23:51:27 +01002843 /* we're not ready yet */
2844 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002845
2846 failed_keep_alive:
2847 /* Here we process low-level errors for keep-alive requests. In
2848 * short, if the request is not the first one and it experiences
2849 * a timeout, read error or shutdown, we just silently close so
2850 * that the client can try again.
2851 */
2852 txn->status = 0;
2853 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01002854 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002855 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002856 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002857 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002858 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002859 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002860 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002861
Willy Tarreaud787e662009-07-07 10:14:51 +02002862 /* OK now we have a complete HTTP request with indexed headers. Let's
2863 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002864 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002865 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002866 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002867 * byte after the last LF. msg->sov points to the first byte of data.
2868 * msg->eol cannot be trusted because it may have been left uninitialized
2869 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002870 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002871
Willy Tarreau87b09662015-04-03 00:22:06 +02002872 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002873 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002874
Willy Tarreaub16a5742010-01-10 14:46:16 +01002875 if (txn->flags & TX_WAIT_NEXT_RQ) {
2876 /* kill the pending keep-alive timeout */
2877 txn->flags &= ~TX_WAIT_NEXT_RQ;
2878 req->analyse_exp = TICK_ETERNITY;
2879 }
2880
2881
Willy Tarreaud787e662009-07-07 10:14:51 +02002882 /* Maybe we found in invalid header name while we were configured not
2883 * to block on that, so we have to capture it now.
2884 */
2885 if (unlikely(msg->err_pos >= 0))
Willy Tarreau10e61cb2017-01-04 14:51:22 +01002886 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002887
Willy Tarreau59234e92008-11-30 23:51:27 +01002888 /*
2889 * 1: identify the method
2890 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002891 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002892
2893 /* we can make use of server redirect on GET and HEAD */
2894 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002895 s->flags |= SF_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002896
Willy Tarreau59234e92008-11-30 23:51:27 +01002897 /*
2898 * 2: check if the URI matches the monitor_uri.
2899 * We have to do this for every request which gets in, because
2900 * the monitor-uri is defined by the frontend.
2901 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002902 if (unlikely((sess->fe->monitor_uri_len != 0) &&
2903 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002904 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002905 sess->fe->monitor_uri,
2906 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002907 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002908 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002909 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002910 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002911
Willy Tarreaue7dff022015-04-03 01:14:29 +02002912 s->flags |= SF_MONITOR;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002913 sess->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002914
Willy Tarreau59234e92008-11-30 23:51:27 +01002915 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002916 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002917 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002918
Willy Tarreau59234e92008-11-30 23:51:27 +01002919 ret = acl_pass(ret);
2920 if (cond->pol == ACL_COND_UNLESS)
2921 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002922
Willy Tarreau59234e92008-11-30 23:51:27 +01002923 if (ret) {
2924 /* we fail this request, let's return 503 service unavail */
2925 txn->status = 503;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002926 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_503));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002927 if (!(s->flags & SF_ERR_MASK))
2928 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002929 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002930 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002931 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002932
Willy Tarreau59234e92008-11-30 23:51:27 +01002933 /* nothing to fail, let's reply normaly */
2934 txn->status = 200;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002935 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_200));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002936 if (!(s->flags & SF_ERR_MASK))
2937 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002938 goto return_prx_cond;
2939 }
2940
2941 /*
2942 * 3: Maybe we have to copy the original REQURI for the logs ?
2943 * Note: we cannot log anymore if the request has been
2944 * classified as invalid.
2945 */
2946 if (unlikely(s->logs.logwait & LW_REQ)) {
2947 /* we have a complete HTTP request that we must log */
2948 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2949 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002950
Willy Tarreau59234e92008-11-30 23:51:27 +01002951 if (urilen >= REQURI_LEN)
2952 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002953 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002954 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002955
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002956 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002957 s->do_log(s);
2958 } else {
2959 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002960 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002961 }
Willy Tarreau06619262006-12-17 08:37:22 +01002962
Willy Tarreau91852eb2015-05-01 13:26:00 +02002963 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
2964 * exactly one digit "." one digit. This check may be disabled using
2965 * option accept-invalid-http-request.
2966 */
2967 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
2968 if (msg->sl.rq.v_l != 8) {
2969 msg->err_pos = msg->sl.rq.v;
2970 goto return_bad_req;
2971 }
2972
2973 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
2974 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
2975 req->buf->p[msg->sl.rq.v + 6] != '.' ||
2976 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
2977 msg->err_pos = msg->sl.rq.v + 4;
2978 goto return_bad_req;
2979 }
2980 }
Willy Tarreau13317662015-05-01 13:47:08 +02002981 else {
2982 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
2983 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
2984 goto return_bad_req;
2985 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02002986
Willy Tarreau5b154472009-12-21 20:11:07 +01002987 /* ... and check if the request is HTTP/1.1 or above */
2988 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002989 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2990 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2991 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002992 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002993
2994 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002995 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 +01002996
Willy Tarreau88d349d2010-01-25 12:15:43 +01002997 /* if the frontend has "option http-use-proxy-header", we'll check if
2998 * we have what looks like a proxied connection instead of a connection,
2999 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
3000 * Note that this is *not* RFC-compliant, however browsers and proxies
3001 * happen to do that despite being non-standard :-(
3002 * We consider that a request not beginning with either '/' or '*' is
3003 * a proxied connection, which covers both "scheme://location" and
3004 * CONNECT ip:port.
3005 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003006 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02003007 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01003008 txn->flags |= TX_USE_PX_CONN;
3009
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003010 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003011 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003012
Willy Tarreau59234e92008-11-30 23:51:27 +01003013 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02003014 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02003015 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02003016 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02003017
Willy Tarreau557f1992015-05-01 10:05:17 +02003018 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
3019 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003020 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003021 * The length of a message body is determined by one of the following
3022 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02003023 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003024 * 1. Any response to a HEAD request and any response with a 1xx
3025 * (Informational), 204 (No Content), or 304 (Not Modified) status
3026 * code is always terminated by the first empty line after the
3027 * header fields, regardless of the header fields present in the
3028 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003029 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003030 * 2. Any 2xx (Successful) response to a CONNECT request implies that
3031 * the connection will become a tunnel immediately after the empty
3032 * line that concludes the header fields. A client MUST ignore any
3033 * Content-Length or Transfer-Encoding header fields received in
3034 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003035 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003036 * 3. If a Transfer-Encoding header field is present and the chunked
3037 * transfer coding (Section 4.1) is the final encoding, the message
3038 * body length is determined by reading and decoding the chunked
3039 * data until the transfer coding indicates the data is complete.
3040 *
3041 * If a Transfer-Encoding header field is present in a response and
3042 * the chunked transfer coding is not the final encoding, the
3043 * message body length is determined by reading the connection until
3044 * it is closed by the server. If a Transfer-Encoding header field
3045 * is present in a request and the chunked transfer coding is not
3046 * the final encoding, the message body length cannot be determined
3047 * reliably; the server MUST respond with the 400 (Bad Request)
3048 * status code and then close the connection.
3049 *
3050 * If a message is received with both a Transfer-Encoding and a
3051 * Content-Length header field, the Transfer-Encoding overrides the
3052 * Content-Length. Such a message might indicate an attempt to
3053 * perform request smuggling (Section 9.5) or response splitting
3054 * (Section 9.4) and ought to be handled as an error. A sender MUST
3055 * remove the received Content-Length field prior to forwarding such
3056 * a message downstream.
3057 *
3058 * 4. If a message is received without Transfer-Encoding and with
3059 * either multiple Content-Length header fields having differing
3060 * field-values or a single Content-Length header field having an
3061 * invalid value, then the message framing is invalid and the
3062 * recipient MUST treat it as an unrecoverable error. If this is a
3063 * request message, the server MUST respond with a 400 (Bad Request)
3064 * status code and then close the connection. If this is a response
3065 * message received by a proxy, the proxy MUST close the connection
3066 * to the server, discard the received response, and send a 502 (Bad
3067 * Gateway) response to the client. If this is a response message
3068 * received by a user agent, the user agent MUST close the
3069 * connection to the server and discard the received response.
3070 *
3071 * 5. If a valid Content-Length header field is present without
3072 * Transfer-Encoding, its decimal value defines the expected message
3073 * body length in octets. If the sender closes the connection or
3074 * the recipient times out before the indicated number of octets are
3075 * received, the recipient MUST consider the message to be
3076 * incomplete and close the connection.
3077 *
3078 * 6. If this is a request message and none of the above are true, then
3079 * the message body length is zero (no message body is present).
3080 *
3081 * 7. Otherwise, this is a response message without a declared message
3082 * body length, so the message body length is determined by the
3083 * number of octets received prior to the server closing the
3084 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003085 */
3086
Willy Tarreau32b47f42009-10-18 20:55:02 +02003087 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003088 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau4979d5c2015-05-01 10:06:30 +02003089 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003090 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003091 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
3092 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02003093 /* chunked not last, return badreq */
3094 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003095 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003096 }
3097
Willy Tarreau1c913912015-04-30 10:57:51 +02003098 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02003099 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02003100 if (msg->flags & HTTP_MSGF_TE_CHNK) {
3101 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
3102 http_remove_header2(msg, &txn->hdr_idx, &ctx);
3103 }
Willy Tarreau34dfc602015-05-01 10:09:49 +02003104 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02003105 signed long long cl;
3106
Willy Tarreauad14f752011-09-02 20:33:27 +02003107 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003108 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003109 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003110 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003111
Willy Tarreauad14f752011-09-02 20:33:27 +02003112 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003113 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003114 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02003115 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003116
Willy Tarreauad14f752011-09-02 20:33:27 +02003117 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003118 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003119 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003120 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003121
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003122 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003123 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003124 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02003125 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003126
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003127 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003128 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003129 }
3130
Willy Tarreau34dfc602015-05-01 10:09:49 +02003131 /* even bodyless requests have a known length */
3132 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003133
Willy Tarreau179085c2014-04-28 16:48:56 +02003134 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3135 * only change if both the request and the config reference something else.
3136 * Option httpclose by itself sets tunnel mode where headers are mangled.
3137 * However, if another mode is set, it will affect it (eg: server-close/
3138 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3139 * if FE and BE have the same settings (common). The method consists in
3140 * checking if options changed between the two calls (implying that either
3141 * one is non-null, or one of them is non-null and we are there for the first
3142 * time.
3143 */
3144 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003145 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02003146 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003147
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02003148 /* we may have to wait for the request's body */
3149 if ((s->be->options & PR_O_WREQ_BODY) &&
3150 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
3151 req->analysers |= AN_REQ_HTTP_BODY;
3152
Willy Tarreaud787e662009-07-07 10:14:51 +02003153 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003154 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003155 req->analyse_exp = TICK_ETERNITY;
3156 return 1;
3157
3158 return_bad_req:
3159 /* We centralize bad requests processing here */
3160 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3161 /* we detected a parsing error. We want to archive this request
3162 * in the dedicated proxy area for later troubleshooting.
3163 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003164 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003165 }
3166
Willy Tarreau10e61cb2017-01-04 14:51:22 +01003167 txn->req.err_state = txn->req.msg_state;
Willy Tarreaud787e662009-07-07 10:14:51 +02003168 txn->req.msg_state = HTTP_MSG_ERROR;
3169 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01003170 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003171
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003172 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003173 if (sess->listener->counters)
3174 sess->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003175
3176 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003177 if (!(s->flags & SF_ERR_MASK))
3178 s->flags |= SF_ERR_PRXCOND;
3179 if (!(s->flags & SF_FINST_MASK))
3180 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02003181
Christopher Faulet0184ea72017-01-05 14:06:34 +01003182 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02003183 req->analyse_exp = TICK_ETERNITY;
3184 return 0;
3185}
3186
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003187
Willy Tarreau347a35d2013-11-22 17:51:09 +01003188/* This function prepares an applet to handle the stats. It can deal with the
3189 * "100-continue" expectation, check that admin rules are met for POST requests,
3190 * and program a response message if something was unexpected. It cannot fail
3191 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003192 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003193 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02003194 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003195 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003196int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003197{
3198 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01003199 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02003200 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003201 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003202 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003203 struct uri_auth *uri_auth = s->be->uri_auth;
3204 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003205 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003206
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003207 appctx = si_appctx(si);
3208 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3209 appctx->st1 = appctx->st2 = 0;
3210 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3211 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003212 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003213 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003214
3215 uri = msg->chn->buf->p + msg->sl.rq.u;
3216 lookup = uri + uri_auth->uri_len;
3217
3218 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3219 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003220 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003221 break;
3222 }
3223 }
3224
3225 if (uri_auth->refresh) {
3226 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3227 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003228 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003229 break;
3230 }
3231 }
3232 }
3233
3234 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3235 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003236 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003237 break;
3238 }
3239 }
3240
Willy Tarreau1e62df92016-01-11 18:57:53 +01003241 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
3242 if (memcmp(h, ";typed", 6) == 0) {
3243 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
3244 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
3245 break;
3246 }
3247 }
3248
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003249 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3250 if (memcmp(h, ";st=", 4) == 0) {
3251 int i;
3252 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003253 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003254 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3255 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003256 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003257 break;
3258 }
3259 }
3260 break;
3261 }
3262 }
3263
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003264 appctx->ctx.stats.scope_str = 0;
3265 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003266 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3267 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3268 int itx = 0;
3269 const char *h2;
3270 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3271 const char *err;
3272
3273 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3274 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003275 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003276 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3277 itx++;
3278 h++;
3279 }
3280
3281 if (itx > STAT_SCOPE_TXT_MAXLEN)
3282 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003283 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003284
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003285 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003286 memcpy(scope_txt, h2, itx);
3287 scope_txt[itx] = '\0';
3288 err = invalid_char(scope_txt);
3289 if (err) {
3290 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003291 appctx->ctx.stats.scope_str = 0;
3292 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003293 }
3294 break;
3295 }
3296 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003297
3298 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003299 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003300 int ret = 1;
3301
3302 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003303 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 +01003304 ret = acl_pass(ret);
3305 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3306 ret = !ret;
3307 }
3308
3309 if (ret) {
3310 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003311 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003312 break;
3313 }
3314 }
3315
3316 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003317 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003318 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003319 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003320 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
3321 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003322 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003323 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003324 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003325 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3326 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003327 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003328 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003329 else {
3330 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003331 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003332 }
3333
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003334 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003335 return 1;
3336}
3337
Lukas Tribus67db8df2013-06-23 17:37:13 +02003338/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3339 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3340 */
Vincent Bernat6e615892016-05-18 16:17:44 +02003341void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003342{
3343#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02003344 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003345 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3346#endif
3347#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02003348 if (from->ss_family == AF_INET6) {
3349 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02003350 /* v4-mapped addresses need IP_TOS */
3351 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3352 else
3353 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3354 }
3355#endif
3356}
3357
Willy Tarreau87b09662015-04-03 00:22:06 +02003358int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003359 const char* name, unsigned int name_len,
3360 const char *str, struct my_regex *re,
3361 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06003362{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003363 struct hdr_ctx ctx;
3364 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003365 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003366 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3367 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003368 struct chunk *output = get_trash_chunk();
3369
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003370 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06003371
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003372 /* Choose the header browsing function. */
3373 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003374 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003375 http_find_hdr_func = http_find_header2;
3376 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003377 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003378 http_find_hdr_func = http_find_full_header2;
3379 break;
3380 default: /* impossible */
3381 return -1;
3382 }
3383
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003384 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
3385 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06003386 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003387 char *val = ctx.line + ctx.val;
3388 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003389
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003390 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
3391 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003392
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003393 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003394 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003395 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003396
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003397 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003398
3399 hdr->len += delta;
3400 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003401
3402 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003403 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003404 }
3405
3406 return 0;
3407}
3408
Willy Tarreau87b09662015-04-03 00:22:06 +02003409static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003410 const char* name, unsigned int name_len,
3411 struct list *fmt, struct my_regex *re,
3412 int action)
3413{
3414 struct chunk *replace = get_trash_chunk();
3415
3416 replace->len = build_logline(s, replace->str, replace->size, fmt);
3417 if (replace->len >= replace->size - 1)
3418 return -1;
3419
3420 return http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
3421}
3422
Willy Tarreau87b09662015-04-03 00:22:06 +02003423/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003424 * transaction <txn>. Returns the verdict of the first rule that prevents
3425 * further processing of the request (auth, deny, ...), and defaults to
3426 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3427 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02003428 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
3429 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
3430 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003431 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003432enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02003433http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003434{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003435 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003436 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003437 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003438 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003439 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003440 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02003441 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02003442 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003443
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003444 /* If "the current_rule_list" match the executed rule list, we are in
3445 * resume condition. If a resume is needed it is always in the action
3446 * and never in the ACL or converters. In this case, we initialise the
3447 * current rule, and go to the action execution point.
3448 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003449 if (s->current_rule) {
3450 rule = s->current_rule;
3451 s->current_rule = NULL;
3452 if (s->current_rule_list == rules)
3453 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003454 }
3455 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003456
Willy Tarreauff011f22011-01-06 17:51:27 +01003457 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003458
Willy Tarreau96257ec2012-12-27 10:46:37 +01003459 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003460 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003461 int ret;
3462
Willy Tarreau192252e2015-04-04 01:47:55 +02003463 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003464 ret = acl_pass(ret);
3465
Willy Tarreauff011f22011-01-06 17:51:27 +01003466 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003467 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003468
3469 if (!ret) /* condition not matched */
3470 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003471 }
3472
Willy Tarreauacc98002015-09-27 23:34:39 +02003473 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003474resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003475 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003476 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003477 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003478
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003479 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02003480 if (deny_status)
3481 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003482 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003483
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003484 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01003485 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003486 if (deny_status)
3487 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003488 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003489
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003490 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003491 /* Auth might be performed on regular http-req rules as well as on stats */
3492 auth_realm = rule->arg.auth.realm;
3493 if (!auth_realm) {
3494 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3495 auth_realm = STATS_DEFAULT_REALM;
3496 else
3497 auth_realm = px->id;
3498 }
3499 /* send 401/407 depending on whether we use a proxy or not. We still
3500 * count one error, because normal browsing won't significantly
3501 * increase the counter but brute force attempts will.
3502 */
3503 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3504 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01003505 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02003506 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003507 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003508
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003509 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003510 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3511 return HTTP_RULE_RES_BADREQ;
3512 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003513
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003514 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003515 s->task->nice = rule->arg.nice;
3516 break;
3517
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003518 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003519 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Vincent Bernat6e615892016-05-18 16:17:44 +02003520 inet_set_tos(cli_conn->t.sock.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003521 break;
3522
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003523 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003524#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003525 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003526 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003527#endif
3528 break;
3529
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003530 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003531 s->logs.level = rule->arg.loglevel;
3532 break;
3533
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003534 case ACT_HTTP_REPLACE_HDR:
3535 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003536 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3537 rule->arg.hdr_add.name_len,
3538 &rule->arg.hdr_add.fmt,
3539 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003540 return HTTP_RULE_RES_BADREQ;
3541 break;
3542
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003543 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003544 ctx.idx = 0;
3545 /* remove all occurrences of the header */
3546 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3547 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3548 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003549 }
Willy Tarreau85603282015-01-21 20:39:27 +01003550 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003551
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003552 case ACT_HTTP_SET_HDR:
3553 case ACT_HTTP_ADD_HDR:
Thierry Fournier4b788f72016-06-01 13:35:36 +02003554 /* The scope of the trash buffer must be limited to this function. The
3555 * build_logline() function can execute a lot of other function which
3556 * can use the trash buffer. So for limiting the scope of this global
3557 * buffer, we build first the header value using build_logline, and
3558 * after we store the header name.
3559 */
3560 len = rule->arg.hdr_add.name_len + 2,
3561 len += build_logline(s, trash.str + len, trash.size - len, &rule->arg.hdr_add.fmt);
Willy Tarreau96257ec2012-12-27 10:46:37 +01003562 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
Thierry Fournier4b788f72016-06-01 13:35:36 +02003563 trash.str[rule->arg.hdr_add.name_len] = ':';
3564 trash.str[rule->arg.hdr_add.name_len + 1] = ' ';
3565 trash.len = len;
Willy Tarreau85603282015-01-21 20:39:27 +01003566
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003567 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003568 /* remove all occurrences of the header */
3569 ctx.idx = 0;
3570 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3571 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3572 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3573 }
3574 }
3575
Willy Tarreau96257ec2012-12-27 10:46:37 +01003576 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3577 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003578
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003579 case ACT_HTTP_DEL_ACL:
3580 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003581 struct pat_ref *ref;
3582 char *key;
3583 int len;
3584
3585 /* collect reference */
3586 ref = pat_ref_lookup(rule->arg.map.ref);
3587 if (!ref)
3588 continue;
3589
3590 /* collect key */
3591 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3592 key = trash.str;
3593 key[len] = '\0';
3594
3595 /* perform update */
3596 /* returned code: 1=ok, 0=ko */
3597 pat_ref_delete(ref, key);
3598
3599 break;
3600 }
3601
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003602 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003603 struct pat_ref *ref;
3604 char *key;
3605 struct chunk *trash_key;
3606 int len;
3607
3608 trash_key = get_trash_chunk();
3609
3610 /* collect reference */
3611 ref = pat_ref_lookup(rule->arg.map.ref);
3612 if (!ref)
3613 continue;
3614
3615 /* collect key */
3616 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3617 key = trash_key->str;
3618 key[len] = '\0';
3619
3620 /* perform update */
3621 /* add entry only if it does not already exist */
3622 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003623 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003624
3625 break;
3626 }
3627
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003628 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003629 struct pat_ref *ref;
3630 char *key, *value;
3631 struct chunk *trash_key, *trash_value;
3632 int len;
3633
3634 trash_key = get_trash_chunk();
3635 trash_value = get_trash_chunk();
3636
3637 /* collect reference */
3638 ref = pat_ref_lookup(rule->arg.map.ref);
3639 if (!ref)
3640 continue;
3641
3642 /* collect key */
3643 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3644 key = trash_key->str;
3645 key[len] = '\0';
3646
3647 /* collect value */
3648 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3649 value = trash_value->str;
3650 value[len] = '\0';
3651
3652 /* perform update */
3653 if (pat_ref_find_elt(ref, key) != NULL)
3654 /* update entry if it exists */
3655 pat_ref_set(ref, key, value, NULL);
3656 else
3657 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003658 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003659
3660 break;
3661 }
William Lallemand73025dd2014-04-24 14:38:37 +02003662
Thierry FOURNIER42148732015-09-02 17:17:33 +02003663 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02003664 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
3665 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003666
Willy Tarreauacc98002015-09-27 23:34:39 +02003667 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003668 case ACT_RET_ERR:
3669 case ACT_RET_CONT:
3670 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003671 case ACT_RET_STOP:
3672 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003673 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003674 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003675 return HTTP_RULE_RES_YIELD;
3676 }
William Lallemand73025dd2014-04-24 14:38:37 +02003677 break;
3678
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003679 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02003680 /* Note: only the first valid tracking parameter of each
3681 * applies.
3682 */
3683
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003684 if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02003685 struct stktable *t;
3686 struct stksess *ts;
3687 struct stktable_key *key;
3688 void *ptr;
3689
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02003690 t = rule->arg.trk_ctr.table.t;
3691 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 +02003692
3693 if (key && (ts = stktable_get_entry(t, key))) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003694 stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02003695
3696 /* let's count a new HTTP request as it's the first time we do it */
3697 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3698 if (ptr)
3699 stktable_data_cast(ptr, http_req_cnt)++;
3700
3701 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3702 if (ptr)
3703 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3704 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3705
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003706 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003707 if (sess->fe != s->be)
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003708 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02003709 }
3710 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003711 break;
3712
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003713 /* other flags exists, but normaly, they never be matched. */
3714 default:
3715 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003716 }
3717 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003718
3719 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003720 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003721}
3722
Willy Tarreau71241ab2012-12-27 11:30:54 +01003723
Willy Tarreau51d861a2015-05-22 17:30:48 +02003724/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3725 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3726 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3727 * is returned, the process can continue the evaluation of next rule list. If
3728 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3729 * is returned, it means the operation could not be processed and a server error
3730 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3731 * deny rule. If *YIELD is returned, the caller must call again the function
3732 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003733 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003734static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003735http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003736{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003737 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003738 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003739 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003740 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003741 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02003742 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003743
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003744 /* If "the current_rule_list" match the executed rule list, we are in
3745 * resume condition. If a resume is needed it is always in the action
3746 * and never in the ACL or converters. In this case, we initialise the
3747 * current rule, and go to the action execution point.
3748 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003749 if (s->current_rule) {
3750 rule = s->current_rule;
3751 s->current_rule = NULL;
3752 if (s->current_rule_list == rules)
3753 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003754 }
3755 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003756
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003757 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003758
3759 /* check optional condition */
3760 if (rule->cond) {
3761 int ret;
3762
Willy Tarreau192252e2015-04-04 01:47:55 +02003763 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003764 ret = acl_pass(ret);
3765
3766 if (rule->cond->pol == ACL_COND_UNLESS)
3767 ret = !ret;
3768
3769 if (!ret) /* condition not matched */
3770 continue;
3771 }
3772
Willy Tarreauacc98002015-09-27 23:34:39 +02003773 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003774resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003775 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003776 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003777 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003778
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003779 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003780 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003781 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003782
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003783 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003784 s->task->nice = rule->arg.nice;
3785 break;
3786
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003787 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003788 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Vincent Bernat6e615892016-05-18 16:17:44 +02003789 inet_set_tos(cli_conn->t.sock.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003790 break;
3791
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003792 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003793#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003794 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003795 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003796#endif
3797 break;
3798
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003799 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003800 s->logs.level = rule->arg.loglevel;
3801 break;
3802
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003803 case ACT_HTTP_REPLACE_HDR:
3804 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003805 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3806 rule->arg.hdr_add.name_len,
3807 &rule->arg.hdr_add.fmt,
3808 &rule->arg.hdr_add.re, rule->action))
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003809 return HTTP_RULE_RES_STOP; /* note: we should report an error here */
Sasha Pachev218f0642014-06-16 12:05:59 -06003810 break;
3811
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003812 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003813 ctx.idx = 0;
3814 /* remove all occurrences of the header */
3815 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3816 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3817 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3818 }
Willy Tarreau85603282015-01-21 20:39:27 +01003819 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003820
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003821 case ACT_HTTP_SET_HDR:
3822 case ACT_HTTP_ADD_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003823 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3824 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3825 trash.len = rule->arg.hdr_add.name_len;
3826 trash.str[trash.len++] = ':';
3827 trash.str[trash.len++] = ' ';
3828 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 +01003829
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003830 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003831 /* remove all occurrences of the header */
3832 ctx.idx = 0;
3833 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3834 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3835 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3836 }
3837 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003838 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3839 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003840
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003841 case ACT_HTTP_DEL_ACL:
3842 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003843 struct pat_ref *ref;
3844 char *key;
3845 int len;
3846
3847 /* collect reference */
3848 ref = pat_ref_lookup(rule->arg.map.ref);
3849 if (!ref)
3850 continue;
3851
3852 /* collect key */
3853 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3854 key = trash.str;
3855 key[len] = '\0';
3856
3857 /* perform update */
3858 /* returned code: 1=ok, 0=ko */
3859 pat_ref_delete(ref, key);
3860
3861 break;
3862 }
3863
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003864 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003865 struct pat_ref *ref;
3866 char *key;
3867 struct chunk *trash_key;
3868 int len;
3869
3870 trash_key = get_trash_chunk();
3871
3872 /* collect reference */
3873 ref = pat_ref_lookup(rule->arg.map.ref);
3874 if (!ref)
3875 continue;
3876
3877 /* collect key */
3878 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3879 key = trash_key->str;
3880 key[len] = '\0';
3881
3882 /* perform update */
3883 /* check if the entry already exists */
3884 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003885 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003886
3887 break;
3888 }
3889
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003890 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003891 struct pat_ref *ref;
3892 char *key, *value;
3893 struct chunk *trash_key, *trash_value;
3894 int len;
3895
3896 trash_key = get_trash_chunk();
3897 trash_value = get_trash_chunk();
3898
3899 /* collect reference */
3900 ref = pat_ref_lookup(rule->arg.map.ref);
3901 if (!ref)
3902 continue;
3903
3904 /* collect key */
3905 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3906 key = trash_key->str;
3907 key[len] = '\0';
3908
3909 /* collect value */
3910 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3911 value = trash_value->str;
3912 value[len] = '\0';
3913
3914 /* perform update */
3915 if (pat_ref_find_elt(ref, key) != NULL)
3916 /* update entry if it exists */
3917 pat_ref_set(ref, key, value, NULL);
3918 else
3919 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003920 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003921
3922 break;
3923 }
William Lallemand73025dd2014-04-24 14:38:37 +02003924
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003925 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02003926 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3927 return HTTP_RULE_RES_BADREQ;
3928 return HTTP_RULE_RES_DONE;
3929
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003930 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3931 /* Note: only the first valid tracking parameter of each
3932 * applies.
3933 */
3934
3935 if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) {
3936 struct stktable *t;
3937 struct stksess *ts;
3938 struct stktable_key *key;
3939 void *ptr;
3940
3941 t = rule->arg.trk_ctr.table.t;
3942 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
3943
3944 if (key && (ts = stktable_get_entry(t, key))) {
3945 stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts);
3946
3947 /* let's count a new HTTP request as it's the first time we do it */
3948 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3949 if (ptr)
3950 stktable_data_cast(ptr, http_req_cnt)++;
3951
3952 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3953 if (ptr)
3954 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3955 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3956
3957 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3958 if (sess->fe != s->be)
3959 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3960
3961 /* When the client triggers a 4xx from the server, it's most often due
3962 * to a missing object or permission. These events should be tracked
3963 * because if they happen often, it may indicate a brute force or a
3964 * vulnerability scan. Normally this is done when receiving the response
3965 * but here we're tracking after this ought to have been done so we have
3966 * to do it on purpose.
3967 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02003968 if ((unsigned)(txn->status - 400) < 100) {
3969 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3970 if (ptr)
3971 stktable_data_cast(ptr, http_err_cnt)++;
3972
3973 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3974 if (ptr)
3975 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3976 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3977 }
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003978 }
3979 }
3980 break;
3981
Thierry FOURNIER42148732015-09-02 17:17:33 +02003982 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02003983 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
3984 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003985
Willy Tarreauacc98002015-09-27 23:34:39 +02003986 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003987 case ACT_RET_ERR:
3988 case ACT_RET_CONT:
3989 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003990 case ACT_RET_STOP:
3991 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003992 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003993 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003994 return HTTP_RULE_RES_YIELD;
3995 }
William Lallemand73025dd2014-04-24 14:38:37 +02003996 break;
3997
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003998 /* other flags exists, but normaly, they never be matched. */
3999 default:
4000 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004001 }
4002 }
4003
4004 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01004005 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02004006}
4007
4008
Willy Tarreau71241ab2012-12-27 11:30:54 +01004009/* Perform an HTTP redirect based on the information in <rule>. The function
4010 * returns non-zero on success, or zero in case of a, irrecoverable error such
4011 * as too large a request to build a valid response.
4012 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004013static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01004014{
Willy Tarreaub329a312015-05-22 16:27:37 +02004015 struct http_msg *req = &txn->req;
4016 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004017 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004018 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004019
4020 /* build redirect message */
4021 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04004022 case 308:
4023 msg_fmt = HTTP_308;
4024 break;
4025 case 307:
4026 msg_fmt = HTTP_307;
4027 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004028 case 303:
4029 msg_fmt = HTTP_303;
4030 break;
4031 case 301:
4032 msg_fmt = HTTP_301;
4033 break;
4034 case 302:
4035 default:
4036 msg_fmt = HTTP_302;
4037 break;
4038 }
4039
4040 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
4041 return 0;
4042
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004043 location = trash.str + trash.len;
4044
Willy Tarreau71241ab2012-12-27 11:30:54 +01004045 switch(rule->type) {
4046 case REDIRECT_TYPE_SCHEME: {
4047 const char *path;
4048 const char *host;
4049 struct hdr_ctx ctx;
4050 int pathlen;
4051 int hostlen;
4052
4053 host = "";
4054 hostlen = 0;
4055 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02004056 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004057 host = ctx.line + ctx.val;
4058 hostlen = ctx.vlen;
4059 }
4060
4061 path = http_get_path(txn);
4062 /* build message using path */
4063 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004064 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004065 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4066 int qs = 0;
4067 while (qs < pathlen) {
4068 if (path[qs] == '?') {
4069 pathlen = qs;
4070 break;
4071 }
4072 qs++;
4073 }
4074 }
4075 } else {
4076 path = "/";
4077 pathlen = 1;
4078 }
4079
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004080 if (rule->rdr_str) { /* this is an old "redirect" rule */
4081 /* check if we can add scheme + "://" + host + path */
4082 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
4083 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004084
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004085 /* add scheme */
4086 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4087 trash.len += rule->rdr_len;
4088 }
4089 else {
4090 /* add scheme with executing log format */
4091 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004092
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004093 /* check if we can add scheme + "://" + host + path */
4094 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
4095 return 0;
4096 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004097 /* add "://" */
4098 memcpy(trash.str + trash.len, "://", 3);
4099 trash.len += 3;
4100
4101 /* add host */
4102 memcpy(trash.str + trash.len, host, hostlen);
4103 trash.len += hostlen;
4104
4105 /* add path */
4106 memcpy(trash.str + trash.len, path, pathlen);
4107 trash.len += pathlen;
4108
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004109 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004110 if (trash.len && trash.str[trash.len - 1] != '/' &&
4111 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4112 if (trash.len > trash.size - 5)
4113 return 0;
4114 trash.str[trash.len] = '/';
4115 trash.len++;
4116 }
4117
4118 break;
4119 }
4120 case REDIRECT_TYPE_PREFIX: {
4121 const char *path;
4122 int pathlen;
4123
4124 path = http_get_path(txn);
4125 /* build message using path */
4126 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004127 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004128 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4129 int qs = 0;
4130 while (qs < pathlen) {
4131 if (path[qs] == '?') {
4132 pathlen = qs;
4133 break;
4134 }
4135 qs++;
4136 }
4137 }
4138 } else {
4139 path = "/";
4140 pathlen = 1;
4141 }
4142
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004143 if (rule->rdr_str) { /* this is an old "redirect" rule */
4144 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
4145 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004146
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004147 /* add prefix. Note that if prefix == "/", we don't want to
4148 * add anything, otherwise it makes it hard for the user to
4149 * configure a self-redirection.
4150 */
4151 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
4152 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4153 trash.len += rule->rdr_len;
4154 }
4155 }
4156 else {
4157 /* add prefix with executing log format */
4158 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
4159
4160 /* Check length */
4161 if (trash.len + pathlen > trash.size - 4)
4162 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004163 }
4164
4165 /* add path */
4166 memcpy(trash.str + trash.len, path, pathlen);
4167 trash.len += pathlen;
4168
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004169 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004170 if (trash.len && trash.str[trash.len - 1] != '/' &&
4171 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4172 if (trash.len > trash.size - 5)
4173 return 0;
4174 trash.str[trash.len] = '/';
4175 trash.len++;
4176 }
4177
4178 break;
4179 }
4180 case REDIRECT_TYPE_LOCATION:
4181 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004182 if (rule->rdr_str) { /* this is an old "redirect" rule */
4183 if (trash.len + rule->rdr_len > trash.size - 4)
4184 return 0;
4185
4186 /* add location */
4187 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4188 trash.len += rule->rdr_len;
4189 }
4190 else {
4191 /* add location with executing log format */
4192 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004193
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004194 /* Check left length */
4195 if (trash.len > trash.size - 4)
4196 return 0;
4197 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004198 break;
4199 }
4200
4201 if (rule->cookie_len) {
4202 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
4203 trash.len += 14;
4204 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
4205 trash.len += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004206 }
4207
4208 /* add end of headers and the keep-alive/close status.
4209 * We may choose to set keep-alive if the Location begins
4210 * with a slash, because the client will come back to the
4211 * same server.
4212 */
4213 txn->status = rule->code;
4214 /* let's log the request time */
4215 s->logs.tv_request = now;
4216
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004217 if (*location == '/' &&
Willy Tarreaub329a312015-05-22 16:27:37 +02004218 (req->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau2de8a502015-05-28 17:23:54 +02004219 ((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004220 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4221 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4222 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02004223 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004224 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4225 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
4226 trash.len += 30;
4227 } else {
4228 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
4229 trash.len += 24;
4230 }
4231 }
4232 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
4233 trash.len += 4;
Christopher Faulet3e344292015-11-24 16:24:13 +01004234 FLT_STRM_CB(s, flt_http_reply(s, txn->status, &trash));
Willy Tarreaub329a312015-05-22 16:27:37 +02004235 bo_inject(res->chn, trash.str, trash.len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004236 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02004237 bi_fast_delete(req->chn->buf, req->sov);
4238 req->next -= req->sov;
4239 req->sov = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01004240 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END);
4241 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->req.analysers & AN_RES_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02004242 req->msg_state = HTTP_MSG_CLOSED;
4243 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02004244 /* Trim any possible response */
4245 res->chn->buf->i = 0;
4246 res->next = res->sov = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004247 } else {
4248 /* keep-alive not possible */
4249 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4250 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4251 trash.len += 29;
4252 } else {
4253 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
4254 trash.len += 23;
4255 }
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004256 http_reply_and_close(s, txn->status, &trash);
Christopher Faulet0184ea72017-01-05 14:06:34 +01004257 req->chn->analysers &= AN_REQ_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004258 }
4259
Willy Tarreaue7dff022015-04-03 01:14:29 +02004260 if (!(s->flags & SF_ERR_MASK))
4261 s->flags |= SF_ERR_LOCAL;
4262 if (!(s->flags & SF_FINST_MASK))
4263 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004264
4265 return 1;
4266}
4267
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004268/* This stream analyser runs all HTTP request processing which is common to
4269 * frontends and backends, which means blocking ACLs, filters, connection-close,
4270 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004271 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004272 * either needs more data or wants to immediately abort the request (eg: deny,
4273 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004274 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004275int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004276{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004277 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004278 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02004279 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004280 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004281 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004282 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02004283 int deny_status = HTTP_ERR_403;
Willy Tarreaud787e662009-07-07 10:14:51 +02004284
Willy Tarreau655dce92009-11-08 13:10:58 +01004285 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004286 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004287 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004288 }
4289
Willy Tarreau87b09662015-04-03 00:22:06 +02004290 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 +02004291 now_ms, __FUNCTION__,
4292 s,
4293 req,
4294 req->rex, req->wex,
4295 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004296 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004297 req->analysers);
4298
Willy Tarreau65410832014-04-28 21:25:43 +02004299 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02004300 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02004301
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004302 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004303 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02004304 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01004305
Willy Tarreau0b748332014-04-29 00:13:29 +02004306 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004307 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4308 goto return_prx_yield;
4309
Willy Tarreau0b748332014-04-29 00:13:29 +02004310 case HTTP_RULE_RES_CONT:
4311 case HTTP_RULE_RES_STOP: /* nothing to do */
4312 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004313
Willy Tarreau0b748332014-04-29 00:13:29 +02004314 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4315 if (txn->flags & TX_CLTARPIT)
4316 goto tarpit;
4317 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004318
Willy Tarreau0b748332014-04-29 00:13:29 +02004319 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4320 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004321
Willy Tarreau0b748332014-04-29 00:13:29 +02004322 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004323 goto done;
4324
Willy Tarreau0b748332014-04-29 00:13:29 +02004325 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4326 goto return_bad_req;
4327 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004328 }
4329
Willy Tarreau52542592014-04-28 18:33:26 +02004330 /* OK at this stage, we know that the request was accepted according to
4331 * the http-request rules, we can check for the stats. Note that the
4332 * URI is detected *before* the req* rules in order not to be affected
4333 * by a possible reqrep, while they are processed *after* so that a
4334 * reqdeny can still block them. This clearly needs to change in 1.6!
4335 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004336 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004337 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004338 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004339 txn->status = 500;
4340 s->logs.tv_request = now;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004341 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004342
Willy Tarreaue7dff022015-04-03 01:14:29 +02004343 if (!(s->flags & SF_ERR_MASK))
4344 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02004345 goto return_prx_cond;
4346 }
4347
4348 /* parse the whole stats request and extract the relevant information */
4349 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02004350 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02004351 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004352
Willy Tarreau0b748332014-04-29 00:13:29 +02004353 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4354 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004355
Willy Tarreau0b748332014-04-29 00:13:29 +02004356 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4357 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004358 }
4359
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004360 /* evaluate the req* rules except reqadd */
4361 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004362 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004363 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004364
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004365 if (txn->flags & TX_CLDENY)
4366 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004367
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004368 if (txn->flags & TX_CLTARPIT)
4369 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004370 }
Willy Tarreau06619262006-12-17 08:37:22 +01004371
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004372 /* add request headers from the rule sets in the same order */
4373 list_for_each_entry(wl, &px->req_add, list) {
4374 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004375 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004376 ret = acl_pass(ret);
4377 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4378 ret = !ret;
4379 if (!ret)
4380 continue;
4381 }
4382
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004383 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004384 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004385 }
4386
Willy Tarreau52542592014-04-28 18:33:26 +02004387
4388 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004389 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004390 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004391 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
4392 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004393
Willy Tarreaue7dff022015-04-03 01:14:29 +02004394 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
4395 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
4396 if (!(s->flags & SF_FINST_MASK))
4397 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004398
Willy Tarreau70730dd2014-04-24 18:06:27 +02004399 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet0184ea72017-01-05 14:06:34 +01004400 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
4401 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004402 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004403 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004404 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004405
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004406 /* check whether we have some ACLs set to redirect this request */
4407 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004408 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004409 int ret;
4410
Willy Tarreau192252e2015-04-04 01:47:55 +02004411 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004412 ret = acl_pass(ret);
4413 if (rule->cond->pol == ACL_COND_UNLESS)
4414 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004415 if (!ret)
4416 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004417 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004418 if (!http_apply_redirect_rule(rule, s, txn))
4419 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004420 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004421 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004422
Willy Tarreau2be39392010-01-03 17:24:51 +01004423 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4424 * If this happens, then the data will not come immediately, so we must
4425 * send all what we have without waiting. Note that due to the small gain
4426 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004427 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004428 * itself once used.
4429 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004430 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004431
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004432 done: /* done with this analyser, continue with next ones that the calling
4433 * points will have set, if any.
4434 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004435 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004436 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4437 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004438 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004439
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004440 tarpit:
4441 /* When a connection is tarpitted, we use the tarpit timeout,
4442 * which may be the same as the connect timeout if unspecified.
4443 * If unset, then set it to zero because we really want it to
4444 * eventually expire. We build the tarpit as an analyser.
4445 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004446 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004447
4448 /* wipe the request out so that we can drop the connection early
4449 * if the client closes first.
4450 */
4451 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004452
4453 /* Allow cookie logging
4454 */
4455 if (s->be->cookie_name || sess->fe->capture_name)
4456 manage_client_side_cookies(s, req);
4457
Christopher Faulet0184ea72017-01-05 14:06:34 +01004458 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004459 req->analysers |= AN_REQ_HTTP_TARPIT;
4460 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4461 if (!req->analyse_exp)
4462 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02004463 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004464 sess->fe->fe_counters.denied_req++;
4465 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004466 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004467 if (sess->listener->counters)
4468 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004469 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004470
4471 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004472
4473 /* Allow cookie logging
4474 */
4475 if (s->be->cookie_name || sess->fe->capture_name)
4476 manage_client_side_cookies(s, req);
4477
Willy Tarreau0b748332014-04-29 00:13:29 +02004478 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02004479 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004480 s->logs.tv_request = now;
Willy Tarreau58727ec2016-05-25 16:23:59 +02004481 http_reply_and_close(s, txn->status, http_error_message(s, deny_status));
Willy Tarreau87b09662015-04-03 00:22:06 +02004482 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004483 sess->fe->fe_counters.denied_req++;
4484 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004485 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004486 if (sess->listener->counters)
4487 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004488 goto return_prx_cond;
4489
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004490 return_bad_req:
4491 /* We centralize bad requests processing here */
4492 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4493 /* we detected a parsing error. We want to archive this request
4494 * in the dedicated proxy area for later troubleshooting.
4495 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004496 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004497 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004498
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004499 txn->req.err_state = txn->req.msg_state;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004500 txn->req.msg_state = HTTP_MSG_ERROR;
4501 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004502 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004503
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004504 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004505 if (sess->listener->counters)
4506 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004507
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004508 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02004509 if (!(s->flags & SF_ERR_MASK))
4510 s->flags |= SF_ERR_PRXCOND;
4511 if (!(s->flags & SF_FINST_MASK))
4512 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004513
Christopher Faulet0184ea72017-01-05 14:06:34 +01004514 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004515 req->analyse_exp = TICK_ETERNITY;
4516 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004517
4518 return_prx_yield:
4519 channel_dont_connect(req);
4520 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004521}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004522
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004523/* This function performs all the processing enabled for the current request.
4524 * It returns 1 if the processing can continue on next analysers, or zero if it
4525 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004526 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004527 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004528int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004529{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004530 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004531 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004532 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02004533 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004534
Willy Tarreau655dce92009-11-08 13:10:58 +01004535 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004536 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004537 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004538 return 0;
4539 }
4540
Willy Tarreau87b09662015-04-03 00:22:06 +02004541 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 +02004542 now_ms, __FUNCTION__,
4543 s,
4544 req,
4545 req->rex, req->wex,
4546 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004547 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004548 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004549
Willy Tarreau59234e92008-11-30 23:51:27 +01004550 /*
4551 * Right now, we know that we have processed the entire headers
4552 * and that unwanted requests have been filtered out. We can do
4553 * whatever we want with the remaining request. Also, now we
4554 * may have separate values for ->fe, ->be.
4555 */
Willy Tarreau06619262006-12-17 08:37:22 +01004556
Willy Tarreau59234e92008-11-30 23:51:27 +01004557 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004558 * If HTTP PROXY is set we simply get remote server address parsing
4559 * incoming request. Note that this requires that a connection is
4560 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004561 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004562 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004563 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004564 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004565
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004566 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau973a5422015-08-05 21:47:23 +02004567 if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004568 txn->req.err_state = txn->req.msg_state;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004569 txn->req.msg_state = HTTP_MSG_ERROR;
4570 txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01004571 req->analysers &= AN_REQ_FLT_END;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004572 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004573
Willy Tarreaue7dff022015-04-03 01:14:29 +02004574 if (!(s->flags & SF_ERR_MASK))
4575 s->flags |= SF_ERR_RESOURCE;
4576 if (!(s->flags & SF_FINST_MASK))
4577 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004578
4579 return 0;
4580 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004581
4582 path = http_get_path(txn);
4583 url2sa(req->buf->p + msg->sl.rq.u,
4584 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004585 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004586 /* if the path was found, we have to remove everything between
4587 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4588 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4589 * u_l characters by a single "/".
4590 */
4591 if (path) {
4592 char *cur_ptr = req->buf->p;
4593 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4594 int delta;
4595
4596 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4597 http_msg_move_end(&txn->req, delta);
4598 cur_end += delta;
4599 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4600 goto return_bad_req;
4601 }
4602 else {
4603 char *cur_ptr = req->buf->p;
4604 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4605 int delta;
4606
4607 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4608 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4609 http_msg_move_end(&txn->req, delta);
4610 cur_end += delta;
4611 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4612 goto return_bad_req;
4613 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004614 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004615
Willy Tarreau59234e92008-11-30 23:51:27 +01004616 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004617 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004618 * Note that doing so might move headers in the request, but
4619 * the fields will stay coherent and the URI will not move.
4620 * This should only be performed in the backend.
4621 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004622 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004623 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004624
William Lallemanda73203e2012-03-12 12:48:57 +01004625 /* add unique-id if "header-unique-id" is specified */
4626
Thierry Fournierf4011dd2016-03-29 17:23:51 +02004627 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004628 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4629 goto return_bad_req;
4630 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004631 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004632 }
William Lallemanda73203e2012-03-12 12:48:57 +01004633
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004634 if (sess->fe->header_unique_id && s->unique_id) {
4635 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004636 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004637 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004638 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004639 goto return_bad_req;
4640 }
4641
Cyril Bontéb21570a2009-11-29 20:04:48 +01004642 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004643 * 9: add X-Forwarded-For if either the frontend or the backend
4644 * asks for it.
4645 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004646 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004647 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004648 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
4649 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
4650 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004651 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004652 /* The header is set to be added only if none is present
4653 * and we found it, so don't do anything.
4654 */
4655 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004656 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004657 /* Add an X-Forwarded-For header unless the source IP is
4658 * in the 'except' network range.
4659 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004660 if ((!sess->fe->except_mask.s_addr ||
4661 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
4662 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01004663 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004664 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004665 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004666 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004667 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004668 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004669
4670 /* Note: we rely on the backend to get the header name to be used for
4671 * x-forwarded-for, because the header is really meant for the backends.
4672 * However, if the backend did not specify any option, we have to rely
4673 * on the frontend's header name.
4674 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004675 if (s->be->fwdfor_hdr_len) {
4676 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004677 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004678 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004679 len = sess->fe->fwdfor_hdr_len;
4680 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004681 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004682 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 +01004683
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004684 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004685 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004686 }
4687 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004688 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004689 /* FIXME: for the sake of completeness, we should also support
4690 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004691 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004692 int len;
4693 char pn[INET6_ADDRSTRLEN];
4694 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004695 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004696 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004697
Willy Tarreau59234e92008-11-30 23:51:27 +01004698 /* Note: we rely on the backend to get the header name to be used for
4699 * x-forwarded-for, because the header is really meant for the backends.
4700 * However, if the backend did not specify any option, we have to rely
4701 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004702 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004703 if (s->be->fwdfor_hdr_len) {
4704 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004705 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004706 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004707 len = sess->fe->fwdfor_hdr_len;
4708 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004709 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004710 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004711
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004712 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004713 goto return_bad_req;
4714 }
4715 }
4716
4717 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004718 * 10: add X-Original-To if either the frontend or the backend
4719 * asks for it.
4720 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004721 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004722
4723 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004724 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004725 /* Add an X-Original-To header unless the destination IP is
4726 * in the 'except' network range.
4727 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004728 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004729
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004730 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004731 ((!sess->fe->except_mask_to.s_addr ||
4732 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
4733 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004734 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004735 (((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 +02004736 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004737 int len;
4738 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004739 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004740
4741 /* Note: we rely on the backend to get the header name to be used for
4742 * x-original-to, because the header is really meant for the backends.
4743 * However, if the backend did not specify any option, we have to rely
4744 * on the frontend's header name.
4745 */
4746 if (s->be->orgto_hdr_len) {
4747 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004748 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004749 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004750 len = sess->fe->orgto_hdr_len;
4751 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004752 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004753 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 +02004754
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004755 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004756 goto return_bad_req;
4757 }
4758 }
4759 }
4760
Willy Tarreau50fc7772012-11-11 22:19:57 +01004761 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4762 * If an "Upgrade" token is found, the header is left untouched in order not to have
4763 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4764 * "Upgrade" is present in the Connection header.
4765 */
4766 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4767 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004768 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004769 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004770 unsigned int want_flags = 0;
4771
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004772 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004773 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004774 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004775 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004776 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004777 want_flags |= TX_CON_CLO_SET;
4778 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004779 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004780 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004781 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004782 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004783 want_flags |= TX_CON_KAL_SET;
4784 }
4785
4786 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004787 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004788 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004789
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004790
Willy Tarreau522d6c02009-12-06 18:49:18 +01004791 /* If we have no server assigned yet and we're balancing on url_param
4792 * with a POST request, we may be interested in checking the body for
4793 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004794 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004795 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02004796 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004797 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004798 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004799 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004800 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004801
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004802 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01004803 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004804 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004805#ifdef TCP_QUICKACK
4806 /* We expect some data from the client. Unless we know for sure
4807 * we already have a full request, we have to re-enable quick-ack
4808 * in case we previously disabled it, otherwise we might cause
4809 * the client to delay further data.
4810 */
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004811 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004812 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004813 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004814 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004815 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004816#endif
4817 }
Willy Tarreau03945942009-12-22 16:50:27 +01004818
Willy Tarreau59234e92008-11-30 23:51:27 +01004819 /*************************************************************
4820 * OK, that's finished for the headers. We have done what we *
4821 * could. Let's switch to the DATA state. *
4822 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004823 req->analyse_exp = TICK_ETERNITY;
4824 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004825
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004826 /* if the server closes the connection, we want to immediately react
4827 * and close the socket to save packets and syscalls.
4828 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004829 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
Willy Tarreau350f4872014-11-28 14:42:25 +01004830 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004831
Willy Tarreau59234e92008-11-30 23:51:27 +01004832 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004833 /* OK let's go on with the BODY now */
4834 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004835
Willy Tarreau59234e92008-11-30 23:51:27 +01004836 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004837 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004838 /* we detected a parsing error. We want to archive this request
4839 * in the dedicated proxy area for later troubleshooting.
4840 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004841 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004842 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004843
Willy Tarreau10e61cb2017-01-04 14:51:22 +01004844 txn->req.err_state = txn->req.msg_state;
Willy Tarreau59234e92008-11-30 23:51:27 +01004845 txn->req.msg_state = HTTP_MSG_ERROR;
4846 txn->status = 400;
Christopher Faulet0184ea72017-01-05 14:06:34 +01004847 req->analysers &= AN_REQ_FLT_END;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004848 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004849
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004850 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004851 if (sess->listener->counters)
4852 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004853
Willy Tarreaue7dff022015-04-03 01:14:29 +02004854 if (!(s->flags & SF_ERR_MASK))
4855 s->flags |= SF_ERR_PRXCOND;
4856 if (!(s->flags & SF_FINST_MASK))
4857 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004858 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004859}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004860
Willy Tarreau60b85b02008-11-30 23:28:40 +01004861/* This function is an analyser which processes the HTTP tarpit. It always
4862 * returns zero, at the beginning because it prevents any other processing
4863 * from occurring, and at the end because it terminates the request.
4864 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004865int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004866{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004867 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004868
4869 /* This connection is being tarpitted. The CLIENT side has
4870 * already set the connect expiration date to the right
4871 * timeout. We just have to check that the client is still
4872 * there and that the timeout has not expired.
4873 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004874 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004875 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004876 !tick_is_expired(req->analyse_exp, now_ms))
4877 return 0;
4878
4879 /* We will set the queue timer to the time spent, just for
4880 * logging purposes. We fake a 500 server error, so that the
4881 * attacker will not suspect his connection has been tarpitted.
4882 * It will not cause trouble to the logs because we can exclude
4883 * the tarpitted connections by filtering on the 'PT' status flags.
4884 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004885 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4886
4887 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004888 if (!(req->flags & CF_READ_ERROR))
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004889 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004890
Christopher Faulet0184ea72017-01-05 14:06:34 +01004891 req->analysers &= AN_REQ_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004892 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004893
Willy Tarreaue7dff022015-04-03 01:14:29 +02004894 if (!(s->flags & SF_ERR_MASK))
4895 s->flags |= SF_ERR_PRXCOND;
4896 if (!(s->flags & SF_FINST_MASK))
4897 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004898 return 0;
4899}
4900
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004901/* This function is an analyser which waits for the HTTP request body. It waits
4902 * for either the buffer to be full, or the full advertised contents to have
4903 * reached the buffer. It must only be called after the standard HTTP request
4904 * processing has occurred, because it expects the request to be parsed and will
4905 * look for the Expect header. It may send a 100-Continue interim response. It
4906 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4907 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4908 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004909 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004910int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004911{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004912 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004913 struct http_txn *txn = s->txn;
4914 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004915
4916 /* We have to parse the HTTP request body to find any required data.
4917 * "balance url_param check_post" should have been the only way to get
4918 * into this. We were brought here after HTTP header analysis, so all
4919 * related structures are ready.
4920 */
4921
Willy Tarreau890988f2014-04-10 11:59:33 +02004922 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4923 /* This is the first call */
4924 if (msg->msg_state < HTTP_MSG_BODY)
4925 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004926
Willy Tarreau890988f2014-04-10 11:59:33 +02004927 if (msg->msg_state < HTTP_MSG_100_SENT) {
4928 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4929 * send an HTTP/1.1 100 Continue intermediate response.
4930 */
4931 if (msg->flags & HTTP_MSGF_VER_11) {
4932 struct hdr_ctx ctx;
4933 ctx.idx = 0;
4934 /* Expect is allowed in 1.1, look for it */
4935 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4936 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004937 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Thierry FOURNIER / OZON.IO43ad11d2016-12-12 15:19:58 +01004938 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau890988f2014-04-10 11:59:33 +02004939 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004940 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004941 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004942 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004943
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004944 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004945 * req->buf->p still points to the beginning of the message. We
4946 * must save the body in msg->next because it survives buffer
4947 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004948 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004949 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004950
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004951 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004952 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4953 else
4954 msg->msg_state = HTTP_MSG_DATA;
4955 }
4956
Willy Tarreau890988f2014-04-10 11:59:33 +02004957 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4958 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004959 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004960 goto missing_data;
4961
4962 /* OK we have everything we need now */
4963 goto http_end;
4964 }
4965
4966 /* OK here we're parsing a chunked-encoded message */
4967
Willy Tarreau522d6c02009-12-06 18:49:18 +01004968 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004969 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004970 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004971 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004972 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004973 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004974
Willy Tarreau115acb92009-12-26 13:56:06 +01004975 if (!ret)
4976 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004977 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004978 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004979 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004980 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01004981 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004982 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01004983 }
4984
Willy Tarreaud98cf932009-12-27 22:54:55 +01004985 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02004986 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4987 * for at least a whole chunk or the whole content length bytes after
4988 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004989 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004990 if (msg->msg_state == HTTP_MSG_TRAILERS)
4991 goto http_end;
4992
Willy Tarreaue115b492015-05-01 23:05:14 +02004993 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004994 goto http_end;
4995
4996 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004997 /* we get here if we need to wait for more data. If the buffer is full,
4998 * we have the maximum we can expect.
4999 */
5000 if (buffer_full(req->buf, global.tune.maxrewrite))
5001 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01005002
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005003 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01005004 txn->status = 408;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005005 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02005006
Willy Tarreaue7dff022015-04-03 01:14:29 +02005007 if (!(s->flags & SF_ERR_MASK))
5008 s->flags |= SF_ERR_CLITO;
5009 if (!(s->flags & SF_FINST_MASK))
5010 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005011 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01005012 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01005013
5014 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02005015 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01005016 /* Not enough data. We'll re-use the http-request
5017 * timeout here. Ideally, we should set the timeout
5018 * relative to the accept() date. We just set the
5019 * request timeout once at the beginning of the
5020 * request.
5021 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005022 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01005023 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02005024 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01005025 return 0;
5026 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01005027
5028 http_end:
5029 /* The situation will not evolve, so let's give up on the analysis. */
5030 s->logs.tv_request = now; /* update the request timer to reflect full request */
5031 req->analysers &= ~an_bit;
5032 req->analyse_exp = TICK_ETERNITY;
5033 return 1;
5034
5035 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005036 txn->req.err_state = txn->req.msg_state;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005037 txn->req.msg_state = HTTP_MSG_ERROR;
5038 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005039 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01005040
Willy Tarreaue7dff022015-04-03 01:14:29 +02005041 if (!(s->flags & SF_ERR_MASK))
5042 s->flags |= SF_ERR_PRXCOND;
5043 if (!(s->flags & SF_FINST_MASK))
5044 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005045
Willy Tarreau522d6c02009-12-06 18:49:18 +01005046 return_err_msg:
Christopher Faulet0184ea72017-01-05 14:06:34 +01005047 req->analysers &= AN_REQ_FLT_END;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005048 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005049 if (sess->listener->counters)
5050 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005051 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01005052}
5053
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005054/* send a server's name with an outgoing request over an established connection.
5055 * Note: this function is designed to be called once the request has been scheduled
5056 * for being forwarded. This is the reason why it rewinds the buffer before
5057 * proceeding.
5058 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01005059int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05005060
5061 struct hdr_ctx ctx;
5062
Mark Lamourinec2247f02012-01-04 13:02:01 -05005063 char *hdr_name = be->server_id_hdr_name;
5064 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02005065 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005066 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005067 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005068
William Lallemandd9e90662012-01-30 17:27:17 +01005069 ctx.idx = 0;
5070
Willy Tarreau211cdec2014-04-17 20:18:08 +02005071 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005072 if (old_o) {
5073 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005074 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02005075 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02005076 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005077 }
5078
Willy Tarreau9b28e032012-10-12 23:49:43 +02005079 old_i = chn->buf->i;
5080 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 -05005081 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005082 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005083 }
5084
5085 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005086 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005087 memcpy(hdr_val, hdr_name, hdr_name_len);
5088 hdr_val += hdr_name_len;
5089 *hdr_val++ = ':';
5090 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005091 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
5092 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005093
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005094 if (old_o) {
5095 /* If this was a forwarded request, we must readjust the amount of
5096 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02005097 * variations. Note that the current state is >= HTTP_MSG_BODY,
5098 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005099 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02005100 old_o += chn->buf->i - old_i;
5101 b_adv(chn->buf, old_o);
5102 txn->req.next -= old_o;
5103 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005104 }
5105
Mark Lamourinec2247f02012-01-04 13:02:01 -05005106 return 0;
5107}
5108
Willy Tarreau610ecce2010-01-04 21:15:02 +01005109/* Terminate current transaction and prepare a new one. This is very tricky
5110 * right now but it works.
5111 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005112void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005113{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005114 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005115 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01005116 struct proxy *be = s->be;
Willy Tarreau323a2d92015-08-04 19:00:17 +02005117 struct connection *srv_conn;
5118 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02005119 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01005120
Willy Tarreau610ecce2010-01-04 21:15:02 +01005121 /* FIXME: We need a more portable way of releasing a backend's and a
5122 * server's connections. We need a safer way to reinitialize buffer
5123 * flags. We also need a more accurate method for computing per-request
5124 * data.
5125 */
Willy Tarreau323a2d92015-08-04 19:00:17 +02005126 srv_conn = objt_conn(s->si[1].end);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005127
Willy Tarreau4213a112013-12-15 10:25:42 +01005128 /* unless we're doing keep-alive, we want to quickly close the connection
5129 * to the server.
5130 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005131 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005132 !si_conn_ready(&s->si[1])) {
5133 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
5134 si_shutr(&s->si[1]);
5135 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01005136 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005137
Willy Tarreaue7dff022015-04-03 01:14:29 +02005138 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau858b1032015-12-07 17:04:59 +01005139 be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01005140 if (unlikely(s->srv_conn))
5141 sess_change_server(s, NULL);
5142 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005143
5144 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02005145 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005146
Willy Tarreaueee5b512015-04-03 23:46:31 +02005147 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005148 int n;
5149
Willy Tarreaueee5b512015-04-03 23:46:31 +02005150 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005151 if (n < 1 || n > 5)
5152 n = 0;
5153
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005154 if (fe->mode == PR_MODE_HTTP) {
5155 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005156 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02005157 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01005158 (be->mode == PR_MODE_HTTP)) {
5159 be->be_counters.p.http.rsp[n]++;
5160 be->be_counters.p.http.cum_req++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005161 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005162 }
5163
5164 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005165 s->logs.bytes_in -= s->req.buf->i;
5166 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005167
5168 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005169 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005170 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005171 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005172 s->do_log(s);
5173 }
5174
Willy Tarreaud713bcc2014-06-25 15:36:04 +02005175 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02005176 stream_stop_content_counters(s);
5177 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02005178
Willy Tarreau610ecce2010-01-04 21:15:02 +01005179 s->logs.accept_date = date; /* user-visible date for logging */
5180 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02005181 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
5182 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005183 tv_zero(&s->logs.tv_request);
5184 s->logs.t_queue = -1;
5185 s->logs.t_connect = -1;
5186 s->logs.t_data = -1;
5187 s->logs.t_close = 0;
5188 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
5189 s->logs.srv_queue_size = 0; /* we will get this number soon */
5190
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005191 s->logs.bytes_in = s->req.total = s->req.buf->i;
5192 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005193
5194 if (s->pend_pos)
5195 pendconn_free(s->pend_pos);
5196
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005197 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005198 if (s->flags & SF_CURR_SESS) {
5199 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005200 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005201 }
Willy Tarreau858b1032015-12-07 17:04:59 +01005202 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005203 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01005204 }
5205
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005206 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005207
Willy Tarreau4213a112013-12-15 10:25:42 +01005208 /* only release our endpoint if we don't intend to reuse the
5209 * connection.
5210 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005211 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005212 !si_conn_ready(&s->si[1])) {
5213 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02005214 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005215 }
5216
Willy Tarreau350f4872014-11-28 14:42:25 +01005217 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
5218 s->si[1].err_type = SI_ET_NONE;
5219 s->si[1].conn_retries = 0; /* used for logging too */
5220 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02005221 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 +01005222 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);
5223 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 +02005224 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
5225 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
5226 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005227
Willy Tarreaueee5b512015-04-03 23:46:31 +02005228 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005229 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02005230 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005231
5232 if (prev_status == 401 || prev_status == 407) {
5233 /* In HTTP keep-alive mode, if we receive a 401, we still have
5234 * a chance of being able to send the visitor again to the same
5235 * server over the same connection. This is required by some
5236 * broken protocols such as NTLM, and anyway whenever there is
5237 * an opportunity for sending the challenge to the proper place,
5238 * it's better to do it (at least it helps with debugging).
5239 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005240 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02005241 if (srv_conn)
5242 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01005243 }
5244
Willy Tarreau53f96852016-02-02 18:50:47 +01005245 /* Never ever allow to reuse a connection from a non-reuse backend */
5246 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
5247 srv_conn->flags |= CO_FL_PRIVATE;
5248
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005249 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005250 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005251
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005252 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005253 s->req.flags |= CF_NEVER_WAIT;
5254 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005255 }
5256
Willy Tarreau610ecce2010-01-04 21:15:02 +01005257 /* if the request buffer is not empty, it means we're
5258 * about to process another request, so send pending
5259 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005260 * Just don't do this if the buffer is close to be full,
5261 * because the request will wait for it to flush a little
5262 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005263 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005264 if (s->req.buf->i) {
5265 if (s->res.buf->o &&
5266 !buffer_full(s->res.buf, global.tune.maxrewrite) &&
5267 bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)
5268 s->res.flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005269 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005270
Willy Tarreau714ea782015-11-25 20:11:11 +01005271 /* we're removing the analysers, we MUST re-enable events detection.
5272 * We don't enable close on the response channel since it's either
5273 * already closed, or in keep-alive with an idle connection handler.
5274 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005275 channel_auto_read(&s->req);
5276 channel_auto_close(&s->req);
5277 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005278
Willy Tarreau1c59bd52015-11-02 20:20:11 +01005279 /* we're in keep-alive with an idle connection, monitor it if not already done */
5280 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02005281 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005282 if (!srv)
5283 si_idle_conn(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01005284 else if (srv_conn->flags & CO_FL_PRIVATE)
Willy Tarreau8dff9982015-08-04 20:45:52 +02005285 si_idle_conn(&s->si[1], &srv->priv_conns);
Willy Tarreau449d74a2015-08-05 17:16:33 +02005286 else if (prev_flags & TX_NOT_FIRST)
5287 /* note: we check the request, not the connection, but
5288 * this is valid for strategies SAFE and AGGR, and in
5289 * case of ALWS, we don't care anyway.
5290 */
5291 si_idle_conn(&s->si[1], &srv->safe_conns);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005292 else
5293 si_idle_conn(&s->si[1], &srv->idle_conns);
Willy Tarreau4320eaa2015-08-05 11:08:30 +02005294 }
Willy Tarreau27375622013-12-17 00:00:28 +01005295
Christopher Faulet3e344292015-11-24 16:24:13 +01005296 if (HAS_FILTERS(s)) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01005297 s->req.analysers &= AN_REQ_FLT_END;
5298 s->res.analysers &= AN_RES_FLT_END;
Christopher Fauletd7c91962015-04-30 11:48:27 +02005299 }
Christopher Faulet3e344292015-11-24 16:24:13 +01005300 else {
5301 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
5302 s->res.analysers = 0;
5303 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005304}
5305
5306
5307/* This function updates the request state machine according to the response
5308 * state machine and buffer flags. It returns 1 if it changes anything (flag
5309 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5310 * it is only used to find when a request/response couple is complete. Both
5311 * this function and its equivalent should loop until both return zero. It
5312 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5313 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005314int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005315{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005316 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005317 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005318 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005319 unsigned int old_state = txn->req.msg_state;
5320
Willy Tarreau610ecce2010-01-04 21:15:02 +01005321 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5322 return 0;
5323
5324 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005325 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005326 * We can shut the read side unless we want to abort_on_close,
5327 * or we have a POST request. The issue with POST requests is
5328 * that some browsers still send a CRLF after the request, and
5329 * this CRLF must be read so that it does not remain in the kernel
5330 * buffers, otherwise a close could cause an RST on some systems
5331 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005332 * Note that if we're using keep-alive on the client side, we'd
5333 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005334 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005335 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005336 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005337 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5338 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5339 !(s->be->options & PR_O_ABRT_CLOSE) &&
5340 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005341 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005342
Willy Tarreau40f151a2012-12-20 12:10:09 +01005343 /* if the server closes the connection, we want to immediately react
5344 * and close the socket to save packets and syscalls.
5345 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005346 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005347
Willy Tarreau7f876a12015-11-18 11:59:55 +01005348 /* In any case we've finished parsing the request so we must
5349 * disable Nagle when sending data because 1) we're not going
5350 * to shut this side, and 2) the server is waiting for us to
5351 * send pending data.
5352 */
5353 chn->flags |= CF_NEVER_WAIT;
5354
Willy Tarreau610ecce2010-01-04 21:15:02 +01005355 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5356 goto wait_other_side;
5357
5358 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5359 /* The server has not finished to respond, so we
5360 * don't want to move in order not to upset it.
5361 */
5362 goto wait_other_side;
5363 }
5364
5365 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5366 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005367 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005368 txn->req.msg_state = HTTP_MSG_TUNNEL;
5369 goto wait_other_side;
5370 }
5371
5372 /* When we get here, it means that both the request and the
5373 * response have finished receiving. Depending on the connection
5374 * mode, we'll have to wait for the last bytes to leave in either
5375 * direction, and sometimes for a close to be effective.
5376 */
5377
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005378 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5379 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005380 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5381 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005382 }
5383 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5384 /* Option forceclose is set, or either side wants to close,
5385 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005386 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005387 * once both states are CLOSED.
5388 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005389 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5390 channel_shutr_now(chn);
5391 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005392 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005393 }
5394 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005395 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5396 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005397 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005398 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5399 channel_auto_read(chn);
5400 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005401 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005402 }
5403
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005404 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005405 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005406 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005407
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005408 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005409 txn->req.msg_state = HTTP_MSG_CLOSING;
5410 goto http_msg_closing;
5411 }
5412 else {
5413 txn->req.msg_state = HTTP_MSG_CLOSED;
5414 goto http_msg_closed;
5415 }
5416 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005417 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005418 }
5419
5420 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5421 http_msg_closing:
5422 /* nothing else to forward, just waiting for the output buffer
5423 * to be empty and for the shutw_now to take effect.
5424 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005425 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005426 txn->req.msg_state = HTTP_MSG_CLOSED;
5427 goto http_msg_closed;
5428 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005429 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005430 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005431 txn->req.msg_state = HTTP_MSG_ERROR;
5432 goto wait_other_side;
5433 }
5434 }
5435
5436 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5437 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005438 /* see above in MSG_DONE why we only do this in these states */
5439 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5440 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5441 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005442 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005443 goto wait_other_side;
5444 }
5445
5446 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005447 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005448}
5449
5450
5451/* This function updates the response state machine according to the request
5452 * state machine and buffer flags. It returns 1 if it changes anything (flag
5453 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5454 * it is only used to find when a request/response couple is complete. Both
5455 * this function and its equivalent should loop until both return zero. It
5456 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5457 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005458int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005459{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005460 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005461 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005462 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005463 unsigned int old_state = txn->rsp.msg_state;
5464
Willy Tarreau610ecce2010-01-04 21:15:02 +01005465 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5466 return 0;
5467
5468 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5469 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005470 * still monitor the server connection for a possible close
5471 * while the request is being uploaded, so we don't disable
5472 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005473 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005474 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005475
5476 if (txn->req.msg_state == HTTP_MSG_ERROR)
5477 goto wait_other_side;
5478
5479 if (txn->req.msg_state < HTTP_MSG_DONE) {
5480 /* The client seems to still be sending data, probably
5481 * because we got an error response during an upload.
5482 * We have the choice of either breaking the connection
5483 * or letting it pass through. Let's do the later.
5484 */
5485 goto wait_other_side;
5486 }
5487
5488 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5489 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005490 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005491 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005492 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005493 goto wait_other_side;
5494 }
5495
5496 /* When we get here, it means that both the request and the
5497 * response have finished receiving. Depending on the connection
5498 * mode, we'll have to wait for the last bytes to leave in either
5499 * direction, and sometimes for a close to be effective.
5500 */
5501
5502 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5503 /* Server-close mode : shut read and wait for the request
5504 * side to close its output buffer. The caller will detect
5505 * when we're in DONE and the other is in CLOSED and will
5506 * catch that for the final cleanup.
5507 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005508 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5509 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005510 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005511 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5512 /* Option forceclose is set, or either side wants to close,
5513 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005514 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005515 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005516 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005517 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5518 channel_shutr_now(chn);
5519 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005520 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005521 }
5522 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005523 /* The last possible modes are keep-alive and tunnel. Tunnel will
5524 * need to forward remaining data. Keep-alive will need to monitor
5525 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005526 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005527 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005528 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005529 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5530 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005531 }
5532
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005533 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005534 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005535 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005536 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5537 goto http_msg_closing;
5538 }
5539 else {
5540 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5541 goto http_msg_closed;
5542 }
5543 }
5544 goto wait_other_side;
5545 }
5546
5547 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5548 http_msg_closing:
5549 /* nothing else to forward, just waiting for the output buffer
5550 * to be empty and for the shutw_now to take effect.
5551 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005552 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005553 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5554 goto http_msg_closed;
5555 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005556 else if (chn->flags & CF_SHUTW) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005557 txn->req.err_state = txn->req.msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005558 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005559 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005560 if (objt_server(s->target))
5561 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005562 goto wait_other_side;
5563 }
5564 }
5565
5566 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5567 http_msg_closed:
5568 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005569 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005570 channel_auto_close(chn);
5571 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005572 goto wait_other_side;
5573 }
5574
5575 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005576 /* We force the response to leave immediately if we're waiting for the
5577 * other side, since there is no pending shutdown to push it out.
5578 */
5579 if (!channel_is_empty(chn))
5580 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005581 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005582}
5583
5584
5585/* Resync the request and response state machines. Return 1 if either state
5586 * changes.
5587 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005588int http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005589{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005590 struct http_txn *txn = s->txn;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005591 int old_req_state = txn->req.msg_state;
5592 int old_res_state = txn->rsp.msg_state;
5593
Willy Tarreau610ecce2010-01-04 21:15:02 +01005594 http_sync_req_state(s);
5595 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005596 if (!http_sync_res_state(s))
5597 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005598 if (!http_sync_req_state(s))
5599 break;
5600 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005601
Willy Tarreau610ecce2010-01-04 21:15:02 +01005602 /* OK, both state machines agree on a compatible state.
5603 * There are a few cases we're interested in :
5604 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5605 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5606 * directions, so let's simply disable both analysers.
5607 * - HTTP_MSG_CLOSED on the response only means we must abort the
5608 * request.
5609 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5610 * with server-close mode means we've completed one request and we
5611 * must re-initialize the server connection.
5612 */
5613
5614 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5615 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5616 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5617 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01005618 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005619 channel_auto_close(&s->req);
5620 channel_auto_read(&s->req);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005621 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005622 channel_auto_close(&s->res);
5623 channel_auto_read(&s->res);
Christopher Faulet6962f4e2016-11-28 14:21:27 +01005624 if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s))
Christopher Faulet0184ea72017-01-05 14:06:34 +01005625 s->req.analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet6962f4e2016-11-28 14:21:27 +01005626 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s))
Christopher Faulet0184ea72017-01-05 14:06:34 +01005627 s->res.analysers |= AN_RES_FLT_XFER_DATA;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005628 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005629 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005630 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005631 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005632 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01005633 s->res.analysers &= AN_RES_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005634 channel_auto_close(&s->res);
5635 channel_auto_read(&s->res);
Christopher Faulet0184ea72017-01-05 14:06:34 +01005636 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005637 channel_abort(&s->req);
5638 channel_auto_close(&s->req);
5639 channel_auto_read(&s->req);
5640 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005641 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005642 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5643 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005644 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005645 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5646 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5647 /* server-close/keep-alive: terminate this transaction,
5648 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02005649 * a fresh-new transaction, but only once we're sure there's
5650 * enough room in the request and response buffer to process
5651 * another request. The request buffer must not hold any
5652 * pending output data and the request buffer must not have
5653 * output data occupying the reserve.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005654 */
Willy Tarreau3de5bd62016-05-02 16:07:07 +02005655 if (s->req.buf->o)
5656 s->req.flags |= CF_WAKE_WRITE;
5657 else if (channel_congested(&s->res))
5658 s->res.flags |= CF_WAKE_WRITE;
5659 else
5660 http_end_txn_clean_session(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005661 }
5662
Willy Tarreau610ecce2010-01-04 21:15:02 +01005663 return txn->req.msg_state != old_req_state ||
5664 txn->rsp.msg_state != old_res_state;
5665}
5666
Willy Tarreaud98cf932009-12-27 22:54:55 +01005667/* This function is an analyser which forwards request body (including chunk
5668 * sizes if any). It is called as soon as we must forward, even if we forward
5669 * zero byte. The only situation where it must not be called is when we're in
5670 * tunnel mode and we want to forward till the close. It's used both to forward
5671 * remaining data and to resync after end of body. It expects the msg_state to
5672 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005673 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005674 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005675 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005676 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005677int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005678{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005679 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005680 struct http_txn *txn = s->txn;
5681 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01005682 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005683
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005684 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5685 return 0;
5686
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005687 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005688 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005689 /* Output closed while we were sending data. We must abort and
5690 * wake the other side up.
5691 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005692 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005693 msg->msg_state = HTTP_MSG_ERROR;
5694 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005695 return 1;
5696 }
5697
Willy Tarreaud98cf932009-12-27 22:54:55 +01005698 /* Note that we don't have to send 100-continue back because we don't
5699 * need the data to complete our job, and it's up to the server to
5700 * decide whether to return 100, 417 or anything else in return of
5701 * an "Expect: 100-continue" header.
5702 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02005703 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005704 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5705 ? HTTP_MSG_CHUNK_SIZE
5706 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005707
5708 /* TODO/filters: when http-buffer-request option is set or if a
5709 * rule on url_param exists, the first chunk size could be
5710 * already parsed. In that case, msg->next is after the chunk
5711 * size (including the CRLF after the size). So this case should
5712 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005713 }
5714
Willy Tarreau7ba23542014-04-17 21:50:00 +02005715 /* Some post-connect processing might want us to refrain from starting to
5716 * forward data. Currently, the only reason for this is "balance url_param"
5717 * whichs need to parse/process the request after we've enabled forwarding.
5718 */
5719 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005720 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005721 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005722 req->flags |= CF_WAKE_CONNECT;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005723 goto missing_data_or_waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005724 }
5725 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5726 }
5727
Willy Tarreau80a92c02014-03-12 10:41:13 +01005728 /* in most states, we should abort in case of early close */
5729 channel_auto_close(req);
5730
Willy Tarreauefdf0942014-04-24 20:08:57 +02005731 if (req->to_forward) {
5732 /* We can't process the buffer's contents yet */
5733 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005734 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005735 }
5736
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005737 if (msg->msg_state < HTTP_MSG_DONE) {
5738 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5739 ? http_msg_forward_chunked_body(s, msg)
5740 : http_msg_forward_body(s, msg));
5741 if (!ret)
5742 goto missing_data_or_waiting;
5743 if (ret < 0)
5744 goto return_bad_req;
5745 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005746
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005747 /* other states, DONE...TUNNEL */
5748 /* we don't want to forward closes on DONE except in tunnel mode. */
5749 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5750 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005751
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005752 if (http_resync_states(s)) {
5753 /* some state changes occurred, maybe the analyser
5754 * was disabled too. */
5755 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5756 if (req->flags & CF_SHUTW) {
5757 /* request errors are most likely due to the
5758 * server aborting the transfer. */
5759 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005760 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005761 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005762 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->err_state, s->be);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005763 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005764 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005765 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005766 }
5767
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005768 /* If "option abortonclose" is set on the backend, we want to monitor
5769 * the client's connection and forward any shutdown notification to the
5770 * server, which will decide whether to close or to go on processing the
5771 * request. We only do that in tunnel mode, and not in other modes since
5772 * it can be abused to exhaust source ports. */
5773 if (s->be->options & PR_O_ABRT_CLOSE) {
5774 channel_auto_read(req);
5775 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
5776 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
5777 s->si[1].flags |= SI_FL_NOLINGER;
5778 channel_auto_close(req);
5779 }
5780 else if (s->txn->meth == HTTP_METH_POST) {
5781 /* POST requests may require to read extra CRLF sent by broken
5782 * browsers and which could cause an RST to be sent upon close
5783 * on some systems (eg: Linux). */
5784 channel_auto_read(req);
5785 }
5786 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005787
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005788 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005789 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005790 if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005791 if (!(s->flags & SF_ERR_MASK))
5792 s->flags |= SF_ERR_CLICL;
5793 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005794 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005795 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005796 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005797 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005798 }
5799
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005800 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005801 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005802 if (objt_server(s->target))
5803 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005804
5805 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005806 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005807
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005808 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005809 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005810 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005811
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005812 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005813 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005814 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005815 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005816 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005817
Willy Tarreau5c620922011-05-11 19:56:11 +02005818 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005819 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005820 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005821 * modes are already handled by the stream sock layer. We must not do
5822 * this in content-length mode because it could present the MSG_MORE
5823 * flag with the last block of forwarded data, which would cause an
5824 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005825 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005826 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005827 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005828
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005829 return 0;
5830
Willy Tarreaud98cf932009-12-27 22:54:55 +01005831 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005832 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005833 if (sess->listener->counters)
5834 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005835
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005836 return_bad_req_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005837 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005838 txn->req.msg_state = HTTP_MSG_ERROR;
5839 if (txn->status) {
5840 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005841 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005842 } else {
5843 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005844 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005845 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01005846 req->analysers &= AN_REQ_FLT_END;
5847 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 +01005848
Willy Tarreaue7dff022015-04-03 01:14:29 +02005849 if (!(s->flags & SF_ERR_MASK))
5850 s->flags |= SF_ERR_PRXCOND;
5851 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005852 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005853 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005854 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005855 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005856 }
5857 return 0;
5858
5859 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005860 txn->req.err_state = txn->req.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005861 txn->req.msg_state = HTTP_MSG_ERROR;
5862 if (txn->status) {
5863 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005864 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005865 } else {
5866 txn->status = 502;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005867 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005868 }
Christopher Faulet0184ea72017-01-05 14:06:34 +01005869 req->analysers &= AN_REQ_FLT_END;
5870 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 +01005871
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005872 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005873 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005874 if (objt_server(s->target))
5875 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005876
Willy Tarreaue7dff022015-04-03 01:14:29 +02005877 if (!(s->flags & SF_ERR_MASK))
5878 s->flags |= SF_ERR_SRVCL;
5879 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005880 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005881 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005882 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005883 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005884 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005885 return 0;
5886}
5887
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005888/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5889 * processing can continue on next analysers, or zero if it either needs more
5890 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005891 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005892 * when it has nothing left to do, and may remove any analyser when it wants to
5893 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005894 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005895int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005896{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005897 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005898 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005899 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005900 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005901 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005902 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005903 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005904
Willy Tarreau87b09662015-04-03 00:22:06 +02005905 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 +02005906 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005907 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005908 rep,
5909 rep->rex, rep->wex,
5910 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005911 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005912 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005913
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005914 /*
5915 * Now parse the partial (or complete) lines.
5916 * We will check the response syntax, and also join multi-line
5917 * headers. An index of all the lines will be elaborated while
5918 * parsing.
5919 *
5920 * For the parsing, we use a 28 states FSM.
5921 *
5922 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005923 * rep->buf->p = beginning of response
5924 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5925 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005926 * msg->eol = end of current header or line (LF or CRLF)
5927 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005928 */
5929
Willy Tarreau628c40c2014-04-24 19:11:26 +02005930 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005931 /* There's a protected area at the end of the buffer for rewriting
5932 * purposes. We don't want to start to parse the request if the
5933 * protected area is affected, because we may have to move processed
5934 * data later, which is much more complicated.
5935 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005936 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005937 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005938 /* some data has still not left the buffer, wake us once that's done */
5939 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5940 goto abort_response;
5941 channel_dont_close(rep);
5942 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005943 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005944 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005945 }
5946
Willy Tarreau379357a2013-06-08 12:55:46 +02005947 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5948 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5949 buffer_slow_realign(rep->buf);
5950
Willy Tarreau9b28e032012-10-12 23:49:43 +02005951 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005952 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005953 }
5954
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005955 /* 1: we might have to print this header in debug mode */
5956 if (unlikely((global.mode & MODE_DEBUG) &&
5957 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005958 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005959 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005960
Willy Tarreau9b28e032012-10-12 23:49:43 +02005961 sol = rep->buf->p;
5962 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005963 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005964
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005965 sol += hdr_idx_first_pos(&txn->hdr_idx);
5966 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005967
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005968 while (cur_idx) {
5969 eol = sol + txn->hdr_idx.v[cur_idx].len;
5970 debug_hdr("srvhdr", s, sol, eol);
5971 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5972 cur_idx = txn->hdr_idx.v[cur_idx].next;
5973 }
5974 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005975
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005976 /*
5977 * Now we quickly check if we have found a full valid response.
5978 * If not so, we check the FD and buffer states before leaving.
5979 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005980 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005981 * responses are checked first.
5982 *
5983 * Depending on whether the client is still there or not, we
5984 * may send an error response back or not. Note that normally
5985 * we should only check for HTTP status there, and check I/O
5986 * errors somewhere else.
5987 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005988
Willy Tarreau655dce92009-11-08 13:10:58 +01005989 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005990 /* Invalid response */
5991 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5992 /* we detected a parsing error. We want to archive this response
5993 * in the dedicated proxy area for later troubleshooting.
5994 */
5995 hdr_response_bad:
5996 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01005997 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005998
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005999 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006000 if (objt_server(s->target)) {
6001 objt_server(s->target)->counters.failed_resp++;
6002 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006003 }
Willy Tarreau64648412010-03-05 10:41:54 +01006004 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006005 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006006 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006007 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006008 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006009 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006010 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006011
Willy Tarreaue7dff022015-04-03 01:14:29 +02006012 if (!(s->flags & SF_ERR_MASK))
6013 s->flags |= SF_ERR_PRXCOND;
6014 if (!(s->flags & SF_FINST_MASK))
6015 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006016
6017 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006018 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006019
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006020 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006021 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02006022 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02006023 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006024 goto hdr_response_bad;
6025 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006026
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006027 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006028 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006029 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006030 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006031 else if (txn->flags & TX_NOT_FIRST)
6032 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02006033
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006034 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006035 if (objt_server(s->target)) {
6036 objt_server(s->target)->counters.failed_resp++;
6037 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006038 }
Willy Tarreau461f6622008-08-15 23:43:19 +02006039
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006040 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006041 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006042 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006043 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006044 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006045 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02006046
Willy Tarreaue7dff022015-04-03 01:14:29 +02006047 if (!(s->flags & SF_ERR_MASK))
6048 s->flags |= SF_ERR_SRVCL;
6049 if (!(s->flags & SF_FINST_MASK))
6050 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02006051 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006052 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006053
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02006054 /* read timeout : return a 504 to the client. */
6055 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006056 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006057 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006058
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006059 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006060 if (objt_server(s->target)) {
6061 objt_server(s->target)->counters.failed_resp++;
6062 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006063 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006064
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006065 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006066 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006067 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01006068 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006069 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006070 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02006071
Willy Tarreaue7dff022015-04-03 01:14:29 +02006072 if (!(s->flags & SF_ERR_MASK))
6073 s->flags |= SF_ERR_SRVTO;
6074 if (!(s->flags & SF_FINST_MASK))
6075 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006076 return 0;
6077 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02006078
Willy Tarreauf003d372012-11-26 13:35:37 +01006079 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006080 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006081 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01006082 s->be->be_counters.cli_aborts++;
6083 if (objt_server(s->target))
6084 objt_server(s->target)->counters.cli_aborts++;
6085
Christopher Faulet0184ea72017-01-05 14:06:34 +01006086 rep->analysers &= AN_RES_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01006087 channel_auto_close(rep);
6088
6089 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01006090 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006091 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreauf003d372012-11-26 13:35:37 +01006092
Willy Tarreaue7dff022015-04-03 01:14:29 +02006093 if (!(s->flags & SF_ERR_MASK))
6094 s->flags |= SF_ERR_CLICL;
6095 if (!(s->flags & SF_FINST_MASK))
6096 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01006097
Willy Tarreau87b09662015-04-03 00:22:06 +02006098 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01006099 return 0;
6100 }
6101
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006102 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006103 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006104 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006105 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006106 else if (txn->flags & TX_NOT_FIRST)
6107 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006108
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006109 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006110 if (objt_server(s->target)) {
6111 objt_server(s->target)->counters.failed_resp++;
6112 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006113 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006114
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006115 channel_auto_close(rep);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006116 rep->analysers &= AN_RES_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006117 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006118 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006119 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006120 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01006121
Willy Tarreaue7dff022015-04-03 01:14:29 +02006122 if (!(s->flags & SF_ERR_MASK))
6123 s->flags |= SF_ERR_SRVCL;
6124 if (!(s->flags & SF_FINST_MASK))
6125 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006126 return 0;
6127 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006128
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006129 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006130 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006131 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006132 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006133 else if (txn->flags & TX_NOT_FIRST)
6134 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006135
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006136 s->be->be_counters.failed_resp++;
Christopher Faulet0184ea72017-01-05 14:06:34 +01006137 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006138 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006139
Willy Tarreaue7dff022015-04-03 01:14:29 +02006140 if (!(s->flags & SF_ERR_MASK))
6141 s->flags |= SF_ERR_CLICL;
6142 if (!(s->flags & SF_FINST_MASK))
6143 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006144
Willy Tarreau87b09662015-04-03 00:22:06 +02006145 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006146 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006147 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006148
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006149 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01006150 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006151 return 0;
6152 }
6153
6154 /* More interesting part now : we know that we have a complete
6155 * response which at least looks like HTTP. We have an indicator
6156 * of each header's length, so we can parse them quickly.
6157 */
6158
6159 if (unlikely(msg->err_pos >= 0))
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006160 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006161
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006162 /*
6163 * 1: get the status code
6164 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006165 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006166 if (n < 1 || n > 5)
6167 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02006168 /* when the client triggers a 4xx from the server, it's most often due
6169 * to a missing object or permission. These events should be tracked
6170 * because if they happen often, it may indicate a brute force or a
6171 * vulnerability scan.
6172 */
6173 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02006174 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02006175
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006176 if (objt_server(s->target))
6177 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006178
Willy Tarreau91852eb2015-05-01 13:26:00 +02006179 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
6180 * exactly one digit "." one digit. This check may be disabled using
6181 * option accept-invalid-http-response.
6182 */
6183 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
6184 if (msg->sl.st.v_l != 8) {
6185 msg->err_pos = 0;
6186 goto hdr_response_bad;
6187 }
6188
6189 if (rep->buf->p[4] != '/' ||
6190 !isdigit((unsigned char)rep->buf->p[5]) ||
6191 rep->buf->p[6] != '.' ||
6192 !isdigit((unsigned char)rep->buf->p[7])) {
6193 msg->err_pos = 4;
6194 goto hdr_response_bad;
6195 }
6196 }
6197
Willy Tarreau5b154472009-12-21 20:11:07 +01006198 /* check if the response is HTTP/1.1 or above */
6199 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006200 ((rep->buf->p[5] > '1') ||
6201 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006202 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006203
6204 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006205 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 +01006206
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006207 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006208 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006209
Willy Tarreau9b28e032012-10-12 23:49:43 +02006210 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006211
Willy Tarreau39650402010-03-15 19:44:39 +01006212 /* Adjust server's health based on status code. Note: status codes 501
6213 * and 505 are triggered on demand by client request, so we must not
6214 * count them as server failures.
6215 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006216 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006217 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006218 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006219 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006220 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006221 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006222
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006223 /*
6224 * 2: check for cacheability.
6225 */
6226
6227 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006228 case 100:
6229 /*
6230 * We may be facing a 100-continue response, in which case this
6231 * is not the right response, and we're waiting for the next one.
6232 * Let's allow this response to go to the client and wait for the
6233 * next one.
6234 */
6235 hdr_idx_init(&txn->hdr_idx);
6236 msg->next -= channel_forward(rep, msg->next);
6237 msg->msg_state = HTTP_MSG_RPBEFORE;
6238 txn->status = 0;
6239 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01006240 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02006241 goto next_one;
6242
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006243 case 200:
6244 case 203:
6245 case 206:
6246 case 300:
6247 case 301:
6248 case 410:
6249 /* RFC2616 @13.4:
6250 * "A response received with a status code of
6251 * 200, 203, 206, 300, 301 or 410 MAY be stored
6252 * by a cache (...) unless a cache-control
6253 * directive prohibits caching."
6254 *
6255 * RFC2616 @9.5: POST method :
6256 * "Responses to this method are not cacheable,
6257 * unless the response includes appropriate
6258 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006259 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006260 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006261 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006262 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6263 break;
6264 default:
6265 break;
6266 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006267
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006268 /*
6269 * 3: we may need to capture headers
6270 */
6271 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006272 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006273 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006274 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006275
Willy Tarreau557f1992015-05-01 10:05:17 +02006276 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6277 * by RFC7230#3.3.3 :
6278 *
6279 * The length of a message body is determined by one of the following
6280 * (in order of precedence):
6281 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006282 * 1. Any 2xx (Successful) response to a CONNECT request implies that
6283 * the connection will become a tunnel immediately after the empty
6284 * line that concludes the header fields. A client MUST ignore
6285 * any Content-Length or Transfer-Encoding header fields received
6286 * in such a message. Any 101 response (Switching Protocols) is
6287 * managed in the same manner.
6288 *
6289 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02006290 * (Informational), 204 (No Content), or 304 (Not Modified) status
6291 * code is always terminated by the first empty line after the
6292 * header fields, regardless of the header fields present in the
6293 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006294 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006295 * 3. If a Transfer-Encoding header field is present and the chunked
6296 * transfer coding (Section 4.1) is the final encoding, the message
6297 * body length is determined by reading and decoding the chunked
6298 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006299 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006300 * If a Transfer-Encoding header field is present in a response and
6301 * the chunked transfer coding is not the final encoding, the
6302 * message body length is determined by reading the connection until
6303 * it is closed by the server. If a Transfer-Encoding header field
6304 * is present in a request and the chunked transfer coding is not
6305 * the final encoding, the message body length cannot be determined
6306 * reliably; the server MUST respond with the 400 (Bad Request)
6307 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006308 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006309 * If a message is received with both a Transfer-Encoding and a
6310 * Content-Length header field, the Transfer-Encoding overrides the
6311 * Content-Length. Such a message might indicate an attempt to
6312 * perform request smuggling (Section 9.5) or response splitting
6313 * (Section 9.4) and ought to be handled as an error. A sender MUST
6314 * remove the received Content-Length field prior to forwarding such
6315 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006316 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006317 * 4. If a message is received without Transfer-Encoding and with
6318 * either multiple Content-Length header fields having differing
6319 * field-values or a single Content-Length header field having an
6320 * invalid value, then the message framing is invalid and the
6321 * recipient MUST treat it as an unrecoverable error. If this is a
6322 * request message, the server MUST respond with a 400 (Bad Request)
6323 * status code and then close the connection. If this is a response
6324 * message received by a proxy, the proxy MUST close the connection
6325 * to the server, discard the received response, and send a 502 (Bad
6326 * Gateway) response to the client. If this is a response message
6327 * received by a user agent, the user agent MUST close the
6328 * connection to the server and discard the received response.
6329 *
6330 * 5. If a valid Content-Length header field is present without
6331 * Transfer-Encoding, its decimal value defines the expected message
6332 * body length in octets. If the sender closes the connection or
6333 * the recipient times out before the indicated number of octets are
6334 * received, the recipient MUST consider the message to be
6335 * incomplete and close the connection.
6336 *
6337 * 6. If this is a request message and none of the above are true, then
6338 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006339 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006340 * 7. Otherwise, this is a response message without a declared message
6341 * body length, so the message body length is determined by the
6342 * number of octets received prior to the server closing the
6343 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006344 */
6345
6346 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006347 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006348 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006349 * FIXME: should we parse anyway and return an error on chunked encoding ?
6350 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006351 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6352 txn->status == 101)) {
6353 /* Either we've established an explicit tunnel, or we're
6354 * switching the protocol. In both cases, we're very unlikely
6355 * to understand the next protocols. We have to switch to tunnel
6356 * mode, so that we transfer the request and responses then let
6357 * this protocol pass unmodified. When we later implement specific
6358 * parsers for such protocols, we'll want to check the Upgrade
6359 * header which contains information about that protocol for
6360 * responses with status 101 (eg: see RFC2817 about TLS).
6361 */
6362 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6363 msg->flags |= HTTP_MSGF_XFER_LEN;
6364 goto end;
6365 }
6366
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006367 if (txn->meth == HTTP_METH_HEAD ||
6368 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006369 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006370 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006371 goto skip_content_length;
6372 }
6373
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006374 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006375 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02006376 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006377 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006378 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6379 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006380 /* bad transfer-encoding (chunked followed by something else) */
6381 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006382 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006383 break;
6384 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006385 }
6386
Willy Tarreau1c913912015-04-30 10:57:51 +02006387 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006388 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006389 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02006390 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6391 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6392 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006393 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006394 signed long long cl;
6395
Willy Tarreauad14f752011-09-02 20:33:27 +02006396 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006397 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006398 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006399 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006400
Willy Tarreauad14f752011-09-02 20:33:27 +02006401 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006402 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006403 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006404 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006405
Willy Tarreauad14f752011-09-02 20:33:27 +02006406 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006407 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006408 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006409 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006410
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006411 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006412 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006413 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006414 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006415
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006416 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006417 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006418 }
6419
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006420 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006421 /* Now we have to check if we need to modify the Connection header.
6422 * This is more difficult on the response than it is on the request,
6423 * because we can have two different HTTP versions and we don't know
6424 * how the client will interprete a response. For instance, let's say
6425 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6426 * HTTP/1.1 response without any header. Maybe it will bound itself to
6427 * HTTP/1.0 because it only knows about it, and will consider the lack
6428 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6429 * the lack of header as a keep-alive. Thus we will use two flags
6430 * indicating how a request MAY be understood by the client. In case
6431 * of multiple possibilities, we'll fix the header to be explicit. If
6432 * ambiguous cases such as both close and keepalive are seen, then we
6433 * will fall back to explicit close. Note that we won't take risks with
6434 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006435 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006436 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006437 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6438 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
6439 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6440 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006441 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006442
Willy Tarreau70dffda2014-01-30 03:07:23 +01006443 /* this situation happens when combining pretend-keepalive with httpclose. */
6444 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006445 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006446 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006447 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6448
Willy Tarreau60466522010-01-18 19:08:45 +01006449 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006450 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006451 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6452 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006453
Willy Tarreau60466522010-01-18 19:08:45 +01006454 /* now adjust header transformations depending on current state */
6455 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6456 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6457 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006458 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006459 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006460 }
Willy Tarreau60466522010-01-18 19:08:45 +01006461 else { /* SCL / KAL */
6462 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006463 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006464 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006465 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006466
Willy Tarreau60466522010-01-18 19:08:45 +01006467 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006468 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006469
Willy Tarreau60466522010-01-18 19:08:45 +01006470 /* Some keep-alive responses are converted to Server-close if
6471 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006472 */
Willy Tarreau60466522010-01-18 19:08:45 +01006473 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6474 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006475 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006476 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006477 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006478 }
6479
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006480 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02006481 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006482 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006483
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006484 /* end of job, return OK */
6485 rep->analysers &= ~an_bit;
6486 rep->analyse_exp = TICK_ETERNITY;
6487 channel_auto_close(rep);
6488 return 1;
6489
6490 abort_keep_alive:
6491 /* A keep-alive request to the server failed on a network error.
6492 * The client is required to retry. We need to close without returning
6493 * any other information so that the client retries.
6494 */
6495 txn->status = 0;
Christopher Faulet0184ea72017-01-05 14:06:34 +01006496 rep->analysers &= AN_RES_FLT_END;
6497 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006498 channel_auto_close(rep);
6499 s->logs.logwait = 0;
6500 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006501 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006502 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006503 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006504 return 0;
6505}
6506
6507/* This function performs all the processing enabled for the current response.
6508 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006509 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006510 * other functions. It works like process_request (see indications above).
6511 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006512int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006513{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006514 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006515 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006516 struct http_msg *msg = &txn->rsp;
6517 struct proxy *cur_proxy;
6518 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006519 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006520
Willy Tarreau87b09662015-04-03 00:22:06 +02006521 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 +02006522 now_ms, __FUNCTION__,
6523 s,
6524 rep,
6525 rep->rex, rep->wex,
6526 rep->flags,
6527 rep->buf->i,
6528 rep->analysers);
6529
6530 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6531 return 0;
6532
Willy Tarreau70730dd2014-04-24 18:06:27 +02006533 /* The stats applet needs to adjust the Connection header but we don't
6534 * apply any filter there.
6535 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006536 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6537 rep->analysers &= ~an_bit;
6538 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006539 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006540 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006541
Willy Tarreau58975672014-04-24 21:13:57 +02006542 /*
6543 * We will have to evaluate the filters.
6544 * As opposed to version 1.2, now they will be evaluated in the
6545 * filters order and not in the header order. This means that
6546 * each filter has to be validated among all headers.
6547 *
6548 * Filters are tried with ->be first, then with ->fe if it is
6549 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006550 *
6551 * Maybe we are in resume condiion. In this case I choose the
6552 * "struct proxy" which contains the rule list matching the resume
6553 * pointer. If none of theses "struct proxy" match, I initialise
6554 * the process with the first one.
6555 *
6556 * In fact, I check only correspondance betwwen the current list
6557 * pointer and the ->fe rule list. If it doesn't match, I initialize
6558 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006559 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006560 if (s->current_rule_list == &sess->fe->http_res_rules)
6561 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006562 else
6563 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006564 while (1) {
6565 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006566
Willy Tarreau58975672014-04-24 21:13:57 +02006567 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02006568 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02006569 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006570
Willy Tarreau51d861a2015-05-22 17:30:48 +02006571 if (ret == HTTP_RULE_RES_BADREQ)
6572 goto return_srv_prx_502;
6573
6574 if (ret == HTTP_RULE_RES_DONE) {
6575 rep->analysers &= ~an_bit;
6576 rep->analyse_exp = TICK_ETERNITY;
6577 return 1;
6578 }
6579 }
6580
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006581 /* we need to be called again. */
6582 if (ret == HTTP_RULE_RES_YIELD) {
6583 channel_dont_close(rep);
6584 return 0;
6585 }
6586
Willy Tarreau58975672014-04-24 21:13:57 +02006587 /* try headers filters */
6588 if (rule_set->rsp_exp != NULL) {
6589 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6590 return_bad_resp:
6591 if (objt_server(s->target)) {
6592 objt_server(s->target)->counters.failed_resp++;
6593 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006594 }
Willy Tarreau58975672014-04-24 21:13:57 +02006595 s->be->be_counters.failed_resp++;
6596 return_srv_prx_502:
Christopher Faulet0184ea72017-01-05 14:06:34 +01006597 rep->analysers &= AN_RES_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02006598 txn->status = 502;
6599 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006600 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006601 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006602 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006603 if (!(s->flags & SF_ERR_MASK))
6604 s->flags |= SF_ERR_PRXCOND;
6605 if (!(s->flags & SF_FINST_MASK))
6606 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006607 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006608 }
Willy Tarreau58975672014-04-24 21:13:57 +02006609 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006610
Willy Tarreau58975672014-04-24 21:13:57 +02006611 /* has the response been denied ? */
6612 if (txn->flags & TX_SVDENY) {
6613 if (objt_server(s->target))
6614 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006615
Willy Tarreau58975672014-04-24 21:13:57 +02006616 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006617 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006618 if (sess->listener->counters)
6619 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006620
Willy Tarreau58975672014-04-24 21:13:57 +02006621 goto return_srv_prx_502;
6622 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006623
Willy Tarreau58975672014-04-24 21:13:57 +02006624 /* add response headers from the rule sets in the same order */
6625 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006626 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006627 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006628 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006629 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02006630 ret = acl_pass(ret);
6631 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6632 ret = !ret;
6633 if (!ret)
6634 continue;
6635 }
6636 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6637 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006638 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006639
Willy Tarreau58975672014-04-24 21:13:57 +02006640 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006641 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006642 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006643 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006644 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006645
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006646 /* After this point, this anayzer can't return yield, so we can
6647 * remove the bit corresponding to this analyzer from the list.
6648 *
6649 * Note that the intermediate returns and goto found previously
6650 * reset the analyzers.
6651 */
6652 rep->analysers &= ~an_bit;
6653 rep->analyse_exp = TICK_ETERNITY;
6654
Willy Tarreau58975672014-04-24 21:13:57 +02006655 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006656 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006657 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006658
Willy Tarreau58975672014-04-24 21:13:57 +02006659 /*
6660 * Now check for a server cookie.
6661 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006662 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02006663 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006664
Willy Tarreau58975672014-04-24 21:13:57 +02006665 /*
6666 * Check for cache-control or pragma headers if required.
6667 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006668 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 +02006669 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006670
Willy Tarreau58975672014-04-24 21:13:57 +02006671 /*
6672 * Add server cookie in the response if needed
6673 */
6674 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6675 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006676 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006677 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6678 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6679 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6680 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6681 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006682 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006683 /* the server is known, it's not the one the client requested, or the
6684 * cookie's last seen date needs to be refreshed. We have to
6685 * insert a set-cookie here, except if we want to insert only on POST
6686 * requests and this one isn't. Note that servers which don't have cookies
6687 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006688 */
Willy Tarreau58975672014-04-24 21:13:57 +02006689 if (!objt_server(s->target)->cookie) {
6690 chunk_printf(&trash,
6691 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6692 s->be->cookie_name);
6693 }
6694 else {
6695 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006696
Willy Tarreau58975672014-04-24 21:13:57 +02006697 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6698 /* emit last_date, which is mandatory */
6699 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6700 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6701 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006702
Willy Tarreau58975672014-04-24 21:13:57 +02006703 if (s->be->cookie_maxlife) {
6704 /* emit first_date, which is either the original one or
6705 * the current date.
6706 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006707 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006708 s30tob64(txn->cookie_first_date ?
6709 txn->cookie_first_date >> 2 :
6710 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006711 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006712 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006713 }
Willy Tarreau58975672014-04-24 21:13:57 +02006714 chunk_appendf(&trash, "; path=/");
6715 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006716
Willy Tarreau58975672014-04-24 21:13:57 +02006717 if (s->be->cookie_domain)
6718 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006719
Willy Tarreau58975672014-04-24 21:13:57 +02006720 if (s->be->ck_opts & PR_CK_HTTPONLY)
6721 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006722
Willy Tarreau58975672014-04-24 21:13:57 +02006723 if (s->be->ck_opts & PR_CK_SECURE)
6724 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006725
Willy Tarreau58975672014-04-24 21:13:57 +02006726 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6727 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006728
Willy Tarreau58975672014-04-24 21:13:57 +02006729 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006730 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006731 /* the server did not change, only the date was updated */
6732 txn->flags |= TX_SCK_UPDATED;
6733 else
6734 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006735
Willy Tarreau58975672014-04-24 21:13:57 +02006736 /* Here, we will tell an eventual cache on the client side that we don't
6737 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6738 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6739 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006740 */
Willy Tarreau58975672014-04-24 21:13:57 +02006741 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006742
Willy Tarreau58975672014-04-24 21:13:57 +02006743 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006744
Willy Tarreau58975672014-04-24 21:13:57 +02006745 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6746 "Cache-control: private", 22) < 0))
6747 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006748 }
Willy Tarreau58975672014-04-24 21:13:57 +02006749 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006750
Willy Tarreau58975672014-04-24 21:13:57 +02006751 /*
6752 * Check if result will be cacheable with a cookie.
6753 * We'll block the response if security checks have caught
6754 * nasty things such as a cacheable cookie.
6755 */
6756 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6757 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6758 (s->be->options & PR_O_CHK_CACHE)) {
6759 /* we're in presence of a cacheable response containing
6760 * a set-cookie header. We'll block it as requested by
6761 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006762 */
Willy Tarreau58975672014-04-24 21:13:57 +02006763 if (objt_server(s->target))
6764 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006765
Willy Tarreau58975672014-04-24 21:13:57 +02006766 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006767 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006768 if (sess->listener->counters)
6769 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006770
Willy Tarreau58975672014-04-24 21:13:57 +02006771 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6772 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6773 send_log(s->be, LOG_ALERT,
6774 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6775 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6776 goto return_srv_prx_502;
6777 }
Willy Tarreau03945942009-12-22 16:50:27 +01006778
Willy Tarreau70730dd2014-04-24 18:06:27 +02006779 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006780 /*
6781 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6782 * If an "Upgrade" token is found, the header is left untouched in order
6783 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006784 * if anything but "Upgrade" is present in the Connection header. We don't
6785 * want to touch any 101 response either since it's switching to another
6786 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006787 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006788 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006789 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006790 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006791 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6792 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006793
Willy Tarreau58975672014-04-24 21:13:57 +02006794 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6795 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6796 /* we want a keep-alive response here. Keep-alive header
6797 * required if either side is not 1.1.
6798 */
6799 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6800 want_flags |= TX_CON_KAL_SET;
6801 }
6802 else {
6803 /* we want a close response here. Close header required if
6804 * the server is 1.1, regardless of the client.
6805 */
6806 if (msg->flags & HTTP_MSGF_VER_11)
6807 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006808 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006809
Willy Tarreau58975672014-04-24 21:13:57 +02006810 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6811 http_change_connection_header(txn, msg, want_flags);
6812 }
6813
6814 skip_header_mangling:
Christopher Faulet3e344292015-11-24 16:24:13 +01006815 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_FILTERS(s) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006816 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01006817 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
Willy Tarreau58975672014-04-24 21:13:57 +02006818 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Christopher Fauletd7c91962015-04-30 11:48:27 +02006819 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006820
Willy Tarreau58975672014-04-24 21:13:57 +02006821 /* if the user wants to log as soon as possible, without counting
6822 * bytes from the server, then this is the right moment. We have
6823 * to temporarily assign bytes_out to log what we currently have.
6824 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006825 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006826 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6827 s->logs.bytes_out = txn->rsp.eoh;
6828 s->do_log(s);
6829 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006830 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006831 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006832}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006833
Willy Tarreaud98cf932009-12-27 22:54:55 +01006834/* This function is an analyser which forwards response body (including chunk
6835 * sizes if any). It is called as soon as we must forward, even if we forward
6836 * zero byte. The only situation where it must not be called is when we're in
6837 * tunnel mode and we want to forward till the close. It's used both to forward
6838 * remaining data and to resync after end of body. It expects the msg_state to
6839 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006840 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006841 *
6842 * It is capable of compressing response data both in content-length mode and
6843 * in chunked mode. The state machines follows different flows depending on
6844 * whether content-length and chunked modes are used, since there are no
6845 * trailers in content-length :
6846 *
6847 * chk-mode cl-mode
6848 * ,----- BODY -----.
6849 * / \
6850 * V size > 0 V chk-mode
6851 * .--> SIZE -------------> DATA -------------> CRLF
6852 * | | size == 0 | last byte |
6853 * | v final crlf v inspected |
6854 * | TRAILERS -----------> DONE |
6855 * | |
6856 * `----------------------------------------------'
6857 *
6858 * Compression only happens in the DATA state, and must be flushed in final
6859 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6860 * is performed at once on final states for all bytes parsed, or when leaving
6861 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006862 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006863int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006864{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006865 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006866 struct http_txn *txn = s->txn;
6867 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01006868 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006869
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006870 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6871 return 0;
6872
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006873 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006874 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006875 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006876 /* Output closed while we were sending data. We must abort and
6877 * wake the other side up.
6878 */
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006879 msg->err_state = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006880 msg->msg_state = HTTP_MSG_ERROR;
6881 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006882 return 1;
6883 }
6884
Willy Tarreau4fe41902010-06-07 22:27:41 +02006885 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006886 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006887
Christopher Fauletd7c91962015-04-30 11:48:27 +02006888 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006889 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
6890 ? HTTP_MSG_CHUNK_SIZE
6891 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006892 }
6893
Willy Tarreauefdf0942014-04-24 20:08:57 +02006894 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006895 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02006896 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006897 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02006898 }
6899
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006900 if (msg->msg_state < HTTP_MSG_DONE) {
6901 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
6902 ? http_msg_forward_chunked_body(s, msg)
6903 : http_msg_forward_body(s, msg));
6904 if (!ret)
6905 goto missing_data_or_waiting;
6906 if (ret < 0)
6907 goto return_bad_res;
6908 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02006909
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006910 /* other states, DONE...TUNNEL */
6911 /* for keep-alive we don't want to forward closes on DONE */
6912 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6913 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
6914 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006915
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006916 if (http_resync_states(s)) {
6917 /* some state changes occurred, maybe the analyser was disabled
6918 * too. */
6919 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6920 if (res->flags & CF_SHUTW) {
6921 /* response errors are most likely due to the
6922 * client aborting the transfer. */
6923 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01006924 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006925 if (msg->err_pos >= 0)
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006926 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->err_state, strm_fe(s));
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006927 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006928 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006929 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006930 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02006931 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006932
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006933 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01006934 if (res->flags & CF_SHUTW)
6935 goto aborted_xfer;
6936
6937 /* stop waiting for data if the input is closed before the end. If the
6938 * client side was already closed, it means that the client has aborted,
6939 * so we don't want to count this as a server abort. Otherwise it's a
6940 * server abort.
6941 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006942 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006943 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006944 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006945 /* If we have some pending data, we continue the processing */
6946 if (!buffer_pending(res->buf)) {
6947 if (!(s->flags & SF_ERR_MASK))
6948 s->flags |= SF_ERR_SRVCL;
6949 s->be->be_counters.srv_aborts++;
6950 if (objt_server(s->target))
6951 objt_server(s->target)->counters.srv_aborts++;
6952 goto return_bad_res_stats_ok;
6953 }
Willy Tarreau40dba092010-03-04 18:14:51 +01006954 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006955
Willy Tarreau40dba092010-03-04 18:14:51 +01006956 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006957 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006958 goto return_bad_res;
6959
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006960 /* When TE: chunked is used, we need to get there again to parse
6961 * remaining chunks even if the server has closed, so we don't want to
6962 * set CF_DONTCLOSE. Similarly, if the body length is undefined, if
6963 * keep-alive is set on the client side or if there are filters
6964 * registered on the stream, we don't want to forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006965 */
Christopher Faulet92d36382015-11-05 13:35:03 +01006966 if ((msg->flags & HTTP_MSGF_TE_CHNK) || !msg->body_len ||
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006967 HAS_FILTERS(s) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006968 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6969 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006970 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006971
Willy Tarreau5c620922011-05-11 19:56:11 +02006972 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006973 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006974 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006975 * modes are already handled by the stream sock layer. We must not do
6976 * this in content-length mode because it could present the MSG_MORE
6977 * flag with the last block of forwarded data, which would cause an
6978 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006979 */
Christopher Faulet92d36382015-11-05 13:35:03 +01006980 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006981 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006982
Willy Tarreau87b09662015-04-03 00:22:06 +02006983 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006984 return 0;
6985
Willy Tarreau40dba092010-03-04 18:14:51 +01006986 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006987 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006988 if (objt_server(s->target))
6989 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006990
6991 return_bad_res_stats_ok:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01006992 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006993 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006994 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006995 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01006996 res->analysers &= AN_RES_FLT_END;
6997 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 +01006998 if (objt_server(s->target))
6999 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01007000
Willy Tarreaue7dff022015-04-03 01:14:29 +02007001 if (!(s->flags & SF_ERR_MASK))
7002 s->flags |= SF_ERR_PRXCOND;
7003 if (!(s->flags & SF_FINST_MASK))
7004 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007005 return 0;
7006
7007 aborted_xfer:
Willy Tarreau10e61cb2017-01-04 14:51:22 +01007008 txn->rsp.err_state = txn->rsp.msg_state;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007009 txn->rsp.msg_state = HTTP_MSG_ERROR;
7010 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01007011 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet0184ea72017-01-05 14:06:34 +01007012 res->analysers &= AN_RES_FLT_END;
7013 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 +01007014
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007015 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01007016 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007017 if (objt_server(s->target))
7018 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01007019
Willy Tarreaue7dff022015-04-03 01:14:29 +02007020 if (!(s->flags & SF_ERR_MASK))
7021 s->flags |= SF_ERR_CLICL;
7022 if (!(s->flags & SF_FINST_MASK))
7023 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007024 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007025}
7026
7027
7028static inline int
7029http_msg_forward_body(struct stream *s, struct http_msg *msg)
7030{
7031 struct channel *chn = msg->chn;
7032 int ret;
7033
7034 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
7035
7036 if (msg->msg_state == HTTP_MSG_ENDING)
7037 goto ending;
7038
7039 /* Neither content-length, nor transfer-encoding was found, so we must
7040 * read the body until the server connection is closed. In that case, we
7041 * eat data as they come. Of course, this happens for response only. */
7042 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
7043 unsigned long long len = (chn->buf->i - msg->next);
7044 msg->chunk_len += len;
7045 msg->body_len += len;
7046 }
Christopher Fauletda02e172015-12-04 09:25:05 +01007047 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
7048 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
7049 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007050 msg->next += ret;
7051 msg->chunk_len -= ret;
7052 if (msg->chunk_len) {
7053 /* input empty or output full */
7054 if (chn->buf->i > msg->next)
7055 chn->flags |= CF_WAKE_WRITE;
7056 goto missing_data_or_waiting;
7057 }
7058
Christopher Faulet32359572016-11-29 14:49:54 +01007059 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && !(chn->flags & CF_SHUTR) &&
7060 HAS_DATA_FILTERS(s, chn)) {
7061 /* The server still sending data that should be filtered */
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007062 goto missing_data_or_waiting;
7063 }
7064 msg->msg_state = HTTP_MSG_ENDING;
7065
7066 ending:
7067 /* we may have some pending data starting at res->buf->p such as a last
7068 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01007069 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
7070 /* default_ret */ msg->next,
7071 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007072 b_adv(chn->buf, ret);
7073 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007074 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
7075 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02007076 if (msg->next)
7077 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007078
Christopher Fauletda02e172015-12-04 09:25:05 +01007079 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
7080 /* default_ret */ 1,
7081 /* on_error */ goto error,
7082 /* on_wait */ goto waiting);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007083 msg->msg_state = HTTP_MSG_DONE;
7084 return 1;
7085
7086 missing_data_or_waiting:
7087 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01007088 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
7089 /* default_ret */ msg->next,
7090 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007091 b_adv(chn->buf, ret);
7092 msg->next -= ret;
7093 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
7094 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01007095 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007096 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02007097 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007098 return 0;
7099 error:
7100 return -1;
7101}
7102
7103static inline int
7104http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
7105{
7106 struct channel *chn = msg->chn;
7107 int ret;
7108
7109 /* Here we have the guarantee to be in one of the following state:
7110 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
7111 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
7112
7113 switch_states:
7114 switch (msg->msg_state) {
7115 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01007116 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
7117 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
7118 /* on_error */ goto error);
7119 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007120 msg->chunk_len -= ret;
7121 if (msg->chunk_len) {
7122 /* input empty or output full */
7123 if (chn->buf->i > msg->next)
7124 chn->flags |= CF_WAKE_WRITE;
7125 goto missing_data_or_waiting;
7126 }
7127
7128 /* nothing left to forward for this chunk*/
7129 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
7130 /* fall through for HTTP_MSG_CHUNK_CRLF */
7131
7132 case HTTP_MSG_CHUNK_CRLF:
7133 /* we want the CRLF after the data */
7134 ret = http_skip_chunk_crlf(msg);
7135 if (ret == 0)
7136 goto missing_data_or_waiting;
7137 if (ret < 0)
7138 goto chunk_parsing_error;
Christopher Faulet113f7de2015-12-14 14:52:13 +01007139 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007140 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
7141 /* fall through for HTTP_MSG_CHUNK_SIZE */
7142
7143 case HTTP_MSG_CHUNK_SIZE:
7144 /* read the chunk size and assign it to ->chunk_len,
7145 * then set ->next to point to the body and switch to
7146 * DATA or TRAILERS state.
7147 */
7148 ret = http_parse_chunk_size(msg);
7149 if (ret == 0)
7150 goto missing_data_or_waiting;
7151 if (ret < 0)
7152 goto chunk_parsing_error;
Christopher Faulet113f7de2015-12-14 14:52:13 +01007153 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007154 if (msg->chunk_len) {
7155 msg->msg_state = HTTP_MSG_DATA;
7156 goto switch_states;
7157 }
7158 msg->msg_state = HTTP_MSG_TRAILERS;
7159 /* fall through for HTTP_MSG_TRAILERS */
7160
7161 case HTTP_MSG_TRAILERS:
7162 ret = http_forward_trailers(msg);
7163 if (ret < 0)
7164 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01007165 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
7166 /* default_ret */ 1,
7167 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007168 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007169 if (!ret)
7170 goto missing_data_or_waiting;
7171 break;
7172
7173 case HTTP_MSG_ENDING:
7174 goto ending;
7175
7176 default:
7177 /* This should no happen in this function */
7178 goto error;
7179 }
7180
7181 msg->msg_state = HTTP_MSG_ENDING;
7182 ending:
7183 /* we may have some pending data starting at res->buf->p such as a last
7184 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01007185 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007186 /* default_ret */ msg->next,
7187 /* on_error */ goto error);
7188 b_adv(chn->buf, ret);
7189 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007190 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
7191 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02007192 if (msg->next)
7193 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007194
Christopher Fauletda02e172015-12-04 09:25:05 +01007195 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007196 /* default_ret */ 1,
7197 /* on_error */ goto error,
7198 /* on_wait */ goto waiting);
7199 msg->msg_state = HTTP_MSG_DONE;
7200 return 1;
7201
7202 missing_data_or_waiting:
7203 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01007204 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007205 /* default_ret */ msg->next,
7206 /* on_error */ goto error);
7207 b_adv(chn->buf, ret);
7208 msg->next -= ret;
7209 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
7210 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01007211 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007212 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02007213 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007214 return 0;
7215
7216 chunk_parsing_error:
7217 if (msg->err_pos >= 0) {
7218 if (chn->flags & CF_ISRESP)
7219 http_capture_bad_message(&s->be->invalid_rep, s, msg,
7220 msg->msg_state, strm_fe(s));
7221 else
7222 http_capture_bad_message(&strm_fe(s)->invalid_req, s,
7223 msg, msg->msg_state, s->be);
7224 }
7225 error:
7226 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007227}
7228
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007229
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007230/* Iterate the same filter through all request headers.
7231 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007232 * Since it can manage the switch to another backend, it updates the per-proxy
7233 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007234 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007235int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007236{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007237 char *cur_ptr, *cur_end, *cur_next;
7238 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007239 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007240 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007241 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01007242
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007243 last_hdr = 0;
7244
Willy Tarreau9b28e032012-10-12 23:49:43 +02007245 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007246 old_idx = 0;
7247
7248 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007249 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007250 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007251 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007252 (exp->action == ACT_ALLOW ||
7253 exp->action == ACT_DENY ||
7254 exp->action == ACT_TARPIT))
7255 return 0;
7256
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007257 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007258 if (!cur_idx)
7259 break;
7260
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007261 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007262 cur_ptr = cur_next;
7263 cur_end = cur_ptr + cur_hdr->len;
7264 cur_next = cur_end + cur_hdr->cr + 1;
7265
7266 /* Now we have one header between cur_ptr and cur_end,
7267 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007268 */
7269
Willy Tarreau15a53a42015-01-21 13:39:42 +01007270 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007271 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007272 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007273 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007274 last_hdr = 1;
7275 break;
7276
7277 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007278 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007279 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007280 break;
7281
7282 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007283 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007284 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007285 break;
7286
7287 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007288 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7289 if (trash.len < 0)
7290 return -1;
7291
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007292 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007293 /* FIXME: if the user adds a newline in the replacement, the
7294 * index will not be recalculated for now, and the new line
7295 * will not be counted as a new header.
7296 */
7297
7298 cur_end += delta;
7299 cur_next += delta;
7300 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007301 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007302 break;
7303
7304 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007305 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007306 cur_next += delta;
7307
Willy Tarreaufa355d42009-11-29 18:12:29 +01007308 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007309 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7310 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007311 cur_hdr->len = 0;
7312 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007313 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007314 break;
7315
7316 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007317 }
7318
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007319 /* keep the link from this header to next one in case of later
7320 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007321 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007322 old_idx = cur_idx;
7323 }
7324 return 0;
7325}
7326
7327
7328/* Apply the filter to the request line.
7329 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7330 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007331 * Since it can manage the switch to another backend, it updates the per-proxy
7332 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007333 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007334int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007335{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007336 char *cur_ptr, *cur_end;
7337 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007338 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007339 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007340
Willy Tarreau3d300592007-03-18 18:34:41 +01007341 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007342 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007343 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007344 (exp->action == ACT_ALLOW ||
7345 exp->action == ACT_DENY ||
7346 exp->action == ACT_TARPIT))
7347 return 0;
7348 else if (exp->action == ACT_REMOVE)
7349 return 0;
7350
7351 done = 0;
7352
Willy Tarreau9b28e032012-10-12 23:49:43 +02007353 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007354 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007355
7356 /* Now we have the request line between cur_ptr and cur_end */
7357
Willy Tarreau15a53a42015-01-21 13:39:42 +01007358 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007359 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007360 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007361 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007362 done = 1;
7363 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007364
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007365 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007366 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007367 done = 1;
7368 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007369
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007370 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007371 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007372 done = 1;
7373 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007374
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007375 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007376 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7377 if (trash.len < 0)
7378 return -1;
7379
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007380 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007381 /* FIXME: if the user adds a newline in the replacement, the
7382 * index will not be recalculated for now, and the new line
7383 * will not be counted as a new header.
7384 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007385
Willy Tarreaufa355d42009-11-29 18:12:29 +01007386 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007387 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007388 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007389 HTTP_MSG_RQMETH,
7390 cur_ptr, cur_end + 1,
7391 NULL, NULL);
7392 if (unlikely(!cur_end))
7393 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007394
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007395 /* we have a full request and we know that we have either a CR
7396 * or an LF at <ptr>.
7397 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007398 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7399 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007400 /* there is no point trying this regex on headers */
7401 return 1;
7402 }
7403 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007404 return done;
7405}
Willy Tarreau97de6242006-12-27 17:18:38 +01007406
Willy Tarreau58f10d72006-12-04 02:26:12 +01007407
Willy Tarreau58f10d72006-12-04 02:26:12 +01007408
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007409/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007410 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007411 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007412 * unparsable request. Since it can manage the switch to another backend, it
7413 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007414 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007415int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007416{
Willy Tarreau192252e2015-04-04 01:47:55 +02007417 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007418 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007419 struct hdr_exp *exp;
7420
7421 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007422 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007423
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007424 /*
7425 * The interleaving of transformations and verdicts
7426 * makes it difficult to decide to continue or stop
7427 * the evaluation.
7428 */
7429
Willy Tarreau6c123b12010-01-28 20:22:06 +01007430 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7431 break;
7432
Willy Tarreau3d300592007-03-18 18:34:41 +01007433 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007434 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007435 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007436 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007437
7438 /* if this filter had a condition, evaluate it now and skip to
7439 * next filter if the condition does not match.
7440 */
7441 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007442 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007443 ret = acl_pass(ret);
7444 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7445 ret = !ret;
7446
7447 if (!ret)
7448 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007449 }
7450
7451 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007452 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007453 if (unlikely(ret < 0))
7454 return -1;
7455
7456 if (likely(ret == 0)) {
7457 /* The filter did not match the request, it can be
7458 * iterated through all headers.
7459 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007460 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7461 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007462 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007463 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007464 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007465}
7466
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007467
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007468/* Find the end of a cookie value contained between <s> and <e>. It works the
7469 * same way as with headers above except that the semi-colon also ends a token.
7470 * See RFC2965 for more information. Note that it requires a valid header to
7471 * return a valid result.
7472 */
7473char *find_cookie_value_end(char *s, const char *e)
7474{
7475 int quoted, qdpair;
7476
7477 quoted = qdpair = 0;
7478 for (; s < e; s++) {
7479 if (qdpair) qdpair = 0;
7480 else if (quoted) {
7481 if (*s == '\\') qdpair = 1;
7482 else if (*s == '"') quoted = 0;
7483 }
7484 else if (*s == '"') quoted = 1;
7485 else if (*s == ',' || *s == ';') return s;
7486 }
7487 return s;
7488}
7489
7490/* Delete a value in a header between delimiters <from> and <next> in buffer
7491 * <buf>. The number of characters displaced is returned, and the pointer to
7492 * the first delimiter is updated if required. The function tries as much as
7493 * possible to respect the following principles :
7494 * - replace <from> delimiter by the <next> one unless <from> points to a
7495 * colon, in which case <next> is simply removed
7496 * - set exactly one space character after the new first delimiter, unless
7497 * there are not enough characters in the block being moved to do so.
7498 * - remove unneeded spaces before the previous delimiter and after the new
7499 * one.
7500 *
7501 * It is the caller's responsibility to ensure that :
7502 * - <from> points to a valid delimiter or the colon ;
7503 * - <next> points to a valid delimiter or the final CR/LF ;
7504 * - there are non-space chars before <from> ;
7505 * - there is a CR/LF at or after <next>.
7506 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007507int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007508{
7509 char *prev = *from;
7510
7511 if (*prev == ':') {
7512 /* We're removing the first value, preserve the colon and add a
7513 * space if possible.
7514 */
Willy Tarreau2235b262016-11-05 15:50:20 +01007515 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007516 next++;
7517 prev++;
7518 if (prev < next)
7519 *prev++ = ' ';
7520
Willy Tarreau2235b262016-11-05 15:50:20 +01007521 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007522 next++;
7523 } else {
7524 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01007525 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007526 prev--;
7527 *from = prev;
7528
7529 /* copy the delimiter and if possible a space if we're
7530 * not at the end of the line.
7531 */
Willy Tarreau2235b262016-11-05 15:50:20 +01007532 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007533 *prev++ = *next++;
7534 if (prev + 1 < next)
7535 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01007536 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007537 next++;
7538 }
7539 }
7540 return buffer_replace2(buf, prev, next, NULL, 0);
7541}
7542
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007543/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007544 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007545 * desirable to call it only when needed. This code is quite complex because
7546 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7547 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007548 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007549void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007550{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007551 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007552 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007553 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007554 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007555 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7556 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007557
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007558 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007559 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007560 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007561
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007562 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007563 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007564 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007565
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007566 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007567 hdr_beg = hdr_next;
7568 hdr_end = hdr_beg + cur_hdr->len;
7569 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007570
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007571 /* We have one full header between hdr_beg and hdr_end, and the
7572 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007573 * "Cookie:" headers.
7574 */
7575
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007576 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007577 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007578 old_idx = cur_idx;
7579 continue;
7580 }
7581
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007582 del_from = NULL; /* nothing to be deleted */
7583 preserve_hdr = 0; /* assume we may kill the whole header */
7584
Willy Tarreau58f10d72006-12-04 02:26:12 +01007585 /* Now look for cookies. Conforming to RFC2109, we have to support
7586 * attributes whose name begin with a '$', and associate them with
7587 * the right cookie, if we want to delete this cookie.
7588 * So there are 3 cases for each cookie read :
7589 * 1) it's a special attribute, beginning with a '$' : ignore it.
7590 * 2) it's a server id cookie that we *MAY* want to delete : save
7591 * some pointers on it (last semi-colon, beginning of cookie...)
7592 * 3) it's an application cookie : we *MAY* have to delete a previous
7593 * "special" cookie.
7594 * At the end of loop, if a "special" cookie remains, we may have to
7595 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007596 * *MUST* delete it.
7597 *
7598 * Note: RFC2965 is unclear about the processing of spaces around
7599 * the equal sign in the ATTR=VALUE form. A careful inspection of
7600 * the RFC explicitly allows spaces before it, and not within the
7601 * tokens (attrs or values). An inspection of RFC2109 allows that
7602 * too but section 10.1.3 lets one think that spaces may be allowed
7603 * after the equal sign too, resulting in some (rare) buggy
7604 * implementations trying to do that. So let's do what servers do.
7605 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7606 * allowed quoted strings in values, with any possible character
7607 * after a backslash, including control chars and delimitors, which
7608 * causes parsing to become ambiguous. Browsers also allow spaces
7609 * within values even without quotes.
7610 *
7611 * We have to keep multiple pointers in order to support cookie
7612 * removal at the beginning, middle or end of header without
7613 * corrupting the header. All of these headers are valid :
7614 *
7615 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7616 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7617 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7618 * | | | | | | | | |
7619 * | | | | | | | | hdr_end <--+
7620 * | | | | | | | +--> next
7621 * | | | | | | +----> val_end
7622 * | | | | | +-----------> val_beg
7623 * | | | | +--------------> equal
7624 * | | | +----------------> att_end
7625 * | | +---------------------> att_beg
7626 * | +--------------------------> prev
7627 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007628 */
7629
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007630 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7631 /* Iterate through all cookies on this line */
7632
7633 /* find att_beg */
7634 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007635 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007636 att_beg++;
7637
7638 /* find att_end : this is the first character after the last non
7639 * space before the equal. It may be equal to hdr_end.
7640 */
7641 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007642
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007643 while (equal < hdr_end) {
7644 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007645 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007646 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007647 continue;
7648 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007649 }
7650
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007651 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7652 * is between <att_beg> and <equal>, both may be identical.
7653 */
7654
7655 /* look for end of cookie if there is an equal sign */
7656 if (equal < hdr_end && *equal == '=') {
7657 /* look for the beginning of the value */
7658 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007659 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007660 val_beg++;
7661
7662 /* find the end of the value, respecting quotes */
7663 next = find_cookie_value_end(val_beg, hdr_end);
7664
7665 /* make val_end point to the first white space or delimitor after the value */
7666 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007667 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007668 val_end--;
7669 } else {
7670 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007671 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007672
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007673 /* We have nothing to do with attributes beginning with '$'. However,
7674 * they will automatically be removed if a header before them is removed,
7675 * since they're supposed to be linked together.
7676 */
7677 if (*att_beg == '$')
7678 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007679
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007680 /* Ignore cookies with no equal sign */
7681 if (equal == next) {
7682 /* This is not our cookie, so we must preserve it. But if we already
7683 * scheduled another cookie for removal, we cannot remove the
7684 * complete header, but we can remove the previous block itself.
7685 */
7686 preserve_hdr = 1;
7687 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007688 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007689 val_end += delta;
7690 next += delta;
7691 hdr_end += delta;
7692 hdr_next += delta;
7693 cur_hdr->len += delta;
7694 http_msg_move_end(&txn->req, delta);
7695 prev = del_from;
7696 del_from = NULL;
7697 }
7698 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007699 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007700
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007701 /* if there are spaces around the equal sign, we need to
7702 * strip them otherwise we'll get trouble for cookie captures,
7703 * or even for rewrites. Since this happens extremely rarely,
7704 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007705 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007706 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7707 int stripped_before = 0;
7708 int stripped_after = 0;
7709
7710 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007711 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007712 equal += stripped_before;
7713 val_beg += stripped_before;
7714 }
7715
7716 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007717 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007718 val_beg += stripped_after;
7719 stripped_before += stripped_after;
7720 }
7721
7722 val_end += stripped_before;
7723 next += stripped_before;
7724 hdr_end += stripped_before;
7725 hdr_next += stripped_before;
7726 cur_hdr->len += stripped_before;
7727 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007728 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007729 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007730
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007731 /* First, let's see if we want to capture this cookie. We check
7732 * that we don't already have a client side cookie, because we
7733 * can only capture one. Also as an optimisation, we ignore
7734 * cookies shorter than the declared name.
7735 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007736 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7737 (val_end - att_beg >= sess->fe->capture_namelen) &&
7738 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007739 int log_len = val_end - att_beg;
7740
7741 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7742 Alert("HTTP logging : out of memory.\n");
7743 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007744 if (log_len > sess->fe->capture_len)
7745 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007746 memcpy(txn->cli_cookie, att_beg, log_len);
7747 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007748 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007749 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007750
Willy Tarreaubca99692010-10-06 19:25:55 +02007751 /* Persistence cookies in passive, rewrite or insert mode have the
7752 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007753 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007754 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007755 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007756 * For cookies in prefix mode, the form is :
7757 *
7758 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007759 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007760 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7761 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7762 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007763 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007764
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007765 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7766 * have the server ID between val_beg and delim, and the original cookie between
7767 * delim+1 and val_end. Otherwise, delim==val_end :
7768 *
7769 * Cookie: NAME=SRV; # in all but prefix modes
7770 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7771 * | || || | |+-> next
7772 * | || || | +--> val_end
7773 * | || || +---------> delim
7774 * | || |+------------> val_beg
7775 * | || +-------------> att_end = equal
7776 * | |+-----------------> att_beg
7777 * | +------------------> prev
7778 * +-------------------------> hdr_beg
7779 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007780
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007781 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007782 for (delim = val_beg; delim < val_end; delim++)
7783 if (*delim == COOKIE_DELIM)
7784 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007785 } else {
7786 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007787 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007788 /* Now check if the cookie contains a date field, which would
7789 * appear after a vertical bar ('|') just after the server name
7790 * and before the delimiter.
7791 */
7792 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7793 if (vbar1) {
7794 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007795 * right is the last seen date. It is a base64 encoded
7796 * 30-bit value representing the UNIX date since the
7797 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007798 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007799 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007800 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007801 if (val_end - vbar1 >= 5) {
7802 val = b64tos30(vbar1);
7803 if (val > 0)
7804 txn->cookie_last_date = val << 2;
7805 }
7806 /* look for a second vertical bar */
7807 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7808 if (vbar1 && (val_end - vbar1 > 5)) {
7809 val = b64tos30(vbar1 + 1);
7810 if (val > 0)
7811 txn->cookie_first_date = val << 2;
7812 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007813 }
7814 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007815
Willy Tarreauf64d1412010-10-07 20:06:11 +02007816 /* if the cookie has an expiration date and the proxy wants to check
7817 * it, then we do that now. We first check if the cookie is too old,
7818 * then only if it has expired. We detect strict overflow because the
7819 * time resolution here is not great (4 seconds). Cookies with dates
7820 * in the future are ignored if their offset is beyond one day. This
7821 * allows an admin to fix timezone issues without expiring everyone
7822 * and at the same time avoids keeping unwanted side effects for too
7823 * long.
7824 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007825 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7826 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007827 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007828 txn->flags &= ~TX_CK_MASK;
7829 txn->flags |= TX_CK_OLD;
7830 delim = val_beg; // let's pretend we have not found the cookie
7831 txn->cookie_first_date = 0;
7832 txn->cookie_last_date = 0;
7833 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007834 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7835 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007836 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007837 txn->flags &= ~TX_CK_MASK;
7838 txn->flags |= TX_CK_EXPIRED;
7839 delim = val_beg; // let's pretend we have not found the cookie
7840 txn->cookie_first_date = 0;
7841 txn->cookie_last_date = 0;
7842 }
7843
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007844 /* Here, we'll look for the first running server which supports the cookie.
7845 * This allows to share a same cookie between several servers, for example
7846 * to dedicate backup servers to specific servers only.
7847 * However, to prevent clients from sticking to cookie-less backup server
7848 * when they have incidentely learned an empty cookie, we simply ignore
7849 * empty cookies and mark them as invalid.
7850 * The same behaviour is applied when persistence must be ignored.
7851 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007852 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007853 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007854
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007855 while (srv) {
7856 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7857 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007858 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007859 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007860 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007861 /* we found the server and we can use it */
7862 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007863 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007864 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007865 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007866 break;
7867 } else {
7868 /* we found a server, but it's down,
7869 * mark it as such and go on in case
7870 * another one is available.
7871 */
7872 txn->flags &= ~TX_CK_MASK;
7873 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007874 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007875 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007876 srv = srv->next;
7877 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007878
Willy Tarreauf64d1412010-10-07 20:06:11 +02007879 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007880 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007881 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007882 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007883 txn->flags |= TX_CK_UNUSED;
7884 else
7885 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007886 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007887
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007888 /* depending on the cookie mode, we may have to either :
7889 * - delete the complete cookie if we're in insert+indirect mode, so that
7890 * the server never sees it ;
7891 * - remove the server id from the cookie value, and tag the cookie as an
7892 * application cookie so that it does not get accidentely removed later,
7893 * if we're in cookie prefix mode
7894 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007895 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007896 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007897
Willy Tarreau9b28e032012-10-12 23:49:43 +02007898 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007899 val_end += delta;
7900 next += delta;
7901 hdr_end += delta;
7902 hdr_next += delta;
7903 cur_hdr->len += delta;
7904 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007905
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007906 del_from = NULL;
7907 preserve_hdr = 1; /* we want to keep this cookie */
7908 }
7909 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007910 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007911 del_from = prev;
7912 }
7913 } else {
7914 /* This is not our cookie, so we must preserve it. But if we already
7915 * scheduled another cookie for removal, we cannot remove the
7916 * complete header, but we can remove the previous block itself.
7917 */
7918 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007919
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007920 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007921 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007922 if (att_beg >= del_from)
7923 att_beg += delta;
7924 if (att_end >= del_from)
7925 att_end += delta;
7926 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007927 val_end += delta;
7928 next += delta;
7929 hdr_end += delta;
7930 hdr_next += delta;
7931 cur_hdr->len += delta;
7932 http_msg_move_end(&txn->req, delta);
7933 prev = del_from;
7934 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007935 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007936 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007937
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007938 /* continue with next cookie on this header line */
7939 att_beg = next;
7940 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007941
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007942 /* There are no more cookies on this line.
7943 * We may still have one (or several) marked for deletion at the
7944 * end of the line. We must do this now in two ways :
7945 * - if some cookies must be preserved, we only delete from the
7946 * mark to the end of line ;
7947 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007948 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007949 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007950 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007951 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007952 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007953 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007954 cur_hdr->len += delta;
7955 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007956 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007957
7958 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007959 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7960 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007961 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007962 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007963 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007964 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007965 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007966 }
7967
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007968 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007969 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007970 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007971}
7972
7973
Willy Tarreaua15645d2007-03-18 16:22:39 +01007974/* Iterate the same filter through all response headers contained in <rtr>.
7975 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7976 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007977int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007978{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007979 char *cur_ptr, *cur_end, *cur_next;
7980 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007981 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007982 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007983 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007984
7985 last_hdr = 0;
7986
Willy Tarreau9b28e032012-10-12 23:49:43 +02007987 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007988 old_idx = 0;
7989
7990 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007991 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007992 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007993 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007994 (exp->action == ACT_ALLOW ||
7995 exp->action == ACT_DENY))
7996 return 0;
7997
7998 cur_idx = txn->hdr_idx.v[old_idx].next;
7999 if (!cur_idx)
8000 break;
8001
8002 cur_hdr = &txn->hdr_idx.v[cur_idx];
8003 cur_ptr = cur_next;
8004 cur_end = cur_ptr + cur_hdr->len;
8005 cur_next = cur_end + cur_hdr->cr + 1;
8006
8007 /* Now we have one header between cur_ptr and cur_end,
8008 * and the next header starts at cur_next.
8009 */
8010
Willy Tarreau15a53a42015-01-21 13:39:42 +01008011 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008012 switch (exp->action) {
8013 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008014 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008015 last_hdr = 1;
8016 break;
8017
8018 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008019 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008020 last_hdr = 1;
8021 break;
8022
8023 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008024 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8025 if (trash.len < 0)
8026 return -1;
8027
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008028 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008029 /* FIXME: if the user adds a newline in the replacement, the
8030 * index will not be recalculated for now, and the new line
8031 * will not be counted as a new header.
8032 */
8033
8034 cur_end += delta;
8035 cur_next += delta;
8036 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008037 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008038 break;
8039
8040 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02008041 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008042 cur_next += delta;
8043
Willy Tarreaufa355d42009-11-29 18:12:29 +01008044 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008045 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8046 txn->hdr_idx.used--;
8047 cur_hdr->len = 0;
8048 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01008049 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008050 break;
8051
8052 }
8053 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008054
8055 /* keep the link from this header to next one in case of later
8056 * removal of next header.
8057 */
8058 old_idx = cur_idx;
8059 }
8060 return 0;
8061}
8062
8063
8064/* Apply the filter to the status line in the response buffer <rtr>.
8065 * Returns 0 if nothing has been done, 1 if the filter has been applied,
8066 * or -1 if a replacement resulted in an invalid status line.
8067 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008068int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008069{
Willy Tarreaua15645d2007-03-18 16:22:39 +01008070 char *cur_ptr, *cur_end;
8071 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008072 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008073 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008074
8075
Willy Tarreau3d300592007-03-18 18:34:41 +01008076 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008077 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008078 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008079 (exp->action == ACT_ALLOW ||
8080 exp->action == ACT_DENY))
8081 return 0;
8082 else if (exp->action == ACT_REMOVE)
8083 return 0;
8084
8085 done = 0;
8086
Willy Tarreau9b28e032012-10-12 23:49:43 +02008087 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008088 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008089
8090 /* Now we have the status line between cur_ptr and cur_end */
8091
Willy Tarreau15a53a42015-01-21 13:39:42 +01008092 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008093 switch (exp->action) {
8094 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008095 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008096 done = 1;
8097 break;
8098
8099 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008100 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008101 done = 1;
8102 break;
8103
8104 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008105 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8106 if (trash.len < 0)
8107 return -1;
8108
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008109 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008110 /* FIXME: if the user adds a newline in the replacement, the
8111 * index will not be recalculated for now, and the new line
8112 * will not be counted as a new header.
8113 */
8114
Willy Tarreaufa355d42009-11-29 18:12:29 +01008115 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008116 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008117 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02008118 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01008119 cur_ptr, cur_end + 1,
8120 NULL, NULL);
8121 if (unlikely(!cur_end))
8122 return -1;
8123
8124 /* we have a full respnse and we know that we have either a CR
8125 * or an LF at <ptr>.
8126 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008127 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008128 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01008129 /* there is no point trying this regex on headers */
8130 return 1;
8131 }
8132 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008133 return done;
8134}
8135
8136
8137
8138/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008139 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008140 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
8141 * unparsable response.
8142 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008143int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008144{
Willy Tarreau192252e2015-04-04 01:47:55 +02008145 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008146 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008147 struct hdr_exp *exp;
8148
8149 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008150 int ret;
8151
8152 /*
8153 * The interleaving of transformations and verdicts
8154 * makes it difficult to decide to continue or stop
8155 * the evaluation.
8156 */
8157
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008158 if (txn->flags & TX_SVDENY)
8159 break;
8160
Willy Tarreau3d300592007-03-18 18:34:41 +01008161 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008162 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
8163 exp->action == ACT_PASS)) {
8164 exp = exp->next;
8165 continue;
8166 }
8167
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008168 /* if this filter had a condition, evaluate it now and skip to
8169 * next filter if the condition does not match.
8170 */
8171 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02008172 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008173 ret = acl_pass(ret);
8174 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8175 ret = !ret;
8176 if (!ret)
8177 continue;
8178 }
8179
Willy Tarreaua15645d2007-03-18 16:22:39 +01008180 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008181 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008182 if (unlikely(ret < 0))
8183 return -1;
8184
8185 if (likely(ret == 0)) {
8186 /* The filter did not match the response, it can be
8187 * iterated through all headers.
8188 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008189 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8190 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008191 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008192 }
8193 return 0;
8194}
8195
8196
Willy Tarreaua15645d2007-03-18 16:22:39 +01008197/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008198 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008199 * desirable to call it only when needed. This function is also used when we
8200 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008201 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008202void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008203{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008204 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008205 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01008206 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008207 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008208 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008209 char *hdr_beg, *hdr_end, *hdr_next;
8210 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008211
Willy Tarreaua15645d2007-03-18 16:22:39 +01008212 /* Iterate through the headers.
8213 * we start with the start line.
8214 */
8215 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008216 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008217
8218 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8219 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008220 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008221
8222 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008223 hdr_beg = hdr_next;
8224 hdr_end = hdr_beg + cur_hdr->len;
8225 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008226
Willy Tarreau24581ba2010-08-31 22:39:35 +02008227 /* We have one full header between hdr_beg and hdr_end, and the
8228 * next header starts at hdr_next. We're only interested in
8229 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008230 */
8231
Willy Tarreau24581ba2010-08-31 22:39:35 +02008232 is_cookie2 = 0;
8233 prev = hdr_beg + 10;
8234 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008235 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008236 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8237 if (!val) {
8238 old_idx = cur_idx;
8239 continue;
8240 }
8241 is_cookie2 = 1;
8242 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008243 }
8244
Willy Tarreau24581ba2010-08-31 22:39:35 +02008245 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8246 * <prev> points to the colon.
8247 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008248 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008249
Willy Tarreau24581ba2010-08-31 22:39:35 +02008250 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8251 * check-cache is enabled) and we are not interested in checking
8252 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008253 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02008254 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008255 return;
8256
Willy Tarreau24581ba2010-08-31 22:39:35 +02008257 /* OK so now we know we have to process this response cookie.
8258 * The format of the Set-Cookie header is slightly different
8259 * from the format of the Cookie header in that it does not
8260 * support the comma as a cookie delimiter (thus the header
8261 * cannot be folded) because the Expires attribute described in
8262 * the original Netscape's spec may contain an unquoted date
8263 * with a comma inside. We have to live with this because
8264 * many browsers don't support Max-Age and some browsers don't
8265 * support quoted strings. However the Set-Cookie2 header is
8266 * clean.
8267 *
8268 * We have to keep multiple pointers in order to support cookie
8269 * removal at the beginning, middle or end of header without
8270 * corrupting the header (in case of set-cookie2). A special
8271 * pointer, <scav> points to the beginning of the set-cookie-av
8272 * fields after the first semi-colon. The <next> pointer points
8273 * either to the end of line (set-cookie) or next unquoted comma
8274 * (set-cookie2). All of these headers are valid :
8275 *
8276 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8277 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8278 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8279 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8280 * | | | | | | | | | |
8281 * | | | | | | | | +-> next hdr_end <--+
8282 * | | | | | | | +------------> scav
8283 * | | | | | | +--------------> val_end
8284 * | | | | | +--------------------> val_beg
8285 * | | | | +----------------------> equal
8286 * | | | +------------------------> att_end
8287 * | | +----------------------------> att_beg
8288 * | +------------------------------> prev
8289 * +-----------------------------------------> hdr_beg
8290 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008291
Willy Tarreau24581ba2010-08-31 22:39:35 +02008292 for (; prev < hdr_end; prev = next) {
8293 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008294
Willy Tarreau24581ba2010-08-31 22:39:35 +02008295 /* find att_beg */
8296 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01008297 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008298 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008299
Willy Tarreau24581ba2010-08-31 22:39:35 +02008300 /* find att_end : this is the first character after the last non
8301 * space before the equal. It may be equal to hdr_end.
8302 */
8303 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008304
Willy Tarreau24581ba2010-08-31 22:39:35 +02008305 while (equal < hdr_end) {
8306 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8307 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01008308 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008309 continue;
8310 att_end = equal;
8311 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008312
Willy Tarreau24581ba2010-08-31 22:39:35 +02008313 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8314 * is between <att_beg> and <equal>, both may be identical.
8315 */
8316
8317 /* look for end of cookie if there is an equal sign */
8318 if (equal < hdr_end && *equal == '=') {
8319 /* look for the beginning of the value */
8320 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01008321 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008322 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008323
Willy Tarreau24581ba2010-08-31 22:39:35 +02008324 /* find the end of the value, respecting quotes */
8325 next = find_cookie_value_end(val_beg, hdr_end);
8326
8327 /* make val_end point to the first white space or delimitor after the value */
8328 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01008329 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008330 val_end--;
8331 } else {
8332 /* <equal> points to next comma, semi-colon or EOL */
8333 val_beg = val_end = next = equal;
8334 }
8335
8336 if (next < hdr_end) {
8337 /* Set-Cookie2 supports multiple cookies, and <next> points to
8338 * a colon or semi-colon before the end. So skip all attr-value
8339 * pairs and look for the next comma. For Set-Cookie, since
8340 * commas are permitted in values, skip to the end.
8341 */
8342 if (is_cookie2)
8343 next = find_hdr_value_end(next, hdr_end);
8344 else
8345 next = hdr_end;
8346 }
8347
8348 /* Now everything is as on the diagram above */
8349
8350 /* Ignore cookies with no equal sign */
8351 if (equal == val_end)
8352 continue;
8353
8354 /* If there are spaces around the equal sign, we need to
8355 * strip them otherwise we'll get trouble for cookie captures,
8356 * or even for rewrites. Since this happens extremely rarely,
8357 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008358 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008359 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8360 int stripped_before = 0;
8361 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008362
Willy Tarreau24581ba2010-08-31 22:39:35 +02008363 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008364 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008365 equal += stripped_before;
8366 val_beg += stripped_before;
8367 }
8368
8369 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008370 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008371 val_beg += stripped_after;
8372 stripped_before += stripped_after;
8373 }
8374
8375 val_end += stripped_before;
8376 next += stripped_before;
8377 hdr_end += stripped_before;
8378 hdr_next += stripped_before;
8379 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008380 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008381 }
8382
8383 /* First, let's see if we want to capture this cookie. We check
8384 * that we don't already have a server side cookie, because we
8385 * can only capture one. Also as an optimisation, we ignore
8386 * cookies shorter than the declared name.
8387 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008388 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008389 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008390 (val_end - att_beg >= sess->fe->capture_namelen) &&
8391 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008392 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008393 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008394 Alert("HTTP logging : out of memory.\n");
8395 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008396 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008397 if (log_len > sess->fe->capture_len)
8398 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008399 memcpy(txn->srv_cookie, att_beg, log_len);
8400 txn->srv_cookie[log_len] = 0;
8401 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008402 }
8403
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008404 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008405 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008406 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008407 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8408 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008409 /* assume passive cookie by default */
8410 txn->flags &= ~TX_SCK_MASK;
8411 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008412
8413 /* If the cookie is in insert mode on a known server, we'll delete
8414 * this occurrence because we'll insert another one later.
8415 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008416 * a direct access.
8417 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008418 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008419 /* The "preserve" flag was set, we don't want to touch the
8420 * server's cookie.
8421 */
8422 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008423 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008424 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008425 /* this cookie must be deleted */
8426 if (*prev == ':' && next == hdr_end) {
8427 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008428 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008429 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8430 txn->hdr_idx.used--;
8431 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008432 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008433 hdr_next += delta;
8434 http_msg_move_end(&txn->rsp, delta);
8435 /* note: while both invalid now, <next> and <hdr_end>
8436 * are still equal, so the for() will stop as expected.
8437 */
8438 } else {
8439 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008440 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008441 next = prev;
8442 hdr_end += delta;
8443 hdr_next += delta;
8444 cur_hdr->len += delta;
8445 http_msg_move_end(&txn->rsp, delta);
8446 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008447 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008448 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008449 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008450 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008451 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008452 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008453 * with this server since we know it.
8454 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008455 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008456 next += delta;
8457 hdr_end += delta;
8458 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008459 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008460 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008461
Willy Tarreauf1348312010-10-07 15:54:11 +02008462 txn->flags &= ~TX_SCK_MASK;
8463 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008464 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008465 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008466 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008467 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008468 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008469 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008470 next += delta;
8471 hdr_end += delta;
8472 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008473 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008474 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008475
Willy Tarreau827aee92011-03-10 16:55:02 +01008476 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008477 txn->flags &= ~TX_SCK_MASK;
8478 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008479 }
8480 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008481 /* that's done for this cookie, check the next one on the same
8482 * line when next != hdr_end (only if is_cookie2).
8483 */
8484 }
8485 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008486 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008487 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008488}
8489
8490
Willy Tarreaua15645d2007-03-18 16:22:39 +01008491/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008492 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008493 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008494void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008495{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008496 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008497 char *p1, *p2;
8498
8499 char *cur_ptr, *cur_end, *cur_next;
8500 int cur_idx;
8501
Willy Tarreau5df51872007-11-25 16:20:08 +01008502 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008503 return;
8504
8505 /* Iterate through the headers.
8506 * we start with the start line.
8507 */
8508 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008509 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008510
8511 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8512 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008513 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008514
8515 cur_hdr = &txn->hdr_idx.v[cur_idx];
8516 cur_ptr = cur_next;
8517 cur_end = cur_ptr + cur_hdr->len;
8518 cur_next = cur_end + cur_hdr->cr + 1;
8519
8520 /* We have one full header between cur_ptr and cur_end, and the
8521 * next header starts at cur_next. We're only interested in
8522 * "Cookie:" headers.
8523 */
8524
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008525 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8526 if (val) {
8527 if ((cur_end - (cur_ptr + val) >= 8) &&
8528 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8529 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8530 return;
8531 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008532 }
8533
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008534 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8535 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008536 continue;
8537
8538 /* OK, right now we know we have a cache-control header at cur_ptr */
8539
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008540 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008541
8542 if (p1 >= cur_end) /* no more info */
8543 continue;
8544
8545 /* p1 is at the beginning of the value */
8546 p2 = p1;
8547
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008548 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008549 p2++;
8550
8551 /* we have a complete value between p1 and p2 */
8552 if (p2 < cur_end && *p2 == '=') {
8553 /* we have something of the form no-cache="set-cookie" */
8554 if ((cur_end - p1 >= 21) &&
8555 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8556 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008557 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008558 continue;
8559 }
8560
8561 /* OK, so we know that either p2 points to the end of string or to a comma */
8562 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008563 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008564 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8565 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8566 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008567 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008568 return;
8569 }
8570
8571 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008572 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008573 continue;
8574 }
8575 }
8576}
8577
Willy Tarreau58f10d72006-12-04 02:26:12 +01008578
Willy Tarreaub2513902006-12-17 14:52:38 +01008579/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008580 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008581 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008582 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008583 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008584 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008585 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008586 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008587 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008588int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008589{
8590 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008591 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008592 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008593
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008594 if (!uri_auth)
8595 return 0;
8596
Cyril Bonté70be45d2010-10-12 00:14:35 +02008597 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008598 return 0;
8599
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008600 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008601 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008602 return 0;
8603
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008604 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008605 return 0;
8606
Willy Tarreaub2513902006-12-17 14:52:38 +01008607 return 1;
8608}
8609
Willy Tarreau4076a152009-04-02 15:18:36 +02008610/*
8611 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008612 * By default it tries to report the error position as msg->err_pos. However if
8613 * this one is not set, it will then report msg->next, which is the last known
8614 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008615 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008616 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008617void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008618 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008619 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008620{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008621 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008622 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008623 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008624
Willy Tarreauf3764b72016-03-31 13:45:10 +02008625 es->len = MIN(chn->buf->i, global.tune.bufsize);
Willy Tarreau9b28e032012-10-12 23:49:43 +02008626 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008627 len1 = MIN(len1, es->len);
8628 len2 = es->len - len1; /* remaining data if buffer wraps */
8629
Willy Tarreauf3764b72016-03-31 13:45:10 +02008630 if (!es->buf)
8631 es->buf = malloc(global.tune.bufsize);
8632
8633 if (es->buf) {
8634 memcpy(es->buf, chn->buf->p, len1);
8635 if (len2)
8636 memcpy(es->buf + len1, chn->buf->data, len2);
8637 }
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008638
Willy Tarreau4076a152009-04-02 15:18:36 +02008639 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008640 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008641 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008642 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008643
Willy Tarreau4076a152009-04-02 15:18:36 +02008644 es->when = date; // user-visible date
8645 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008646 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008647 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008648 if (objt_conn(sess->origin))
8649 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008650 else
8651 memset(&es->src, 0, sizeof(es->src));
8652
Willy Tarreau078272e2010-12-12 12:46:33 +01008653 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008654 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008655 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008656 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008657 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008658 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008659 es->b_out = chn->buf->o;
8660 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008661 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008662 es->m_clen = msg->chunk_len;
8663 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008664}
Willy Tarreaub2513902006-12-17 14:52:38 +01008665
Willy Tarreau294c4732011-12-16 21:35:50 +01008666/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8667 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8668 * performed over the whole headers. Otherwise it must contain a valid header
8669 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8670 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8671 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8672 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008673 * -1. The value fetch stops at commas, so this function is suited for use with
8674 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008675 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008676 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008677unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008678 struct hdr_idx *idx, int occ,
8679 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008680{
Willy Tarreau294c4732011-12-16 21:35:50 +01008681 struct hdr_ctx local_ctx;
8682 char *ptr_hist[MAX_HDR_HISTORY];
8683 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008684 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008685 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008686
Willy Tarreau294c4732011-12-16 21:35:50 +01008687 if (!ctx) {
8688 local_ctx.idx = 0;
8689 ctx = &local_ctx;
8690 }
8691
Willy Tarreaubce70882009-09-07 11:51:47 +02008692 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008693 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008694 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008695 occ--;
8696 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008697 *vptr = ctx->line + ctx->val;
8698 *vlen = ctx->vlen;
8699 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008700 }
8701 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008702 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008703 }
8704
8705 /* negative occurrence, we scan all the list then walk back */
8706 if (-occ > MAX_HDR_HISTORY)
8707 return 0;
8708
Willy Tarreau294c4732011-12-16 21:35:50 +01008709 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008710 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008711 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8712 len_hist[hist_ptr] = ctx->vlen;
8713 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008714 hist_ptr = 0;
8715 found++;
8716 }
8717 if (-occ > found)
8718 return 0;
8719 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008720 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8721 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8722 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008723 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008724 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008725 if (hist_ptr >= MAX_HDR_HISTORY)
8726 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008727 *vptr = ptr_hist[hist_ptr];
8728 *vlen = len_hist[hist_ptr];
8729 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008730}
8731
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008732/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8733 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8734 * performed over the whole headers. Otherwise it must contain a valid header
8735 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8736 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8737 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8738 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8739 * -1. This function differs from http_get_hdr() in that it only returns full
8740 * line header values and does not stop at commas.
8741 * The return value is 0 if nothing was found, or non-zero otherwise.
8742 */
8743unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8744 struct hdr_idx *idx, int occ,
8745 struct hdr_ctx *ctx, char **vptr, int *vlen)
8746{
8747 struct hdr_ctx local_ctx;
8748 char *ptr_hist[MAX_HDR_HISTORY];
8749 int len_hist[MAX_HDR_HISTORY];
8750 unsigned int hist_ptr;
8751 int found;
8752
8753 if (!ctx) {
8754 local_ctx.idx = 0;
8755 ctx = &local_ctx;
8756 }
8757
8758 if (occ >= 0) {
8759 /* search from the beginning */
8760 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8761 occ--;
8762 if (occ <= 0) {
8763 *vptr = ctx->line + ctx->val;
8764 *vlen = ctx->vlen;
8765 return 1;
8766 }
8767 }
8768 return 0;
8769 }
8770
8771 /* negative occurrence, we scan all the list then walk back */
8772 if (-occ > MAX_HDR_HISTORY)
8773 return 0;
8774
8775 found = hist_ptr = 0;
8776 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8777 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8778 len_hist[hist_ptr] = ctx->vlen;
8779 if (++hist_ptr >= MAX_HDR_HISTORY)
8780 hist_ptr = 0;
8781 found++;
8782 }
8783 if (-occ > found)
8784 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008785
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008786 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008787 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8788 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8789 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008790 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008791 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008792 if (hist_ptr >= MAX_HDR_HISTORY)
8793 hist_ptr -= MAX_HDR_HISTORY;
8794 *vptr = ptr_hist[hist_ptr];
8795 *vlen = len_hist[hist_ptr];
8796 return 1;
8797}
8798
Willy Tarreaubaaee002006-06-26 02:48:02 +02008799/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008800 * Print a debug line with a header. Always stop at the first CR or LF char,
8801 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8802 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008803 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008804void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008805{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008806 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008807 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008808
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008809 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008810 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008811 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008812 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 +02008813
8814 for (max = 0; start + max < end; max++)
8815 if (start[max] == '\r' || start[max] == '\n')
8816 break;
8817
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008818 UBOUND(max, trash.size - trash.len - 3);
8819 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8820 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008821 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008822}
8823
Willy Tarreaueee5b512015-04-03 23:46:31 +02008824
8825/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8826 * The hdr_idx is allocated as well. In case of allocation failure, everything
8827 * allocated is freed and NULL is returned. Otherwise the new transaction is
8828 * assigned to the stream and returned.
8829 */
8830struct http_txn *http_alloc_txn(struct stream *s)
8831{
8832 struct http_txn *txn = s->txn;
8833
8834 if (txn)
8835 return txn;
8836
8837 txn = pool_alloc2(pool2_http_txn);
8838 if (!txn)
8839 return txn;
8840
8841 txn->hdr_idx.size = global.tune.max_http_hdr;
8842 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8843 if (!txn->hdr_idx.v) {
8844 pool_free2(pool2_http_txn, txn);
8845 return NULL;
8846 }
8847
8848 s->txn = txn;
8849 return txn;
8850}
8851
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008852void http_txn_reset_req(struct http_txn *txn)
8853{
8854 txn->req.flags = 0;
8855 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8856 txn->req.next = 0;
8857 txn->req.chunk_len = 0LL;
8858 txn->req.body_len = 0LL;
8859 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8860}
8861
8862void http_txn_reset_res(struct http_txn *txn)
8863{
8864 txn->rsp.flags = 0;
8865 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8866 txn->rsp.next = 0;
8867 txn->rsp.chunk_len = 0LL;
8868 txn->rsp.body_len = 0LL;
8869 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8870}
8871
Willy Tarreau0937bc42009-12-22 15:03:09 +01008872/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008873 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008874 * the required fields are properly allocated and that we only need to (re)init
8875 * them. This should be used before processing any new request.
8876 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008877void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008878{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008879 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008880 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008881
8882 txn->flags = 0;
8883 txn->status = -1;
8884
Willy Tarreauf64d1412010-10-07 20:06:11 +02008885 txn->cookie_first_date = 0;
8886 txn->cookie_last_date = 0;
8887
Willy Tarreaueee5b512015-04-03 23:46:31 +02008888 txn->srv_cookie = NULL;
8889 txn->cli_cookie = NULL;
8890 txn->uri = NULL;
8891
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008892 http_txn_reset_req(txn);
8893 http_txn_reset_res(txn);
8894
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008895 txn->req.chn = &s->req;
8896 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008897
8898 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008899
8900 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8901 if (fe->options2 & PR_O2_REQBUG_OK)
8902 txn->req.err_pos = -1; /* let buggy requests pass */
8903
Willy Tarreau0937bc42009-12-22 15:03:09 +01008904 if (txn->hdr_idx.v)
8905 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008906
8907 vars_init(&s->vars_txn, SCOPE_TXN);
8908 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008909}
8910
8911/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008912void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008913{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008914 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008915 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008916
8917 /* these ones will have been dynamically allocated */
8918 pool_free2(pool2_requri, txn->uri);
8919 pool_free2(pool2_capture, txn->cli_cookie);
8920 pool_free2(pool2_capture, txn->srv_cookie);
William Lallemanda73203e2012-03-12 12:48:57 +01008921 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008922
William Lallemanda73203e2012-03-12 12:48:57 +01008923 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008924 txn->uri = NULL;
8925 txn->srv_cookie = NULL;
8926 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008927
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008928 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008929 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008930 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008931 pool_free2(h->pool, s->req_cap[h->index]);
8932 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008933 }
8934
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008935 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008936 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008937 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008938 pool_free2(h->pool, s->res_cap[h->index]);
8939 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008940 }
8941
Willy Tarreau6204cd92016-03-10 16:33:04 +01008942 vars_prune(&s->vars_txn, s->sess, s);
8943 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008944}
8945
8946/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008947void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008948{
8949 http_end_txn(s);
8950 http_init_txn(s);
8951
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008952 /* reinitialise the current rule list pointer to NULL. We are sure that
8953 * any rulelist match the NULL pointer.
8954 */
8955 s->current_rule_list = NULL;
8956
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008957 s->be = strm_fe(s);
8958 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008959 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008960 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008961 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008962 /* re-init store persistence */
8963 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008964 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008965
Willy Tarreau0937bc42009-12-22 15:03:09 +01008966 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008967
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008968 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008969
Willy Tarreau739cfba2010-01-25 23:11:14 +01008970 /* We must trim any excess data from the response buffer, because we
8971 * may have blocked an invalid response from a server that we don't
8972 * want to accidentely forward once we disable the analysers, nor do
8973 * we want those data to come along with next response. A typical
8974 * example of such data would be from a buggy server responding to
8975 * a HEAD with some data, or sending more than the advertised
8976 * content-length.
8977 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008978 if (unlikely(s->res.buf->i))
8979 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008980
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008981 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008982 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008983
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008984 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008985 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008986
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008987 s->req.rex = TICK_ETERNITY;
8988 s->req.wex = TICK_ETERNITY;
8989 s->req.analyse_exp = TICK_ETERNITY;
8990 s->res.rex = TICK_ETERNITY;
8991 s->res.wex = TICK_ETERNITY;
8992 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008993}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008994
Sasha Pachev218f0642014-06-16 12:05:59 -06008995void free_http_res_rules(struct list *r)
8996{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008997 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06008998
8999 list_for_each_entry_safe(pr, tr, r, list) {
9000 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009001 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06009002 free(pr);
9003 }
9004}
9005
9006void free_http_req_rules(struct list *r)
9007{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009008 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01009009
9010 list_for_each_entry_safe(pr, tr, r, list) {
9011 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009012 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009013 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01009014
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009015 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01009016 free(pr);
9017 }
9018}
9019
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009020/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009021struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01009022{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009023 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009024 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009025 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009026 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01009027
Vincent Bernat02779b62016-04-03 13:48:43 +02009028 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01009029 if (!rule) {
9030 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009031 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009032 }
9033
CJ Ess108b1dd2015-04-07 12:03:37 -04009034 rule->deny_status = HTTP_ERR_403;
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009035 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009036 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01009037 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02009038 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04009039 int code;
9040 int hc;
9041
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009042 rule->action = ACT_ACTION_DENY;
Willy Tarreauff011f22011-01-06 17:51:27 +01009043 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04009044 if (strcmp(args[cur_arg], "deny_status") == 0) {
9045 cur_arg++;
9046 if (!args[cur_arg]) {
9047 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
9048 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9049 goto out_err;
9050 }
9051
9052 code = atol(args[cur_arg]);
9053 cur_arg++;
9054 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
9055 if (http_err_codes[hc] == code) {
9056 rule->deny_status = hc;
9057 break;
9058 }
9059 }
9060
9061 if (hc >= HTTP_ERR_SIZE) {
9062 Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n",
9063 file, linenum, code);
9064 }
9065 }
Willy Tarreauccbcc372012-12-27 12:37:57 +01009066 } else if (!strcmp(args[0], "tarpit")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009067 rule->action = ACT_HTTP_REQ_TARPIT;
Willy Tarreauccbcc372012-12-27 12:37:57 +01009068 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01009069 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009070 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01009071 cur_arg = 1;
9072
9073 while(*args[cur_arg]) {
9074 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009075 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01009076 cur_arg+=2;
9077 continue;
9078 } else
9079 break;
9080 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009081 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009082 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009083 cur_arg = 1;
9084
9085 if (!*args[cur_arg] ||
9086 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9087 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
9088 file, linenum, args[0]);
9089 goto out_err;
9090 }
9091 rule->arg.nice = atoi(args[cur_arg]);
9092 if (rule->arg.nice < -1024)
9093 rule->arg.nice = -1024;
9094 else if (rule->arg.nice > 1024)
9095 rule->arg.nice = 1024;
9096 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009097 } else if (!strcmp(args[0], "set-tos")) {
9098#ifdef IP_TOS
9099 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009100 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009101 cur_arg = 1;
9102
9103 if (!*args[cur_arg] ||
9104 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9105 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9106 file, linenum, args[0]);
9107 goto out_err;
9108 }
9109
9110 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9111 if (err && *err != '\0') {
9112 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9113 file, linenum, err, args[0]);
9114 goto out_err;
9115 }
9116 cur_arg++;
9117#else
9118 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9119 goto out_err;
9120#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009121 } else if (!strcmp(args[0], "set-mark")) {
9122#ifdef SO_MARK
9123 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009124 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009125 cur_arg = 1;
9126
9127 if (!*args[cur_arg] ||
9128 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9129 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9130 file, linenum, args[0]);
9131 goto out_err;
9132 }
9133
9134 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9135 if (err && *err != '\0') {
9136 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9137 file, linenum, err, args[0]);
9138 goto out_err;
9139 }
9140 cur_arg++;
9141 global.last_checks |= LSTCHK_NETADM;
9142#else
9143 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9144 goto out_err;
9145#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009146 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009147 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009148 cur_arg = 1;
9149
9150 if (!*args[cur_arg] ||
9151 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9152 bad_log_level:
9153 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9154 file, linenum, args[0]);
9155 goto out_err;
9156 }
9157 if (strcmp(args[cur_arg], "silent") == 0)
9158 rule->arg.loglevel = -1;
9159 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9160 goto bad_log_level;
9161 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009162 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009163 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009164 cur_arg = 1;
9165
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009166 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9167 (*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 +01009168 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9169 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009170 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009171 }
9172
9173 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9174 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9175 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009176
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009177 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009178 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009179 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 +01009180 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9181 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9182 file, linenum, args[0], error);
9183 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009184 goto out_err;
9185 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009186 free(proxy->conf.lfs_file);
9187 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9188 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009189 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009190 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009191 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009192 cur_arg = 1;
9193
9194 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009195 (*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 -06009196 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9197 file, linenum, args[0]);
9198 goto out_err;
9199 }
9200
9201 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9202 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9203 LIST_INIT(&rule->arg.hdr_add.fmt);
9204
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009205 error = NULL;
9206 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9207 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9208 args[cur_arg + 1], error);
9209 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009210 goto out_err;
9211 }
9212
9213 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009214 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009215 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 +01009216 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9217 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9218 file, linenum, args[0], error);
9219 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009220 goto out_err;
9221 }
Sasha Pachev218f0642014-06-16 12:05:59 -06009222
9223 free(proxy->conf.lfs_file);
9224 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9225 proxy->conf.lfs_line = proxy->conf.args.line;
9226 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009227 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009228 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009229 cur_arg = 1;
9230
9231 if (!*args[cur_arg] ||
9232 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9233 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9234 file, linenum, args[0]);
9235 goto out_err;
9236 }
9237
9238 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9239 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9240
9241 proxy->conf.args.ctx = ARGC_HRQ;
9242 free(proxy->conf.lfs_file);
9243 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9244 proxy->conf.lfs_line = proxy->conf.args.line;
9245 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009246 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9247 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009248 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009249 struct sample_expr *expr;
9250 unsigned int where;
9251 char *err = NULL;
9252
9253 cur_arg = 1;
9254 proxy->conf.args.ctx = ARGC_TRK;
9255
9256 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9257 if (!expr) {
9258 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9259 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9260 free(err);
9261 goto out_err;
9262 }
9263
9264 where = 0;
9265 if (proxy->cap & PR_CAP_FE)
9266 where |= SMP_VAL_FE_HRQ_HDR;
9267 if (proxy->cap & PR_CAP_BE)
9268 where |= SMP_VAL_BE_HRQ_HDR;
9269
9270 if (!(expr->fetch->val & where)) {
9271 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9272 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9273 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9274 args[cur_arg-1], sample_src_names(expr->fetch->use));
9275 free(expr);
9276 goto out_err;
9277 }
9278
9279 if (strcmp(args[cur_arg], "table") == 0) {
9280 cur_arg++;
9281 if (!args[cur_arg]) {
9282 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9283 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9284 free(expr);
9285 goto out_err;
9286 }
9287 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009288 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02009289 cur_arg++;
9290 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009291 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009292 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009293 } else if (strcmp(args[0], "redirect") == 0) {
9294 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009295 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009296
Willy Tarreaube4653b2015-05-28 15:26:58 +02009297 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009298 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9299 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9300 goto out_err;
9301 }
9302
9303 /* this redirect rule might already contain a parsed condition which
9304 * we'll pass to the http-request rule.
9305 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009306 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009307 rule->arg.redir = redir;
9308 rule->cond = redir->cond;
9309 redir->cond = NULL;
9310 cur_arg = 2;
9311 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009312 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9313 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009314 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009315 /*
9316 * '+ 8' for 'add-acl('
9317 * '- 9' for 'add-acl(' + trailing ')'
9318 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009319 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009320
9321 cur_arg = 1;
9322
9323 if (!*args[cur_arg] ||
9324 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9325 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9326 file, linenum, args[0]);
9327 goto out_err;
9328 }
9329
9330 LIST_INIT(&rule->arg.map.key);
9331 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009332 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009333 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009334 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9335 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9336 file, linenum, args[0], error);
9337 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009338 goto out_err;
9339 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009340 free(proxy->conf.lfs_file);
9341 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9342 proxy->conf.lfs_line = proxy->conf.args.line;
9343 cur_arg += 1;
9344 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9345 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009346 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009347 /*
9348 * '+ 8' for 'del-acl('
9349 * '- 9' for 'del-acl(' + trailing ')'
9350 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009351 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009352
9353 cur_arg = 1;
9354
9355 if (!*args[cur_arg] ||
9356 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9357 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9358 file, linenum, args[0]);
9359 goto out_err;
9360 }
9361
9362 LIST_INIT(&rule->arg.map.key);
9363 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009364 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009365 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009366 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9367 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9368 file, linenum, args[0], error);
9369 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009370 goto out_err;
9371 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009372 free(proxy->conf.lfs_file);
9373 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9374 proxy->conf.lfs_line = proxy->conf.args.line;
9375 cur_arg += 1;
9376 } else if (strncmp(args[0], "del-map", 7) == 0) {
9377 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009378 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009379 /*
9380 * '+ 8' for 'del-map('
9381 * '- 9' for 'del-map(' + trailing ')'
9382 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009383 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009384
9385 cur_arg = 1;
9386
9387 if (!*args[cur_arg] ||
9388 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9389 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9390 file, linenum, args[0]);
9391 goto out_err;
9392 }
9393
9394 LIST_INIT(&rule->arg.map.key);
9395 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009396 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009397 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009398 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9399 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9400 file, linenum, args[0], error);
9401 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009402 goto out_err;
9403 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009404 free(proxy->conf.lfs_file);
9405 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9406 proxy->conf.lfs_line = proxy->conf.args.line;
9407 cur_arg += 1;
9408 } else if (strncmp(args[0], "set-map", 7) == 0) {
9409 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009410 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009411 /*
9412 * '+ 8' for 'set-map('
9413 * '- 9' for 'set-map(' + trailing ')'
9414 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009415 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009416
9417 cur_arg = 1;
9418
9419 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9420 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9421 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9422 file, linenum, args[0]);
9423 goto out_err;
9424 }
9425
9426 LIST_INIT(&rule->arg.map.key);
9427 LIST_INIT(&rule->arg.map.value);
9428 proxy->conf.args.ctx = ARGC_HRQ;
9429
9430 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009431 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009432 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009433 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9434 Alert("parsing [%s:%d]: 'http-request %s' key: %s.\n",
9435 file, linenum, args[0], error);
9436 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009437 goto out_err;
9438 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009439
9440 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009441 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009442 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 +01009443 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9444 Alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n",
9445 file, linenum, args[0], error);
9446 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009447 goto out_err;
9448 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009449 free(proxy->conf.lfs_file);
9450 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9451 proxy->conf.lfs_line = proxy->conf.args.line;
9452
9453 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009454 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9455 char *errmsg = NULL;
9456 cur_arg = 1;
9457 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009458 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009459 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009460 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009461 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9462 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9463 free(errmsg);
9464 goto out_err;
9465 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009466 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009467 action_build_list(&http_req_keywords.list, &trash);
9468 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
9469 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009470 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02009471 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009472 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009473 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009474 }
9475
9476 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9477 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009478 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009479
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009480 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9481 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9482 file, linenum, args[0], errmsg);
9483 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009484 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009485 }
9486 rule->cond = cond;
9487 }
9488 else if (*args[cur_arg]) {
9489 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9490 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9491 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009492 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009493 }
9494
9495 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009496 out_err:
9497 free(rule);
9498 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009499}
9500
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009501/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009502struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009503{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009504 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009505 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009506 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009507 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009508
9509 rule = calloc(1, sizeof(*rule));
9510 if (!rule) {
9511 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9512 goto out_err;
9513 }
9514
9515 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009516 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009517 cur_arg = 1;
9518 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009519 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009520 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009521 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009522 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009523 cur_arg = 1;
9524
9525 if (!*args[cur_arg] ||
9526 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9527 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9528 file, linenum, args[0]);
9529 goto out_err;
9530 }
9531 rule->arg.nice = atoi(args[cur_arg]);
9532 if (rule->arg.nice < -1024)
9533 rule->arg.nice = -1024;
9534 else if (rule->arg.nice > 1024)
9535 rule->arg.nice = 1024;
9536 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009537 } else if (!strcmp(args[0], "set-tos")) {
9538#ifdef IP_TOS
9539 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009540 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009541 cur_arg = 1;
9542
9543 if (!*args[cur_arg] ||
9544 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9545 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9546 file, linenum, args[0]);
9547 goto out_err;
9548 }
9549
9550 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9551 if (err && *err != '\0') {
9552 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9553 file, linenum, err, args[0]);
9554 goto out_err;
9555 }
9556 cur_arg++;
9557#else
9558 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9559 goto out_err;
9560#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009561 } else if (!strcmp(args[0], "set-mark")) {
9562#ifdef SO_MARK
9563 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009564 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009565 cur_arg = 1;
9566
9567 if (!*args[cur_arg] ||
9568 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9569 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9570 file, linenum, args[0]);
9571 goto out_err;
9572 }
9573
9574 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9575 if (err && *err != '\0') {
9576 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9577 file, linenum, err, args[0]);
9578 goto out_err;
9579 }
9580 cur_arg++;
9581 global.last_checks |= LSTCHK_NETADM;
9582#else
9583 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9584 goto out_err;
9585#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009586 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009587 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009588 cur_arg = 1;
9589
9590 if (!*args[cur_arg] ||
9591 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9592 bad_log_level:
9593 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9594 file, linenum, args[0]);
9595 goto out_err;
9596 }
9597 if (strcmp(args[cur_arg], "silent") == 0)
9598 rule->arg.loglevel = -1;
Ruoshan Huangdd016782016-06-15 22:16:03 +08009599 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009600 goto bad_log_level;
9601 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009602 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009603 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009604 cur_arg = 1;
9605
9606 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9607 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9608 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9609 file, linenum, args[0]);
9610 goto out_err;
9611 }
9612
9613 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9614 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9615 LIST_INIT(&rule->arg.hdr_add.fmt);
9616
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009617 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009618 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009619 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 +01009620 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9621 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9622 file, linenum, args[0], error);
9623 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009624 goto out_err;
9625 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009626 free(proxy->conf.lfs_file);
9627 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9628 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009629 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009630 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009631 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009632 cur_arg = 1;
9633
9634 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009635 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9636 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009637 file, linenum, args[0]);
9638 goto out_err;
9639 }
9640
9641 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9642 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9643 LIST_INIT(&rule->arg.hdr_add.fmt);
9644
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009645 error = NULL;
9646 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9647 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9648 args[cur_arg + 1], error);
9649 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009650 goto out_err;
9651 }
9652
9653 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009654 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009655 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 +01009656 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9657 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9658 file, linenum, args[0], error);
9659 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009660 goto out_err;
9661 }
Sasha Pachev218f0642014-06-16 12:05:59 -06009662
9663 free(proxy->conf.lfs_file);
9664 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9665 proxy->conf.lfs_line = proxy->conf.args.line;
9666 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009667 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009668 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009669 cur_arg = 1;
9670
9671 if (!*args[cur_arg] ||
9672 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9673 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9674 file, linenum, args[0]);
9675 goto out_err;
9676 }
9677
9678 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9679 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9680
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009681 proxy->conf.args.ctx = ARGC_HRS;
9682 free(proxy->conf.lfs_file);
9683 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9684 proxy->conf.lfs_line = proxy->conf.args.line;
9685 cur_arg += 1;
9686 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9687 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009688 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009689 /*
9690 * '+ 8' for 'add-acl('
9691 * '- 9' for 'add-acl(' + trailing ')'
9692 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009693 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009694
9695 cur_arg = 1;
9696
9697 if (!*args[cur_arg] ||
9698 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9699 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9700 file, linenum, args[0]);
9701 goto out_err;
9702 }
9703
9704 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009705 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009706 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009707 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009708 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9709 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9710 file, linenum, args[0], error);
9711 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009712 goto out_err;
9713 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009714 free(proxy->conf.lfs_file);
9715 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9716 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009717
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009718 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009719 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9720 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009721 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009722 /*
9723 * '+ 8' for 'del-acl('
9724 * '- 9' for 'del-acl(' + trailing ')'
9725 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009726 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009727
9728 cur_arg = 1;
9729
9730 if (!*args[cur_arg] ||
9731 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9732 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9733 file, linenum, args[0]);
9734 goto out_err;
9735 }
9736
9737 LIST_INIT(&rule->arg.map.key);
9738 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009739 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009740 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009741 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9742 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9743 file, linenum, args[0], error);
9744 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009745 goto out_err;
9746 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009747 free(proxy->conf.lfs_file);
9748 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9749 proxy->conf.lfs_line = proxy->conf.args.line;
9750 cur_arg += 1;
9751 } else if (strncmp(args[0], "del-map", 7) == 0) {
9752 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009753 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009754 /*
9755 * '+ 8' for 'del-map('
9756 * '- 9' for 'del-map(' + trailing ')'
9757 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009758 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009759
9760 cur_arg = 1;
9761
9762 if (!*args[cur_arg] ||
9763 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9764 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9765 file, linenum, args[0]);
9766 goto out_err;
9767 }
9768
9769 LIST_INIT(&rule->arg.map.key);
9770 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009771 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009772 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009773 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9774 Alert("parsing [%s:%d]: 'http-response %s' %s.\n",
9775 file, linenum, args[0], error);
9776 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009777 goto out_err;
9778 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009779 free(proxy->conf.lfs_file);
9780 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9781 proxy->conf.lfs_line = proxy->conf.args.line;
9782 cur_arg += 1;
9783 } else if (strncmp(args[0], "set-map", 7) == 0) {
9784 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009785 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009786 /*
9787 * '+ 8' for 'set-map('
9788 * '- 9' for 'set-map(' + trailing ')'
9789 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009790 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009791
9792 cur_arg = 1;
9793
9794 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9795 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9796 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9797 file, linenum, args[0]);
9798 goto out_err;
9799 }
9800
9801 LIST_INIT(&rule->arg.map.key);
9802 LIST_INIT(&rule->arg.map.value);
9803
9804 proxy->conf.args.ctx = ARGC_HRS;
9805
9806 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009807 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009808 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009809 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9810 Alert("parsing [%s:%d]: 'http-response %s' name: %s.\n",
9811 file, linenum, args[0], error);
9812 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009813 goto out_err;
9814 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009815
9816 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009817 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009818 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 +01009819 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9820 Alert("parsing [%s:%d]: 'http-response %s' value: %s.\n",
9821 file, linenum, args[0], error);
9822 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009823 goto out_err;
9824 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009825
9826 free(proxy->conf.lfs_file);
9827 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9828 proxy->conf.lfs_line = proxy->conf.args.line;
9829
9830 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009831 } else if (strcmp(args[0], "redirect") == 0) {
9832 struct redirect_rule *redir;
9833 char *errmsg = NULL;
9834
9835 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
9836 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9837 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9838 goto out_err;
9839 }
9840
9841 /* this redirect rule might already contain a parsed condition which
9842 * we'll pass to the http-request rule.
9843 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009844 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009845 rule->arg.redir = redir;
9846 rule->cond = redir->cond;
9847 redir->cond = NULL;
9848 cur_arg = 2;
9849 return rule;
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009850 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9851 args[0][9] == '\0' && args[0][8] >= '0' &&
9852 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
9853 struct sample_expr *expr;
9854 unsigned int where;
9855 char *err = NULL;
9856
9857 cur_arg = 1;
9858 proxy->conf.args.ctx = ARGC_TRK;
9859
9860 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9861 if (!expr) {
9862 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9863 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9864 free(err);
9865 goto out_err;
9866 }
9867
9868 where = 0;
9869 if (proxy->cap & PR_CAP_FE)
9870 where |= SMP_VAL_FE_HRS_HDR;
9871 if (proxy->cap & PR_CAP_BE)
9872 where |= SMP_VAL_BE_HRS_HDR;
9873
9874 if (!(expr->fetch->val & where)) {
9875 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :"
9876 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9877 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9878 args[cur_arg-1], sample_src_names(expr->fetch->use));
9879 free(expr);
9880 goto out_err;
9881 }
9882
9883 if (strcmp(args[cur_arg], "table") == 0) {
9884 cur_arg++;
9885 if (!args[cur_arg]) {
9886 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n",
9887 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9888 free(expr);
9889 goto out_err;
9890 }
9891 /* we copy the table name for now, it will be resolved later */
9892 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
9893 cur_arg++;
9894 }
9895 rule->arg.trk_ctr.expr = expr;
9896 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
William Lallemand73025dd2014-04-24 14:38:37 +02009897 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9898 char *errmsg = NULL;
9899 cur_arg = 1;
9900 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009901 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009902 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009903 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009904 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9905 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9906 free(errmsg);
9907 goto out_err;
9908 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009909 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009910 action_build_list(&http_res_keywords.list, &trash);
9911 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9912 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009913 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02009914 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009915 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009916 goto out_err;
9917 }
9918
9919 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9920 struct acl_cond *cond;
9921 char *errmsg = NULL;
9922
9923 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9924 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9925 file, linenum, args[0], errmsg);
9926 free(errmsg);
9927 goto out_err;
9928 }
9929 rule->cond = cond;
9930 }
9931 else if (*args[cur_arg]) {
9932 Alert("parsing [%s:%d]: 'http-response %s' expects"
9933 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9934 file, linenum, args[0], args[cur_arg]);
9935 goto out_err;
9936 }
9937
9938 return rule;
9939 out_err:
9940 free(rule);
9941 return NULL;
9942}
9943
Willy Tarreau4baae242012-12-27 12:00:31 +01009944/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009945 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009946 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9947 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009948 */
9949struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009950 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009951{
9952 struct redirect_rule *rule;
9953 int cur_arg;
9954 int type = REDIRECT_TYPE_NONE;
9955 int code = 302;
9956 const char *destination = NULL;
9957 const char *cookie = NULL;
9958 int cookie_set = 0;
9959 unsigned int flags = REDIRECT_FLAG_NONE;
9960 struct acl_cond *cond = NULL;
9961
9962 cur_arg = 0;
9963 while (*(args[cur_arg])) {
9964 if (strcmp(args[cur_arg], "location") == 0) {
9965 if (!*args[cur_arg + 1])
9966 goto missing_arg;
9967
9968 type = REDIRECT_TYPE_LOCATION;
9969 cur_arg++;
9970 destination = args[cur_arg];
9971 }
9972 else if (strcmp(args[cur_arg], "prefix") == 0) {
9973 if (!*args[cur_arg + 1])
9974 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009975 type = REDIRECT_TYPE_PREFIX;
9976 cur_arg++;
9977 destination = args[cur_arg];
9978 }
9979 else if (strcmp(args[cur_arg], "scheme") == 0) {
9980 if (!*args[cur_arg + 1])
9981 goto missing_arg;
9982
9983 type = REDIRECT_TYPE_SCHEME;
9984 cur_arg++;
9985 destination = args[cur_arg];
9986 }
9987 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9988 if (!*args[cur_arg + 1])
9989 goto missing_arg;
9990
9991 cur_arg++;
9992 cookie = args[cur_arg];
9993 cookie_set = 1;
9994 }
9995 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9996 if (!*args[cur_arg + 1])
9997 goto missing_arg;
9998
9999 cur_arg++;
10000 cookie = args[cur_arg];
10001 cookie_set = 0;
10002 }
10003 else if (strcmp(args[cur_arg], "code") == 0) {
10004 if (!*args[cur_arg + 1])
10005 goto missing_arg;
10006
10007 cur_arg++;
10008 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040010009 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +010010010 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040010011 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +010010012 args[cur_arg - 1], args[cur_arg]);
10013 return NULL;
10014 }
10015 }
10016 else if (!strcmp(args[cur_arg],"drop-query")) {
10017 flags |= REDIRECT_FLAG_DROP_QS;
10018 }
10019 else if (!strcmp(args[cur_arg],"append-slash")) {
10020 flags |= REDIRECT_FLAG_APPEND_SLASH;
10021 }
10022 else if (strcmp(args[cur_arg], "if") == 0 ||
10023 strcmp(args[cur_arg], "unless") == 0) {
10024 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
10025 if (!cond) {
10026 memprintf(errmsg, "error in condition: %s", *errmsg);
10027 return NULL;
10028 }
10029 break;
10030 }
10031 else {
10032 memprintf(errmsg,
10033 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
10034 args[cur_arg]);
10035 return NULL;
10036 }
10037 cur_arg++;
10038 }
10039
10040 if (type == REDIRECT_TYPE_NONE) {
10041 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
10042 return NULL;
10043 }
10044
Willy Tarreaube4653b2015-05-28 15:26:58 +020010045 if (dir && type != REDIRECT_TYPE_LOCATION) {
10046 memprintf(errmsg, "response only supports redirect type 'location'");
10047 return NULL;
10048 }
10049
Vincent Bernat3c2f2f22016-04-03 13:48:42 +020010050 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +010010051 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +010010052 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010053
10054 if (!use_fmt) {
10055 /* old-style static redirect rule */
10056 rule->rdr_str = strdup(destination);
10057 rule->rdr_len = strlen(destination);
10058 }
10059 else {
10060 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010061
10062 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
10063 * if prefix == "/", we don't want to add anything, otherwise it
10064 * makes it hard for the user to configure a self-redirection.
10065 */
Godbachd9722032014-12-18 15:44:58 +080010066 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010067 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010010068 if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
10069 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 +010010070 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
10071 errmsg)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010010072 return NULL;
10073 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +010010074 free(curproxy->conf.lfs_file);
10075 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
10076 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010077 }
10078 }
10079
Willy Tarreau4baae242012-12-27 12:00:31 +010010080 if (cookie) {
10081 /* depending on cookie_set, either we want to set the cookie, or to clear it.
10082 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
10083 */
10084 rule->cookie_len = strlen(cookie);
10085 if (cookie_set) {
10086 rule->cookie_str = malloc(rule->cookie_len + 10);
10087 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10088 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
10089 rule->cookie_len += 9;
10090 } else {
10091 rule->cookie_str = malloc(rule->cookie_len + 21);
10092 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10093 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
10094 rule->cookie_len += 20;
10095 }
10096 }
10097 rule->type = type;
10098 rule->code = code;
10099 rule->flags = flags;
10100 LIST_INIT(&rule->list);
10101 return rule;
10102
10103 missing_arg:
10104 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
10105 return NULL;
10106}
10107
Willy Tarreau8797c062007-05-07 00:55:35 +020010108/************************************************************************/
10109/* The code below is dedicated to ACL parsing and matching */
10110/************************************************************************/
10111
10112
Willy Tarreau14174bc2012-04-16 14:34:04 +020010113/* This function ensures that the prerequisites for an L7 fetch are ready,
10114 * which means that a request or response is ready. If some data is missing,
10115 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +020010116 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
10117 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010118 *
10119 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +020010120 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
10121 * decide whether or not an HTTP message is present ;
10122 * 0 if the requested data cannot be fetched or if it is certain that
10123 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010124 * 1 if an HTTP message is ready
10125 */
James Rosewell91a41cb2015-09-18 17:11:16 +010010126int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010127 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +020010128{
Willy Tarreau192252e2015-04-04 01:47:55 +020010129 struct http_txn *txn;
10130 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010131
Willy Tarreaube508f12016-03-10 11:47:01 +010010132 /* Note: it is possible that <s> is NULL when called before stream
10133 * initialization (eg: tcp-request connection), so this function is the
10134 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010135 */
Willy Tarreau192252e2015-04-04 01:47:55 +020010136 if (!s)
10137 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +010010138
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +020010139 if (!s->txn) {
10140 if (unlikely(!http_alloc_txn(s)))
10141 return 0; /* not enough memory */
10142 http_init_txn(s);
10143 }
Willy Tarreau192252e2015-04-04 01:47:55 +020010144 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +020010145 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010146
10147 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010148 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010149
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010150 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010151 /* If the buffer does not leave enough free space at the end,
10152 * we must first realign it.
10153 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010154 if (s->req.buf->p > s->req.buf->data &&
10155 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
10156 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +010010157
Willy Tarreau14174bc2012-04-16 14:34:04 +020010158 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +020010159 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +020010160 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010161
10162 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010163 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +020010164 http_msg_analyzer(msg, &txn->hdr_idx);
10165
10166 /* Still no valid request ? */
10167 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +020010168 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010169 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +020010170 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010171 }
10172 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +020010173 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010174 return 0;
10175 }
10176
10177 /* OK we just got a valid HTTP request. We have some minor
10178 * preparation to perform so that further checks can rely
10179 * on HTTP tests.
10180 */
Willy Tarreauaae75e32013-03-29 12:31:49 +010010181
10182 /* If the request was parsed but was too large, we must absolutely
10183 * return an error so that it is not processed. At the moment this
10184 * cannot happen, but if the parsers are to change in the future,
10185 * we want this check to be maintained.
10186 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010187 if (unlikely(s->req.buf->i + s->req.buf->p >
10188 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreau10e61cb2017-01-04 14:51:22 +010010189 msg->err_state = msg->msg_state;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010190 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010191 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010192 return 1;
10193 }
10194
Willy Tarreau9b28e032012-10-12 23:49:43 +020010195 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +020010196 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +020010197 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010198
Willy Tarreau506d0502013-07-06 13:29:24 +020010199 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
10200 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010201 }
10202
Willy Tarreau506d0502013-07-06 13:29:24 +020010203 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +020010204 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +020010205 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010206
10207 /* otherwise everything's ready for the request */
10208 }
Willy Tarreau24e32d82012-04-23 23:55:44 +020010209 else {
10210 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +020010211 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
10212 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010213 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +020010214 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010215 }
10216
10217 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010218 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010219 return 1;
10220}
Willy Tarreau8797c062007-05-07 00:55:35 +020010221
Willy Tarreau8797c062007-05-07 00:55:35 +020010222/* 1. Check on METHOD
10223 * We use the pre-parsed method if it is known, and store its number as an
10224 * integer. If it is unknown, we use the pointer and the length.
10225 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010226static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +020010227{
10228 int len, meth;
10229
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010010230 len = strlen(text);
10231 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +020010232
10233 pattern->val.i = meth;
10234 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +020010235 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +020010236 pattern->len = len;
10237 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010238 else {
10239 pattern->ptr.str = NULL;
10240 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010241 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010242 return 1;
10243}
10244
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010245/* This function fetches the method of current HTTP request and stores
10246 * it in the global pattern struct as a chunk. There are two possibilities :
10247 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10248 * in <len> and <ptr> is NULL ;
10249 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10250 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010251 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010252 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010253static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010254smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010255{
10256 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +010010257 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010258
Willy Tarreau24e32d82012-04-23 23:55:44 +020010259 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010260
Willy Tarreaube508f12016-03-10 11:47:01 +010010261 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010262 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010263 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010264 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010265 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010266 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10267 /* ensure the indexes are not affected */
10268 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010269 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010270 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
10271 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010272 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010273 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010274 return 1;
10275}
10276
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010277/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010278static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010279{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010280 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010281 struct pattern_list *lst;
10282 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010283
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010284 list_for_each_entry(lst, &expr->patterns, list) {
10285 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010286
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010287 /* well-known method */
10288 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010289 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010290 return pattern;
10291 else
10292 continue;
10293 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010294
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010295 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010296 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010297 continue;
10298
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010299 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010300 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
10301 (!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 +010010302 return pattern;
10303 }
10304 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010305}
10306
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010307static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010308smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010309{
Willy Tarreaube508f12016-03-10 11:47:01 +010010310 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010311 char *ptr;
10312 int len;
10313
Willy Tarreauc0239e02012-04-16 14:42:55 +020010314 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010315
Willy Tarreaube508f12016-03-10 11:47:01 +010010316 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010317 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010318 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010319
10320 while ((len-- > 0) && (*ptr++ != '/'));
10321 if (len <= 0)
10322 return 0;
10323
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010324 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010325 smp->data.u.str.str = ptr;
10326 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010327
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010328 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010329 return 1;
10330}
10331
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010332static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010333smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010334{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010335 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
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010341 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010342 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10343 return 0;
10344
Willy Tarreau8797c062007-05-07 00:55:35 +020010345 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010346 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010347
10348 while ((len-- > 0) && (*ptr++ != '/'));
10349 if (len <= 0)
10350 return 0;
10351
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010352 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010353 smp->data.u.str.str = ptr;
10354 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010355
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010356 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010357 return 1;
10358}
10359
10360/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010361static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010362smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010363{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010364 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010365 char *ptr;
10366 int len;
10367
Willy Tarreauc0239e02012-04-16 14:42:55 +020010368 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010369
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010370 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010371 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10372 return 0;
10373
Willy Tarreau8797c062007-05-07 00:55:35 +020010374 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010375 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010376
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010377 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010378 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010379 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010380 return 1;
10381}
10382
Thierry Fournierf4011dd2016-03-29 17:23:51 +020010383static int
10384smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
10385{
10386 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
10387 return 0;
10388
10389 if (!smp->strm->unique_id) {
10390 if ((smp->strm->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
10391 return 0;
10392 smp->strm->unique_id[0] = '\0';
10393 }
10394 smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id,
10395 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
10396
10397 smp->data.type = SMP_T_STR;
10398 smp->data.u.str.str = smp->strm->unique_id;
10399 smp->flags = SMP_F_CONST;
10400 return 1;
10401}
10402
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010403/* returns the longest available part of the body. This requires that the body
10404 * has been waited for using http-buffer-request.
10405 */
10406static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010407smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010408{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010409 struct http_msg *msg;
10410 unsigned long len;
10411 unsigned long block1;
10412 char *body;
10413 struct chunk *temp;
10414
10415 CHECK_HTTP_MESSAGE_FIRST();
10416
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010417 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010418 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010419 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010420 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010421
10422 len = http_body_bytes(msg);
10423 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
10424
10425 block1 = len;
10426 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
10427 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
10428
10429 if (block1 == len) {
10430 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010431 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010432 smp->data.u.str.str = body;
10433 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010434 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
10435 }
10436 else {
10437 /* buffer is wrapped, we need to defragment it */
10438 temp = get_trash_chunk();
10439 memcpy(temp->str, body, block1);
10440 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010441 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010442 smp->data.u.str.str = temp->str;
10443 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010444 smp->flags = SMP_F_VOL_TEST;
10445 }
10446 return 1;
10447}
10448
10449
10450/* returns the available length of the body. This requires that the body
10451 * has been waited for using http-buffer-request.
10452 */
10453static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010454smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010455{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010456 struct http_msg *msg;
10457
10458 CHECK_HTTP_MESSAGE_FIRST();
10459
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010460 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010461 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010462 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010463 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010464
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010465 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010466 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010467
10468 smp->flags = SMP_F_VOL_TEST;
10469 return 1;
10470}
10471
10472
10473/* returns the advertised length of the body, or the advertised size of the
10474 * chunks available in the buffer. This requires that the body has been waited
10475 * for using http-buffer-request.
10476 */
10477static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010478smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010479{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010480 struct http_msg *msg;
10481
10482 CHECK_HTTP_MESSAGE_FIRST();
10483
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010484 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010485 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010486 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010487 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010488
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010489 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010490 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010491
10492 smp->flags = SMP_F_VOL_TEST;
10493 return 1;
10494}
10495
10496
Willy Tarreau8797c062007-05-07 00:55:35 +020010497/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010498static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010499smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010500{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010501 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010502
Willy Tarreauc0239e02012-04-16 14:42:55 +020010503 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010504
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010505 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010506 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010507 smp->data.u.str.len = txn->req.sl.rq.u_l;
10508 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010509 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010510 return 1;
10511}
10512
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010513static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010514smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010515{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010516 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010517 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010518
Willy Tarreauc0239e02012-04-16 14:42:55 +020010519 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010520
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010521 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010522 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 +020010523 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010524 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010525
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010526 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010527 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010528 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010529 return 1;
10530}
10531
10532static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010533smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010534{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010535 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010536 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010537
Willy Tarreauc0239e02012-04-16 14:42:55 +020010538 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010539
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010540 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010541 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 +020010542 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10543 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010544
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010545 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010546 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010547 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010548 return 1;
10549}
10550
Willy Tarreau185b5c42012-04-26 15:11:51 +020010551/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10552 * Accepts an optional argument of type string containing the header field name,
10553 * and an optional argument of type signed or unsigned integer to request an
10554 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010555 * headers are considered from the first one. It does not stop on commas and
10556 * returns full lines instead (useful for User-Agent or Date for example).
10557 */
10558static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010559smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010560{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010561 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010562 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010563 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010564 int occ = 0;
10565 const char *name_str = NULL;
10566 int name_len = 0;
10567
10568 if (!ctx) {
10569 /* first call */
10570 ctx = &static_hdr_ctx;
10571 ctx->idx = 0;
10572 smp->ctx.a[0] = ctx;
10573 }
10574
10575 if (args) {
10576 if (args[0].type != ARGT_STR)
10577 return 0;
10578 name_str = args[0].data.str.str;
10579 name_len = args[0].data.str.len;
10580
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010581 if (args[1].type == ARGT_SINT)
10582 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010583 }
10584
10585 CHECK_HTTP_MESSAGE_FIRST();
10586
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010587 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010588 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 +020010589
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010590 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10591 /* search for header from the beginning */
10592 ctx->idx = 0;
10593
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010594 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010595 /* no explicit occurrence and single fetch => last header by default */
10596 occ = -1;
10597
10598 if (!occ)
10599 /* prepare to report multiple occurrences for ACL fetches */
10600 smp->flags |= SMP_F_NOT_LAST;
10601
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010602 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010603 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010604 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 +020010605 return 1;
10606
10607 smp->flags &= ~SMP_F_NOT_LAST;
10608 return 0;
10609}
10610
10611/* 6. Check on HTTP header count. The number of occurrences is returned.
10612 * Accepts exactly 1 argument of type string. It does not stop on commas and
10613 * returns full lines instead (useful for User-Agent or Date for example).
10614 */
10615static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010616smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010617{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010618 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010619 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010620 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010621 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010622 const char *name = NULL;
10623 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010624
Willy Tarreau601a4d12015-04-01 19:16:09 +020010625 if (args && args->type == ARGT_STR) {
10626 name = args->data.str.str;
10627 len = args->data.str.len;
10628 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010629
10630 CHECK_HTTP_MESSAGE_FIRST();
10631
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010632 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010633 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 +020010634
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010635 ctx.idx = 0;
10636 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010637 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010638 cnt++;
10639
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010640 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010641 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010642 smp->flags = SMP_F_VOL_HDR;
10643 return 1;
10644}
10645
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010646static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010647smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010648{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010649 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010650 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010651 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010652 struct chunk *temp;
10653 char del = ',';
10654
10655 if (args && args->type == ARGT_STR)
10656 del = *args[0].data.str.str;
10657
10658 CHECK_HTTP_MESSAGE_FIRST();
10659
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010660 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010661 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 +020010662
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010663 temp = get_trash_chunk();
10664
10665 ctx.idx = 0;
10666 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10667 if (temp->len)
10668 temp->str[temp->len++] = del;
10669 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10670 temp->len += ctx.del;
10671 }
10672
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010673 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010674 smp->data.u.str.str = temp->str;
10675 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010676 smp->flags = SMP_F_VOL_HDR;
10677 return 1;
10678}
10679
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010680/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10681 * Accepts an optional argument of type string containing the header field name,
10682 * and an optional argument of type signed or unsigned integer to request an
10683 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010684 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010685 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010686static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010687smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010688{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010689 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010690 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010691 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010692 int occ = 0;
10693 const char *name_str = NULL;
10694 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010695
Willy Tarreaua890d072013-04-02 12:01:06 +020010696 if (!ctx) {
10697 /* first call */
10698 ctx = &static_hdr_ctx;
10699 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010700 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010701 }
10702
Willy Tarreau185b5c42012-04-26 15:11:51 +020010703 if (args) {
10704 if (args[0].type != ARGT_STR)
10705 return 0;
10706 name_str = args[0].data.str.str;
10707 name_len = args[0].data.str.len;
10708
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010709 if (args[1].type == ARGT_SINT)
10710 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010711 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010712
Willy Tarreaue333ec92012-04-16 16:26:40 +020010713 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010714
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010715 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010716 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 +020010717
Willy Tarreau185b5c42012-04-26 15:11:51 +020010718 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010719 /* search for header from the beginning */
10720 ctx->idx = 0;
10721
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010722 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010723 /* no explicit occurrence and single fetch => last header by default */
10724 occ = -1;
10725
10726 if (!occ)
10727 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010728 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010729
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010730 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010731 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010732 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 +020010733 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010734
Willy Tarreau37406352012-04-23 16:16:37 +020010735 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010736 return 0;
10737}
10738
Willy Tarreauc11416f2007-06-17 16:58:38 +020010739/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010740 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010741 */
10742static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010743smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010744{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010745 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010746 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010747 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010748 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010749 const char *name = NULL;
10750 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010751
Willy Tarreau601a4d12015-04-01 19:16:09 +020010752 if (args && args->type == ARGT_STR) {
10753 name = args->data.str.str;
10754 len = args->data.str.len;
10755 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010756
Willy Tarreaue333ec92012-04-16 16:26:40 +020010757 CHECK_HTTP_MESSAGE_FIRST();
10758
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010759 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010760 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 +020010761
Willy Tarreau33a7e692007-06-10 19:45:56 +020010762 ctx.idx = 0;
10763 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010764 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010765 cnt++;
10766
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010767 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010768 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010769 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010770 return 1;
10771}
10772
Willy Tarreau185b5c42012-04-26 15:11:51 +020010773/* Fetch an HTTP header's integer value. The integer value is returned. It
10774 * takes a mandatory argument of type string and an optional one of type int
10775 * to designate a specific occurrence. It returns an unsigned integer, which
10776 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010777 */
10778static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010779smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010780{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010781 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010782
Willy Tarreauf853c462012-04-23 18:53:56 +020010783 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010784 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010785 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010786 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010787
Willy Tarreaud53e2422012-04-16 17:21:11 +020010788 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010789}
10790
Cyril Bonté69fa9922012-10-25 00:01:06 +020010791/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10792 * and an optional one of type int to designate a specific occurrence.
10793 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010794 */
10795static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010796smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010797{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010798 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010799
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010800 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010801 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010802 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010803 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010804 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010805 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010806 if (smp->data.u.str.len < temp->size - 1) {
10807 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10808 temp->str[smp->data.u.str.len] = '\0';
10809 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010810 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010811 break;
10812 }
10813 }
10814 }
10815
Willy Tarreaud53e2422012-04-16 17:21:11 +020010816 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010817 if (!(smp->flags & SMP_F_NOT_LAST))
10818 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010819 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010820 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010821}
10822
Willy Tarreau737b0c12007-06-10 21:28:46 +020010823/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10824 * the first '/' after the possible hostname, and ends before the possible '?'.
10825 */
10826static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010827smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010828{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010829 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010830 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010831
Willy Tarreauc0239e02012-04-16 14:42:55 +020010832 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010833
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010834 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010835 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010836 ptr = http_get_path(txn);
10837 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010838 return 0;
10839
10840 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010841 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010842 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010843
10844 while (ptr < end && *ptr != '?')
10845 ptr++;
10846
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010847 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010848 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010849 return 1;
10850}
10851
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010852/* This produces a concatenation of the first occurrence of the Host header
10853 * followed by the path component if it begins with a slash ('/'). This means
10854 * that '*' will not be added, resulting in exactly the first Host entry.
10855 * If no Host header is found, then the path is returned as-is. The returned
10856 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010857 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010858 */
10859static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010860smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010861{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010862 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010863 char *ptr, *end, *beg;
10864 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010865 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010866
10867 CHECK_HTTP_MESSAGE_FIRST();
10868
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010869 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010870 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010871 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010872 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010873
10874 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010875 temp = get_trash_chunk();
10876 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010877 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010878 smp->data.u.str.str = temp->str;
10879 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010880
10881 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010882 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010883 beg = http_get_path(txn);
10884 if (!beg)
10885 beg = end;
10886
10887 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10888
10889 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010890 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10891 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010892 }
10893
10894 smp->flags = SMP_F_VOL_1ST;
10895 return 1;
10896}
10897
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010898/* This produces a 32-bit hash of the concatenation of the first occurrence of
10899 * the Host header followed by the path component if it begins with a slash ('/').
10900 * This means that '*' will not be added, resulting in exactly the first Host
10901 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010902 * is hashed using the path hash followed by a full avalanche hash and provides a
10903 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010904 * high-traffic sites without having to store whole paths.
10905 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010906int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010907smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010908{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010909 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010910 struct hdr_ctx ctx;
10911 unsigned int hash = 0;
10912 char *ptr, *beg, *end;
10913 int len;
10914
10915 CHECK_HTTP_MESSAGE_FIRST();
10916
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010917 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010918 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010919 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010920 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10921 ptr = ctx.line + ctx.val;
10922 len = ctx.vlen;
10923 while (len--)
10924 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10925 }
10926
10927 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010928 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010929 beg = http_get_path(txn);
10930 if (!beg)
10931 beg = end;
10932
10933 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10934
10935 if (beg < ptr && *beg == '/') {
10936 while (beg < ptr)
10937 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10938 }
10939 hash = full_hash(hash);
10940
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010941 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010942 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010943 smp->flags = SMP_F_VOL_1ST;
10944 return 1;
10945}
10946
Willy Tarreau4a550602012-12-09 14:53:32 +010010947/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010948 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10949 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10950 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010951 * that in environments where IPv6 is insignificant, truncating the output to
10952 * 8 bytes would still work.
10953 */
10954static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010955smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010956{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010957 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010958 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010959
10960 if (!cli_conn)
10961 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010962
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010963 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010964 return 0;
10965
Willy Tarreau47ca5452012-12-23 20:22:19 +010010966 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010967 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010968 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010969
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010970 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010971 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010972 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010973 temp->len += 4;
10974 break;
10975 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010976 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010977 temp->len += 16;
10978 break;
10979 default:
10980 return 0;
10981 }
10982
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010983 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010984 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010985 return 1;
10986}
10987
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010988/* Extracts the query string, which comes after the question mark '?'. If no
10989 * question mark is found, nothing is returned. Otherwise it returns a sample
10990 * of type string carrying the whole query string.
10991 */
10992static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010993smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010994{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010995 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010996 char *ptr, *end;
10997
10998 CHECK_HTTP_MESSAGE_FIRST();
10999
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011000 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011001 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
11002 end = ptr + txn->req.sl.rq.u_l;
11003
11004 /* look up the '?' */
11005 do {
11006 if (ptr == end)
11007 return 0;
11008 } while (*ptr++ != '?');
11009
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011010 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011011 smp->data.u.str.str = ptr;
11012 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010011013 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11014 return 1;
11015}
11016
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011017static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011018smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011019{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011020 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
11021 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
11022 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011023
Willy Tarreau24e32d82012-04-23 23:55:44 +020011024 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011025
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011026 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011027 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020011028 return 1;
11029}
11030
Willy Tarreau7f18e522010-10-22 20:04:13 +020011031/* return a valid test if the current request is the first one on the connection */
11032static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011033smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020011034{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011035 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011036 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020011037 return 1;
11038}
11039
Willy Tarreau34db1082012-04-19 17:16:54 +020011040/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011041static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011042smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011043{
11044
Willy Tarreau24e32d82012-04-23 23:55:44 +020011045 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011046 return 0;
11047
Willy Tarreauc0239e02012-04-16 14:42:55 +020011048 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011049
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011050 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011051 return 0;
11052
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011053 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011054 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011055 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011056 return 1;
11057}
Willy Tarreau8797c062007-05-07 00:55:35 +020011058
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011059/* Accepts exactly 1 argument of type userlist */
11060static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011061smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011062{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011063 if (!args || args->type != ARGT_USR)
11064 return 0;
11065
11066 CHECK_HTTP_MESSAGE_FIRST();
11067
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011068 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011069 return 0;
11070
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011071 /* if the user does not belong to the userlist or has a wrong password,
11072 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010011073 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011074 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011075 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
11076 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010011077 return 0;
11078
11079 /* pat_match_auth() will need the user list */
11080 smp->ctx.a[0] = args->data.usr;
11081
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011082 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011083 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011084 smp->data.u.str.str = smp->strm->txn->auth.user;
11085 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011086
11087 return 1;
11088}
11089
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011090/* Try to find the next occurrence of a cookie name in a cookie header value.
11091 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
11092 * the cookie value is returned into *value and *value_l, and the function
11093 * returns a pointer to the next pointer to search from if the value was found.
11094 * Otherwise if the cookie was not found, NULL is returned and neither value
11095 * nor value_l are touched. The input <hdr> string should first point to the
11096 * header's value, and the <hdr_end> pointer must point to the first character
11097 * not part of the value. <list> must be non-zero if value may represent a list
11098 * of values (cookie headers). This makes it faster to abort parsing when no
11099 * list is expected.
11100 */
David Carlier4686f792015-09-25 14:10:50 +010011101char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011102extract_cookie_value(char *hdr, const char *hdr_end,
11103 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020011104 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011105{
11106 char *equal, *att_end, *att_beg, *val_beg, *val_end;
11107 char *next;
11108
11109 /* we search at least a cookie name followed by an equal, and more
11110 * generally something like this :
11111 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
11112 */
11113 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
11114 /* Iterate through all cookies on this line */
11115
Willy Tarreau2235b262016-11-05 15:50:20 +010011116 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011117 att_beg++;
11118
11119 /* find att_end : this is the first character after the last non
11120 * space before the equal. It may be equal to hdr_end.
11121 */
11122 equal = att_end = att_beg;
11123
11124 while (equal < hdr_end) {
11125 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
11126 break;
Willy Tarreau2235b262016-11-05 15:50:20 +010011127 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011128 continue;
11129 att_end = equal;
11130 }
11131
11132 /* here, <equal> points to '=', a delimitor or the end. <att_end>
11133 * is between <att_beg> and <equal>, both may be identical.
11134 */
11135
11136 /* look for end of cookie if there is an equal sign */
11137 if (equal < hdr_end && *equal == '=') {
11138 /* look for the beginning of the value */
11139 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +010011140 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011141 val_beg++;
11142
11143 /* find the end of the value, respecting quotes */
11144 next = find_cookie_value_end(val_beg, hdr_end);
11145
11146 /* make val_end point to the first white space or delimitor after the value */
11147 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +010011148 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011149 val_end--;
11150 } else {
11151 val_beg = val_end = next = equal;
11152 }
11153
11154 /* We have nothing to do with attributes beginning with '$'. However,
11155 * they will automatically be removed if a header before them is removed,
11156 * since they're supposed to be linked together.
11157 */
11158 if (*att_beg == '$')
11159 continue;
11160
11161 /* Ignore cookies with no equal sign */
11162 if (equal == next)
11163 continue;
11164
11165 /* Now we have the cookie name between att_beg and att_end, and
11166 * its value between val_beg and val_end.
11167 */
11168
11169 if (att_end - att_beg == cookie_name_l &&
11170 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
11171 /* let's return this value and indicate where to go on from */
11172 *value = val_beg;
11173 *value_l = val_end - val_beg;
11174 return next + 1;
11175 }
11176
11177 /* Set-Cookie headers only have the name in the first attr=value part */
11178 if (!list)
11179 break;
11180 }
11181
11182 return NULL;
11183}
11184
William Lallemanda43ba4e2014-01-28 18:14:25 +010011185/* Fetch a captured HTTP request header. The index is the position of
11186 * the "capture" option in the configuration file
11187 */
11188static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011189smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011190{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011191 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011192 int idx;
11193
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011194 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011195 return 0;
11196
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011197 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011198
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011199 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 +010011200 return 0;
11201
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011202 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011203 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011204 smp->data.u.str.str = smp->strm->req_cap[idx];
11205 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011206
11207 return 1;
11208}
11209
11210/* Fetch a captured HTTP response 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_res(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_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_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->res_cap[idx];
11230 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011231
11232 return 1;
11233}
11234
William Lallemand65ad6e12014-01-31 15:08:02 +010011235/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
11236static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011237smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011238{
11239 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011240 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010011241 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011242
Willy Tarreau15e91e12015-04-04 00:52:09 +020011243 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011244 return 0;
11245
William Lallemand96a77852014-02-05 00:30:02 +010011246 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011247
William Lallemand96a77852014-02-05 00:30:02 +010011248 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11249 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011250
William Lallemand96a77852014-02-05 00:30:02 +010011251 temp = get_trash_chunk();
11252 temp->str = txn->uri;
11253 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011254 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011255 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011256 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011257
11258 return 1;
11259
11260}
11261
11262/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
11263static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011264smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011265{
11266 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011267 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010011268 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011269
Willy Tarreau15e91e12015-04-04 00:52:09 +020011270 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011271 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010011272
William Lallemand65ad6e12014-01-31 15:08:02 +010011273 ptr = txn->uri;
11274
11275 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11276 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010011277
William Lallemand65ad6e12014-01-31 15:08:02 +010011278 if (!*ptr)
11279 return 0;
11280
11281 ptr++; /* skip the space */
11282
11283 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010011284 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010011285 if (!ptr)
11286 return 0;
11287 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
11288 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011289
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011290 smp->data.u.str = *temp;
11291 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011292 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011293 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011294
11295 return 1;
11296}
11297
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011298/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
11299 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11300 */
11301static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011302smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011303{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011304 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011305
Willy Tarreau15e91e12015-04-04 00:52:09 +020011306 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011307 return 0;
11308
11309 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011310 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011311 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011312 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011313
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011314 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011315 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011316 smp->flags = SMP_F_CONST;
11317 return 1;
11318
11319}
11320
11321/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
11322 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11323 */
11324static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011325smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011326{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011327 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011328
Willy Tarreau15e91e12015-04-04 00:52:09 +020011329 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011330 return 0;
11331
11332 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011333 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011334 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011335 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011336
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011337 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011338 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011339 smp->flags = SMP_F_CONST;
11340 return 1;
11341
11342}
11343
William Lallemand65ad6e12014-01-31 15:08:02 +010011344
Willy Tarreaue333ec92012-04-16 16:26:40 +020011345/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011346 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011347 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011348 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011349 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011350 * Accepts exactly 1 argument of type string. If the input options indicate
11351 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011352 * The returned sample is of type CSTR. Can be used to parse cookies in other
11353 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011354 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011355int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011356{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011357 struct http_txn *txn;
11358 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011359 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011360 const struct http_msg *msg;
11361 const char *hdr_name;
11362 int hdr_name_len;
11363 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011364 int occ = 0;
11365 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011366
Willy Tarreau24e32d82012-04-23 23:55:44 +020011367 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011368 return 0;
11369
Willy Tarreaua890d072013-04-02 12:01:06 +020011370 if (!ctx) {
11371 /* first call */
11372 ctx = &static_hdr_ctx;
11373 ctx->idx = 0;
11374 smp->ctx.a[2] = ctx;
11375 }
11376
Willy Tarreaue333ec92012-04-16 16:26:40 +020011377 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011378
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011379 txn = smp->strm->txn;
11380 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011381
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011382 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011383 msg = &txn->req;
11384 hdr_name = "Cookie";
11385 hdr_name_len = 6;
11386 } else {
11387 msg = &txn->rsp;
11388 hdr_name = "Set-Cookie";
11389 hdr_name_len = 10;
11390 }
11391
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011392 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020011393 /* no explicit occurrence and single fetch => last cookie by default */
11394 occ = -1;
11395
11396 /* OK so basically here, either we want only one value and it's the
11397 * last one, or we want to iterate over all of them and we fetch the
11398 * next one.
11399 */
11400
Willy Tarreau9b28e032012-10-12 23:49:43 +020011401 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011402 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011403 /* search for the header from the beginning, we must first initialize
11404 * the search parameters.
11405 */
Willy Tarreau37406352012-04-23 16:16:37 +020011406 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011407 ctx->idx = 0;
11408 }
11409
Willy Tarreau28376d62012-04-26 21:26:10 +020011410 smp->flags |= SMP_F_VOL_HDR;
11411
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011412 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011413 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11414 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011415 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11416 goto out;
11417
Willy Tarreau24e32d82012-04-23 23:55:44 +020011418 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011419 continue;
11420
Willy Tarreau37406352012-04-23 16:16:37 +020011421 smp->ctx.a[0] = ctx->line + ctx->val;
11422 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011423 }
11424
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011425 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011426 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011427 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011428 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011429 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011430 &smp->data.u.str.str,
11431 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011432 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011433 found = 1;
11434 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011435 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020011436 smp->flags |= SMP_F_NOT_LAST;
11437 return 1;
11438 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011439 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011440 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011441 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011442 /* all cookie headers and values were scanned. If we're looking for the
11443 * last occurrence, we may return it now.
11444 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011445 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011446 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011447 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011448}
11449
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011450/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011451 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011452 * multiple cookies may be parsed on the same line. The returned sample is of
11453 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011454 */
11455static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011456smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011457{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011458 struct http_txn *txn;
11459 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011460 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011461 const struct http_msg *msg;
11462 const char *hdr_name;
11463 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011464 int cnt;
11465 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011466 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011467
Willy Tarreau24e32d82012-04-23 23:55:44 +020011468 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011469 return 0;
11470
Willy Tarreaue333ec92012-04-16 16:26:40 +020011471 CHECK_HTTP_MESSAGE_FIRST();
11472
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011473 txn = smp->strm->txn;
11474 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011475
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011476 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011477 msg = &txn->req;
11478 hdr_name = "Cookie";
11479 hdr_name_len = 6;
11480 } else {
11481 msg = &txn->rsp;
11482 hdr_name = "Set-Cookie";
11483 hdr_name_len = 10;
11484 }
11485
Willy Tarreau9b28e032012-10-12 23:49:43 +020011486 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011487 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011488 ctx.idx = 0;
11489 cnt = 0;
11490
11491 while (1) {
11492 /* Note: val_beg == NULL every time we need to fetch a new header */
11493 if (!val_beg) {
11494 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11495 break;
11496
Willy Tarreau24e32d82012-04-23 23:55:44 +020011497 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011498 continue;
11499
11500 val_beg = ctx.line + ctx.val;
11501 val_end = val_beg + ctx.vlen;
11502 }
11503
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011504 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011505 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011506 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011507 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011508 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011509 &smp->data.u.str.str,
11510 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011511 cnt++;
11512 }
11513 }
11514
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011515 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011516 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011517 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011518 return 1;
11519}
11520
Willy Tarreau51539362012-05-08 12:46:28 +020011521/* Fetch an cookie's integer value. The integer value is returned. It
11522 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11523 */
11524static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011525smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011526{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011527 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011528
11529 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011530 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011531 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020011532 }
11533
11534 return ret;
11535}
11536
Willy Tarreau8797c062007-05-07 00:55:35 +020011537/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011538/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011539/************************************************************************/
11540
David Cournapeau16023ee2010-12-23 20:55:41 +090011541/*
11542 * Given a path string and its length, find the position of beginning of the
11543 * query string. Returns NULL if no query string is found in the path.
11544 *
11545 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11546 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011547 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011548 */
bedis4c75cca2012-10-05 08:38:24 +020011549static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011550{
11551 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011552
bedis4c75cca2012-10-05 08:38:24 +020011553 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011554 return p ? p + 1 : NULL;
11555}
11556
bedis4c75cca2012-10-05 08:38:24 +020011557static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011558{
bedis4c75cca2012-10-05 08:38:24 +020011559 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011560}
11561
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011562/* after increasing a pointer value, it can exceed the first buffer
11563 * size. This function transform the value of <ptr> according with
11564 * the expected position. <chunks> is an array of the one or two
11565 * avalaible chunks. The first value is the start of the first chunk,
11566 * the second value if the end+1 of the first chunks. The third value
11567 * is NULL or the start of the second chunk and the fourth value is
11568 * the end+1 of the second chunk. The function returns 1 if does a
11569 * wrap, else returns 0.
11570 */
11571static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11572{
11573 if (*ptr < chunks[1])
11574 return 0;
11575 if (!chunks[2])
11576 return 0;
11577 *ptr = chunks[2] + ( *ptr - chunks[1] );
11578 return 1;
11579}
11580
David Cournapeau16023ee2010-12-23 20:55:41 +090011581/*
11582 * Given a url parameter, find the starting position of the first occurence,
11583 * or NULL if the parameter is not found.
11584 *
11585 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11586 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011587 *
Willy Tarreauf6625822015-12-27 14:51:01 +010011588 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011589 * or the second chunk. The caller must be check the position before using the
11590 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011591 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011592static const char *
11593find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011594 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011595 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011596{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011597 const char *pos, *last, *equal;
11598 const char **bufs = chunks;
11599 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011600
David Cournapeau16023ee2010-12-23 20:55:41 +090011601
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011602 pos = bufs[0];
11603 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010011604 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011605 /* Check the equal. */
11606 equal = pos + url_param_name_l;
11607 if (fix_pointer_if_wrap(chunks, &equal)) {
11608 if (equal >= chunks[3])
11609 return NULL;
11610 } else {
11611 if (equal >= chunks[1])
11612 return NULL;
11613 }
11614 if (*equal == '=') {
11615 if (pos + url_param_name_l > last) {
11616 /* process wrap case, we detect a wrap. In this case, the
11617 * comparison is performed in two parts.
11618 */
11619
11620 /* This is the end, we dont have any other chunk. */
11621 if (bufs != chunks || !bufs[2])
11622 return NULL;
11623
11624 /* Compute the length of each part of the comparison. */
11625 l1 = last - pos;
11626 l2 = url_param_name_l - l1;
11627
11628 /* The second buffer is too short to contain the compared string. */
11629 if (bufs[2] + l2 > bufs[3])
11630 return NULL;
11631
11632 if (memcmp(pos, url_param_name, l1) == 0 &&
11633 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11634 return pos;
11635
11636 /* Perform wrapping and jump the string who fail the comparison. */
11637 bufs += 2;
11638 pos = bufs[0] + l2;
11639 last = bufs[1];
11640
11641 } else {
11642 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011643 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11644 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011645 pos += url_param_name_l + 1;
11646 if (fix_pointer_if_wrap(chunks, &pos))
11647 last = bufs[2];
11648 }
11649 }
11650
11651 while (1) {
11652 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011653 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011654 pos++;
11655 if (pos < last)
11656 break;
11657 /* process buffer wrapping. */
11658 if (bufs != chunks || !bufs[2])
11659 return NULL;
11660 bufs += 2;
11661 pos = bufs[0];
11662 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011663 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011664 pos++;
11665 }
11666 return NULL;
11667}
11668
11669/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011670 * Given a url parameter name and a query string, find the next value.
11671 * An empty url_param_name matches the first available parameter.
11672 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
11673 * respectively provide a pointer to the value and its end.
11674 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090011675 */
11676static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011677find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011678 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011679 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011680{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011681 const char *arg_start, *qs_end;
11682 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011683
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011684 arg_start = chunks[0];
11685 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011686 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011687 /* Looks for an argument name. */
11688 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011689 url_param_name, url_param_name_l,
11690 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011691 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011692 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011693 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011694 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011695 if (!arg_start)
11696 return 0;
11697
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011698 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011699 while (1) {
11700 /* looks for the first argument. */
11701 value_start = memchr(arg_start, '=', qs_end - arg_start);
11702 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011703 /* Check for wrapping. */
11704 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011705 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011706 chunks[2]) {
11707 arg_start = chunks[2];
11708 qs_end = chunks[3];
11709 continue;
11710 }
11711 return 0;
11712 }
11713 break;
11714 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011715 value_start++;
11716 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011717 else {
11718 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011719 value_start = arg_start + url_param_name_l + 1;
11720
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011721 /* Check for pointer wrapping. */
11722 if (fix_pointer_if_wrap(chunks, &value_start)) {
11723 /* Update the end pointer. */
11724 qs_end = chunks[3];
11725
11726 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011727 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011728 return 0;
11729 }
11730 }
11731
David Cournapeau16023ee2010-12-23 20:55:41 +090011732 value_end = value_start;
11733
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011734 while (1) {
11735 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11736 value_end++;
11737 if (value_end < qs_end)
11738 break;
11739 /* process buffer wrapping. */
11740 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011741 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011742 chunks[2]) {
11743 value_end = chunks[2];
11744 qs_end = chunks[3];
11745 continue;
11746 }
11747 break;
11748 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011749
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011750 *vstart = value_start;
11751 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011752 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011753}
11754
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011755/* This scans a URL-encoded query string. It takes an optionally wrapping
11756 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11757 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11758 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011759 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011760static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011761smp_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 +090011762{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011763 const char *vstart, *vend;
11764 struct chunk *temp;
11765 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011766
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011767 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011768 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011769 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011770 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011771 return 0;
11772
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011773 /* Create sample. If the value is contiguous, return the pointer as CONST,
11774 * if the value is wrapped, copy-it in a buffer.
11775 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011776 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011777 if (chunks[2] &&
11778 vstart >= chunks[0] && vstart <= chunks[1] &&
11779 vend >= chunks[2] && vend <= chunks[3]) {
11780 /* Wrapped case. */
11781 temp = get_trash_chunk();
11782 memcpy(temp->str, vstart, chunks[1] - vstart);
11783 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011784 smp->data.u.str.str = temp->str;
11785 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011786 } else {
11787 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011788 smp->data.u.str.str = (char *)vstart;
11789 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011790 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11791 }
11792
11793 /* Update context, check wrapping. */
11794 chunks[0] = vend;
11795 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11796 chunks[1] = chunks[3];
11797 chunks[2] = NULL;
11798 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011799
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011800 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011801 smp->flags |= SMP_F_NOT_LAST;
11802
David Cournapeau16023ee2010-12-23 20:55:41 +090011803 return 1;
11804}
11805
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011806/* This function iterates over each parameter of the query string. It uses
11807 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011808 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11809 * An optional parameter name is passed in args[0], otherwise any parameter is
11810 * considered. It supports an optional delimiter argument for the beginning of
11811 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011812 */
11813static int
11814smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11815{
11816 struct http_msg *msg;
11817 char delim = '?';
11818 const char *name;
11819 int name_len;
11820
Dragan Dosen26f77e52015-05-25 10:02:11 +020011821 if (!args ||
11822 (args[0].type && args[0].type != ARGT_STR) ||
11823 (args[1].type && args[1].type != ARGT_STR))
11824 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011825
Dragan Dosen26f77e52015-05-25 10:02:11 +020011826 name = "";
11827 name_len = 0;
11828 if (args->type == ARGT_STR) {
11829 name = args->data.str.str;
11830 name_len = args->data.str.len;
11831 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011832
Dragan Dosen26f77e52015-05-25 10:02:11 +020011833 if (args[1].type)
11834 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011835
Dragan Dosen26f77e52015-05-25 10:02:11 +020011836 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011837 CHECK_HTTP_MESSAGE_FIRST();
11838
11839 msg = &smp->strm->txn->req;
11840
11841 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11842 msg->sl.rq.u_l, delim);
11843 if (!smp->ctx.a[0])
11844 return 0;
11845
11846 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011847
11848 /* Assume that the context is filled with NULL pointer
11849 * before the first call.
11850 * smp->ctx.a[2] = NULL;
11851 * smp->ctx.a[3] = NULL;
11852 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011853 }
11854
11855 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11856}
11857
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011858/* This function iterates over each parameter of the body. This requires
11859 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011860 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11861 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11862 * optional second part if the body wraps at the end of the buffer. An optional
11863 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011864 */
11865static int
11866smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11867{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011868 struct http_msg *msg;
11869 unsigned long len;
11870 unsigned long block1;
11871 char *body;
11872 const char *name;
11873 int name_len;
11874
11875 if (!args || (args[0].type && args[0].type != ARGT_STR))
11876 return 0;
11877
11878 name = "";
11879 name_len = 0;
11880 if (args[0].type == ARGT_STR) {
11881 name = args[0].data.str.str;
11882 name_len = args[0].data.str.len;
11883 }
11884
11885 if (!smp->ctx.a[0]) { // first call, find the query string
11886 CHECK_HTTP_MESSAGE_FIRST();
11887
11888 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010011889 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011890 else
Willy Tarreaube508f12016-03-10 11:47:01 +010011891 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011892
11893 len = http_body_bytes(msg);
11894 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11895
11896 block1 = len;
11897 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11898 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11899
11900 if (block1 == len) {
11901 /* buffer is not wrapped (or empty) */
11902 smp->ctx.a[0] = body;
11903 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011904
11905 /* Assume that the context is filled with NULL pointer
11906 * before the first call.
11907 * smp->ctx.a[2] = NULL;
11908 * smp->ctx.a[3] = NULL;
11909 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011910 }
11911 else {
11912 /* buffer is wrapped, we need to defragment it */
11913 smp->ctx.a[0] = body;
11914 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011915 smp->ctx.a[2] = msg->chn->buf->data;
11916 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011917 }
11918 }
11919 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11920}
11921
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011922/* Return the signed integer value for the specified url parameter (see url_param
11923 * above).
11924 */
11925static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011926smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011927{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011928 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011929
11930 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011931 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011932 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011933 }
11934
11935 return ret;
11936}
11937
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011938/* This produces a 32-bit hash of the concatenation of the first occurrence of
11939 * the Host header followed by the path component if it begins with a slash ('/').
11940 * This means that '*' will not be added, resulting in exactly the first Host
11941 * entry. If no Host header is found, then the path is used. The resulting value
11942 * is hashed using the url hash followed by a full avalanche hash and provides a
11943 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11944 * high-traffic sites without having to store whole paths.
11945 * this differs from the base32 functions in that it includes the url parameters
11946 * as well as the path
11947 */
11948static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011949smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011950{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011951 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011952 struct hdr_ctx ctx;
11953 unsigned int hash = 0;
11954 char *ptr, *beg, *end;
11955 int len;
11956
11957 CHECK_HTTP_MESSAGE_FIRST();
11958
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011959 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011960 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011961 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011962 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11963 ptr = ctx.line + ctx.val;
11964 len = ctx.vlen;
11965 while (len--)
11966 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11967 }
11968
11969 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011970 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 +000011971 beg = http_get_path(txn);
11972 if (!beg)
11973 beg = end;
11974
11975 for (ptr = beg; ptr < end ; ptr++);
11976
11977 if (beg < ptr && *beg == '/') {
11978 while (beg < ptr)
11979 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11980 }
11981 hash = full_hash(hash);
11982
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011983 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011984 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011985 smp->flags = SMP_F_VOL_1ST;
11986 return 1;
11987}
11988
11989/* This concatenates the source address with the 32-bit hash of the Host and
11990 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11991 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11992 * on the source address length. The URL hash is stored before the address so
11993 * that in environments where IPv6 is insignificant, truncating the output to
11994 * 8 bytes would still work.
11995 */
11996static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011997smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011998{
11999 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012000 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012001
Dragan Dosendb5af612016-06-16 11:23:01 +020012002 if (!cli_conn)
12003 return 0;
12004
Thierry FOURNIER0786d052015-05-11 15:42:45 +020012005 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012006 return 0;
12007
12008 temp = get_trash_chunk();
Dragan Dosene5f41332016-06-16 11:08:08 +020012009 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
12010 temp->len += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012011
Willy Tarreaub363a1f2013-10-01 10:45:07 +020012012 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012013 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020012014 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012015 temp->len += 4;
12016 break;
12017 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020012018 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012019 temp->len += 16;
12020 break;
12021 default:
12022 return 0;
12023 }
12024
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012025 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012026 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000012027 return 1;
12028}
12029
Willy Tarreau185b5c42012-04-26 15:11:51 +020012030/* This function is used to validate the arguments passed to any "hdr" fetch
12031 * keyword. These keywords support an optional positive or negative occurrence
12032 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
12033 * is assumed that the types are already the correct ones. Returns 0 on error,
12034 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
12035 * error message in case of error, that the caller is responsible for freeing.
12036 * The initial location must either be freeable or NULL.
12037 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010012038int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020012039{
12040 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020012041 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020012042 return 0;
12043 }
12044 return 1;
12045}
12046
Willy Tarreau276fae92013-07-25 14:36:01 +020012047/* takes an UINT value on input supposed to represent the time since EPOCH,
12048 * adds an optional offset found in args[0] and emits a string representing
12049 * the date in RFC-1123/5322 format.
12050 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012051static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020012052{
Cyril Bontéf78d8962016-01-22 19:40:28 +010012053 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020012054 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
12055 struct chunk *temp;
12056 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012057 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012058 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020012059
12060 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012061 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020012062 curr_date += args[0].data.sint;
12063
12064 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020012065 if (!tm)
12066 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020012067
12068 temp = get_trash_chunk();
12069 temp->len = snprintf(temp->str, temp->size - temp->len,
12070 "%s, %02d %s %04d %02d:%02d:%02d GMT",
12071 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
12072 tm->tm_hour, tm->tm_min, tm->tm_sec);
12073
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012074 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012075 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020012076 return 1;
12077}
12078
Thierry FOURNIERad903512014-04-11 17:51:01 +020012079/* Match language range with language tag. RFC2616 14.4:
12080 *
12081 * A language-range matches a language-tag if it exactly equals
12082 * the tag, or if it exactly equals a prefix of the tag such
12083 * that the first tag character following the prefix is "-".
12084 *
12085 * Return 1 if the strings match, else return 0.
12086 */
12087static inline int language_range_match(const char *range, int range_len,
12088 const char *tag, int tag_len)
12089{
12090 const char *end = range + range_len;
12091 const char *tend = tag + tag_len;
12092 while (range < end) {
12093 if (*range == '-' && tag == tend)
12094 return 1;
12095 if (*range != *tag || tag == tend)
12096 return 0;
12097 range++;
12098 tag++;
12099 }
12100 /* Return true only if the last char of the tag is matched. */
12101 return tag == tend;
12102}
12103
12104/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012105static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020012106{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012107 const char *al = smp->data.u.str.str;
12108 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012109 const char *token;
12110 int toklen;
12111 int qvalue;
12112 const char *str;
12113 const char *w;
12114 int best_q = 0;
12115
12116 /* Set the constant to the sample, because the output of the
12117 * function will be peek in the constant configuration string.
12118 */
12119 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012120 smp->data.u.str.size = 0;
12121 smp->data.u.str.str = "";
12122 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012123
12124 /* Parse the accept language */
12125 while (1) {
12126
12127 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012128 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012129 al++;
12130 if (al >= end)
12131 break;
12132
12133 /* Start of the fisrt word. */
12134 token = al;
12135
12136 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012137 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012138 al++;
12139 if (al == token)
12140 goto expect_comma;
12141
12142 /* Length of the token. */
12143 toklen = al - token;
12144 qvalue = 1000;
12145
12146 /* Check if the token exists in the list. If the token not exists,
12147 * jump to the next token.
12148 */
12149 str = args[0].data.str.str;
12150 w = str;
12151 while (1) {
12152 if (*str == ';' || *str == '\0') {
12153 if (language_range_match(token, toklen, w, str-w))
12154 goto look_for_q;
12155 if (*str == '\0')
12156 goto expect_comma;
12157 w = str + 1;
12158 }
12159 str++;
12160 }
12161 goto expect_comma;
12162
12163look_for_q:
12164
12165 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012166 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012167 al++;
12168 if (al >= end)
12169 goto process_value;
12170
12171 /* If ',' is found, process the result */
12172 if (*al == ',')
12173 goto process_value;
12174
12175 /* If the character is different from ';', look
12176 * for the end of the header part in best effort.
12177 */
12178 if (*al != ';')
12179 goto expect_comma;
12180
12181 /* Assumes that the char is ';', now expect "q=". */
12182 al++;
12183
12184 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012185 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012186 al++;
12187 if (al >= end)
12188 goto process_value;
12189
12190 /* Expect 'q'. If no 'q', continue in best effort */
12191 if (*al != 'q')
12192 goto process_value;
12193 al++;
12194
12195 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012196 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012197 al++;
12198 if (al >= end)
12199 goto process_value;
12200
12201 /* Expect '='. If no '=', continue in best effort */
12202 if (*al != '=')
12203 goto process_value;
12204 al++;
12205
12206 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012207 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012208 al++;
12209 if (al >= end)
12210 goto process_value;
12211
12212 /* Parse the q value. */
12213 qvalue = parse_qvalue(al, &al);
12214
12215process_value:
12216
12217 /* If the new q value is the best q value, then store the associated
12218 * language in the response. If qvalue is the biggest value (1000),
12219 * break the process.
12220 */
12221 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012222 smp->data.u.str.str = (char *)w;
12223 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012224 if (qvalue >= 1000)
12225 break;
12226 best_q = qvalue;
12227 }
12228
12229expect_comma:
12230
12231 /* Expect comma or end. If the end is detected, quit the loop. */
12232 while (al < end && *al != ',')
12233 al++;
12234 if (al >= end)
12235 break;
12236
12237 /* Comma is found, jump it and restart the analyzer. */
12238 al++;
12239 }
12240
12241 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012242 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
12243 smp->data.u.str.str = args[1].data.str.str;
12244 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012245 }
12246
12247 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012248 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012249}
12250
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012251/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012252static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012253{
12254 /* If the constant flag is set or if not size is avalaible at
12255 * the end of the buffer, copy the string in other buffer
12256 * before decoding.
12257 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012258 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012259 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012260 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
12261 smp->data.u.str.str = str->str;
12262 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012263 smp->flags &= ~SMP_F_CONST;
12264 }
12265
12266 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012267 smp->data.u.str.str[smp->data.u.str.len] = '\0';
12268 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012269 return 1;
12270}
12271
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012272static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
12273{
12274 struct proxy *fe = strm_fe(smp->strm);
12275 int idx, i;
12276 struct cap_hdr *hdr;
12277 int len;
12278
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012279 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012280 return 0;
12281
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012282 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012283
12284 /* Check the availibity of the capture id. */
12285 if (idx > fe->nb_req_cap - 1)
12286 return 0;
12287
12288 /* Look for the original configuration. */
12289 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
12290 hdr != NULL && i != idx ;
12291 i--, hdr = hdr->next);
12292 if (!hdr)
12293 return 0;
12294
12295 /* check for the memory allocation */
12296 if (smp->strm->req_cap[hdr->index] == NULL)
12297 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
12298 if (smp->strm->req_cap[hdr->index] == NULL)
12299 return 0;
12300
12301 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012302 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012303 if (len > hdr->len)
12304 len = hdr->len;
12305
12306 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012307 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012308 smp->strm->req_cap[idx][len] = '\0';
12309
12310 return 1;
12311}
12312
12313static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
12314{
12315 struct proxy *fe = strm_fe(smp->strm);
12316 int idx, i;
12317 struct cap_hdr *hdr;
12318 int len;
12319
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012320 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012321 return 0;
12322
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012323 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012324
12325 /* Check the availibity of the capture id. */
12326 if (idx > fe->nb_rsp_cap - 1)
12327 return 0;
12328
12329 /* Look for the original configuration. */
12330 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
12331 hdr != NULL && i != idx ;
12332 i--, hdr = hdr->next);
12333 if (!hdr)
12334 return 0;
12335
12336 /* check for the memory allocation */
12337 if (smp->strm->res_cap[hdr->index] == NULL)
12338 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
12339 if (smp->strm->res_cap[hdr->index] == NULL)
12340 return 0;
12341
12342 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012343 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012344 if (len > hdr->len)
12345 len = hdr->len;
12346
12347 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012348 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012349 smp->strm->res_cap[idx][len] = '\0';
12350
12351 return 1;
12352}
12353
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012354/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012355 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012356 * the relevant part of the request line accordingly. Then it updates various
12357 * pointers to the next elements which were moved, and the total buffer length.
12358 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012359 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
12360 * error, though this can be revisited when this code is finally exploited.
12361 *
12362 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
12363 * query string and 3 to replace uri.
12364 *
12365 * In query string case, the mark question '?' must be set at the start of the
12366 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012367 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012368int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012369 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012370{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012371 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012372 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012373 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012374 int delta;
12375
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012376 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012377 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012378 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012379 cur_end = cur_ptr + txn->req.sl.rq.m_l;
12380
12381 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012382 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012383 txn->req.sl.rq.m_l += delta;
12384 txn->req.sl.rq.u += delta;
12385 txn->req.sl.rq.v += delta;
12386 break;
12387
12388 case 1: // path
12389 cur_ptr = http_get_path(txn);
12390 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012391 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012392
12393 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012394 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 +010012395 cur_end++;
12396
12397 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012398 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012399 txn->req.sl.rq.u_l += delta;
12400 txn->req.sl.rq.v += delta;
12401 break;
12402
12403 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012404 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012405 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012406 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12407 while (cur_ptr < cur_end && *cur_ptr != '?')
12408 cur_ptr++;
12409
12410 /* skip the question mark or indicate that we must insert it
12411 * (but only if the format string is not empty then).
12412 */
12413 if (cur_ptr < cur_end)
12414 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012415 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012416 offset = 0;
12417
12418 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012419 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012420 txn->req.sl.rq.u_l += delta;
12421 txn->req.sl.rq.v += delta;
12422 break;
12423
12424 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012425 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012426 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12427
12428 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012429 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012430 txn->req.sl.rq.u_l += delta;
12431 txn->req.sl.rq.v += delta;
12432 break;
12433
12434 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012435 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012436 }
12437
12438 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012439 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020012440 txn->req.sl.rq.l += delta;
12441 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012442 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012443 return 0;
12444}
12445
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012446/* This function replace the HTTP status code and the associated message. The
12447 * variable <status> contains the new status code. This function never fails.
12448 */
12449void http_set_status(unsigned int status, struct stream *s)
12450{
12451 struct http_txn *txn = s->txn;
12452 char *cur_ptr, *cur_end;
12453 int delta;
12454 char *res;
12455 int c_l;
12456 const char *msg;
12457 int msg_len;
12458
12459 chunk_reset(&trash);
12460
12461 res = ultoa_o(status, trash.str, trash.size);
12462 c_l = res - trash.str;
12463
12464 trash.str[c_l] = ' ';
12465 trash.len = c_l + 1;
12466
12467 msg = get_reason(status);
12468 msg_len = strlen(msg);
12469
12470 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
12471 trash.len += msg_len;
12472
12473 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
12474 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
12475
12476 /* commit changes and adjust message */
12477 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
12478
12479 /* adjust res line offsets and lengths */
12480 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
12481 txn->rsp.sl.st.c_l = c_l;
12482 txn->rsp.sl.st.r_l = msg_len;
12483
12484 delta = trash.len - (cur_end - cur_ptr);
12485 txn->rsp.sl.st.l += delta;
12486 txn->hdr_idx.v[0].len += delta;
12487 http_msg_move_end(&txn->rsp, delta);
12488}
12489
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012490/* This function executes one of the set-{method,path,query,uri} actions. It
12491 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012492 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012493 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012494 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
12495 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012496 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012497enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012498 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012499{
12500 chunk_reset(&trash);
12501
12502 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012503 if (rule->arg.http.action == 2)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012504 trash.str[trash.len++] = '?';
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012505 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012506
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012507 http_replace_req_line(rule->arg.http.action, trash.str, trash.len, px, s);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012508 return ACT_RET_CONT;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012509}
12510
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012511/* This function is just a compliant action wrapper for "set-status". */
12512enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012513 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012514{
12515 http_set_status(rule->arg.status.code, s);
12516 return ACT_RET_CONT;
12517}
12518
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012519/* parse an http-request action among :
12520 * set-method
12521 * set-path
12522 * set-query
12523 * set-uri
12524 *
12525 * All of them accept a single argument of type string representing a log-format.
12526 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
12527 * 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 +020012528 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012529 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012530enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
12531 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012532{
12533 int cur_arg = *orig_arg;
12534
Thierry FOURNIER42148732015-09-02 17:17:33 +020012535 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012536
12537 switch (args[0][4]) {
12538 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012539 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012540 rule->action_ptr = http_action_set_req_line;
12541 break;
12542 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012543 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012544 rule->action_ptr = http_action_set_req_line;
12545 break;
12546 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012547 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012548 rule->action_ptr = http_action_set_req_line;
12549 break;
12550 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012551 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012552 rule->action_ptr = http_action_set_req_line;
12553 break;
12554 default:
12555 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012556 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012557 }
12558
12559 if (!*args[cur_arg] ||
12560 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12561 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012562 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012563 }
12564
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012565 LIST_INIT(&rule->arg.http.logfmt);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012566 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010012567 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +010012568 (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 +010012569 return ACT_RET_PRS_ERR;
12570 }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012571
12572 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012573 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012574}
12575
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012576/* parse set-status action:
12577 * This action accepts a single argument of type int representing
12578 * an http status code. It returns ACT_RET_PRS_OK on success,
12579 * ACT_RET_PRS_ERR on error.
12580 */
12581enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12582 struct act_rule *rule, char **err)
12583{
12584 char *error;
12585
Thierry FOURNIER42148732015-09-02 17:17:33 +020012586 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012587 rule->action_ptr = action_http_set_status;
12588
12589 /* Check if an argument is available */
12590 if (!*args[*orig_arg]) {
12591 memprintf(err, "expects exactly 1 argument <status>");
12592 return ACT_RET_PRS_ERR;
12593 }
12594
12595 /* convert status code as integer */
12596 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12597 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12598 memprintf(err, "expects an integer status code between 100 and 999");
12599 return ACT_RET_PRS_ERR;
12600 }
12601
12602 (*orig_arg)++;
12603 return ACT_RET_PRS_OK;
12604}
12605
Willy Tarreaua9083d02015-05-08 15:27:59 +020012606/* This function executes the "capture" action. It executes a fetch expression,
12607 * turns the result into a string and puts it in a capture slot. It always
12608 * returns 1. If an error occurs the action is cancelled, but the rule
12609 * processing continues.
12610 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012611enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012612 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012613{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012614 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012615 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012616 char **cap = s->req_cap;
12617 int len;
12618
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012619 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 +020012620 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012621 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012622
12623 if (cap[h->index] == NULL)
12624 cap[h->index] = pool_alloc2(h->pool);
12625
12626 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012627 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012628
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012629 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012630 if (len > h->len)
12631 len = h->len;
12632
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012633 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012634 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012635 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012636}
12637
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012638/* This function executes the "capture" action and store the result in a
12639 * capture slot if exists. It executes a fetch expression, turns the result
12640 * into a string and puts it in a capture slot. It always returns 1. If an
12641 * error occurs the action is cancelled, but the rule processing continues.
12642 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012643enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012644 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012645{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012646 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012647 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012648 char **cap = s->req_cap;
12649 struct proxy *fe = strm_fe(s);
12650 int len;
12651 int i;
12652
12653 /* Look for the original configuration. */
12654 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012655 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012656 i--, h = h->next);
12657 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012658 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012659
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012660 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 +020012661 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012662 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012663
12664 if (cap[h->index] == NULL)
12665 cap[h->index] = pool_alloc2(h->pool);
12666
12667 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012668 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012669
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012670 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012671 if (len > h->len)
12672 len = h->len;
12673
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012674 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012675 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012676 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012677}
12678
Willy Tarreaua9083d02015-05-08 15:27:59 +020012679/* parse an "http-request capture" action. It takes a single argument which is
12680 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012681 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012682 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012683 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012684enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12685 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012686{
12687 struct sample_expr *expr;
12688 struct cap_hdr *hdr;
12689 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012690 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012691
12692 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12693 if (strcmp(args[cur_arg], "if") == 0 ||
12694 strcmp(args[cur_arg], "unless") == 0)
12695 break;
12696
12697 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012698 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012699 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012700 }
12701
Willy Tarreaua9083d02015-05-08 15:27:59 +020012702 cur_arg = *orig_arg;
12703 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12704 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012705 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012706
12707 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12708 memprintf(err,
12709 "fetch method '%s' extracts information from '%s', none of which is available here",
12710 args[cur_arg-1], sample_src_names(expr->fetch->use));
12711 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012712 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012713 }
12714
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012715 if (!args[cur_arg] || !*args[cur_arg]) {
12716 memprintf(err, "expects 'len or 'id'");
12717 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012718 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012719 }
12720
Willy Tarreaua9083d02015-05-08 15:27:59 +020012721 if (strcmp(args[cur_arg], "len") == 0) {
12722 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012723
12724 if (!(px->cap & PR_CAP_FE)) {
12725 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012726 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012727 }
12728
12729 proxy->conf.args.ctx = ARGC_CAP;
12730
Willy Tarreaua9083d02015-05-08 15:27:59 +020012731 if (!args[cur_arg]) {
12732 memprintf(err, "missing length value");
12733 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012734 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012735 }
12736 /* we copy the table name for now, it will be resolved later */
12737 len = atoi(args[cur_arg]);
12738 if (len <= 0) {
12739 memprintf(err, "length must be > 0");
12740 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012741 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012742 }
12743 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012744
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012745 if (!len) {
12746 memprintf(err, "a positive 'len' argument is mandatory");
12747 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012748 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012749 }
12750
Vincent Bernat02779b62016-04-03 13:48:43 +020012751 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012752 hdr->next = px->req_cap;
12753 hdr->name = NULL; /* not a header capture */
12754 hdr->namelen = 0;
12755 hdr->len = len;
12756 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12757 hdr->index = px->nb_req_cap++;
12758
12759 px->req_cap = hdr;
12760 px->to_log |= LW_REQHDR;
12761
Thierry FOURNIER42148732015-09-02 17:17:33 +020012762 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012763 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012764 rule->arg.cap.expr = expr;
12765 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012766 }
12767
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012768 else if (strcmp(args[cur_arg], "id") == 0) {
12769 int id;
12770 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012771
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012772 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012773
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012774 if (!args[cur_arg]) {
12775 memprintf(err, "missing id value");
12776 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012777 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012778 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012779
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012780 id = strtol(args[cur_arg], &error, 10);
12781 if (*error != '\0') {
12782 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
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 cur_arg++;
12787
12788 proxy->conf.args.ctx = ARGC_CAP;
12789
Thierry FOURNIER42148732015-09-02 17:17:33 +020012790 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012791 rule->action_ptr = http_action_req_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012792 rule->arg.capid.expr = expr;
12793 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012794 }
12795
12796 else {
12797 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12798 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012799 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012800 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012801
12802 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012803 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012804}
12805
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012806/* This function executes the "capture" action and store the result in a
12807 * capture slot if exists. It executes a fetch expression, turns the result
12808 * into a string and puts it in a capture slot. It always returns 1. If an
12809 * error occurs the action is cancelled, but the rule processing continues.
12810 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012811enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012812 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012813{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012814 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012815 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012816 char **cap = s->res_cap;
12817 struct proxy *fe = strm_fe(s);
12818 int len;
12819 int i;
12820
12821 /* Look for the original configuration. */
12822 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012823 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012824 i--, h = h->next);
12825 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012826 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012827
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012828 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 +020012829 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012830 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012831
12832 if (cap[h->index] == NULL)
12833 cap[h->index] = pool_alloc2(h->pool);
12834
12835 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012836 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012837
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012838 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012839 if (len > h->len)
12840 len = h->len;
12841
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012842 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012843 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012844 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012845}
12846
12847/* parse an "http-response capture" action. It takes a single argument which is
12848 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12849 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012850 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012851 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012852enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12853 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012854{
12855 struct sample_expr *expr;
12856 int cur_arg;
12857 int id;
12858 char *error;
12859
12860 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12861 if (strcmp(args[cur_arg], "if") == 0 ||
12862 strcmp(args[cur_arg], "unless") == 0)
12863 break;
12864
12865 if (cur_arg < *orig_arg + 3) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012866 memprintf(err, "expects <expression> id <idx>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012867 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012868 }
12869
12870 cur_arg = *orig_arg;
12871 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12872 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012873 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012874
12875 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12876 memprintf(err,
12877 "fetch method '%s' extracts information from '%s', none of which is available here",
12878 args[cur_arg-1], sample_src_names(expr->fetch->use));
12879 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012880 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012881 }
12882
12883 if (!args[cur_arg] || !*args[cur_arg]) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012884 memprintf(err, "expects 'id'");
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012885 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012886 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012887 }
12888
12889 if (strcmp(args[cur_arg], "id") != 0) {
12890 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12891 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012892 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012893 }
12894
12895 cur_arg++;
12896
12897 if (!args[cur_arg]) {
12898 memprintf(err, "missing id value");
12899 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012900 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012901 }
12902
12903 id = strtol(args[cur_arg], &error, 10);
12904 if (*error != '\0') {
12905 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12906 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012907 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012908 }
12909 cur_arg++;
12910
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012911 proxy->conf.args.ctx = ARGC_CAP;
12912
Thierry FOURNIER42148732015-09-02 17:17:33 +020012913 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012914 rule->action_ptr = http_action_res_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012915 rule->arg.capid.expr = expr;
12916 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012917
12918 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012919 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012920}
12921
William Lallemand73025dd2014-04-24 14:38:37 +020012922/*
12923 * Return the struct http_req_action_kw associated to a keyword.
12924 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012925struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012926{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012927 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012928}
12929
12930/*
12931 * Return the struct http_res_action_kw associated to a keyword.
12932 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012933struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012934{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012935 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012936}
12937
Willy Tarreau12207b32016-11-22 19:48:51 +010012938
12939/* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop
12940 * now.
12941 */
12942static int cli_parse_show_errors(char **args, struct appctx *appctx, void *private)
12943{
12944 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
12945 return 1;
12946
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012947 if (*args[2]) {
12948 struct proxy *px;
12949
12950 px = proxy_find_by_name(args[2], 0, 0);
12951 if (px)
12952 appctx->ctx.errors.iid = px->uuid;
12953 else
12954 appctx->ctx.errors.iid = atoi(args[2]);
12955
12956 if (!appctx->ctx.errors.iid) {
12957 appctx->ctx.cli.msg = "No such proxy.\n";
12958 appctx->st0 = CLI_ST_PRINT;
12959 return 1;
12960 }
12961 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012962 else
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012963 appctx->ctx.errors.iid = -1; // dump all proxies
12964
Willy Tarreau35069f82016-11-25 09:16:37 +010012965 appctx->ctx.errors.flag = 0;
12966 if (strcmp(args[3], "request") == 0)
12967 appctx->ctx.errors.flag |= 4; // ignore response
12968 else if (strcmp(args[3], "response") == 0)
12969 appctx->ctx.errors.flag |= 2; // ignore request
Willy Tarreau12207b32016-11-22 19:48:51 +010012970 appctx->ctx.errors.px = NULL;
Willy Tarreau12207b32016-11-22 19:48:51 +010012971 return 0;
12972}
12973
12974/* This function dumps all captured errors onto the stream interface's
12975 * read buffer. It returns 0 if the output buffer is full and it needs
12976 * to be called again, otherwise non-zero.
12977 */
12978static int cli_io_handler_show_errors(struct appctx *appctx)
12979{
12980 struct stream_interface *si = appctx->owner;
12981 extern const char *monthname[12];
12982
12983 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
12984 return 1;
12985
12986 chunk_reset(&trash);
12987
12988 if (!appctx->ctx.errors.px) {
12989 /* the function had not been called yet, let's prepare the
12990 * buffer for a response.
12991 */
12992 struct tm tm;
12993
12994 get_localtime(date.tv_sec, &tm);
12995 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
12996 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12997 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
12998 error_snapshot_id);
12999
13000 if (bi_putchk(si_ic(si), &trash) == -1) {
13001 /* Socket buffer full. Let's try again later from the same point */
13002 si_applet_cant_put(si);
13003 return 0;
13004 }
13005
13006 appctx->ctx.errors.px = proxy;
Willy Tarreau12207b32016-11-22 19:48:51 +010013007 appctx->ctx.errors.bol = 0;
13008 appctx->ctx.errors.ptr = -1;
13009 }
13010
13011 /* we have two inner loops here, one for the proxy, the other one for
13012 * the buffer.
13013 */
13014 while (appctx->ctx.errors.px) {
13015 struct error_snapshot *es;
13016
Willy Tarreau35069f82016-11-25 09:16:37 +010013017 if ((appctx->ctx.errors.flag & 1) == 0) {
Willy Tarreau12207b32016-11-22 19:48:51 +010013018 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau35069f82016-11-25 09:16:37 +010013019 if (appctx->ctx.errors.flag & 2) // skip req
13020 goto next;
13021 }
13022 else {
Willy Tarreau12207b32016-11-22 19:48:51 +010013023 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau35069f82016-11-25 09:16:37 +010013024 if (appctx->ctx.errors.flag & 4) // skip resp
13025 goto next;
13026 }
Willy Tarreau12207b32016-11-22 19:48:51 +010013027
13028 if (!es->when.tv_sec)
13029 goto next;
13030
13031 if (appctx->ctx.errors.iid >= 0 &&
13032 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
13033 es->oe->uuid != appctx->ctx.errors.iid)
13034 goto next;
13035
13036 if (appctx->ctx.errors.ptr < 0) {
13037 /* just print headers now */
13038
13039 char pn[INET6_ADDRSTRLEN];
13040 struct tm tm;
13041 int port;
13042
13043 get_localtime(es->when.tv_sec, &tm);
13044 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
13045 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
13046 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
13047
13048 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
13049 case AF_INET:
13050 case AF_INET6:
13051 port = get_host_port(&es->src);
13052 break;
13053 default:
13054 port = 0;
13055 }
13056
Willy Tarreau35069f82016-11-25 09:16:37 +010013057 switch (appctx->ctx.errors.flag & 1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010013058 case 0:
13059 chunk_appendf(&trash,
13060 " frontend %s (#%d): invalid request\n"
13061 " backend %s (#%d)",
13062 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
13063 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
13064 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
13065 break;
13066 case 1:
13067 chunk_appendf(&trash,
13068 " backend %s (#%d): invalid response\n"
13069 " frontend %s (#%d)",
13070 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
13071 es->oe->id, es->oe->uuid);
13072 break;
13073 }
13074
13075 chunk_appendf(&trash,
13076 ", server %s (#%d), event #%u\n"
13077 " src %s:%d, session #%d, session flags 0x%08x\n"
Willy Tarreau10e61cb2017-01-04 14:51:22 +010013078 " HTTP msg state %s(%d), msg flags 0x%08x, tx flags 0x%08x\n"
Willy Tarreau12207b32016-11-22 19:48:51 +010013079 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
13080 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
13081 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
13082 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
13083 es->ev_id,
13084 pn, port, es->sid, es->s_flags,
Willy Tarreau10e61cb2017-01-04 14:51:22 +010013085 http_msg_state_str(es->state), es->state, es->m_flags, es->t_flags,
Willy Tarreau12207b32016-11-22 19:48:51 +010013086 es->m_clen, es->m_blen,
13087 es->b_flags, es->b_out, es->b_tot,
13088 es->len, es->b_wrap, es->pos);
13089
13090 if (bi_putchk(si_ic(si), &trash) == -1) {
13091 /* Socket buffer full. Let's try again later from the same point */
13092 si_applet_cant_put(si);
13093 return 0;
13094 }
13095 appctx->ctx.errors.ptr = 0;
13096 appctx->ctx.errors.sid = es->sid;
13097 }
13098
13099 if (appctx->ctx.errors.sid != es->sid) {
13100 /* the snapshot changed while we were dumping it */
13101 chunk_appendf(&trash,
13102 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
13103 if (bi_putchk(si_ic(si), &trash) == -1) {
13104 si_applet_cant_put(si);
13105 return 0;
13106 }
13107 goto next;
13108 }
13109
13110 /* OK, ptr >= 0, so we have to dump the current line */
13111 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
13112 int newptr;
13113 int newline;
13114
13115 newline = appctx->ctx.errors.bol;
13116 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
13117 if (newptr == appctx->ctx.errors.ptr)
13118 return 0;
13119
13120 if (bi_putchk(si_ic(si), &trash) == -1) {
13121 /* Socket buffer full. Let's try again later from the same point */
13122 si_applet_cant_put(si);
13123 return 0;
13124 }
13125 appctx->ctx.errors.ptr = newptr;
13126 appctx->ctx.errors.bol = newline;
13127 };
13128 next:
13129 appctx->ctx.errors.bol = 0;
13130 appctx->ctx.errors.ptr = -1;
Willy Tarreau35069f82016-11-25 09:16:37 +010013131 appctx->ctx.errors.flag ^= 1;
13132 if (!(appctx->ctx.errors.flag & 1))
Willy Tarreau12207b32016-11-22 19:48:51 +010013133 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau12207b32016-11-22 19:48:51 +010013134 }
13135
13136 /* dump complete */
13137 return 1;
13138}
13139
13140/* register cli keywords */
13141static struct cli_kw_list cli_kws = {{ },{
13142 { { "show", "errors", NULL },
13143 "show errors : report last request and response errors for each proxy",
13144 cli_parse_show_errors, cli_io_handler_show_errors, NULL,
13145 },
13146 {{},}
13147}};
13148
Willy Tarreau4a568972010-05-12 08:08:50 +020013149/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013150/* All supported ACL keywords must be declared here. */
13151/************************************************************************/
13152
13153/* Note: must not be declared <const> as its list will be overwritten.
13154 * Please take care of keeping this list alphabetically sorted.
13155 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020013156static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013157 { "base", "base", PAT_MATCH_STR },
13158 { "base_beg", "base", PAT_MATCH_BEG },
13159 { "base_dir", "base", PAT_MATCH_DIR },
13160 { "base_dom", "base", PAT_MATCH_DOM },
13161 { "base_end", "base", PAT_MATCH_END },
13162 { "base_len", "base", PAT_MATCH_LEN },
13163 { "base_reg", "base", PAT_MATCH_REG },
13164 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020013165
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013166 { "cook", "req.cook", PAT_MATCH_STR },
13167 { "cook_beg", "req.cook", PAT_MATCH_BEG },
13168 { "cook_dir", "req.cook", PAT_MATCH_DIR },
13169 { "cook_dom", "req.cook", PAT_MATCH_DOM },
13170 { "cook_end", "req.cook", PAT_MATCH_END },
13171 { "cook_len", "req.cook", PAT_MATCH_LEN },
13172 { "cook_reg", "req.cook", PAT_MATCH_REG },
13173 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013174
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013175 { "hdr", "req.hdr", PAT_MATCH_STR },
13176 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
13177 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
13178 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
13179 { "hdr_end", "req.hdr", PAT_MATCH_END },
13180 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
13181 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
13182 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013183
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013184 /* these two declarations uses strings with list storage (in place
13185 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
13186 * and delete functions are relative to the list management. The parse
13187 * and match method are related to the corresponding fetch methods. This
13188 * is very particular ACL declaration mode.
13189 */
13190 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
13191 { "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 +020013192
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013193 { "path", "path", PAT_MATCH_STR },
13194 { "path_beg", "path", PAT_MATCH_BEG },
13195 { "path_dir", "path", PAT_MATCH_DIR },
13196 { "path_dom", "path", PAT_MATCH_DOM },
13197 { "path_end", "path", PAT_MATCH_END },
13198 { "path_len", "path", PAT_MATCH_LEN },
13199 { "path_reg", "path", PAT_MATCH_REG },
13200 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013201
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013202 { "req_ver", "req.ver", PAT_MATCH_STR },
13203 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013204
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013205 { "scook", "res.cook", PAT_MATCH_STR },
13206 { "scook_beg", "res.cook", PAT_MATCH_BEG },
13207 { "scook_dir", "res.cook", PAT_MATCH_DIR },
13208 { "scook_dom", "res.cook", PAT_MATCH_DOM },
13209 { "scook_end", "res.cook", PAT_MATCH_END },
13210 { "scook_len", "res.cook", PAT_MATCH_LEN },
13211 { "scook_reg", "res.cook", PAT_MATCH_REG },
13212 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013213
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013214 { "shdr", "res.hdr", PAT_MATCH_STR },
13215 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
13216 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
13217 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
13218 { "shdr_end", "res.hdr", PAT_MATCH_END },
13219 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
13220 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
13221 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013222
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013223 { "url", "url", PAT_MATCH_STR },
13224 { "url_beg", "url", PAT_MATCH_BEG },
13225 { "url_dir", "url", PAT_MATCH_DIR },
13226 { "url_dom", "url", PAT_MATCH_DOM },
13227 { "url_end", "url", PAT_MATCH_END },
13228 { "url_len", "url", PAT_MATCH_LEN },
13229 { "url_reg", "url", PAT_MATCH_REG },
13230 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013231
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013232 { "urlp", "urlp", PAT_MATCH_STR },
13233 { "urlp_beg", "urlp", PAT_MATCH_BEG },
13234 { "urlp_dir", "urlp", PAT_MATCH_DIR },
13235 { "urlp_dom", "urlp", PAT_MATCH_DOM },
13236 { "urlp_end", "urlp", PAT_MATCH_END },
13237 { "urlp_len", "urlp", PAT_MATCH_LEN },
13238 { "urlp_reg", "urlp", PAT_MATCH_REG },
13239 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013240
Willy Tarreau8ed669b2013-01-11 15:49:37 +010013241 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013242}};
13243
13244/************************************************************************/
13245/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020013246/************************************************************************/
13247/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020013248static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013249 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013250 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013251 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
13252
Willy Tarreau87b09662015-04-03 00:22:06 +020013253 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013254 { "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 +020013255
13256 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013257 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
13258 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
13259 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020013260
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013261 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
13262 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010013263
Willy Tarreau409bcde2013-01-08 00:31:00 +010013264 /* cookie is valid in both directions (eg: for "stick ...") but cook*
13265 * are only here to match the ACL's name, are request-only and are used
13266 * for ACL compatibility only.
13267 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013268 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13269 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013270 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
13271 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013272
13273 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
13274 * only here to match the ACL's name, are request-only and are used for
13275 * ACL compatibility only.
13276 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013277 { "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 +020013278 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013279 { "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 +020013280 { "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 +010013281
Willy Tarreau0a0daec2013-04-02 22:44:58 +020013282 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013283 { "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 +010013284 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010013285 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013286 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010013287 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013288
13289 /* HTTP protocol on the request path */
13290 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013291 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013292
13293 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013294 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
13295 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013296
Willy Tarreaua5910cc2015-05-02 00:46:08 +020013297 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013298 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
13299 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020013300 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020013301
Willy Tarreau18ed2562013-01-14 15:56:36 +010013302 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013303 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
13304 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013305
Willy Tarreau18ed2562013-01-14 15:56:36 +010013306 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013307 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013308 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
13309 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013310
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013311 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013312 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013313 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013314 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013315 { "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 +010013316 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013317 { "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 +010013318
13319 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013320 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013321 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13322 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013323
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013324 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013325 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013326 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013327 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013328 { "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 +010013329 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013330 { "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 +010013331
Willy Tarreau409bcde2013-01-08 00:31:00 +010013332 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013333 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013334 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13335 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013336 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010013337
13338 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013339 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013340 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013341 { "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 +020013342 { "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 +010013343
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013344 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020013345 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013346 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013347 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000013348 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013349 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013350 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020013351 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13352 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013353 { "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 +010013354 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020013355}};
13356
Willy Tarreau8797c062007-05-07 00:55:35 +020013357
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013358/************************************************************************/
13359/* All supported converter keywords must be declared here. */
13360/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020013361/* Note: must not be declared <const> as its list will be overwritten */
13362static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013363 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020013364 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013365 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
13366 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020013367 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020013368 { NULL, NULL, 0, 0, 0 },
13369}};
13370
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020013371
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013372/************************************************************************/
13373/* All supported http-request action keywords must be declared here. */
13374/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013375struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013376 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020013377 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013378 { "set-method", parse_set_req_line },
13379 { "set-path", parse_set_req_line },
13380 { "set-query", parse_set_req_line },
13381 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020013382 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013383 }
13384};
13385
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013386struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013387 .kw = {
13388 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020013389 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013390 { NULL, NULL }
13391 }
13392};
13393
Willy Tarreau8797c062007-05-07 00:55:35 +020013394__attribute__((constructor))
13395static void __http_protocol_init(void)
13396{
13397 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020013398 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020013399 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013400 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013401 http_res_keywords_register(&http_res_actions);
Willy Tarreau12207b32016-11-22 19:48:51 +010013402 cli_register_kw(&cli_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020013403}
13404
13405
Willy Tarreau58f10d72006-12-04 02:26:12 +010013406/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020013407 * Local variables:
13408 * c-indent-level: 8
13409 * c-basic-offset: 8
13410 * End:
13411 */