blob: 5ac9bf3cfeaa0d026bb279eba87536627877cc1f [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 */
1353#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1354
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 Tarreau7552c032009-03-01 11:10:40 +01001404 state = HTTP_MSG_ERROR;
1405 break;
1406
Willy Tarreau8973c702007-01-21 23:58:29 +01001407 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001408 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001409 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001410 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001411 goto http_msg_rpcode;
1412 }
1413 if (likely(HTTP_IS_SPHT(*ptr)))
1414 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1415 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001416 state = HTTP_MSG_ERROR;
1417 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001418
Willy Tarreau8973c702007-01-21 23:58:29 +01001419 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001420 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001421 if (likely(!HTTP_IS_LWS(*ptr)))
1422 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1423
1424 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001425 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001426 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1427 }
1428
1429 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001430 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001431 http_msg_rsp_reason:
1432 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001433 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001434 msg->sl.st.r_l = 0;
1435 goto http_msg_rpline_eol;
1436
Willy Tarreau8973c702007-01-21 23:58:29 +01001437 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001438 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001439 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001440 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001441 goto http_msg_rpreason;
1442 }
1443 if (likely(HTTP_IS_SPHT(*ptr)))
1444 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1445 /* so it's a CR/LF, so there is no reason phrase */
1446 goto http_msg_rsp_reason;
1447
Willy Tarreau8973c702007-01-21 23:58:29 +01001448 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001449 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001450 if (likely(!HTTP_IS_CRLF(*ptr)))
1451 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001452 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001453 http_msg_rpline_eol:
1454 /* We have seen the end of line. Note that we do not
1455 * necessarily have the \n yet, but at least we know that we
1456 * have EITHER \r OR \n, otherwise the response would not be
1457 * complete. We can then record the response length and return
1458 * to the caller which will be able to register it.
1459 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001460 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001461 return ptr;
1462
Willy Tarreau8973c702007-01-21 23:58:29 +01001463 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001464#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001465 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1466 exit(1);
1467#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001468 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001469 }
1470
1471 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001472 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001473 if (ret_state)
1474 *ret_state = state;
1475 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001476 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001477 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001478}
1479
Willy Tarreau8973c702007-01-21 23:58:29 +01001480/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001481 * This function parses a request line between <ptr> and <end>, starting with
1482 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1483 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1484 * will give undefined results.
1485 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1486 * and that msg->sol points to the beginning of the request.
1487 * If a complete line is found (which implies that at least one CR or LF is
1488 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1489 * returned indicating an incomplete line (which does not mean that parts have
1490 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1491 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1492 * upon next call.
1493 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001494 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001495 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1496 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001497 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001498 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001499const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001500 enum ht_state state, const char *ptr, const char *end,
1501 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001502{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001503 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001504
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001505 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001506 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001507 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001508 if (likely(HTTP_IS_TOKEN(*ptr)))
1509 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001510
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001511 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001512 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001513 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1514 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001515
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001516 if (likely(HTTP_IS_CRLF(*ptr))) {
1517 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001518 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001519 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001520 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001521 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001522 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001523 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001524 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001525 msg->sl.rq.v_l = 0;
1526 goto http_msg_rqline_eol;
1527 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001528 state = HTTP_MSG_ERROR;
1529 break;
1530
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001531 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001532 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001533 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001534 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001535 goto http_msg_rquri;
1536 }
1537 if (likely(HTTP_IS_SPHT(*ptr)))
1538 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1539 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1540 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001541
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001542 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001543 http_msg_rquri:
Willy Tarreau5f10ea32016-11-05 17:52:06 +01001544#if defined(__x86_64__) || \
1545 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
1546 defined(__ARM_ARCH_7A__)
1547 /* speedup: skip bytes not between 0x21 and 0x7e inclusive */
1548 while (ptr <= end - sizeof(int)) {
1549 int x = *(int *)ptr - 0x21212121;
1550 if (x & 0x80808080)
1551 break;
1552
1553 x -= 0x5e5e5e5e;
1554 if (!(x & 0x80808080))
1555 break;
1556
1557 ptr += sizeof(int);
1558 }
1559#endif
1560 http_msg_rquri2:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001561 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau5f10ea32016-11-05 17:52:06 +01001562 EAT_AND_JUMP_OR_RETURN(http_msg_rquri2, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001563
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001564 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001565 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001566 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1567 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001568
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001569 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001570 /* non-ASCII chars are forbidden unless option
1571 * accept-invalid-http-request is enabled in the frontend.
1572 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001573 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001574 if (msg->err_pos < -1)
1575 goto invalid_char;
1576 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001577 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001578 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1579 }
1580
1581 if (likely(HTTP_IS_CRLF(*ptr))) {
1582 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1583 goto http_msg_req09_uri_e;
1584 }
1585
1586 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001587 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001588 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001589 state = HTTP_MSG_ERROR;
1590 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001591
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001592 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001593 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001594 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001595 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001596 goto http_msg_rqver;
1597 }
1598 if (likely(HTTP_IS_SPHT(*ptr)))
1599 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1600 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1601 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001602
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001603 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001604 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001605 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001606 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001607
1608 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001609 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001610 http_msg_rqline_eol:
1611 /* We have seen the end of line. Note that we do not
1612 * necessarily have the \n yet, but at least we know that we
1613 * have EITHER \r OR \n, otherwise the request would not be
1614 * complete. We can then record the request length and return
1615 * to the caller which will be able to register it.
1616 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001617 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001618 return ptr;
1619 }
1620
1621 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001622 state = HTTP_MSG_ERROR;
1623 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001624
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001625 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001626#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001627 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1628 exit(1);
1629#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001630 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001631 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001632
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001633 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001634 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001635 if (ret_state)
1636 *ret_state = state;
1637 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001638 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001639 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001640}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001641
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001642/*
1643 * Returns the data from Authorization header. Function may be called more
1644 * than once so data is stored in txn->auth_data. When no header is found
1645 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001646 * searching again for something we are unable to find anyway. However, if
1647 * the result if valid, the cache is not reused because we would risk to
Willy Tarreau87b09662015-04-03 00:22:06 +02001648 * have the credentials overwritten by another stream in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001649 */
1650
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001651/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1652 * set according to global.tune.bufsize.
1653 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001654char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001655
1656int
Willy Tarreau87b09662015-04-03 00:22:06 +02001657get_http_auth(struct stream *s)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001658{
1659
Willy Tarreaueee5b512015-04-03 23:46:31 +02001660 struct http_txn *txn = s->txn;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001661 struct chunk auth_method;
1662 struct hdr_ctx ctx;
1663 char *h, *p;
1664 int len;
1665
1666#ifdef DEBUG_AUTH
Willy Tarreau87b09662015-04-03 00:22:06 +02001667 printf("Auth for stream %p: %d\n", s, txn->auth.method);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001668#endif
1669
1670 if (txn->auth.method == HTTP_AUTH_WRONG)
1671 return 0;
1672
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001673 txn->auth.method = HTTP_AUTH_WRONG;
1674
1675 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001676
1677 if (txn->flags & TX_USE_PX_CONN) {
1678 h = "Proxy-Authorization";
1679 len = strlen(h);
1680 } else {
1681 h = "Authorization";
1682 len = strlen(h);
1683 }
1684
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001685 if (!http_find_header2(h, len, s->req.buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001686 return 0;
1687
1688 h = ctx.line + ctx.val;
1689
1690 p = memchr(h, ' ', ctx.vlen);
Willy Tarreau5c557d12016-03-13 08:17:02 +01001691 len = p - h;
1692 if (!p || len <= 0)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001693 return 0;
1694
David Carlier7365f7d2016-04-04 11:54:42 +01001695 if (chunk_initlen(&auth_method, h, 0, len) != 1)
1696 return 0;
1697
Willy Tarreau5c557d12016-03-13 08:17:02 +01001698 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001699
1700 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1701
1702 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001703 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001704
1705 if (len < 0)
1706 return 0;
1707
1708
1709 get_http_auth_buff[len] = '\0';
1710
1711 p = strchr(get_http_auth_buff, ':');
1712
1713 if (!p)
1714 return 0;
1715
1716 txn->auth.user = get_http_auth_buff;
1717 *p = '\0';
1718 txn->auth.pass = p+1;
1719
1720 txn->auth.method = HTTP_AUTH_BASIC;
1721 return 1;
1722 }
1723
1724 return 0;
1725}
1726
Willy Tarreau58f10d72006-12-04 02:26:12 +01001727
Willy Tarreau8973c702007-01-21 23:58:29 +01001728/*
1729 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001730 * depending on the initial msg->msg_state. The caller is responsible for
1731 * ensuring that the message does not wrap. The function can be preempted
1732 * everywhere when data are missing and recalled at the exact same location
1733 * with no information loss. The message may even be realigned between two
1734 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001735 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001736 * fields. Note that msg->sol will be initialized after completing the first
1737 * state, so that none of the msg pointers has to be initialized prior to the
1738 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001739 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001740void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001741{
Willy Tarreau3770f232013-12-07 00:01:53 +01001742 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001743 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001744 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001745
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001746 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001747 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001748 ptr = buf->p + msg->next;
1749 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001750
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001751 if (unlikely(ptr >= end))
1752 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001753
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001754 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001755 /*
1756 * First, states that are specific to the response only.
1757 * We check them first so that request and headers are
1758 * closer to each other (accessed more often).
1759 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001760 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001761 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001762 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001763 /* we have a start of message, but we have to check
1764 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001765 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001766 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001767 if (unlikely(ptr != buf->p)) {
1768 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001769 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001770 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001771 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001772 }
Willy Tarreau26927362012-05-18 23:22:52 +02001773 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001774 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001775 hdr_idx_init(idx);
1776 state = HTTP_MSG_RPVER;
1777 goto http_msg_rpver;
1778 }
1779
1780 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1781 goto http_msg_invalid;
1782
1783 if (unlikely(*ptr == '\n'))
1784 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1785 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1786 /* stop here */
1787
Willy Tarreau8973c702007-01-21 23:58:29 +01001788 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001789 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001790 EXPECT_LF_HERE(ptr, http_msg_invalid);
1791 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1792 /* stop here */
1793
Willy Tarreau8973c702007-01-21 23:58:29 +01001794 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001795 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001796 case HTTP_MSG_RPVER_SP:
1797 case HTTP_MSG_RPCODE:
1798 case HTTP_MSG_RPCODE_SP:
1799 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001800 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001801 state, ptr, end,
1802 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001803 if (unlikely(!ptr))
1804 return;
1805
1806 /* we have a full response and we know that we have either a CR
1807 * or an LF at <ptr>.
1808 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001809 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1810
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001811 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001812 if (likely(*ptr == '\r'))
1813 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1814 goto http_msg_rpline_end;
1815
Willy Tarreau8973c702007-01-21 23:58:29 +01001816 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001817 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001818 /* msg->sol must point to the first of CR or LF. */
1819 EXPECT_LF_HERE(ptr, http_msg_invalid);
1820 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1821 /* stop here */
1822
1823 /*
1824 * Second, states that are specific to the request only
1825 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001826 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001827 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001828 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001829 /* we have a start of message, but we have to check
1830 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001831 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001832 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001833 if (likely(ptr != buf->p)) {
1834 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001835 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001836 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001837 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001838 }
Willy Tarreau26927362012-05-18 23:22:52 +02001839 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001840 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001841 state = HTTP_MSG_RQMETH;
1842 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001843 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001844
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001845 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1846 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001847
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001848 if (unlikely(*ptr == '\n'))
1849 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1850 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001851 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001852
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001853 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001854 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001855 EXPECT_LF_HERE(ptr, http_msg_invalid);
1856 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001857 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001858
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001859 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001860 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001861 case HTTP_MSG_RQMETH_SP:
1862 case HTTP_MSG_RQURI:
1863 case HTTP_MSG_RQURI_SP:
1864 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001865 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001866 state, ptr, end,
1867 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001868 if (unlikely(!ptr))
1869 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001870
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001871 /* we have a full request and we know that we have either a CR
1872 * or an LF at <ptr>.
1873 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001874 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001875
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001876 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001877 if (likely(*ptr == '\r'))
1878 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001879 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001880
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001881 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001882 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001883 /* check for HTTP/0.9 request : no version information available.
1884 * msg->sol must point to the first of CR or LF.
1885 */
1886 if (unlikely(msg->sl.rq.v_l == 0))
1887 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001888
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001889 EXPECT_LF_HERE(ptr, http_msg_invalid);
1890 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001891 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001892
Willy Tarreau8973c702007-01-21 23:58:29 +01001893 /*
1894 * Common states below
1895 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001896 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001897 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001898 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001899 if (likely(!HTTP_IS_CRLF(*ptr))) {
1900 goto http_msg_hdr_name;
1901 }
1902
1903 if (likely(*ptr == '\r'))
1904 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1905 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001906
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001907 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001908 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001909 /* assumes msg->sol points to the first char */
1910 if (likely(HTTP_IS_TOKEN(*ptr)))
1911 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001912
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001913 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001914 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001915
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001916 if (likely(msg->err_pos < -1) || *ptr == '\n')
1917 goto http_msg_invalid;
1918
1919 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001920 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001921
1922 /* and we still accept this non-token character */
1923 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001924
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001925 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001926 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001927 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001928 if (likely(HTTP_IS_SPHT(*ptr)))
1929 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001930
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001931 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001932 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001933
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001934 if (likely(!HTTP_IS_CRLF(*ptr))) {
1935 goto http_msg_hdr_val;
1936 }
1937
1938 if (likely(*ptr == '\r'))
1939 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1940 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001941
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001942 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001943 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001944 EXPECT_LF_HERE(ptr, http_msg_invalid);
1945 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001946
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001947 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001948 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001949 if (likely(HTTP_IS_SPHT(*ptr))) {
1950 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001951 for (; buf->p + msg->sov < ptr; msg->sov++)
1952 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001953 goto http_msg_hdr_l1_sp;
1954 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001955 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001956 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001957 goto http_msg_complete_header;
1958
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001959 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001960 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001961 /* assumes msg->sol points to the first char, and msg->sov
1962 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001963 */
Willy Tarreau0431f9d2016-11-05 17:35:40 +01001964
1965 /* speedup: we'll skip packs of 4 or 8 bytes not containing bytes 0x0D
1966 * and lower. In fact since most of the time is spent in the loop, we
1967 * also remove the sign bit test so that bytes 0x8e..0x0d break the
1968 * loop, but we don't care since they're very rare in header values.
1969 */
1970#if defined(__x86_64__)
1971 while (ptr <= end - sizeof(long)) {
1972 if ((*(long *)ptr - 0x0e0e0e0e0e0e0e0eULL) & 0x8080808080808080ULL)
1973 goto http_msg_hdr_val2;
1974 ptr += sizeof(long);
1975 }
1976#endif
1977#if defined(__x86_64__) || \
1978 defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \
1979 defined(__ARM_ARCH_7A__)
1980 while (ptr <= end - sizeof(int)) {
1981 if ((*(int*)ptr - 0x0e0e0e0e) & 0x80808080)
1982 goto http_msg_hdr_val2;
1983 ptr += sizeof(int);
1984 }
1985#endif
1986 http_msg_hdr_val2:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001987 if (likely(!HTTP_IS_CRLF(*ptr)))
Willy Tarreau0431f9d2016-11-05 17:35:40 +01001988 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val2, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001989
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001990 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001991 /* Note: we could also copy eol into ->eoh so that we have the
1992 * real header end in case it ends with lots of LWS, but is this
1993 * really needed ?
1994 */
1995 if (likely(*ptr == '\r'))
1996 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1997 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001998
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001999 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02002000 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002001 EXPECT_LF_HERE(ptr, http_msg_invalid);
2002 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002003
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002004 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02002005 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002006 if (unlikely(HTTP_IS_SPHT(*ptr))) {
2007 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002008 for (; buf->p + msg->eol < ptr; msg->eol++)
2009 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002010 goto http_msg_hdr_val;
2011 }
2012 http_msg_complete_header:
2013 /*
2014 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002015 * Assumes msg->sol points to the first char, msg->sov points
2016 * to the first character of the value and msg->eol to the
2017 * first CR or LF so we know how the line ends. We insert last
2018 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002019 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002020 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002021 idx, idx->tail) < 0))
2022 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002023
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002024 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002025 if (likely(!HTTP_IS_CRLF(*ptr))) {
2026 goto http_msg_hdr_name;
2027 }
2028
2029 if (likely(*ptr == '\r'))
2030 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
2031 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002032
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002033 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02002034 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01002035 /* Assumes msg->sol points to the first of either CR or LF.
2036 * Sets ->sov and ->next to the total header length, ->eoh to
2037 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
2038 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002039 EXPECT_LF_HERE(ptr, http_msg_invalid);
2040 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002041 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01002042 msg->eoh = msg->sol;
2043 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01002044 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002045 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002046 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02002047
2048 case HTTP_MSG_ERROR:
2049 /* this may only happen if we call http_msg_analyser() twice with an error */
2050 break;
2051
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002052 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01002053#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002054 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
2055 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01002056#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01002057 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002058 }
2059 http_msg_ood:
2060 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002061 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002062 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002063 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002064
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002065 http_msg_invalid:
2066 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01002067 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002068 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002069 return;
2070}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01002071
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002072/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
2073 * conversion succeeded, 0 in case of error. If the request was already 1.X,
2074 * nothing is done and 1 is returned.
2075 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002076static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002077{
2078 int delta;
2079 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002080 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002081
2082 if (msg->sl.rq.v_l != 0)
2083 return 1;
2084
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03002085 /* RFC 1945 allows only GET for HTTP/0.9 requests */
2086 if (txn->meth != HTTP_METH_GET)
2087 return 0;
2088
Willy Tarreau9b28e032012-10-12 23:49:43 +02002089 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002090
2091 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03002092 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
2093 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002094 }
2095 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002096 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01002097 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002098 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02002099 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002100 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002101 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002102 NULL, NULL);
2103 if (unlikely(!cur_end))
2104 return 0;
2105
2106 /* we have a full HTTP/1.0 request now and we know that
2107 * we have either a CR or an LF at <ptr>.
2108 */
2109 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
2110 return 1;
2111}
2112
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002113/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002114 * and "keep-alive" values. If we already know that some headers may safely
2115 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002116 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
2117 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01002118 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002119 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
2120 * found, and TX_CON_*_SET is adjusted depending on what is left so only
2121 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002122 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01002123 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002124void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01002125{
Willy Tarreau5b154472009-12-21 20:11:07 +01002126 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002127 const char *hdr_val = "Connection";
2128 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01002129
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002130 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01002131 return;
2132
Willy Tarreau88d349d2010-01-25 12:15:43 +01002133 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2134 hdr_val = "Proxy-Connection";
2135 hdr_len = 16;
2136 }
2137
Willy Tarreau5b154472009-12-21 20:11:07 +01002138 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002139 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002140 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002141 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2142 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002143 if (to_del & 2)
2144 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002145 else
2146 txn->flags |= TX_CON_KAL_SET;
2147 }
2148 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2149 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002150 if (to_del & 1)
2151 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002152 else
2153 txn->flags |= TX_CON_CLO_SET;
2154 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01002155 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
2156 txn->flags |= TX_HDR_CONN_UPG;
2157 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002158 }
2159
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002160 txn->flags |= TX_HDR_CONN_PRS;
2161 return;
2162}
Willy Tarreau5b154472009-12-21 20:11:07 +01002163
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002164/* Apply desired changes on the Connection: header. Values may be removed and/or
2165 * added depending on the <wanted> flags, which are exclusively composed of
2166 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
2167 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
2168 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002169void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002170{
2171 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002172 const char *hdr_val = "Connection";
2173 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002174
2175 ctx.idx = 0;
2176
Willy Tarreau88d349d2010-01-25 12:15:43 +01002177
2178 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2179 hdr_val = "Proxy-Connection";
2180 hdr_len = 16;
2181 }
2182
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002183 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02002184 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002185 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2186 if (wanted & TX_CON_KAL_SET)
2187 txn->flags |= TX_CON_KAL_SET;
2188 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002189 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01002190 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002191 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2192 if (wanted & TX_CON_CLO_SET)
2193 txn->flags |= TX_CON_CLO_SET;
2194 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002195 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002196 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002197 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002198
2199 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
2200 return;
2201
2202 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
2203 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002204 hdr_val = "Connection: close";
2205 hdr_len = 17;
2206 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2207 hdr_val = "Proxy-Connection: close";
2208 hdr_len = 23;
2209 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002210 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002211 }
2212
2213 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
2214 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002215 hdr_val = "Connection: keep-alive";
2216 hdr_len = 22;
2217 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2218 hdr_val = "Proxy-Connection: keep-alive";
2219 hdr_len = 28;
2220 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002221 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002222 }
2223 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01002224}
2225
Christopher Faulet113f7de2015-12-14 14:52:13 +01002226/* Parse the chunk size at msg->next. Once done, caller should adjust ->next to
2227 * point to the first byte of data after the chunk size, so that we know we can
2228 * forward exactly msg->next bytes. msg->sol contains the exact number of bytes
2229 * forming the chunk size. That way it is always possible to differentiate
2230 * between the start of the body and the start of the data. Return the number
2231 * of byte parsed on success, 0 when some data is missing, <0 on error. Note:
2232 * this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002233 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002234static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002235{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002236 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002237 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002238 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002239 const char *end = buf->data + buf->size;
2240 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01002241 unsigned int chunk = 0;
2242
2243 /* The chunk size is in the following form, though we are only
2244 * interested in the size and CRLF :
2245 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2246 */
2247 while (1) {
2248 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002249 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002250 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002251 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002252 if (c < 0) /* not a hex digit anymore */
2253 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02002254 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002255 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01002256 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002257 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002258 chunk = (chunk << 4) + c;
2259 }
2260
Willy Tarreaud98cf932009-12-27 22:54:55 +01002261 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02002262 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002263 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002264
Willy Tarreau2235b262016-11-05 15:50:20 +01002265 while (HTTP_IS_SPHT(*ptr)) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01002266 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002267 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02002268 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01002269 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002270 }
2271
Willy Tarreaud98cf932009-12-27 22:54:55 +01002272 /* Up to there, we know that at least one byte is present at *ptr. Check
2273 * for the end of chunk size.
2274 */
2275 while (1) {
2276 if (likely(HTTP_IS_CRLF(*ptr))) {
2277 /* we now have a CR or an LF at ptr */
2278 if (likely(*ptr == '\r')) {
2279 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002280 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002281 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002282 return 0;
2283 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002284
Willy Tarreaud98cf932009-12-27 22:54:55 +01002285 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002286 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002287 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002288 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002289 /* done */
2290 break;
2291 }
2292 else if (*ptr == ';') {
2293 /* chunk extension, ends at next CRLF */
2294 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002295 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002296 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01002297 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002298
2299 while (!HTTP_IS_CRLF(*ptr)) {
2300 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002301 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002302 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002303 return 0;
2304 }
2305 /* we have a CRLF now, loop above */
2306 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002307 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002308 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002309 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002310 }
2311
Christopher Faulet113f7de2015-12-14 14:52:13 +01002312 /* OK we found our CRLF and now <ptr> points to the next byte, which may
2313 * or may not be present. We save the number of bytes parsed into
2314 * msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002315 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002316 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002317 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002318 msg->sol += buf->size;
Willy Tarreau124d9912011-03-01 20:30:48 +01002319 msg->chunk_len = chunk;
2320 msg->body_len += chunk;
Christopher Faulet113f7de2015-12-14 14:52:13 +01002321 return msg->sol;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002322 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002323 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002324 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002325}
2326
Christopher Faulet113f7de2015-12-14 14:52:13 +01002327/* This function skips trailers in the buffer associated with HTTP message
2328 * <msg>. The first visited position is msg->next. If the end of the trailers is
2329 * found, the function returns >0. So, the caller can automatically schedul it
2330 * to be forwarded, and switch msg->msg_state to HTTP_MSG_DONE. If not enough
2331 * data are available, the function does not change anything except maybe
2332 * msg->sol if it could parse some lines, and returns zero. If a parse error
2333 * is encountered, the function returns < 0 and does not change anything except
2334 * maybe msg->sol. Note that the message must already be in HTTP_MSG_TRAILERS
2335 * state before calling this function, which implies that all non-trailers data
2336 * have already been scheduled for forwarding, and that msg->next exactly
2337 * matches the length of trailers already parsed and not forwarded. It is also
2338 * important to note that this function is designed to be able to parse wrapped
2339 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002340 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002341static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002342{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002343 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002344
Christopher Faulet113f7de2015-12-14 14:52:13 +01002345 /* we have msg->next which points to next line. Look for CRLF. But
2346 * first, we reset msg->sol */
2347 msg->sol = 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002348 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002349 const char *p1 = NULL, *p2 = NULL;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002350 const char *start = b_ptr(buf, msg->next + msg->sol);
2351 const char *stop = bi_end(buf);
2352 const char *ptr = start;
2353 int bytes = 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002354
2355 /* scan current line and stop at LF or CRLF */
2356 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002357 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002358 return 0;
2359
2360 if (*ptr == '\n') {
2361 if (!p1)
2362 p1 = ptr;
2363 p2 = ptr;
2364 break;
2365 }
2366
2367 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002368 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002369 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002370 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002371 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002372 p1 = ptr;
2373 }
2374
2375 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002376 if (ptr >= buf->data + buf->size)
2377 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002378 }
2379
2380 /* after LF; point to beginning of next line */
2381 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002382 if (p2 >= buf->data + buf->size)
2383 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002384
Christopher Faulet2fb28802015-12-01 10:40:57 +01002385 bytes = p2 - start;
Willy Tarreau638cd022010-01-03 07:42:04 +01002386 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002387 bytes += buf->size;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002388 msg->sol += bytes;
Willy Tarreau638cd022010-01-03 07:42:04 +01002389
Christopher Fauletd7c91962015-04-30 11:48:27 +02002390 /* LF/CRLF at beginning of line => end of trailers at p2.
2391 * Everything was scheduled for forwarding, there's nothing left
Christopher Faulet2fb28802015-12-01 10:40:57 +01002392 * from this message. */
2393 if (p1 == start)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002394 return 1;
Christopher Faulet2fb28802015-12-01 10:40:57 +01002395
Willy Tarreaud98cf932009-12-27 22:54:55 +01002396 /* OK, next line then */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002397 }
2398}
2399
Christopher Faulet113f7de2015-12-14 14:52:13 +01002400/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
2401 * a possible LF alone at the end of a chunk. The caller should adjust msg->next
2402 * in order to include this part into the next forwarding phase. Note that the
2403 * caller must ensure that ->p points to the first byte to parse. It returns
2404 * the number of bytes parsed on success, so the caller can set msg_state to
2405 * HTTP_MSG_CHUNK_SIZE. If not enough data are available, the function does not
2406 * change anything and returns zero. If a parse error is encountered, the
2407 * function returns < 0. Note: this function is designed to parse wrapped CRLF
2408 * at the end of the buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002409 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002410static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002411{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002412 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002413 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002414 int bytes;
2415
2416 /* NB: we'll check data availabilty at the end. It's not a
2417 * problem because whatever we match first will be checked
2418 * against the correct length.
2419 */
2420 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002421 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002422 if (*ptr == '\r') {
2423 bytes++;
2424 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002425 if (ptr >= buf->data + buf->size)
2426 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002427 }
2428
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002429 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002430 return 0;
2431
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002432 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002433 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002434 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002435 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01002436 return bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002437}
Willy Tarreau5b154472009-12-21 20:11:07 +01002438
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002439/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2440 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2441 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2442 * Unparsable qvalues return 1000 as "q=1.000".
2443 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002444int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002445{
2446 int q = 1000;
2447
Willy Tarreau506c69a2014-07-08 00:59:48 +02002448 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002449 goto out;
2450 q = (*qvalue++ - '0') * 1000;
2451
2452 if (*qvalue++ != '.')
2453 goto out;
2454
Willy Tarreau506c69a2014-07-08 00:59:48 +02002455 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002456 goto out;
2457 q += (*qvalue++ - '0') * 100;
2458
Willy Tarreau506c69a2014-07-08 00:59:48 +02002459 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002460 goto out;
2461 q += (*qvalue++ - '0') * 10;
2462
Willy Tarreau506c69a2014-07-08 00:59:48 +02002463 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002464 goto out;
2465 q += (*qvalue++ - '0') * 1;
2466 out:
2467 if (q > 1000)
2468 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002469 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002470 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002471 return q;
2472}
William Lallemand82fe75c2012-10-23 10:25:10 +02002473
Willy Tarreau87b09662015-04-03 00:22:06 +02002474void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg)
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002475{
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002476 struct proxy *fe = strm_fe(s);
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002477 int tmp = TX_CON_WANT_KAL;
2478
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002479 if (!((fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2480 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002481 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2482 tmp = TX_CON_WANT_TUN;
2483
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002484 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002485 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2486 tmp = TX_CON_WANT_TUN;
2487 }
2488
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002489 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002490 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2491 /* option httpclose + server_close => forceclose */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002492 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002493 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2494 tmp = TX_CON_WANT_CLO;
2495 else
2496 tmp = TX_CON_WANT_SCL;
2497 }
2498
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002499 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002500 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2501 tmp = TX_CON_WANT_CLO;
2502
2503 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2504 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2505
2506 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2507 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2508 /* parse the Connection header and possibly clean it */
2509 int to_del = 0;
2510 if ((msg->flags & HTTP_MSGF_VER_11) ||
2511 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002512 !((fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002513 to_del |= 2; /* remove "keep-alive" */
2514 if (!(msg->flags & HTTP_MSGF_VER_11))
2515 to_del |= 1; /* remove "close" */
2516 http_parse_connection_header(txn, msg, to_del);
2517 }
2518
2519 /* check if client or config asks for explicit close in KAL/SCL */
2520 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2521 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2522 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2523 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2524 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002525 fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002526 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2527}
William Lallemand82fe75c2012-10-23 10:25:10 +02002528
Willy Tarreaud787e662009-07-07 10:14:51 +02002529/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2530 * processing can continue on next analysers, or zero if it either needs more
2531 * data or wants to immediately abort the request (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002532 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
Willy Tarreaud787e662009-07-07 10:14:51 +02002533 * when it has nothing left to do, and may remove any analyser when it wants to
2534 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002535 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002536int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002537{
Willy Tarreau59234e92008-11-30 23:51:27 +01002538 /*
2539 * We will parse the partial (or complete) lines.
2540 * We will check the request syntax, and also join multi-line
2541 * headers. An index of all the lines will be elaborated while
2542 * parsing.
2543 *
2544 * For the parsing, we use a 28 states FSM.
2545 *
2546 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002547 * req->buf->p = beginning of request
2548 * req->buf->p + msg->eoh = end of processed headers / start of current one
2549 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002550 * msg->eol = end of current header or line (LF or CRLF)
2551 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002552 *
2553 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreaue7dff022015-04-03 01:14:29 +02002554 * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002555 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2556 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002557 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002558
Willy Tarreau59234e92008-11-30 23:51:27 +01002559 int cur_idx;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002560 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002561 struct http_txn *txn = s->txn;
Willy Tarreau59234e92008-11-30 23:51:27 +01002562 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002563 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002564
Willy Tarreau87b09662015-04-03 00:22:06 +02002565 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 +01002566 now_ms, __FUNCTION__,
2567 s,
2568 req,
2569 req->rex, req->wex,
2570 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002571 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002572 req->analysers);
2573
Willy Tarreau52a0c602009-08-16 22:45:38 +02002574 /* we're speaking HTTP here, so let's speak HTTP to the client */
2575 s->srv_error = http_return_srv_error;
2576
Willy Tarreau83e3af02009-12-28 17:39:57 +01002577 /* There's a protected area at the end of the buffer for rewriting
2578 * purposes. We don't want to start to parse the request if the
2579 * protected area is affected, because we may have to move processed
2580 * data later, which is much more complicated.
2581 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002582 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02002583
2584 /* This point is executed when some data is avalaible for analysis,
2585 * so we log the end of the idle time. */
2586 if (s->logs.t_idle == -1)
2587 s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake;
2588
Willy Tarreau379357a2013-06-08 12:55:46 +02002589 if (txn->flags & TX_NOT_FIRST) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01002590 if (unlikely(!channel_is_rewritable(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002591 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002592 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002593 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002594 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002595 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002596 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002597 return 0;
2598 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002599 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2600 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2601 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002602 }
2603
Willy Tarreau065e8332010-01-08 00:30:20 +01002604 /* Note that we have the same problem with the response ; we
2605 * may want to send a redirect, error or anything which requires
2606 * some spare space. So we'll ensure that we have at least
2607 * maxrewrite bytes available in the response buffer before
2608 * processing that one. This will only affect pipelined
2609 * keep-alive requests.
2610 */
2611 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002612 unlikely(!channel_is_rewritable(&s->res) ||
2613 bi_end(s->res.buf) < b_ptr(s->res.buf, txn->rsp.next) ||
2614 bi_end(s->res.buf) > s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)) {
2615 if (s->res.buf->o) {
2616 if (s->res.flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002617 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002618 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002619 channel_dont_connect(req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002620 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
2621 s->res.flags |= CF_WAKE_WRITE;
2622 s->res.analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002623 return 0;
2624 }
2625 }
2626
Willy Tarreau9b28e032012-10-12 23:49:43 +02002627 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002628 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002629 }
2630
Willy Tarreau59234e92008-11-30 23:51:27 +01002631 /* 1: we might have to print this header in debug mode */
2632 if (unlikely((global.mode & MODE_DEBUG) &&
2633 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02002634 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002635 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002636
Willy Tarreau9b28e032012-10-12 23:49:43 +02002637 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002638 /* this is a bit complex : in case of error on the request line,
2639 * we know that rq.l is still zero, so we display only the part
2640 * up to the end of the line (truncated by debug_hdr).
2641 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002642 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002643 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002644
Willy Tarreau59234e92008-11-30 23:51:27 +01002645 sol += hdr_idx_first_pos(&txn->hdr_idx);
2646 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002647
Willy Tarreau59234e92008-11-30 23:51:27 +01002648 while (cur_idx) {
2649 eol = sol + txn->hdr_idx.v[cur_idx].len;
2650 debug_hdr("clihdr", s, sol, eol);
2651 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2652 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002653 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002654 }
2655
Willy Tarreau58f10d72006-12-04 02:26:12 +01002656
Willy Tarreau59234e92008-11-30 23:51:27 +01002657 /*
2658 * Now we quickly check if we have found a full valid request.
2659 * If not so, we check the FD and buffer states before leaving.
2660 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002661 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002662 * requests are checked first. When waiting for a second request
Willy Tarreau87b09662015-04-03 00:22:06 +02002663 * on a keep-alive stream, if we encounter and error, close, t/o,
2664 * we note the error in the stream flags but don't set any state.
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002665 * Since the error will be noted there, it will not be counted by
Willy Tarreau87b09662015-04-03 00:22:06 +02002666 * process_stream() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002667 * Last, we may increase some tracked counters' http request errors on
2668 * the cases that are deliberately the client's fault. For instance,
2669 * a timeout or connection reset is not counted as an error. However
2670 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002671 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002672
Willy Tarreau655dce92009-11-08 13:10:58 +01002673 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002674 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002675 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002676 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002677 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002678 stream_inc_http_req_ctr(s);
2679 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002680 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002681 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002682 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002683
Willy Tarreau59234e92008-11-30 23:51:27 +01002684 /* 1: Since we are in header mode, if there's no space
2685 * left for headers, we won't be able to free more
Willy Tarreau87b09662015-04-03 00:22:06 +02002686 * later, so the stream will never terminate. We
Willy Tarreau59234e92008-11-30 23:51:27 +01002687 * must terminate it now.
2688 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002689 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002690 /* FIXME: check if URI is set and return Status
2691 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002692 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002693 stream_inc_http_req_ctr(s);
2694 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002695 proxy_inc_fe_req_ctr(sess->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002696 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002697 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002698 goto return_bad_req;
2699 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002700
Willy Tarreau59234e92008-11-30 23:51:27 +01002701 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002702 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002703 if (!(s->flags & SF_ERR_MASK))
2704 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002705
Willy Tarreaufcffa692010-01-10 14:21:19 +01002706 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002707 goto failed_keep_alive;
2708
Willy Tarreau0f228a02015-05-01 15:37:53 +02002709 if (sess->fe->options & PR_O_IGNORE_PRB)
2710 goto failed_keep_alive;
2711
Willy Tarreau59234e92008-11-30 23:51:27 +01002712 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002713 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002714 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002715 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002716 }
2717
Willy Tarreaudc979f22012-12-04 10:39:01 +01002718 txn->status = 400;
Willy Tarreau59234e92008-11-30 23:51:27 +01002719 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002720 http_reply_and_close(s, txn->status, NULL);
Christopher Fauletd7c91962015-04-30 11:48:27 +02002721 req->analysers &= AN_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02002722 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002723 proxy_inc_fe_req_ctr(sess->fe);
2724 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002725 if (sess->listener->counters)
2726 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002727
Willy Tarreaue7dff022015-04-03 01:14:29 +02002728 if (!(s->flags & SF_FINST_MASK))
2729 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002730 return 0;
2731 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002732
Willy Tarreau59234e92008-11-30 23:51:27 +01002733 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002734 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002735 if (!(s->flags & SF_ERR_MASK))
2736 s->flags |= SF_ERR_CLITO;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002737
Willy Tarreaufcffa692010-01-10 14:21:19 +01002738 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002739 goto failed_keep_alive;
2740
Willy Tarreau0f228a02015-05-01 15:37:53 +02002741 if (sess->fe->options & PR_O_IGNORE_PRB)
2742 goto failed_keep_alive;
2743
Willy Tarreau59234e92008-11-30 23:51:27 +01002744 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002745 if (msg->err_pos >= 0) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002746 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau87b09662015-04-03 00:22:06 +02002747 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002748 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002749 txn->status = 408;
Willy Tarreau59234e92008-11-30 23:51:27 +01002750 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002751 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_408));
Christopher Fauletd7c91962015-04-30 11:48:27 +02002752 req->analysers &= AN_FLT_END;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002753
Willy Tarreau87b09662015-04-03 00:22:06 +02002754 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002755 proxy_inc_fe_req_ctr(sess->fe);
2756 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002757 if (sess->listener->counters)
2758 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002759
Willy Tarreaue7dff022015-04-03 01:14:29 +02002760 if (!(s->flags & SF_FINST_MASK))
2761 s->flags |= SF_FINST_R;
Willy Tarreau59234e92008-11-30 23:51:27 +01002762 return 0;
2763 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002764
Willy Tarreau59234e92008-11-30 23:51:27 +01002765 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002766 else if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002767 if (!(s->flags & SF_ERR_MASK))
2768 s->flags |= SF_ERR_CLICL;
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002769
Willy Tarreaufcffa692010-01-10 14:21:19 +01002770 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002771 goto failed_keep_alive;
2772
Willy Tarreau0f228a02015-05-01 15:37:53 +02002773 if (sess->fe->options & PR_O_IGNORE_PRB)
2774 goto failed_keep_alive;
2775
Willy Tarreau4076a152009-04-02 15:18:36 +02002776 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002777 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002778 txn->status = 400;
Willy Tarreau59234e92008-11-30 23:51:27 +01002779 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002780 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Christopher Fauletd7c91962015-04-30 11:48:27 +02002781 req->analysers &= AN_FLT_END;
Willy Tarreau87b09662015-04-03 00:22:06 +02002782 stream_inc_http_err_ctr(s);
2783 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002784 proxy_inc_fe_req_ctr(sess->fe);
2785 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002786 if (sess->listener->counters)
2787 sess->listener->counters->failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002788
Willy Tarreaue7dff022015-04-03 01:14:29 +02002789 if (!(s->flags & SF_FINST_MASK))
2790 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002791 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002792 }
2793
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002794 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002795 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002796 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002797#ifdef TCP_QUICKACK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002798 if (sess->listener->options & LI_O_NOQUICKACK && req->buf->i &&
2799 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002800 /* We need more data, we have to re-enable quick-ack in case we
2801 * previously disabled it, otherwise we might cause the client
2802 * to delay next data.
2803 */
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002804 setsockopt(__objt_conn(sess->origin)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002805 }
2806#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002807
Willy Tarreaufcffa692010-01-10 14:21:19 +01002808 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2809 /* If the client starts to talk, let's fall back to
2810 * request timeout processing.
2811 */
2812 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002813 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002814 }
2815
Willy Tarreau59234e92008-11-30 23:51:27 +01002816 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002817 if (!tick_isset(req->analyse_exp)) {
2818 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2819 (txn->flags & TX_WAIT_NEXT_RQ) &&
2820 tick_isset(s->be->timeout.httpka))
2821 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2822 else
2823 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2824 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002825
Willy Tarreau59234e92008-11-30 23:51:27 +01002826 /* we're not ready yet */
2827 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002828
2829 failed_keep_alive:
2830 /* Here we process low-level errors for keep-alive requests. In
2831 * short, if the request is not the first one and it experiences
2832 * a timeout, read error or shutdown, we just silently close so
2833 * that the client can try again.
2834 */
2835 txn->status = 0;
2836 msg->msg_state = HTTP_MSG_RQBEFORE;
Christopher Fauletd7c91962015-04-30 11:48:27 +02002837 req->analysers &= AN_FLT_END;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002838 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002839 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01002840 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002841 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002842 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002843 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002844
Willy Tarreaud787e662009-07-07 10:14:51 +02002845 /* OK now we have a complete HTTP request with indexed headers. Let's
2846 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002847 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002848 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002849 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002850 * byte after the last LF. msg->sov points to the first byte of data.
2851 * msg->eol cannot be trusted because it may have been left uninitialized
2852 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002853 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002854
Willy Tarreau87b09662015-04-03 00:22:06 +02002855 stream_inc_http_req_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002856 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002857
Willy Tarreaub16a5742010-01-10 14:46:16 +01002858 if (txn->flags & TX_WAIT_NEXT_RQ) {
2859 /* kill the pending keep-alive timeout */
2860 txn->flags &= ~TX_WAIT_NEXT_RQ;
2861 req->analyse_exp = TICK_ETERNITY;
2862 }
2863
2864
Willy Tarreaud787e662009-07-07 10:14:51 +02002865 /* Maybe we found in invalid header name while we were configured not
2866 * to block on that, so we have to capture it now.
2867 */
2868 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002869 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002870
Willy Tarreau59234e92008-11-30 23:51:27 +01002871 /*
2872 * 1: identify the method
2873 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002874 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002875
2876 /* we can make use of server redirect on GET and HEAD */
2877 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002878 s->flags |= SF_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002879
Willy Tarreau59234e92008-11-30 23:51:27 +01002880 /*
2881 * 2: check if the URI matches the monitor_uri.
2882 * We have to do this for every request which gets in, because
2883 * the monitor-uri is defined by the frontend.
2884 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002885 if (unlikely((sess->fe->monitor_uri_len != 0) &&
2886 (sess->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002887 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002888 sess->fe->monitor_uri,
2889 sess->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002890 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002891 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002892 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002893 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002894
Willy Tarreaue7dff022015-04-03 01:14:29 +02002895 s->flags |= SF_MONITOR;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002896 sess->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002897
Willy Tarreau59234e92008-11-30 23:51:27 +01002898 /* Check if we want to fail this monitor request or not */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002899 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
Willy Tarreau192252e2015-04-04 01:47:55 +02002900 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002901
Willy Tarreau59234e92008-11-30 23:51:27 +01002902 ret = acl_pass(ret);
2903 if (cond->pol == ACL_COND_UNLESS)
2904 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002905
Willy Tarreau59234e92008-11-30 23:51:27 +01002906 if (ret) {
2907 /* we fail this request, let's return 503 service unavail */
2908 txn->status = 503;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002909 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_503));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002910 if (!(s->flags & SF_ERR_MASK))
2911 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002912 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002913 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002914 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002915
Willy Tarreau59234e92008-11-30 23:51:27 +01002916 /* nothing to fail, let's reply normaly */
2917 txn->status = 200;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01002918 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_200));
Willy Tarreaue7dff022015-04-03 01:14:29 +02002919 if (!(s->flags & SF_ERR_MASK))
2920 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002921 goto return_prx_cond;
2922 }
2923
2924 /*
2925 * 3: Maybe we have to copy the original REQURI for the logs ?
2926 * Note: we cannot log anymore if the request has been
2927 * classified as invalid.
2928 */
2929 if (unlikely(s->logs.logwait & LW_REQ)) {
2930 /* we have a complete HTTP request that we must log */
2931 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2932 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002933
Willy Tarreau59234e92008-11-30 23:51:27 +01002934 if (urilen >= REQURI_LEN)
2935 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002936 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002937 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002938
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002939 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002940 s->do_log(s);
2941 } else {
2942 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002943 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002944 }
Willy Tarreau06619262006-12-17 08:37:22 +01002945
Willy Tarreau91852eb2015-05-01 13:26:00 +02002946 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
2947 * exactly one digit "." one digit. This check may be disabled using
2948 * option accept-invalid-http-request.
2949 */
2950 if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) {
2951 if (msg->sl.rq.v_l != 8) {
2952 msg->err_pos = msg->sl.rq.v;
2953 goto return_bad_req;
2954 }
2955
2956 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
2957 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
2958 req->buf->p[msg->sl.rq.v + 6] != '.' ||
2959 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
2960 msg->err_pos = msg->sl.rq.v + 4;
2961 goto return_bad_req;
2962 }
2963 }
Willy Tarreau13317662015-05-01 13:47:08 +02002964 else {
2965 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
2966 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
2967 goto return_bad_req;
2968 }
Willy Tarreau91852eb2015-05-01 13:26:00 +02002969
Willy Tarreau5b154472009-12-21 20:11:07 +01002970 /* ... and check if the request is HTTP/1.1 or above */
2971 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002972 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2973 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2974 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002975 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002976
2977 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002978 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 +01002979
Willy Tarreau88d349d2010-01-25 12:15:43 +01002980 /* if the frontend has "option http-use-proxy-header", we'll check if
2981 * we have what looks like a proxied connection instead of a connection,
2982 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2983 * Note that this is *not* RFC-compliant, however browsers and proxies
2984 * happen to do that despite being non-standard :-(
2985 * We consider that a request not beginning with either '/' or '*' is
2986 * a proxied connection, which covers both "scheme://location" and
2987 * CONNECT ip:port.
2988 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002989 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002990 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002991 txn->flags |= TX_USE_PX_CONN;
2992
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002993 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002994 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002995
Willy Tarreau59234e92008-11-30 23:51:27 +01002996 /* 5: we may need to capture headers */
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002997 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002998 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02002999 s->req_cap, sess->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02003000
Willy Tarreau557f1992015-05-01 10:05:17 +02003001 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
3002 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003003 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003004 * The length of a message body is determined by one of the following
3005 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02003006 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003007 * 1. Any response to a HEAD request and any response with a 1xx
3008 * (Informational), 204 (No Content), or 304 (Not Modified) status
3009 * code is always terminated by the first empty line after the
3010 * header fields, regardless of the header fields present in the
3011 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003012 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003013 * 2. Any 2xx (Successful) response to a CONNECT request implies that
3014 * the connection will become a tunnel immediately after the empty
3015 * line that concludes the header fields. A client MUST ignore any
3016 * Content-Length or Transfer-Encoding header fields received in
3017 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003018 *
Willy Tarreau557f1992015-05-01 10:05:17 +02003019 * 3. If a Transfer-Encoding header field is present and the chunked
3020 * transfer coding (Section 4.1) is the final encoding, the message
3021 * body length is determined by reading and decoding the chunked
3022 * data until the transfer coding indicates the data is complete.
3023 *
3024 * If a Transfer-Encoding header field is present in a response and
3025 * the chunked transfer coding is not the final encoding, the
3026 * message body length is determined by reading the connection until
3027 * it is closed by the server. If a Transfer-Encoding header field
3028 * is present in a request and the chunked transfer coding is not
3029 * the final encoding, the message body length cannot be determined
3030 * reliably; the server MUST respond with the 400 (Bad Request)
3031 * status code and then close the connection.
3032 *
3033 * If a message is received with both a Transfer-Encoding and a
3034 * Content-Length header field, the Transfer-Encoding overrides the
3035 * Content-Length. Such a message might indicate an attempt to
3036 * perform request smuggling (Section 9.5) or response splitting
3037 * (Section 9.4) and ought to be handled as an error. A sender MUST
3038 * remove the received Content-Length field prior to forwarding such
3039 * a message downstream.
3040 *
3041 * 4. If a message is received without Transfer-Encoding and with
3042 * either multiple Content-Length header fields having differing
3043 * field-values or a single Content-Length header field having an
3044 * invalid value, then the message framing is invalid and the
3045 * recipient MUST treat it as an unrecoverable error. If this is a
3046 * request message, the server MUST respond with a 400 (Bad Request)
3047 * status code and then close the connection. If this is a response
3048 * message received by a proxy, the proxy MUST close the connection
3049 * to the server, discard the received response, and send a 502 (Bad
3050 * Gateway) response to the client. If this is a response message
3051 * received by a user agent, the user agent MUST close the
3052 * connection to the server and discard the received response.
3053 *
3054 * 5. If a valid Content-Length header field is present without
3055 * Transfer-Encoding, its decimal value defines the expected message
3056 * body length in octets. If the sender closes the connection or
3057 * the recipient times out before the indicated number of octets are
3058 * received, the recipient MUST consider the message to be
3059 * incomplete and close the connection.
3060 *
3061 * 6. If this is a request message and none of the above are true, then
3062 * the message body length is zero (no message body is present).
3063 *
3064 * 7. Otherwise, this is a response message without a declared message
3065 * body length, so the message body length is determined by the
3066 * number of octets received prior to the server closing the
3067 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02003068 */
3069
Willy Tarreau32b47f42009-10-18 20:55:02 +02003070 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003071 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau4979d5c2015-05-01 10:06:30 +02003072 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003073 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003074 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
3075 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau34dfc602015-05-01 10:09:49 +02003076 /* chunked not last, return badreq */
3077 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003078 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003079 }
3080
Willy Tarreau1c913912015-04-30 10:57:51 +02003081 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02003082 ctx.idx = 0;
Willy Tarreau1c913912015-04-30 10:57:51 +02003083 if (msg->flags & HTTP_MSGF_TE_CHNK) {
3084 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
3085 http_remove_header2(msg, &txn->hdr_idx, &ctx);
3086 }
Willy Tarreau34dfc602015-05-01 10:09:49 +02003087 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02003088 signed long long cl;
3089
Willy Tarreauad14f752011-09-02 20:33:27 +02003090 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003091 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003092 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003093 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003094
Willy Tarreauad14f752011-09-02 20:33:27 +02003095 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003096 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003097 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02003098 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003099
Willy Tarreauad14f752011-09-02 20:33:27 +02003100 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003101 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003102 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003103 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003104
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003105 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003106 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003107 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02003108 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003109
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003110 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003111 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003112 }
3113
Willy Tarreau34dfc602015-05-01 10:09:49 +02003114 /* even bodyless requests have a known length */
3115 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003116
Willy Tarreau179085c2014-04-28 16:48:56 +02003117 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3118 * only change if both the request and the config reference something else.
3119 * Option httpclose by itself sets tunnel mode where headers are mangled.
3120 * However, if another mode is set, it will affect it (eg: server-close/
3121 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3122 * if FE and BE have the same settings (common). The method consists in
3123 * checking if options changed between the two calls (implying that either
3124 * one is non-null, or one of them is non-null and we are there for the first
3125 * time.
3126 */
3127 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003128 ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
Willy Tarreau4e21ff92014-09-30 18:44:22 +02003129 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003130
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02003131 /* we may have to wait for the request's body */
3132 if ((s->be->options & PR_O_WREQ_BODY) &&
3133 (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK)))
3134 req->analysers |= AN_REQ_HTTP_BODY;
3135
Willy Tarreaud787e662009-07-07 10:14:51 +02003136 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003137 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003138 req->analyse_exp = TICK_ETERNITY;
3139 return 1;
3140
3141 return_bad_req:
3142 /* We centralize bad requests processing here */
3143 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3144 /* we detected a parsing error. We want to archive this request
3145 * in the dedicated proxy area for later troubleshooting.
3146 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003147 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003148 }
3149
3150 txn->req.msg_state = HTTP_MSG_ERROR;
3151 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01003152 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003153
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02003154 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02003155 if (sess->listener->counters)
3156 sess->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003157
3158 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02003159 if (!(s->flags & SF_ERR_MASK))
3160 s->flags |= SF_ERR_PRXCOND;
3161 if (!(s->flags & SF_FINST_MASK))
3162 s->flags |= SF_FINST_R;
Willy Tarreaud787e662009-07-07 10:14:51 +02003163
Christopher Fauletd7c91962015-04-30 11:48:27 +02003164 req->analysers &= AN_FLT_END;
Willy Tarreaud787e662009-07-07 10:14:51 +02003165 req->analyse_exp = TICK_ETERNITY;
3166 return 0;
3167}
3168
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003169
Willy Tarreau347a35d2013-11-22 17:51:09 +01003170/* This function prepares an applet to handle the stats. It can deal with the
3171 * "100-continue" expectation, check that admin rules are met for POST requests,
3172 * and program a response message if something was unexpected. It cannot fail
3173 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003174 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003175 * s->target which is supposed to already point to the stats applet. The caller
Willy Tarreau87b09662015-04-03 00:22:06 +02003176 * is expected to have already assigned an appctx to the stream.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003177 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003178int http_handle_stats(struct stream *s, struct channel *req)
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003179{
3180 struct stats_admin_rule *stats_admin_rule;
Willy Tarreau350f4872014-11-28 14:42:25 +01003181 struct stream_interface *si = &s->si[1];
Willy Tarreau192252e2015-04-04 01:47:55 +02003182 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003183 struct http_txn *txn = s->txn;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003184 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003185 struct uri_auth *uri_auth = s->be->uri_auth;
3186 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003187 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003188
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003189 appctx = si_appctx(si);
3190 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3191 appctx->st1 = appctx->st2 = 0;
3192 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3193 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreaueee5b512015-04-03 23:46:31 +02003194 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD))
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003195 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003196
3197 uri = msg->chn->buf->p + msg->sl.rq.u;
3198 lookup = uri + uri_auth->uri_len;
3199
3200 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3201 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003202 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003203 break;
3204 }
3205 }
3206
3207 if (uri_auth->refresh) {
3208 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3209 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003210 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003211 break;
3212 }
3213 }
3214 }
3215
3216 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3217 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003218 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003219 break;
3220 }
3221 }
3222
Willy Tarreau1e62df92016-01-11 18:57:53 +01003223 for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) {
3224 if (memcmp(h, ";typed", 6) == 0) {
3225 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
3226 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
3227 break;
3228 }
3229 }
3230
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003231 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3232 if (memcmp(h, ";st=", 4) == 0) {
3233 int i;
3234 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003235 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003236 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3237 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003238 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003239 break;
3240 }
3241 }
3242 break;
3243 }
3244 }
3245
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003246 appctx->ctx.stats.scope_str = 0;
3247 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003248 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3249 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3250 int itx = 0;
3251 const char *h2;
3252 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3253 const char *err;
3254
3255 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3256 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003257 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003258 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3259 itx++;
3260 h++;
3261 }
3262
3263 if (itx > STAT_SCOPE_TXT_MAXLEN)
3264 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003265 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003266
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003267 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003268 memcpy(scope_txt, h2, itx);
3269 scope_txt[itx] = '\0';
3270 err = invalid_char(scope_txt);
3271 if (err) {
3272 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003273 appctx->ctx.stats.scope_str = 0;
3274 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003275 }
3276 break;
3277 }
3278 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003279
3280 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003281 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003282 int ret = 1;
3283
3284 if (stats_admin_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02003285 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 +01003286 ret = acl_pass(ret);
3287 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3288 ret = !ret;
3289 }
3290
3291 if (ret) {
3292 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003293 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003294 break;
3295 }
3296 }
3297
3298 /* Was the status page requested with a POST ? */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003299 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003300 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003301 /* we'll need the request body, possibly after sending 100-continue */
Willy Tarreaub8cdf522015-05-29 01:09:15 +02003302 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE)
3303 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003304 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003305 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003306 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003307 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3308 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003309 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003310 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003311 else {
3312 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003313 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003314 }
3315
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003316 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003317 return 1;
3318}
3319
Lukas Tribus67db8df2013-06-23 17:37:13 +02003320/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3321 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3322 */
Vincent Bernat6e615892016-05-18 16:17:44 +02003323void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003324{
3325#ifdef IP_TOS
Vincent Bernat6e615892016-05-18 16:17:44 +02003326 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003327 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3328#endif
3329#ifdef IPV6_TCLASS
Vincent Bernat6e615892016-05-18 16:17:44 +02003330 if (from->ss_family == AF_INET6) {
3331 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02003332 /* v4-mapped addresses need IP_TOS */
3333 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3334 else
3335 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3336 }
3337#endif
3338}
3339
Willy Tarreau87b09662015-04-03 00:22:06 +02003340int http_transform_header_str(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003341 const char* name, unsigned int name_len,
3342 const char *str, struct my_regex *re,
3343 int action)
Sasha Pachev218f0642014-06-16 12:05:59 -06003344{
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003345 struct hdr_ctx ctx;
3346 char *buf = msg->chn->buf->p;
Willy Tarreaueee5b512015-04-03 23:46:31 +02003347 struct hdr_idx *idx = &s->txn->hdr_idx;
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003348 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3349 struct hdr_idx *idx, struct hdr_ctx *ctx);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003350 struct chunk *output = get_trash_chunk();
3351
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003352 ctx.idx = 0;
Sasha Pachev218f0642014-06-16 12:05:59 -06003353
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003354 /* Choose the header browsing function. */
3355 switch (action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003356 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003357 http_find_hdr_func = http_find_header2;
3358 break;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003359 case ACT_HTTP_REPLACE_HDR:
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003360 http_find_hdr_func = http_find_full_header2;
3361 break;
3362 default: /* impossible */
3363 return -1;
3364 }
3365
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003366 while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) {
3367 struct hdr_idx_elem *hdr = idx->v + ctx.idx;
Sasha Pachev218f0642014-06-16 12:05:59 -06003368 int delta;
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003369 char *val = ctx.line + ctx.val;
3370 char* val_end = val + ctx.vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003371
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003372 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0))
3373 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003374
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003375 output->len = exp_replace(output->str, output->size, val, str, pmatch);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003376 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003377 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003378
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003379 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003380
3381 hdr->len += delta;
3382 http_msg_move_end(msg, delta);
Thierry FOURNIER191f9ef2015-03-16 23:23:53 +01003383
3384 /* Adjust the length of the current value of the index. */
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003385 ctx.vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003386 }
3387
3388 return 0;
3389}
3390
Willy Tarreau87b09662015-04-03 00:22:06 +02003391static int http_transform_header(struct stream* s, struct http_msg *msg,
Thierry FOURNIER5531f872015-03-16 11:15:50 +01003392 const char* name, unsigned int name_len,
3393 struct list *fmt, struct my_regex *re,
3394 int action)
3395{
3396 struct chunk *replace = get_trash_chunk();
3397
3398 replace->len = build_logline(s, replace->str, replace->size, fmt);
3399 if (replace->len >= replace->size - 1)
3400 return -1;
3401
3402 return http_transform_header_str(s, msg, name, name_len, replace->str, re, action);
3403}
3404
Willy Tarreau87b09662015-04-03 00:22:06 +02003405/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003406 * transaction <txn>. Returns the verdict of the first rule that prevents
3407 * further processing of the request (auth, deny, ...), and defaults to
3408 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3409 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
Willy Tarreau58727ec2016-05-25 16:23:59 +02003410 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
3411 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
3412 * status.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003413 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003414enum rule_result
Willy Tarreau58727ec2016-05-25 16:23:59 +02003415http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003416{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003417 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003418 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003419 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003420 struct act_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003421 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003422 const char *auth_realm;
Willy Tarreauacc98002015-09-27 23:34:39 +02003423 int act_flags = 0;
Thierry Fournier4b788f72016-06-01 13:35:36 +02003424 int len;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003425
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003426 /* If "the current_rule_list" match the executed rule list, we are in
3427 * resume condition. If a resume is needed it is always in the action
3428 * and never in the ACL or converters. In this case, we initialise the
3429 * current rule, and go to the action execution point.
3430 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003431 if (s->current_rule) {
3432 rule = s->current_rule;
3433 s->current_rule = NULL;
3434 if (s->current_rule_list == rules)
3435 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003436 }
3437 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003438
Willy Tarreauff011f22011-01-06 17:51:27 +01003439 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003440
Willy Tarreau96257ec2012-12-27 10:46:37 +01003441 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003442 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003443 int ret;
3444
Willy Tarreau192252e2015-04-04 01:47:55 +02003445 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003446 ret = acl_pass(ret);
3447
Willy Tarreauff011f22011-01-06 17:51:27 +01003448 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003449 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003450
3451 if (!ret) /* condition not matched */
3452 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003453 }
3454
Willy Tarreauacc98002015-09-27 23:34:39 +02003455 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003456resume_execution:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003457 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003458 case ACT_ACTION_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003459 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003460
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003461 case ACT_ACTION_DENY:
Willy Tarreau58727ec2016-05-25 16:23:59 +02003462 if (deny_status)
3463 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003464 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003465
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003466 case ACT_HTTP_REQ_TARPIT:
Willy Tarreauccbcc372012-12-27 12:37:57 +01003467 txn->flags |= TX_CLTARPIT;
Willy Tarreau58727ec2016-05-25 16:23:59 +02003468 if (deny_status)
3469 *deny_status = rule->deny_status;
Willy Tarreau0b748332014-04-29 00:13:29 +02003470 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003471
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003472 case ACT_HTTP_REQ_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003473 /* Auth might be performed on regular http-req rules as well as on stats */
3474 auth_realm = rule->arg.auth.realm;
3475 if (!auth_realm) {
3476 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3477 auth_realm = STATS_DEFAULT_REALM;
3478 else
3479 auth_realm = px->id;
3480 }
3481 /* send 401/407 depending on whether we use a proxy or not. We still
3482 * count one error, because normal browsing won't significantly
3483 * increase the counter but brute force attempts will.
3484 */
3485 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3486 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01003487 http_reply_and_close(s, txn->status, &trash);
Willy Tarreau87b09662015-04-03 00:22:06 +02003488 stream_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003489 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003490
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003491 case ACT_HTTP_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003492 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3493 return HTTP_RULE_RES_BADREQ;
3494 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003495
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003496 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003497 s->task->nice = rule->arg.nice;
3498 break;
3499
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003500 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003501 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Vincent Bernat6e615892016-05-18 16:17:44 +02003502 inet_set_tos(cli_conn->t.sock.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003503 break;
3504
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003505 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003506#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003507 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003508 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003509#endif
3510 break;
3511
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003512 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003513 s->logs.level = rule->arg.loglevel;
3514 break;
3515
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003516 case ACT_HTTP_REPLACE_HDR:
3517 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003518 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name,
3519 rule->arg.hdr_add.name_len,
3520 &rule->arg.hdr_add.fmt,
3521 &rule->arg.hdr_add.re, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003522 return HTTP_RULE_RES_BADREQ;
3523 break;
3524
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003525 case ACT_HTTP_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003526 ctx.idx = 0;
3527 /* remove all occurrences of the header */
3528 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3529 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3530 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003531 }
Willy Tarreau85603282015-01-21 20:39:27 +01003532 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003533
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003534 case ACT_HTTP_SET_HDR:
3535 case ACT_HTTP_ADD_HDR:
Thierry Fournier4b788f72016-06-01 13:35:36 +02003536 /* The scope of the trash buffer must be limited to this function. The
3537 * build_logline() function can execute a lot of other function which
3538 * can use the trash buffer. So for limiting the scope of this global
3539 * buffer, we build first the header value using build_logline, and
3540 * after we store the header name.
3541 */
3542 len = rule->arg.hdr_add.name_len + 2,
3543 len += build_logline(s, trash.str + len, trash.size - len, &rule->arg.hdr_add.fmt);
Willy Tarreau96257ec2012-12-27 10:46:37 +01003544 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
Thierry Fournier4b788f72016-06-01 13:35:36 +02003545 trash.str[rule->arg.hdr_add.name_len] = ':';
3546 trash.str[rule->arg.hdr_add.name_len + 1] = ' ';
3547 trash.len = len;
Willy Tarreau85603282015-01-21 20:39:27 +01003548
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003549 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003550 /* remove all occurrences of the header */
3551 ctx.idx = 0;
3552 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3553 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3554 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3555 }
3556 }
3557
Willy Tarreau96257ec2012-12-27 10:46:37 +01003558 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3559 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003560
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003561 case ACT_HTTP_DEL_ACL:
3562 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003563 struct pat_ref *ref;
3564 char *key;
3565 int len;
3566
3567 /* collect reference */
3568 ref = pat_ref_lookup(rule->arg.map.ref);
3569 if (!ref)
3570 continue;
3571
3572 /* collect key */
3573 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3574 key = trash.str;
3575 key[len] = '\0';
3576
3577 /* perform update */
3578 /* returned code: 1=ok, 0=ko */
3579 pat_ref_delete(ref, key);
3580
3581 break;
3582 }
3583
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003584 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003585 struct pat_ref *ref;
3586 char *key;
3587 struct chunk *trash_key;
3588 int len;
3589
3590 trash_key = get_trash_chunk();
3591
3592 /* collect reference */
3593 ref = pat_ref_lookup(rule->arg.map.ref);
3594 if (!ref)
3595 continue;
3596
3597 /* collect key */
3598 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3599 key = trash_key->str;
3600 key[len] = '\0';
3601
3602 /* perform update */
3603 /* add entry only if it does not already exist */
3604 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003605 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003606
3607 break;
3608 }
3609
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003610 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003611 struct pat_ref *ref;
3612 char *key, *value;
3613 struct chunk *trash_key, *trash_value;
3614 int len;
3615
3616 trash_key = get_trash_chunk();
3617 trash_value = get_trash_chunk();
3618
3619 /* collect reference */
3620 ref = pat_ref_lookup(rule->arg.map.ref);
3621 if (!ref)
3622 continue;
3623
3624 /* collect key */
3625 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3626 key = trash_key->str;
3627 key[len] = '\0';
3628
3629 /* collect value */
3630 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3631 value = trash_value->str;
3632 value[len] = '\0';
3633
3634 /* perform update */
3635 if (pat_ref_find_elt(ref, key) != NULL)
3636 /* update entry if it exists */
3637 pat_ref_set(ref, key, value, NULL);
3638 else
3639 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003640 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003641
3642 break;
3643 }
William Lallemand73025dd2014-04-24 14:38:37 +02003644
Thierry FOURNIER42148732015-09-02 17:17:33 +02003645 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02003646 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
3647 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003648
Willy Tarreauacc98002015-09-27 23:34:39 +02003649 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003650 case ACT_RET_ERR:
3651 case ACT_RET_CONT:
3652 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003653 case ACT_RET_STOP:
3654 return HTTP_RULE_RES_DONE;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003655 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003656 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003657 return HTTP_RULE_RES_YIELD;
3658 }
William Lallemand73025dd2014-04-24 14:38:37 +02003659 break;
3660
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003661 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
Willy Tarreau09448f72014-06-25 18:12:15 +02003662 /* Note: only the first valid tracking parameter of each
3663 * applies.
3664 */
3665
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003666 if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) {
Willy Tarreau09448f72014-06-25 18:12:15 +02003667 struct stktable *t;
3668 struct stksess *ts;
3669 struct stktable_key *key;
3670 void *ptr;
3671
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02003672 t = rule->arg.trk_ctr.table.t;
3673 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 +02003674
3675 if (key && (ts = stktable_get_entry(t, key))) {
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003676 stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts);
Willy Tarreau09448f72014-06-25 18:12:15 +02003677
3678 /* let's count a new HTTP request as it's the first time we do it */
3679 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3680 if (ptr)
3681 stktable_data_cast(ptr, http_req_cnt)++;
3682
3683 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3684 if (ptr)
3685 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3686 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3687
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003688 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003689 if (sess->fe != s->be)
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003690 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau09448f72014-06-25 18:12:15 +02003691 }
3692 }
Adis Nezirovic2fbcafc2015-07-06 15:44:30 +02003693 break;
3694
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003695 /* other flags exists, but normaly, they never be matched. */
3696 default:
3697 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003698 }
3699 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003700
3701 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003702 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003703}
3704
Willy Tarreau71241ab2012-12-27 11:30:54 +01003705
Willy Tarreau51d861a2015-05-22 17:30:48 +02003706/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3707 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3708 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3709 * is returned, the process can continue the evaluation of next rule list. If
3710 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3711 * is returned, it means the operation could not be processed and a server error
3712 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3713 * deny rule. If *YIELD is returned, the caller must call again the function
3714 * with the same context.
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003715 */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003716static enum rule_result
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003717http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003718{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003719 struct session *sess = strm_sess(s);
Willy Tarreau987e3fb2015-04-04 01:09:08 +02003720 struct http_txn *txn = s->txn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003721 struct connection *cli_conn;
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02003722 struct act_rule *rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003723 struct hdr_ctx ctx;
Willy Tarreauacc98002015-09-27 23:34:39 +02003724 int act_flags = 0;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003725
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003726 /* If "the current_rule_list" match the executed rule list, we are in
3727 * resume condition. If a resume is needed it is always in the action
3728 * and never in the ACL or converters. In this case, we initialise the
3729 * current rule, and go to the action execution point.
3730 */
Willy Tarreau152b81e2015-04-20 13:26:17 +02003731 if (s->current_rule) {
3732 rule = s->current_rule;
3733 s->current_rule = NULL;
3734 if (s->current_rule_list == rules)
3735 goto resume_execution;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003736 }
3737 s->current_rule_list = rules;
Willy Tarreau152b81e2015-04-20 13:26:17 +02003738
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003739 list_for_each_entry(rule, rules, list) {
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003740
3741 /* check optional condition */
3742 if (rule->cond) {
3743 int ret;
3744
Willy Tarreau192252e2015-04-04 01:47:55 +02003745 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003746 ret = acl_pass(ret);
3747
3748 if (rule->cond->pol == ACL_COND_UNLESS)
3749 ret = !ret;
3750
3751 if (!ret) /* condition not matched */
3752 continue;
3753 }
3754
Willy Tarreauacc98002015-09-27 23:34:39 +02003755 act_flags |= ACT_FLAG_FIRST;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003756resume_execution:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003757 switch (rule->action) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003758 case ACT_ACTION_ALLOW:
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003759 return HTTP_RULE_RES_STOP; /* "allow" rules are OK */
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003760
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003761 case ACT_ACTION_DENY:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003762 txn->flags |= TX_SVDENY;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003763 return HTTP_RULE_RES_STOP;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003764
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003765 case ACT_HTTP_SET_NICE:
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003766 s->task->nice = rule->arg.nice;
3767 break;
3768
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003769 case ACT_HTTP_SET_TOS:
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003770 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Vincent Bernat6e615892016-05-18 16:17:44 +02003771 inet_set_tos(cli_conn->t.sock.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003772 break;
3773
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003774 case ACT_HTTP_SET_MARK:
Willy Tarreau51347ed2013-06-11 19:34:13 +02003775#ifdef SO_MARK
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02003776 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003777 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003778#endif
3779 break;
3780
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003781 case ACT_HTTP_SET_LOGL:
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003782 s->logs.level = rule->arg.loglevel;
3783 break;
3784
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003785 case ACT_HTTP_REPLACE_HDR:
3786 case ACT_HTTP_REPLACE_VAL:
Thierry FOURNIER5a33ac72015-03-16 23:54:35 +01003787 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name,
3788 rule->arg.hdr_add.name_len,
3789 &rule->arg.hdr_add.fmt,
3790 &rule->arg.hdr_add.re, rule->action))
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003791 return HTTP_RULE_RES_STOP; /* note: we should report an error here */
Sasha Pachev218f0642014-06-16 12:05:59 -06003792 break;
3793
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003794 case ACT_HTTP_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003795 ctx.idx = 0;
3796 /* remove all occurrences of the header */
3797 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3798 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3799 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3800 }
Willy Tarreau85603282015-01-21 20:39:27 +01003801 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003802
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003803 case ACT_HTTP_SET_HDR:
3804 case ACT_HTTP_ADD_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003805 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3806 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3807 trash.len = rule->arg.hdr_add.name_len;
3808 trash.str[trash.len++] = ':';
3809 trash.str[trash.len++] = ' ';
3810 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 +01003811
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003812 if (rule->action == ACT_HTTP_SET_HDR) {
Willy Tarreau85603282015-01-21 20:39:27 +01003813 /* remove all occurrences of the header */
3814 ctx.idx = 0;
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 }
3819 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003820 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3821 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003822
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003823 case ACT_HTTP_DEL_ACL:
3824 case ACT_HTTP_DEL_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003825 struct pat_ref *ref;
3826 char *key;
3827 int len;
3828
3829 /* collect reference */
3830 ref = pat_ref_lookup(rule->arg.map.ref);
3831 if (!ref)
3832 continue;
3833
3834 /* collect key */
3835 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3836 key = trash.str;
3837 key[len] = '\0';
3838
3839 /* perform update */
3840 /* returned code: 1=ok, 0=ko */
3841 pat_ref_delete(ref, key);
3842
3843 break;
3844 }
3845
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003846 case ACT_HTTP_ADD_ACL: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003847 struct pat_ref *ref;
3848 char *key;
3849 struct chunk *trash_key;
3850 int len;
3851
3852 trash_key = get_trash_chunk();
3853
3854 /* collect reference */
3855 ref = pat_ref_lookup(rule->arg.map.ref);
3856 if (!ref)
3857 continue;
3858
3859 /* collect key */
3860 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3861 key = trash_key->str;
3862 key[len] = '\0';
3863
3864 /* perform update */
3865 /* check if the entry already exists */
3866 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003867 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003868
3869 break;
3870 }
3871
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003872 case ACT_HTTP_SET_MAP: {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003873 struct pat_ref *ref;
3874 char *key, *value;
3875 struct chunk *trash_key, *trash_value;
3876 int len;
3877
3878 trash_key = get_trash_chunk();
3879 trash_value = get_trash_chunk();
3880
3881 /* collect reference */
3882 ref = pat_ref_lookup(rule->arg.map.ref);
3883 if (!ref)
3884 continue;
3885
3886 /* collect key */
3887 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3888 key = trash_key->str;
3889 key[len] = '\0';
3890
3891 /* collect value */
3892 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3893 value = trash_value->str;
3894 value[len] = '\0';
3895
3896 /* perform update */
3897 if (pat_ref_find_elt(ref, key) != NULL)
3898 /* update entry if it exists */
3899 pat_ref_set(ref, key, value, NULL);
3900 else
3901 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003902 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003903
3904 break;
3905 }
William Lallemand73025dd2014-04-24 14:38:37 +02003906
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02003907 case ACT_HTTP_REDIR:
Willy Tarreau51d861a2015-05-22 17:30:48 +02003908 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3909 return HTTP_RULE_RES_BADREQ;
3910 return HTTP_RULE_RES_DONE;
3911
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003912 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3913 /* Note: only the first valid tracking parameter of each
3914 * applies.
3915 */
3916
3917 if (stkctr_entry(&s->stkctr[http_trk_idx(rule->action)]) == NULL) {
3918 struct stktable *t;
3919 struct stksess *ts;
3920 struct stktable_key *key;
3921 void *ptr;
3922
3923 t = rule->arg.trk_ctr.table.t;
3924 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
3925
3926 if (key && (ts = stktable_get_entry(t, key))) {
3927 stream_track_stkctr(&s->stkctr[http_trk_idx(rule->action)], t, ts);
3928
3929 /* let's count a new HTTP request as it's the first time we do it */
3930 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3931 if (ptr)
3932 stktable_data_cast(ptr, http_req_cnt)++;
3933
3934 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3935 if (ptr)
3936 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3937 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3938
3939 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3940 if (sess->fe != s->be)
3941 stkctr_set_flags(&s->stkctr[http_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3942
3943 /* When the client triggers a 4xx from the server, it's most often due
3944 * to a missing object or permission. These events should be tracked
3945 * because if they happen often, it may indicate a brute force or a
3946 * vulnerability scan. Normally this is done when receiving the response
3947 * but here we're tracking after this ought to have been done so we have
3948 * to do it on purpose.
3949 */
Willy Tarreau3146a4c2016-07-26 15:22:33 +02003950 if ((unsigned)(txn->status - 400) < 100) {
3951 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3952 if (ptr)
3953 stktable_data_cast(ptr, http_err_cnt)++;
3954
3955 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3956 if (ptr)
3957 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3958 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3959 }
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08003960 }
3961 }
3962 break;
3963
Thierry FOURNIER42148732015-09-02 17:17:33 +02003964 case ACT_CUSTOM:
Willy Tarreauacc98002015-09-27 23:34:39 +02003965 if ((px->options & PR_O_ABRT_CLOSE) && (s->req.flags & (CF_SHUTR|CF_READ_NULL|CF_READ_ERROR)))
3966 act_flags |= ACT_FLAG_FINAL;
Willy Tarreau39458682015-09-27 10:33:15 +02003967
Willy Tarreauacc98002015-09-27 23:34:39 +02003968 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003969 case ACT_RET_ERR:
3970 case ACT_RET_CONT:
3971 break;
Thierry FOURNIER42148732015-09-02 17:17:33 +02003972 case ACT_RET_STOP:
3973 return HTTP_RULE_RES_STOP;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +02003974 case ACT_RET_YIELD:
Willy Tarreau152b81e2015-04-20 13:26:17 +02003975 s->current_rule = rule;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01003976 return HTTP_RULE_RES_YIELD;
3977 }
William Lallemand73025dd2014-04-24 14:38:37 +02003978 break;
3979
Thierry FOURNIER22e49012015-08-05 19:13:48 +02003980 /* other flags exists, but normaly, they never be matched. */
3981 default:
3982 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003983 }
3984 }
3985
3986 /* we reached the end of the rules, nothing to report */
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01003987 return HTTP_RULE_RES_CONT;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003988}
3989
3990
Willy Tarreau71241ab2012-12-27 11:30:54 +01003991/* Perform an HTTP redirect based on the information in <rule>. The function
3992 * returns non-zero on success, or zero in case of a, irrecoverable error such
3993 * as too large a request to build a valid response.
3994 */
Willy Tarreau87b09662015-04-03 00:22:06 +02003995static int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Willy Tarreau71241ab2012-12-27 11:30:54 +01003996{
Willy Tarreaub329a312015-05-22 16:27:37 +02003997 struct http_msg *req = &txn->req;
3998 struct http_msg *res = &txn->rsp;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003999 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004000 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004001
4002 /* build redirect message */
4003 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04004004 case 308:
4005 msg_fmt = HTTP_308;
4006 break;
4007 case 307:
4008 msg_fmt = HTTP_307;
4009 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004010 case 303:
4011 msg_fmt = HTTP_303;
4012 break;
4013 case 301:
4014 msg_fmt = HTTP_301;
4015 break;
4016 case 302:
4017 default:
4018 msg_fmt = HTTP_302;
4019 break;
4020 }
4021
4022 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
4023 return 0;
4024
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004025 location = trash.str + trash.len;
4026
Willy Tarreau71241ab2012-12-27 11:30:54 +01004027 switch(rule->type) {
4028 case REDIRECT_TYPE_SCHEME: {
4029 const char *path;
4030 const char *host;
4031 struct hdr_ctx ctx;
4032 int pathlen;
4033 int hostlen;
4034
4035 host = "";
4036 hostlen = 0;
4037 ctx.idx = 0;
Willy Tarreaub329a312015-05-22 16:27:37 +02004038 if (http_find_header2("Host", 4, req->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004039 host = ctx.line + ctx.val;
4040 hostlen = ctx.vlen;
4041 }
4042
4043 path = http_get_path(txn);
4044 /* build message using path */
4045 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004046 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004047 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4048 int qs = 0;
4049 while (qs < pathlen) {
4050 if (path[qs] == '?') {
4051 pathlen = qs;
4052 break;
4053 }
4054 qs++;
4055 }
4056 }
4057 } else {
4058 path = "/";
4059 pathlen = 1;
4060 }
4061
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004062 if (rule->rdr_str) { /* this is an old "redirect" rule */
4063 /* check if we can add scheme + "://" + host + path */
4064 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
4065 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004066
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004067 /* add scheme */
4068 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4069 trash.len += rule->rdr_len;
4070 }
4071 else {
4072 /* add scheme with executing log format */
4073 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004074
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004075 /* check if we can add scheme + "://" + host + path */
4076 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
4077 return 0;
4078 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004079 /* add "://" */
4080 memcpy(trash.str + trash.len, "://", 3);
4081 trash.len += 3;
4082
4083 /* add host */
4084 memcpy(trash.str + trash.len, host, hostlen);
4085 trash.len += hostlen;
4086
4087 /* add path */
4088 memcpy(trash.str + trash.len, path, pathlen);
4089 trash.len += pathlen;
4090
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004091 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004092 if (trash.len && trash.str[trash.len - 1] != '/' &&
4093 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4094 if (trash.len > trash.size - 5)
4095 return 0;
4096 trash.str[trash.len] = '/';
4097 trash.len++;
4098 }
4099
4100 break;
4101 }
4102 case REDIRECT_TYPE_PREFIX: {
4103 const char *path;
4104 int pathlen;
4105
4106 path = http_get_path(txn);
4107 /* build message using path */
4108 if (path) {
Willy Tarreaub329a312015-05-22 16:27:37 +02004109 pathlen = req->sl.rq.u_l + (req->chn->buf->p + req->sl.rq.u) - path;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004110 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
4111 int qs = 0;
4112 while (qs < pathlen) {
4113 if (path[qs] == '?') {
4114 pathlen = qs;
4115 break;
4116 }
4117 qs++;
4118 }
4119 }
4120 } else {
4121 path = "/";
4122 pathlen = 1;
4123 }
4124
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004125 if (rule->rdr_str) { /* this is an old "redirect" rule */
4126 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
4127 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004128
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004129 /* add prefix. Note that if prefix == "/", we don't want to
4130 * add anything, otherwise it makes it hard for the user to
4131 * configure a self-redirection.
4132 */
4133 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
4134 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4135 trash.len += rule->rdr_len;
4136 }
4137 }
4138 else {
4139 /* add prefix with executing log format */
4140 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
4141
4142 /* Check length */
4143 if (trash.len + pathlen > trash.size - 4)
4144 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004145 }
4146
4147 /* add path */
4148 memcpy(trash.str + trash.len, path, pathlen);
4149 trash.len += pathlen;
4150
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004151 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01004152 if (trash.len && trash.str[trash.len - 1] != '/' &&
4153 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
4154 if (trash.len > trash.size - 5)
4155 return 0;
4156 trash.str[trash.len] = '/';
4157 trash.len++;
4158 }
4159
4160 break;
4161 }
4162 case REDIRECT_TYPE_LOCATION:
4163 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004164 if (rule->rdr_str) { /* this is an old "redirect" rule */
4165 if (trash.len + rule->rdr_len > trash.size - 4)
4166 return 0;
4167
4168 /* add location */
4169 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
4170 trash.len += rule->rdr_len;
4171 }
4172 else {
4173 /* add location with executing log format */
4174 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004175
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004176 /* Check left length */
4177 if (trash.len > trash.size - 4)
4178 return 0;
4179 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004180 break;
4181 }
4182
4183 if (rule->cookie_len) {
4184 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
4185 trash.len += 14;
4186 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
4187 trash.len += rule->cookie_len;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004188 }
4189
4190 /* add end of headers and the keep-alive/close status.
4191 * We may choose to set keep-alive if the Location begins
4192 * with a slash, because the client will come back to the
4193 * same server.
4194 */
4195 txn->status = rule->code;
4196 /* let's log the request time */
4197 s->logs.tv_request = now;
4198
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01004199 if (*location == '/' &&
Willy Tarreaub329a312015-05-22 16:27:37 +02004200 (req->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau2de8a502015-05-28 17:23:54 +02004201 ((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01004202 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4203 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4204 /* keep-alive possible */
Willy Tarreaub329a312015-05-22 16:27:37 +02004205 if (!(req->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004206 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4207 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
4208 trash.len += 30;
4209 } else {
4210 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
4211 trash.len += 24;
4212 }
4213 }
4214 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
4215 trash.len += 4;
Christopher Faulet3e344292015-11-24 16:24:13 +01004216 FLT_STRM_CB(s, flt_http_reply(s, txn->status, &trash));
Willy Tarreaub329a312015-05-22 16:27:37 +02004217 bo_inject(res->chn, trash.str, trash.len);
Willy Tarreau71241ab2012-12-27 11:30:54 +01004218 /* "eat" the request */
Willy Tarreaub329a312015-05-22 16:27:37 +02004219 bi_fast_delete(req->chn->buf, req->sov);
4220 req->next -= req->sov;
4221 req->sov = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004222 s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_FLT_END);
4223 s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->req.analysers & AN_FLT_END);
Willy Tarreaub329a312015-05-22 16:27:37 +02004224 req->msg_state = HTTP_MSG_CLOSED;
4225 res->msg_state = HTTP_MSG_DONE;
Willy Tarreau51d861a2015-05-22 17:30:48 +02004226 /* Trim any possible response */
4227 res->chn->buf->i = 0;
4228 res->next = res->sov = 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004229 } else {
4230 /* keep-alive not possible */
4231 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
4232 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
4233 trash.len += 29;
4234 } else {
4235 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
4236 trash.len += 23;
4237 }
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004238 http_reply_and_close(s, txn->status, &trash);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004239 req->chn->analysers &= AN_FLT_END;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004240 }
4241
Willy Tarreaue7dff022015-04-03 01:14:29 +02004242 if (!(s->flags & SF_ERR_MASK))
4243 s->flags |= SF_ERR_LOCAL;
4244 if (!(s->flags & SF_FINST_MASK))
4245 s->flags |= SF_FINST_R;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004246
4247 return 1;
4248}
4249
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004250/* This stream analyser runs all HTTP request processing which is common to
4251 * frontends and backends, which means blocking ACLs, filters, connection-close,
4252 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02004253 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004254 * either needs more data or wants to immediately abort the request (eg: deny,
4255 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02004256 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004257int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02004258{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004259 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004260 struct http_txn *txn = s->txn;
Willy Tarreaud787e662009-07-07 10:14:51 +02004261 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004262 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004263 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004264 enum rule_result verdict;
Willy Tarreau58727ec2016-05-25 16:23:59 +02004265 int deny_status = HTTP_ERR_403;
Willy Tarreaud787e662009-07-07 10:14:51 +02004266
Willy Tarreau655dce92009-11-08 13:10:58 +01004267 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004268 /* we need more data */
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004269 goto return_prx_yield;
Willy Tarreau51aecc72009-07-12 09:47:04 +02004270 }
4271
Willy Tarreau87b09662015-04-03 00:22:06 +02004272 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 +02004273 now_ms, __FUNCTION__,
4274 s,
4275 req,
4276 req->rex, req->wex,
4277 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004278 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004279 req->analysers);
4280
Willy Tarreau65410832014-04-28 21:25:43 +02004281 /* just in case we have some per-backend tracking */
Willy Tarreau87b09662015-04-03 00:22:06 +02004282 stream_inc_be_http_req_ctr(s);
Willy Tarreau65410832014-04-28 21:25:43 +02004283
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004284 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004285 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Willy Tarreau58727ec2016-05-25 16:23:59 +02004286 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Willy Tarreau51425942010-02-01 10:40:19 +01004287
Willy Tarreau0b748332014-04-29 00:13:29 +02004288 switch (verdict) {
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004289 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
4290 goto return_prx_yield;
4291
Willy Tarreau0b748332014-04-29 00:13:29 +02004292 case HTTP_RULE_RES_CONT:
4293 case HTTP_RULE_RES_STOP: /* nothing to do */
4294 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004295
Willy Tarreau0b748332014-04-29 00:13:29 +02004296 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4297 if (txn->flags & TX_CLTARPIT)
4298 goto tarpit;
4299 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004300
Willy Tarreau0b748332014-04-29 00:13:29 +02004301 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4302 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004303
Willy Tarreau0b748332014-04-29 00:13:29 +02004304 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004305 goto done;
4306
Willy Tarreau0b748332014-04-29 00:13:29 +02004307 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4308 goto return_bad_req;
4309 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004310 }
4311
Willy Tarreau52542592014-04-28 18:33:26 +02004312 /* OK at this stage, we know that the request was accepted according to
4313 * the http-request rules, we can check for the stats. Note that the
4314 * URI is detected *before* the req* rules in order not to be affected
4315 * by a possible reqrep, while they are processed *after* so that a
4316 * reqdeny can still block them. This clearly needs to change in 1.6!
4317 */
Willy Tarreau350f4872014-11-28 14:42:25 +01004318 if (stats_check_uri(&s->si[1], txn, px)) {
Willy Tarreau52542592014-04-28 18:33:26 +02004319 s->target = &http_stats_applet.obj_type;
Willy Tarreau350f4872014-11-28 14:42:25 +01004320 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
Willy Tarreau52542592014-04-28 18:33:26 +02004321 txn->status = 500;
4322 s->logs.tv_request = now;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004323 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004324
Willy Tarreaue7dff022015-04-03 01:14:29 +02004325 if (!(s->flags & SF_ERR_MASK))
4326 s->flags |= SF_ERR_RESOURCE;
Willy Tarreau52542592014-04-28 18:33:26 +02004327 goto return_prx_cond;
4328 }
4329
4330 /* parse the whole stats request and extract the relevant information */
4331 http_handle_stats(s, req);
Willy Tarreau58727ec2016-05-25 16:23:59 +02004332 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Willy Tarreau0b748332014-04-29 00:13:29 +02004333 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004334
Willy Tarreau0b748332014-04-29 00:13:29 +02004335 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4336 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004337
Willy Tarreau0b748332014-04-29 00:13:29 +02004338 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4339 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004340 }
4341
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004342 /* evaluate the req* rules except reqadd */
4343 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004344 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004345 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004346
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004347 if (txn->flags & TX_CLDENY)
4348 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004349
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004350 if (txn->flags & TX_CLTARPIT)
4351 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004352 }
Willy Tarreau06619262006-12-17 08:37:22 +01004353
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004354 /* add request headers from the rule sets in the same order */
4355 list_for_each_entry(wl, &px->req_add, list) {
4356 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02004357 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004358 ret = acl_pass(ret);
4359 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4360 ret = !ret;
4361 if (!ret)
4362 continue;
4363 }
4364
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004365 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004366 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004367 }
4368
Willy Tarreau52542592014-04-28 18:33:26 +02004369
4370 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004371 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004372 /* process the stats request now */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004373 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
4374 sess->fe->fe_counters.intercepted_req++;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004375
Willy Tarreaue7dff022015-04-03 01:14:29 +02004376 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
4377 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
4378 if (!(s->flags & SF_FINST_MASK))
4379 s->flags |= SF_FINST_R;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004380
Willy Tarreau70730dd2014-04-24 18:06:27 +02004381 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Christopher Faulet309c6412015-12-02 09:57:32 +01004382 req->analysers &= (AN_REQ_HTTP_BODY | AN_FLT_HTTP_HDRS | AN_FLT_END);
Christopher Fauletd7c91962015-04-30 11:48:27 +02004383 req->analysers &= ~AN_FLT_XFER_DATA;
4384 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004385 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004386 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004387
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004388 /* check whether we have some ACLs set to redirect this request */
4389 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004390 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004391 int ret;
4392
Willy Tarreau192252e2015-04-04 01:47:55 +02004393 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004394 ret = acl_pass(ret);
4395 if (rule->cond->pol == ACL_COND_UNLESS)
4396 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004397 if (!ret)
4398 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004399 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004400 if (!http_apply_redirect_rule(rule, s, txn))
4401 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004402 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004403 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004404
Willy Tarreau2be39392010-01-03 17:24:51 +01004405 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4406 * If this happens, then the data will not come immediately, so we must
4407 * send all what we have without waiting. Note that due to the small gain
4408 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004409 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004410 * itself once used.
4411 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004412 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004413
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004414 done: /* done with this analyser, continue with next ones that the calling
4415 * points will have set, if any.
4416 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004417 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004418 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4419 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004420 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004421
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004422 tarpit:
4423 /* When a connection is tarpitted, we use the tarpit timeout,
4424 * which may be the same as the connect timeout if unspecified.
4425 * If unset, then set it to zero because we really want it to
4426 * eventually expire. We build the tarpit as an analyser.
4427 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004428 channel_erase(&s->req);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004429
4430 /* wipe the request out so that we can drop the connection early
4431 * if the client closes first.
4432 */
4433 channel_dont_connect(req);
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004434
4435 /* Allow cookie logging
4436 */
4437 if (s->be->cookie_name || sess->fe->capture_name)
4438 manage_client_side_cookies(s, req);
4439
Christopher Fauletd7c91962015-04-30 11:48:27 +02004440 req->analysers &= AN_FLT_END; /* remove switching rules etc... */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004441 req->analysers |= AN_REQ_HTTP_TARPIT;
4442 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4443 if (!req->analyse_exp)
4444 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreau87b09662015-04-03 00:22:06 +02004445 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004446 sess->fe->fe_counters.denied_req++;
4447 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004448 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004449 if (sess->listener->counters)
4450 sess->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004451 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004452
4453 deny: /* this request was blocked (denied) */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004454
4455 /* Allow cookie logging
4456 */
4457 if (s->be->cookie_name || sess->fe->capture_name)
4458 manage_client_side_cookies(s, req);
4459
Willy Tarreau0b748332014-04-29 00:13:29 +02004460 txn->flags |= TX_CLDENY;
Willy Tarreau58727ec2016-05-25 16:23:59 +02004461 txn->status = http_err_codes[deny_status];
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004462 s->logs.tv_request = now;
Willy Tarreau58727ec2016-05-25 16:23:59 +02004463 http_reply_and_close(s, txn->status, http_error_message(s, deny_status));
Willy Tarreau87b09662015-04-03 00:22:06 +02004464 stream_inc_http_err_ctr(s);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004465 sess->fe->fe_counters.denied_req++;
4466 if (sess->fe != s->be)
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004467 s->be->be_counters.denied_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004468 if (sess->listener->counters)
4469 sess->listener->counters->denied_req++;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004470 goto return_prx_cond;
4471
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004472 return_bad_req:
4473 /* We centralize bad requests processing here */
4474 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4475 /* we detected a parsing error. We want to archive this request
4476 * in the dedicated proxy area for later troubleshooting.
4477 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004478 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004479 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004480
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004481 txn->req.msg_state = HTTP_MSG_ERROR;
4482 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004483 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004484
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004485 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004486 if (sess->listener->counters)
4487 sess->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004488
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004489 return_prx_cond:
Willy Tarreaue7dff022015-04-03 01:14:29 +02004490 if (!(s->flags & SF_ERR_MASK))
4491 s->flags |= SF_ERR_PRXCOND;
4492 if (!(s->flags & SF_FINST_MASK))
4493 s->flags |= SF_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004494
Christopher Fauletd7c91962015-04-30 11:48:27 +02004495 req->analysers &= AN_FLT_END;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004496 req->analyse_exp = TICK_ETERNITY;
4497 return 0;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01004498
4499 return_prx_yield:
4500 channel_dont_connect(req);
4501 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004502}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004503
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004504/* This function performs all the processing enabled for the current request.
4505 * It returns 1 if the processing can continue on next analysers, or zero if it
4506 * needs more data, encounters an error, or wants to immediately abort the
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004507 * request. It relies on buffers flags, and updates s->req.analysers.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004508 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004509int http_process_request(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004510{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004511 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004512 struct http_txn *txn = s->txn;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004513 struct http_msg *msg = &txn->req;
Willy Tarreauee335e62015-04-21 18:15:13 +02004514 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004515
Willy Tarreau655dce92009-11-08 13:10:58 +01004516 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004517 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004518 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004519 return 0;
4520 }
4521
Willy Tarreau87b09662015-04-03 00:22:06 +02004522 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 +02004523 now_ms, __FUNCTION__,
4524 s,
4525 req,
4526 req->rex, req->wex,
4527 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004528 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004529 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004530
Willy Tarreau59234e92008-11-30 23:51:27 +01004531 /*
4532 * Right now, we know that we have processed the entire headers
4533 * and that unwanted requests have been filtered out. We can do
4534 * whatever we want with the remaining request. Also, now we
4535 * may have separate values for ->fe, ->be.
4536 */
Willy Tarreau06619262006-12-17 08:37:22 +01004537
Willy Tarreau59234e92008-11-30 23:51:27 +01004538 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004539 * If HTTP PROXY is set we simply get remote server address parsing
4540 * incoming request. Note that this requires that a connection is
4541 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004542 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004543 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004544 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004545 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004546
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004547 /* Note that for now we don't reuse existing proxy connections */
Willy Tarreau973a5422015-08-05 21:47:23 +02004548 if (unlikely((conn = si_alloc_conn(&s->si[1])) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004549 txn->req.msg_state = HTTP_MSG_ERROR;
4550 txn->status = 500;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004551 req->analysers &= AN_FLT_END;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004552 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500));
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004553
Willy Tarreaue7dff022015-04-03 01:14:29 +02004554 if (!(s->flags & SF_ERR_MASK))
4555 s->flags |= SF_ERR_RESOURCE;
4556 if (!(s->flags & SF_FINST_MASK))
4557 s->flags |= SF_FINST_R;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004558
4559 return 0;
4560 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004561
4562 path = http_get_path(txn);
4563 url2sa(req->buf->p + msg->sl.rq.u,
4564 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004565 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004566 /* if the path was found, we have to remove everything between
4567 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4568 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4569 * u_l characters by a single "/".
4570 */
4571 if (path) {
4572 char *cur_ptr = req->buf->p;
4573 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4574 int delta;
4575
4576 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4577 http_msg_move_end(&txn->req, delta);
4578 cur_end += delta;
4579 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4580 goto return_bad_req;
4581 }
4582 else {
4583 char *cur_ptr = req->buf->p;
4584 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4585 int delta;
4586
4587 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4588 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4589 http_msg_move_end(&txn->req, delta);
4590 cur_end += delta;
4591 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4592 goto return_bad_req;
4593 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004594 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004595
Willy Tarreau59234e92008-11-30 23:51:27 +01004596 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004597 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004598 * Note that doing so might move headers in the request, but
4599 * the fields will stay coherent and the URI will not move.
4600 * This should only be performed in the backend.
4601 */
Bertrand Paquet83a2c3d2016-04-06 11:58:31 +02004602 if (s->be->cookie_name || sess->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004603 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004604
William Lallemanda73203e2012-03-12 12:48:57 +01004605 /* add unique-id if "header-unique-id" is specified */
4606
Thierry Fournierf4011dd2016-03-29 17:23:51 +02004607 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004608 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4609 goto return_bad_req;
4610 s->unique_id[0] = '\0';
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004611 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004612 }
William Lallemanda73203e2012-03-12 12:48:57 +01004613
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004614 if (sess->fe->header_unique_id && s->unique_id) {
4615 chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004616 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004617 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004618 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004619 goto return_bad_req;
4620 }
4621
Cyril Bontéb21570a2009-11-29 20:04:48 +01004622 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004623 * 9: add X-Forwarded-For if either the frontend or the backend
4624 * asks for it.
4625 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004626 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004627 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004628 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
4629 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
4630 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004631 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004632 /* The header is set to be added only if none is present
4633 * and we found it, so don't do anything.
4634 */
4635 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004636 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004637 /* Add an X-Forwarded-For header unless the source IP is
4638 * in the 'except' network range.
4639 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004640 if ((!sess->fe->except_mask.s_addr ||
4641 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
4642 != sess->fe->except_net.s_addr) &&
Willy Tarreau59234e92008-11-30 23:51:27 +01004643 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004644 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004645 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004646 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004647 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004648 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004649
4650 /* Note: we rely on the backend to get the header name to be used for
4651 * x-forwarded-for, because the header is really meant for the backends.
4652 * However, if the backend did not specify any option, we have to rely
4653 * on the frontend's header name.
4654 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004655 if (s->be->fwdfor_hdr_len) {
4656 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004657 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004658 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004659 len = sess->fe->fwdfor_hdr_len;
4660 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004661 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004662 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 +01004663
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004664 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004665 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004666 }
4667 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004668 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004669 /* FIXME: for the sake of completeness, we should also support
4670 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004671 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004672 int len;
4673 char pn[INET6_ADDRSTRLEN];
4674 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004675 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004676 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004677
Willy Tarreau59234e92008-11-30 23:51:27 +01004678 /* Note: we rely on the backend to get the header name to be used for
4679 * x-forwarded-for, because the header is really meant for the backends.
4680 * However, if the backend did not specify any option, we have to rely
4681 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004682 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004683 if (s->be->fwdfor_hdr_len) {
4684 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004685 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004686 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004687 len = sess->fe->fwdfor_hdr_len;
4688 memcpy(trash.str, sess->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004689 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004690 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004691
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004692 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004693 goto return_bad_req;
4694 }
4695 }
4696
4697 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004698 * 10: add X-Original-To if either the frontend or the backend
4699 * asks for it.
4700 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004701 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004702
4703 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004704 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004705 /* Add an X-Original-To header unless the destination IP is
4706 * in the 'except' network range.
4707 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004708 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004709
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004710 if (cli_conn->addr.to.ss_family == AF_INET &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004711 ((!sess->fe->except_mask_to.s_addr ||
4712 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
4713 != sess->fe->except_to.s_addr) &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004714 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004715 (((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 +02004716 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004717 int len;
4718 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004719 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004720
4721 /* Note: we rely on the backend to get the header name to be used for
4722 * x-original-to, because the header is really meant for the backends.
4723 * However, if the backend did not specify any option, we have to rely
4724 * on the frontend's header name.
4725 */
4726 if (s->be->orgto_hdr_len) {
4727 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004728 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004729 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004730 len = sess->fe->orgto_hdr_len;
4731 memcpy(trash.str, sess->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004732 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004733 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 +02004734
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004735 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004736 goto return_bad_req;
4737 }
4738 }
4739 }
4740
Willy Tarreau50fc7772012-11-11 22:19:57 +01004741 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4742 * If an "Upgrade" token is found, the header is left untouched in order not to have
4743 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4744 * "Upgrade" is present in the Connection header.
4745 */
4746 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4747 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004748 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004749 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004750 unsigned int want_flags = 0;
4751
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004752 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004753 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004754 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004755 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004756 !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004757 want_flags |= TX_CON_CLO_SET;
4758 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004759 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004760 ((sess->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004761 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004762 ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004763 want_flags |= TX_CON_KAL_SET;
4764 }
4765
4766 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004767 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004768 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004769
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004770
Willy Tarreau522d6c02009-12-06 18:49:18 +01004771 /* If we have no server assigned yet and we're balancing on url_param
4772 * with a POST request, we may be interested in checking the body for
4773 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004774 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02004775 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreaueee5b512015-04-03 23:46:31 +02004776 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004777 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004778 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004779 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004780 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004781
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004782 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02004783 req->analysers &= ~AN_FLT_XFER_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004784 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004785#ifdef TCP_QUICKACK
4786 /* We expect some data from the client. Unless we know for sure
4787 * we already have a full request, we have to re-enable quick-ack
4788 * in case we previously disabled it, otherwise we might cause
4789 * the client to delay further data.
4790 */
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004791 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004792 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004793 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004794 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004795 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004796#endif
4797 }
Willy Tarreau03945942009-12-22 16:50:27 +01004798
Willy Tarreau59234e92008-11-30 23:51:27 +01004799 /*************************************************************
4800 * OK, that's finished for the headers. We have done what we *
4801 * could. Let's switch to the DATA state. *
4802 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004803 req->analyse_exp = TICK_ETERNITY;
4804 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004805
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004806 /* if the server closes the connection, we want to immediately react
4807 * and close the socket to save packets and syscalls.
4808 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004809 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
Willy Tarreau350f4872014-11-28 14:42:25 +01004810 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004811
Willy Tarreau59234e92008-11-30 23:51:27 +01004812 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004813 /* OK let's go on with the BODY now */
4814 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004815
Willy Tarreau59234e92008-11-30 23:51:27 +01004816 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004817 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004818 /* we detected a parsing error. We want to archive this request
4819 * in the dedicated proxy area for later troubleshooting.
4820 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004821 http_capture_bad_message(&sess->fe->invalid_req, s, msg, msg->msg_state, sess->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004822 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004823
Willy Tarreau59234e92008-11-30 23:51:27 +01004824 txn->req.msg_state = HTTP_MSG_ERROR;
4825 txn->status = 400;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004826 req->analysers &= AN_FLT_END;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004827 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004828
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02004829 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004830 if (sess->listener->counters)
4831 sess->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004832
Willy Tarreaue7dff022015-04-03 01:14:29 +02004833 if (!(s->flags & SF_ERR_MASK))
4834 s->flags |= SF_ERR_PRXCOND;
4835 if (!(s->flags & SF_FINST_MASK))
4836 s->flags |= SF_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004837 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004838}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004839
Willy Tarreau60b85b02008-11-30 23:28:40 +01004840/* This function is an analyser which processes the HTTP tarpit. It always
4841 * returns zero, at the beginning because it prevents any other processing
4842 * from occurring, and at the end because it terminates the request.
4843 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004844int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004845{
Willy Tarreaueee5b512015-04-03 23:46:31 +02004846 struct http_txn *txn = s->txn;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004847
4848 /* This connection is being tarpitted. The CLIENT side has
4849 * already set the connect expiration date to the right
4850 * timeout. We just have to check that the client is still
4851 * there and that the timeout has not expired.
4852 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004853 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004854 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004855 !tick_is_expired(req->analyse_exp, now_ms))
4856 return 0;
4857
4858 /* We will set the queue timer to the time spent, just for
4859 * logging purposes. We fake a 500 server error, so that the
4860 * attacker will not suspect his connection has been tarpitted.
4861 * It will not cause trouble to the logs because we can exclude
4862 * the tarpitted connections by filtering on the 'PT' status flags.
4863 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004864 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4865
4866 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004867 if (!(req->flags & CF_READ_ERROR))
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004868 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004869
Christopher Fauletd7c91962015-04-30 11:48:27 +02004870 req->analysers &= AN_FLT_END;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004871 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004872
Willy Tarreaue7dff022015-04-03 01:14:29 +02004873 if (!(s->flags & SF_ERR_MASK))
4874 s->flags |= SF_ERR_PRXCOND;
4875 if (!(s->flags & SF_FINST_MASK))
4876 s->flags |= SF_FINST_T;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004877 return 0;
4878}
4879
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004880/* This function is an analyser which waits for the HTTP request body. It waits
4881 * for either the buffer to be full, or the full advertised contents to have
4882 * reached the buffer. It must only be called after the standard HTTP request
4883 * processing has occurred, because it expects the request to be parsed and will
4884 * look for the Expect header. It may send a 100-Continue interim response. It
4885 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4886 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4887 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004888 */
Willy Tarreau87b09662015-04-03 00:22:06 +02004889int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004890{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02004891 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02004892 struct http_txn *txn = s->txn;
4893 struct http_msg *msg = &s->txn->req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004894
4895 /* We have to parse the HTTP request body to find any required data.
4896 * "balance url_param check_post" should have been the only way to get
4897 * into this. We were brought here after HTTP header analysis, so all
4898 * related structures are ready.
4899 */
4900
Willy Tarreau890988f2014-04-10 11:59:33 +02004901 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4902 /* This is the first call */
4903 if (msg->msg_state < HTTP_MSG_BODY)
4904 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004905
Willy Tarreau890988f2014-04-10 11:59:33 +02004906 if (msg->msg_state < HTTP_MSG_100_SENT) {
4907 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4908 * send an HTTP/1.1 100 Continue intermediate response.
4909 */
4910 if (msg->flags & HTTP_MSGF_VER_11) {
4911 struct hdr_ctx ctx;
4912 ctx.idx = 0;
4913 /* Expect is allowed in 1.1, look for it */
4914 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4915 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01004916 bo_inject(&s->res, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau890988f2014-04-10 11:59:33 +02004917 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004918 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004919 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004920 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004921
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004922 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004923 * req->buf->p still points to the beginning of the message. We
4924 * must save the body in msg->next because it survives buffer
4925 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004926 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004927 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004928
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004929 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004930 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4931 else
4932 msg->msg_state = HTTP_MSG_DATA;
4933 }
4934
Willy Tarreau890988f2014-04-10 11:59:33 +02004935 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4936 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreaue115b492015-05-01 23:05:14 +02004937 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004938 goto missing_data;
4939
4940 /* OK we have everything we need now */
4941 goto http_end;
4942 }
4943
4944 /* OK here we're parsing a chunked-encoded message */
4945
Willy Tarreau522d6c02009-12-06 18:49:18 +01004946 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004947 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004948 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004949 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004950 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004951 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004952
Willy Tarreau115acb92009-12-26 13:56:06 +01004953 if (!ret)
4954 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004955 else if (ret < 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004956 stream_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004957 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004958 }
Christopher Faulet113f7de2015-12-14 14:52:13 +01004959 msg->next += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02004960 msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreaud34af782008-11-30 23:36:37 +01004961 }
4962
Willy Tarreaud98cf932009-12-27 22:54:55 +01004963 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaue115b492015-05-01 23:05:14 +02004964 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4965 * for at least a whole chunk or the whole content length bytes after
4966 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004967 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004968 if (msg->msg_state == HTTP_MSG_TRAILERS)
4969 goto http_end;
4970
Willy Tarreaue115b492015-05-01 23:05:14 +02004971 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004972 goto http_end;
4973
4974 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004975 /* we get here if we need to wait for more data. If the buffer is full,
4976 * we have the maximum we can expect.
4977 */
4978 if (buffer_full(req->buf, global.tune.maxrewrite))
4979 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004980
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004981 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004982 txn->status = 408;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01004983 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004984
Willy Tarreaue7dff022015-04-03 01:14:29 +02004985 if (!(s->flags & SF_ERR_MASK))
4986 s->flags |= SF_ERR_CLITO;
4987 if (!(s->flags & SF_FINST_MASK))
4988 s->flags |= SF_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004989 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004990 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004991
4992 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004993 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004994 /* Not enough data. We'll re-use the http-request
4995 * timeout here. Ideally, we should set the timeout
4996 * relative to the accept() date. We just set the
4997 * request timeout once at the beginning of the
4998 * request.
4999 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005000 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01005001 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02005002 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01005003 return 0;
5004 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01005005
5006 http_end:
5007 /* The situation will not evolve, so let's give up on the analysis. */
5008 s->logs.tv_request = now; /* update the request timer to reflect full request */
5009 req->analysers &= ~an_bit;
5010 req->analyse_exp = TICK_ETERNITY;
5011 return 1;
5012
5013 return_bad_req: /* let's centralize all bad requests */
5014 txn->req.msg_state = HTTP_MSG_ERROR;
5015 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005016 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01005017
Willy Tarreaue7dff022015-04-03 01:14:29 +02005018 if (!(s->flags & SF_ERR_MASK))
5019 s->flags |= SF_ERR_PRXCOND;
5020 if (!(s->flags & SF_FINST_MASK))
5021 s->flags |= SF_FINST_R;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005022
Willy Tarreau522d6c02009-12-06 18:49:18 +01005023 return_err_msg:
Christopher Fauletd7c91962015-04-30 11:48:27 +02005024 req->analysers &= AN_FLT_END;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005025 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005026 if (sess->listener->counters)
5027 sess->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01005028 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01005029}
5030
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005031/* send a server's name with an outgoing request over an established connection.
5032 * Note: this function is designed to be called once the request has been scheduled
5033 * for being forwarded. This is the reason why it rewinds the buffer before
5034 * proceeding.
5035 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01005036int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05005037
5038 struct hdr_ctx ctx;
5039
Mark Lamourinec2247f02012-01-04 13:02:01 -05005040 char *hdr_name = be->server_id_hdr_name;
5041 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02005042 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005043 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005044 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005045
William Lallemandd9e90662012-01-30 17:27:17 +01005046 ctx.idx = 0;
5047
Willy Tarreau211cdec2014-04-17 20:18:08 +02005048 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005049 if (old_o) {
5050 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005051 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02005052 txn->req.next += old_o;
Christopher Fauletd7c91962015-04-30 11:48:27 +02005053 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005054 }
5055
Willy Tarreau9b28e032012-10-12 23:49:43 +02005056 old_i = chn->buf->i;
5057 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 -05005058 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005059 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005060 }
5061
5062 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005063 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05005064 memcpy(hdr_val, hdr_name, hdr_name_len);
5065 hdr_val += hdr_name_len;
5066 *hdr_val++ = ':';
5067 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005068 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
5069 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05005070
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005071 if (old_o) {
5072 /* If this was a forwarded request, we must readjust the amount of
5073 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02005074 * variations. Note that the current state is >= HTTP_MSG_BODY,
5075 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005076 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02005077 old_o += chn->buf->i - old_i;
5078 b_adv(chn->buf, old_o);
5079 txn->req.next -= old_o;
5080 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02005081 }
5082
Mark Lamourinec2247f02012-01-04 13:02:01 -05005083 return 0;
5084}
5085
Willy Tarreau610ecce2010-01-04 21:15:02 +01005086/* Terminate current transaction and prepare a new one. This is very tricky
5087 * right now but it works.
5088 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005089void http_end_txn_clean_session(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005090{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005091 int prev_status = s->txn->status;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005092 struct proxy *fe = strm_fe(s);
Willy Tarreau858b1032015-12-07 17:04:59 +01005093 struct proxy *be = s->be;
Willy Tarreau323a2d92015-08-04 19:00:17 +02005094 struct connection *srv_conn;
5095 struct server *srv;
Willy Tarreau449d74a2015-08-05 17:16:33 +02005096 unsigned int prev_flags = s->txn->flags;
Willy Tarreau068621e2013-12-23 15:11:25 +01005097
Willy Tarreau610ecce2010-01-04 21:15:02 +01005098 /* FIXME: We need a more portable way of releasing a backend's and a
5099 * server's connections. We need a safer way to reinitialize buffer
5100 * flags. We also need a more accurate method for computing per-request
5101 * data.
5102 */
Willy Tarreau323a2d92015-08-04 19:00:17 +02005103 srv_conn = objt_conn(s->si[1].end);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005104
Willy Tarreau4213a112013-12-15 10:25:42 +01005105 /* unless we're doing keep-alive, we want to quickly close the connection
5106 * to the server.
5107 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005108 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005109 !si_conn_ready(&s->si[1])) {
5110 s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
5111 si_shutr(&s->si[1]);
5112 si_shutw(&s->si[1]);
Willy Tarreau4213a112013-12-15 10:25:42 +01005113 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005114
Willy Tarreaue7dff022015-04-03 01:14:29 +02005115 if (s->flags & SF_BE_ASSIGNED) {
Willy Tarreau858b1032015-12-07 17:04:59 +01005116 be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01005117 if (unlikely(s->srv_conn))
5118 sess_change_server(s, NULL);
5119 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005120
5121 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02005122 stream_process_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005123
Willy Tarreaueee5b512015-04-03 23:46:31 +02005124 if (s->txn->status) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005125 int n;
5126
Willy Tarreaueee5b512015-04-03 23:46:31 +02005127 n = s->txn->status / 100;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005128 if (n < 1 || n > 5)
5129 n = 0;
5130
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005131 if (fe->mode == PR_MODE_HTTP) {
5132 fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005133 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02005134 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau858b1032015-12-07 17:04:59 +01005135 (be->mode == PR_MODE_HTTP)) {
5136 be->be_counters.p.http.rsp[n]++;
5137 be->be_counters.p.http.cum_req++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005138 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005139 }
5140
5141 /* don't count other requests' data */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005142 s->logs.bytes_in -= s->req.buf->i;
5143 s->logs.bytes_out -= s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005144
5145 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005146 if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02005147 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005148 (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005149 s->do_log(s);
5150 }
5151
Willy Tarreaud713bcc2014-06-25 15:36:04 +02005152 /* stop tracking content-based counters */
Willy Tarreau87b09662015-04-03 00:22:06 +02005153 stream_stop_content_counters(s);
5154 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02005155
Willy Tarreau610ecce2010-01-04 21:15:02 +01005156 s->logs.accept_date = date; /* user-visible date for logging */
5157 s->logs.tv_accept = now; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +02005158 s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */
5159 s->logs.t_idle = -1;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005160 tv_zero(&s->logs.tv_request);
5161 s->logs.t_queue = -1;
5162 s->logs.t_connect = -1;
5163 s->logs.t_data = -1;
5164 s->logs.t_close = 0;
5165 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
5166 s->logs.srv_queue_size = 0; /* we will get this number soon */
5167
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005168 s->logs.bytes_in = s->req.total = s->req.buf->i;
5169 s->logs.bytes_out = s->res.total = s->res.buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005170
5171 if (s->pend_pos)
5172 pendconn_free(s->pend_pos);
5173
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005174 if (objt_server(s->target)) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005175 if (s->flags & SF_CURR_SESS) {
5176 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005177 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005178 }
Willy Tarreau858b1032015-12-07 17:04:59 +01005179 if (may_dequeue_tasks(objt_server(s->target), be))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005180 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01005181 }
5182
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005183 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005184
Willy Tarreau4213a112013-12-15 10:25:42 +01005185 /* only release our endpoint if we don't intend to reuse the
5186 * connection.
5187 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005188 if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
Willy Tarreau350f4872014-11-28 14:42:25 +01005189 !si_conn_ready(&s->si[1])) {
5190 si_release_endpoint(&s->si[1]);
Willy Tarreau323a2d92015-08-04 19:00:17 +02005191 srv_conn = NULL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005192 }
5193
Willy Tarreau350f4872014-11-28 14:42:25 +01005194 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
5195 s->si[1].err_type = SI_ET_NONE;
5196 s->si[1].conn_retries = 0; /* used for logging too */
5197 s->si[1].exp = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02005198 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 +01005199 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);
5200 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 +02005201 s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST);
5202 s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED);
5203 s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01005204
Willy Tarreaueee5b512015-04-03 23:46:31 +02005205 s->txn->meth = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005206 http_reset_txn(s);
Willy Tarreaueee5b512015-04-03 23:46:31 +02005207 s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01005208
5209 if (prev_status == 401 || prev_status == 407) {
5210 /* In HTTP keep-alive mode, if we receive a 401, we still have
5211 * a chance of being able to send the visitor again to the same
5212 * server over the same connection. This is required by some
5213 * broken protocols such as NTLM, and anyway whenever there is
5214 * an opportunity for sending the challenge to the proper place,
5215 * it's better to do it (at least it helps with debugging).
5216 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02005217 s->txn->flags |= TX_PREFER_LAST;
Willy Tarreaubd99d582015-09-02 10:40:43 +02005218 if (srv_conn)
5219 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreau068621e2013-12-23 15:11:25 +01005220 }
5221
Willy Tarreau53f96852016-02-02 18:50:47 +01005222 /* Never ever allow to reuse a connection from a non-reuse backend */
5223 if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
5224 srv_conn->flags |= CO_FL_PRIVATE;
5225
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005226 if (fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau350f4872014-11-28 14:42:25 +01005227 s->si[1].flags |= SI_FL_INDEP_STR;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005228
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005229 if (fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005230 s->req.flags |= CF_NEVER_WAIT;
5231 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02005232 }
5233
Willy Tarreau610ecce2010-01-04 21:15:02 +01005234 /* if the request buffer is not empty, it means we're
5235 * about to process another request, so send pending
5236 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01005237 * Just don't do this if the buffer is close to be full,
5238 * because the request will wait for it to flush a little
5239 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005240 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005241 if (s->req.buf->i) {
5242 if (s->res.buf->o &&
5243 !buffer_full(s->res.buf, global.tune.maxrewrite) &&
5244 bi_end(s->res.buf) <= s->res.buf->data + s->res.buf->size - global.tune.maxrewrite)
5245 s->res.flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01005246 }
Willy Tarreau90deb182010-01-07 00:20:41 +01005247
Willy Tarreau714ea782015-11-25 20:11:11 +01005248 /* we're removing the analysers, we MUST re-enable events detection.
5249 * We don't enable close on the response channel since it's either
5250 * already closed, or in keep-alive with an idle connection handler.
5251 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005252 channel_auto_read(&s->req);
5253 channel_auto_close(&s->req);
5254 channel_auto_read(&s->res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005255
Willy Tarreau1c59bd52015-11-02 20:20:11 +01005256 /* we're in keep-alive with an idle connection, monitor it if not already done */
5257 if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) {
Willy Tarreau323a2d92015-08-04 19:00:17 +02005258 srv = objt_server(srv_conn->target);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005259 if (!srv)
5260 si_idle_conn(&s->si[1], NULL);
Willy Tarreau53f96852016-02-02 18:50:47 +01005261 else if (srv_conn->flags & CO_FL_PRIVATE)
Willy Tarreau8dff9982015-08-04 20:45:52 +02005262 si_idle_conn(&s->si[1], &srv->priv_conns);
Willy Tarreau449d74a2015-08-05 17:16:33 +02005263 else if (prev_flags & TX_NOT_FIRST)
5264 /* note: we check the request, not the connection, but
5265 * this is valid for strategies SAFE and AGGR, and in
5266 * case of ALWS, we don't care anyway.
5267 */
5268 si_idle_conn(&s->si[1], &srv->safe_conns);
Willy Tarreau8dff9982015-08-04 20:45:52 +02005269 else
5270 si_idle_conn(&s->si[1], &srv->idle_conns);
Willy Tarreau4320eaa2015-08-05 11:08:30 +02005271 }
Willy Tarreau27375622013-12-17 00:00:28 +01005272
Christopher Faulet3e344292015-11-24 16:24:13 +01005273 if (HAS_FILTERS(s)) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02005274 s->req.analysers &= AN_FLT_END;
5275 s->res.analysers &= AN_FLT_END;
5276 }
Christopher Faulet3e344292015-11-24 16:24:13 +01005277 else {
5278 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
5279 s->res.analysers = 0;
5280 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005281}
5282
5283
5284/* This function updates the request state machine according to the response
5285 * state machine and buffer flags. It returns 1 if it changes anything (flag
5286 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5287 * it is only used to find when a request/response couple is complete. Both
5288 * this function and its equivalent should loop until both return zero. It
5289 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5290 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005291int http_sync_req_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005292{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005293 struct channel *chn = &s->req;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005294 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005295 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005296 unsigned int old_state = txn->req.msg_state;
5297
Willy Tarreau610ecce2010-01-04 21:15:02 +01005298 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
5299 return 0;
5300
5301 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005302 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005303 * We can shut the read side unless we want to abort_on_close,
5304 * or we have a POST request. The issue with POST requests is
5305 * that some browsers still send a CRLF after the request, and
5306 * this CRLF must be read so that it does not remain in the kernel
5307 * buffers, otherwise a close could cause an RST on some systems
5308 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005309 * Note that if we're using keep-alive on the client side, we'd
5310 * rather poll now and keep the polling enabled for the whole
Willy Tarreau87b09662015-04-03 00:22:06 +02005311 * stream's life than enabling/disabling it between each
Willy Tarreau3988d932013-12-27 23:03:08 +01005312 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005313 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005314 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5315 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5316 !(s->be->options & PR_O_ABRT_CLOSE) &&
5317 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005318 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005319
Willy Tarreau40f151a2012-12-20 12:10:09 +01005320 /* if the server closes the connection, we want to immediately react
5321 * and close the socket to save packets and syscalls.
5322 */
Willy Tarreau350f4872014-11-28 14:42:25 +01005323 s->si[1].flags |= SI_FL_NOHALF;
Willy Tarreau40f151a2012-12-20 12:10:09 +01005324
Willy Tarreau7f876a12015-11-18 11:59:55 +01005325 /* In any case we've finished parsing the request so we must
5326 * disable Nagle when sending data because 1) we're not going
5327 * to shut this side, and 2) the server is waiting for us to
5328 * send pending data.
5329 */
5330 chn->flags |= CF_NEVER_WAIT;
5331
Willy Tarreau610ecce2010-01-04 21:15:02 +01005332 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5333 goto wait_other_side;
5334
5335 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5336 /* The server has not finished to respond, so we
5337 * don't want to move in order not to upset it.
5338 */
5339 goto wait_other_side;
5340 }
5341
5342 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5343 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005344 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005345 txn->req.msg_state = HTTP_MSG_TUNNEL;
5346 goto wait_other_side;
5347 }
5348
5349 /* When we get here, it means that both the request and the
5350 * response have finished receiving. Depending on the connection
5351 * mode, we'll have to wait for the last bytes to leave in either
5352 * direction, and sometimes for a close to be effective.
5353 */
5354
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005355 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5356 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005357 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5358 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005359 }
5360 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5361 /* Option forceclose is set, or either side wants to close,
5362 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005363 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005364 * once both states are CLOSED.
5365 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005366 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5367 channel_shutr_now(chn);
5368 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005369 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005370 }
5371 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005372 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5373 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005374 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005375 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5376 channel_auto_read(chn);
5377 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005378 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005379 }
5380
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005381 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005382 /* if we've just closed an output, let's switch */
Willy Tarreau350f4872014-11-28 14:42:25 +01005383 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005384
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005385 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005386 txn->req.msg_state = HTTP_MSG_CLOSING;
5387 goto http_msg_closing;
5388 }
5389 else {
5390 txn->req.msg_state = HTTP_MSG_CLOSED;
5391 goto http_msg_closed;
5392 }
5393 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005394 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005395 }
5396
5397 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5398 http_msg_closing:
5399 /* nothing else to forward, just waiting for the output buffer
5400 * to be empty and for the shutw_now to take effect.
5401 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005402 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005403 txn->req.msg_state = HTTP_MSG_CLOSED;
5404 goto http_msg_closed;
5405 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005406 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005407 txn->req.msg_state = HTTP_MSG_ERROR;
5408 goto wait_other_side;
5409 }
5410 }
5411
5412 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5413 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005414 /* see above in MSG_DONE why we only do this in these states */
5415 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5416 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5417 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005418 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005419 goto wait_other_side;
5420 }
5421
5422 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005423 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005424}
5425
5426
5427/* This function updates the response state machine according to the request
5428 * state machine and buffer flags. It returns 1 if it changes anything (flag
5429 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5430 * it is only used to find when a request/response couple is complete. Both
5431 * this function and its equivalent should loop until both return zero. It
5432 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5433 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005434int http_sync_res_state(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005435{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005436 struct channel *chn = &s->res;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005437 struct http_txn *txn = s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005438 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005439 unsigned int old_state = txn->rsp.msg_state;
5440
Willy Tarreau610ecce2010-01-04 21:15:02 +01005441 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5442 return 0;
5443
5444 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5445 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005446 * still monitor the server connection for a possible close
5447 * while the request is being uploaded, so we don't disable
5448 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005449 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005450 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005451
5452 if (txn->req.msg_state == HTTP_MSG_ERROR)
5453 goto wait_other_side;
5454
5455 if (txn->req.msg_state < HTTP_MSG_DONE) {
5456 /* The client seems to still be sending data, probably
5457 * because we got an error response during an upload.
5458 * We have the choice of either breaking the connection
5459 * or letting it pass through. Let's do the later.
5460 */
5461 goto wait_other_side;
5462 }
5463
5464 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5465 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005466 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005467 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005468 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005469 goto wait_other_side;
5470 }
5471
5472 /* When we get here, it means that both the request and the
5473 * response have finished receiving. Depending on the connection
5474 * mode, we'll have to wait for the last bytes to leave in either
5475 * direction, and sometimes for a close to be effective.
5476 */
5477
5478 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5479 /* Server-close mode : shut read and wait for the request
5480 * side to close its output buffer. The caller will detect
5481 * when we're in DONE and the other is in CLOSED and will
5482 * catch that for the final cleanup.
5483 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005484 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5485 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005486 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005487 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5488 /* Option forceclose is set, or either side wants to close,
5489 * let's enforce it now that we're not expecting any new
Willy Tarreau87b09662015-04-03 00:22:06 +02005490 * data to come. The caller knows the stream is complete
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005491 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005492 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005493 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5494 channel_shutr_now(chn);
5495 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005496 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005497 }
5498 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005499 /* The last possible modes are keep-alive and tunnel. Tunnel will
5500 * need to forward remaining data. Keep-alive will need to monitor
5501 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005502 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005503 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005504 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005505 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5506 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005507 }
5508
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005509 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005510 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005511 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005512 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5513 goto http_msg_closing;
5514 }
5515 else {
5516 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5517 goto http_msg_closed;
5518 }
5519 }
5520 goto wait_other_side;
5521 }
5522
5523 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5524 http_msg_closing:
5525 /* nothing else to forward, just waiting for the output buffer
5526 * to be empty and for the shutw_now to take effect.
5527 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005528 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005529 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5530 goto http_msg_closed;
5531 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005532 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005533 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005534 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005535 if (objt_server(s->target))
5536 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005537 goto wait_other_side;
5538 }
5539 }
5540
5541 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5542 http_msg_closed:
5543 /* drop any pending data */
Willy Tarreau319f7452015-01-14 20:32:59 +01005544 channel_truncate(chn);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005545 channel_auto_close(chn);
5546 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005547 goto wait_other_side;
5548 }
5549
5550 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005551 /* We force the response to leave immediately if we're waiting for the
5552 * other side, since there is no pending shutdown to push it out.
5553 */
5554 if (!channel_is_empty(chn))
5555 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005556 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005557}
5558
5559
5560/* Resync the request and response state machines. Return 1 if either state
5561 * changes.
5562 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005563int http_resync_states(struct stream *s)
Willy Tarreau610ecce2010-01-04 21:15:02 +01005564{
Willy Tarreaueee5b512015-04-03 23:46:31 +02005565 struct http_txn *txn = s->txn;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005566 int old_req_state = txn->req.msg_state;
5567 int old_res_state = txn->rsp.msg_state;
5568
Willy Tarreau610ecce2010-01-04 21:15:02 +01005569 http_sync_req_state(s);
5570 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005571 if (!http_sync_res_state(s))
5572 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005573 if (!http_sync_req_state(s))
5574 break;
5575 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005576
Willy Tarreau610ecce2010-01-04 21:15:02 +01005577 /* OK, both state machines agree on a compatible state.
5578 * There are a few cases we're interested in :
5579 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5580 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5581 * directions, so let's simply disable both analysers.
5582 * - HTTP_MSG_CLOSED on the response only means we must abort the
5583 * request.
5584 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5585 * with server-close mode means we've completed one request and we
5586 * must re-initialize the server connection.
5587 */
5588
5589 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5590 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5591 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5592 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02005593 s->req.analysers &= AN_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005594 channel_auto_close(&s->req);
5595 channel_auto_read(&s->req);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005596 s->res.analysers &= AN_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005597 channel_auto_close(&s->res);
5598 channel_auto_read(&s->res);
Christopher Faulet6962f4e2016-11-28 14:21:27 +01005599 if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s))
5600 s->req.analysers |= AN_FLT_XFER_DATA;
5601 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s))
5602 s->res.analysers |= AN_FLT_XFER_DATA;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005603 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005604 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005605 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->res.flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005606 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005607 txn->req.msg_state == HTTP_MSG_ERROR) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02005608 s->res.analysers &= AN_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005609 channel_auto_close(&s->res);
5610 channel_auto_read(&s->res);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005611 s->req.analysers &= AN_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005612 channel_abort(&s->req);
5613 channel_auto_close(&s->req);
5614 channel_auto_read(&s->req);
5615 channel_truncate(&s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005616 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005617 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5618 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005619 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005620 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5621 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5622 /* server-close/keep-alive: terminate this transaction,
5623 * possibly killing the server connection and reinitialize
Willy Tarreau3de5bd62016-05-02 16:07:07 +02005624 * a fresh-new transaction, but only once we're sure there's
5625 * enough room in the request and response buffer to process
5626 * another request. The request buffer must not hold any
5627 * pending output data and the request buffer must not have
5628 * output data occupying the reserve.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005629 */
Willy Tarreau3de5bd62016-05-02 16:07:07 +02005630 if (s->req.buf->o)
5631 s->req.flags |= CF_WAKE_WRITE;
5632 else if (channel_congested(&s->res))
5633 s->res.flags |= CF_WAKE_WRITE;
5634 else
5635 http_end_txn_clean_session(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005636 }
5637
Willy Tarreau610ecce2010-01-04 21:15:02 +01005638 return txn->req.msg_state != old_req_state ||
5639 txn->rsp.msg_state != old_res_state;
5640}
5641
Willy Tarreaud98cf932009-12-27 22:54:55 +01005642/* This function is an analyser which forwards request body (including chunk
5643 * sizes if any). It is called as soon as we must forward, even if we forward
5644 * zero byte. The only situation where it must not be called is when we're in
5645 * tunnel mode and we want to forward till the close. It's used both to forward
5646 * remaining data and to resync after end of body. It expects the msg_state to
5647 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02005648 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreau124d9912011-03-01 20:30:48 +01005649 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005650 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005651 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005652int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005653{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005654 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005655 struct http_txn *txn = s->txn;
5656 struct http_msg *msg = &s->txn->req;
Christopher Faulet3e344292015-11-24 16:24:13 +01005657 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005658
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005659 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5660 return 0;
5661
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005662 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005663 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005664 /* Output closed while we were sending data. We must abort and
5665 * wake the other side up.
5666 */
5667 msg->msg_state = HTTP_MSG_ERROR;
5668 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005669 return 1;
5670 }
5671
Willy Tarreaud98cf932009-12-27 22:54:55 +01005672 /* Note that we don't have to send 100-continue back because we don't
5673 * need the data to complete our job, and it's up to the server to
5674 * decide whether to return 100, 417 or anything else in return of
5675 * an "Expect: 100-continue" header.
5676 */
Christopher Fauletd7c91962015-04-30 11:48:27 +02005677 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005678 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
5679 ? HTTP_MSG_CHUNK_SIZE
5680 : HTTP_MSG_DATA);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005681
5682 /* TODO/filters: when http-buffer-request option is set or if a
5683 * rule on url_param exists, the first chunk size could be
5684 * already parsed. In that case, msg->next is after the chunk
5685 * size (including the CRLF after the size). So this case should
5686 * be handled to */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005687 }
5688
Willy Tarreau7ba23542014-04-17 21:50:00 +02005689 /* Some post-connect processing might want us to refrain from starting to
5690 * forward data. Currently, the only reason for this is "balance url_param"
5691 * whichs need to parse/process the request after we've enabled forwarding.
5692 */
5693 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005694 if (!(s->res.flags & CF_READ_ATTACHED)) {
Willy Tarreau7ba23542014-04-17 21:50:00 +02005695 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005696 req->flags |= CF_WAKE_CONNECT;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005697 goto missing_data_or_waiting;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005698 }
5699 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5700 }
5701
Willy Tarreau80a92c02014-03-12 10:41:13 +01005702 /* in most states, we should abort in case of early close */
5703 channel_auto_close(req);
5704
Willy Tarreauefdf0942014-04-24 20:08:57 +02005705 if (req->to_forward) {
5706 /* We can't process the buffer's contents yet */
5707 req->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005708 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02005709 }
5710
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005711 if (msg->msg_state < HTTP_MSG_DONE) {
5712 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
5713 ? http_msg_forward_chunked_body(s, msg)
5714 : http_msg_forward_body(s, msg));
5715 if (!ret)
5716 goto missing_data_or_waiting;
5717 if (ret < 0)
5718 goto return_bad_req;
5719 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005720
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005721 /* other states, DONE...TUNNEL */
5722 /* we don't want to forward closes on DONE except in tunnel mode. */
5723 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5724 channel_dont_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005725
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005726 ret = msg->msg_state;
5727 if (http_resync_states(s)) {
5728 /* some state changes occurred, maybe the analyser
5729 * was disabled too. */
5730 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5731 if (req->flags & CF_SHUTW) {
5732 /* request errors are most likely due to the
5733 * server aborting the transfer. */
5734 goto aborted_xfer;
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005735 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005736 if (msg->err_pos >= 0)
5737 http_capture_bad_message(&sess->fe->invalid_req, s, msg, ret, s->be);
5738 goto return_bad_req;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005739 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005740 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005741 }
5742
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005743 /* If "option abortonclose" is set on the backend, we want to monitor
5744 * the client's connection and forward any shutdown notification to the
5745 * server, which will decide whether to close or to go on processing the
5746 * request. We only do that in tunnel mode, and not in other modes since
5747 * it can be abused to exhaust source ports. */
5748 if (s->be->options & PR_O_ABRT_CLOSE) {
5749 channel_auto_read(req);
5750 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
5751 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
5752 s->si[1].flags |= SI_FL_NOLINGER;
5753 channel_auto_close(req);
5754 }
5755 else if (s->txn->meth == HTTP_METH_POST) {
5756 /* POST requests may require to read extra CRLF sent by broken
5757 * browsers and which could cause an RST to be sent upon close
5758 * on some systems (eg: Linux). */
5759 channel_auto_read(req);
5760 }
5761 return 0;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005762
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01005763 missing_data_or_waiting:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005764 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005765 if (req->flags & CF_SHUTR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02005766 if (!(s->flags & SF_ERR_MASK))
5767 s->flags |= SF_ERR_CLICL;
5768 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005769 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005770 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005771 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005772 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005773 }
5774
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005775 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005776 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005777 if (objt_server(s->target))
5778 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005779
5780 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005781 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005782
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005783 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005784 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005785 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005786
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005787 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005788 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005789 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005790 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005791 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005792
Willy Tarreau5c620922011-05-11 19:56:11 +02005793 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005794 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005795 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005796 * modes are already handled by the stream sock layer. We must not do
5797 * this in content-length mode because it could present the MSG_MORE
5798 * flag with the last block of forwarded data, which would cause an
5799 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005800 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005801 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005802 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005803
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005804 return 0;
5805
Willy Tarreaud98cf932009-12-27 22:54:55 +01005806 return_bad_req: /* let's centralize all bad requests */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005807 sess->fe->fe_counters.failed_req++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02005808 if (sess->listener->counters)
5809 sess->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005810
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005811 return_bad_req_stats_ok:
5812 txn->req.msg_state = HTTP_MSG_ERROR;
5813 if (txn->status) {
5814 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005815 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005816 } else {
5817 txn->status = 400;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005818 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005819 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005820 req->analysers &= AN_FLT_END;
5821 s->res.analysers &= AN_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005822
Willy Tarreaue7dff022015-04-03 01:14:29 +02005823 if (!(s->flags & SF_ERR_MASK))
5824 s->flags |= SF_ERR_PRXCOND;
5825 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005826 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005827 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005828 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005829 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005830 }
5831 return 0;
5832
5833 aborted_xfer:
5834 txn->req.msg_state = HTTP_MSG_ERROR;
5835 if (txn->status) {
5836 /* Note: we don't send any error if some data were already sent */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005837 http_reply_and_close(s, txn->status, NULL);
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005838 } else {
5839 txn->status = 502;
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005840 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005841 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02005842 req->analysers &= AN_FLT_END;
5843 s->res.analysers &= AN_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005844
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005845 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005846 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005847 if (objt_server(s->target))
5848 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005849
Willy Tarreaue7dff022015-04-03 01:14:29 +02005850 if (!(s->flags & SF_ERR_MASK))
5851 s->flags |= SF_ERR_SRVCL;
5852 if (!(s->flags & SF_FINST_MASK)) {
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005853 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005854 s->flags |= SF_FINST_H;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005855 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02005856 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005857 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005858 return 0;
5859}
5860
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005861/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5862 * processing can continue on next analysers, or zero if it either needs more
5863 * data or wants to immediately abort the response (eg: timeout, error, ...). It
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005864 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005865 * when it has nothing left to do, and may remove any analyser when it wants to
5866 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005867 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005868int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005869{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005870 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02005871 struct http_txn *txn = s->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005872 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005873 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005874 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005875 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005876 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005877
Willy Tarreau87b09662015-04-03 00:22:06 +02005878 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 +02005879 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005880 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005881 rep,
5882 rep->rex, rep->wex,
5883 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005884 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005885 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005886
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005887 /*
5888 * Now parse the partial (or complete) lines.
5889 * We will check the response syntax, and also join multi-line
5890 * headers. An index of all the lines will be elaborated while
5891 * parsing.
5892 *
5893 * For the parsing, we use a 28 states FSM.
5894 *
5895 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005896 * rep->buf->p = beginning of response
5897 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5898 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005899 * msg->eol = end of current header or line (LF or CRLF)
5900 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005901 */
5902
Willy Tarreau628c40c2014-04-24 19:11:26 +02005903 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005904 /* There's a protected area at the end of the buffer for rewriting
5905 * purposes. We don't want to start to parse the request if the
5906 * protected area is affected, because we may have to move processed
5907 * data later, which is much more complicated.
5908 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005909 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreauba0902e2015-01-13 14:39:16 +01005910 if (unlikely(!channel_is_rewritable(rep))) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005911 /* some data has still not left the buffer, wake us once that's done */
5912 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5913 goto abort_response;
5914 channel_dont_close(rep);
5915 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005916 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005917 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005918 }
5919
Willy Tarreau379357a2013-06-08 12:55:46 +02005920 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5921 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5922 buffer_slow_realign(rep->buf);
5923
Willy Tarreau9b28e032012-10-12 23:49:43 +02005924 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005925 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005926 }
5927
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005928 /* 1: we might have to print this header in debug mode */
5929 if (unlikely((global.mode & MODE_DEBUG) &&
5930 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005931 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005932 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005933
Willy Tarreau9b28e032012-10-12 23:49:43 +02005934 sol = rep->buf->p;
5935 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005936 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005937
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005938 sol += hdr_idx_first_pos(&txn->hdr_idx);
5939 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005940
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005941 while (cur_idx) {
5942 eol = sol + txn->hdr_idx.v[cur_idx].len;
5943 debug_hdr("srvhdr", s, sol, eol);
5944 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5945 cur_idx = txn->hdr_idx.v[cur_idx].next;
5946 }
5947 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005948
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005949 /*
5950 * Now we quickly check if we have found a full valid response.
5951 * If not so, we check the FD and buffer states before leaving.
5952 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005953 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005954 * responses are checked first.
5955 *
5956 * Depending on whether the client is still there or not, we
5957 * may send an error response back or not. Note that normally
5958 * we should only check for HTTP status there, and check I/O
5959 * errors somewhere else.
5960 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005961
Willy Tarreau655dce92009-11-08 13:10:58 +01005962 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005963 /* Invalid response */
5964 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5965 /* we detected a parsing error. We want to archive this response
5966 * in the dedicated proxy area for later troubleshooting.
5967 */
5968 hdr_response_bad:
5969 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02005970 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005971
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005972 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005973 if (objt_server(s->target)) {
5974 objt_server(s->target)->counters.failed_resp++;
5975 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005976 }
Willy Tarreau64648412010-03-05 10:41:54 +01005977 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005978 channel_auto_close(rep);
Christopher Fauletd7c91962015-04-30 11:48:27 +02005979 rep->analysers &= AN_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005980 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01005981 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01005982 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01005983 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005984
Willy Tarreaue7dff022015-04-03 01:14:29 +02005985 if (!(s->flags & SF_ERR_MASK))
5986 s->flags |= SF_ERR_PRXCOND;
5987 if (!(s->flags & SF_FINST_MASK))
5988 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005989
5990 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005991 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005992
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005993 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005994 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005995 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005996 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005997 goto hdr_response_bad;
5998 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005999
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006000 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006001 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006002 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006003 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006004 else if (txn->flags & TX_NOT_FIRST)
6005 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02006006
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006007 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006008 if (objt_server(s->target)) {
6009 objt_server(s->target)->counters.failed_resp++;
6010 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006011 }
Willy Tarreau461f6622008-08-15 23:43:19 +02006012
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006013 channel_auto_close(rep);
Christopher Fauletd7c91962015-04-30 11:48:27 +02006014 rep->analysers &= AN_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006015 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006016 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006017 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006018 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02006019
Willy Tarreaue7dff022015-04-03 01:14:29 +02006020 if (!(s->flags & SF_ERR_MASK))
6021 s->flags |= SF_ERR_SRVCL;
6022 if (!(s->flags & SF_FINST_MASK))
6023 s->flags |= SF_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02006024 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006025 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006026
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02006027 /* read timeout : return a 504 to the client. */
6028 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006029 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006030 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006031
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006032 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006033 if (objt_server(s->target)) {
6034 objt_server(s->target)->counters.failed_resp++;
6035 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006036 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006037
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006038 channel_auto_close(rep);
Christopher Fauletd7c91962015-04-30 11:48:27 +02006039 rep->analysers &= AN_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006040 txn->status = 504;
Willy Tarreau350f4872014-11-28 14:42:25 +01006041 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006042 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006043 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02006044
Willy Tarreaue7dff022015-04-03 01:14:29 +02006045 if (!(s->flags & SF_ERR_MASK))
6046 s->flags |= SF_ERR_SRVTO;
6047 if (!(s->flags & SF_FINST_MASK))
6048 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006049 return 0;
6050 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02006051
Willy Tarreauf003d372012-11-26 13:35:37 +01006052 /* client abort with an abortonclose */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006053 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006054 sess->fe->fe_counters.cli_aborts++;
Willy Tarreauf003d372012-11-26 13:35:37 +01006055 s->be->be_counters.cli_aborts++;
6056 if (objt_server(s->target))
6057 objt_server(s->target)->counters.cli_aborts++;
6058
Christopher Fauletd7c91962015-04-30 11:48:27 +02006059 rep->analysers &= AN_FLT_END;
Willy Tarreauf003d372012-11-26 13:35:37 +01006060 channel_auto_close(rep);
6061
6062 txn->status = 400;
Willy Tarreau319f7452015-01-14 20:32:59 +01006063 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006064 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_400));
Willy Tarreauf003d372012-11-26 13:35:37 +01006065
Willy Tarreaue7dff022015-04-03 01:14:29 +02006066 if (!(s->flags & SF_ERR_MASK))
6067 s->flags |= SF_ERR_CLICL;
6068 if (!(s->flags & SF_FINST_MASK))
6069 s->flags |= SF_FINST_H;
Willy Tarreauf003d372012-11-26 13:35:37 +01006070
Willy Tarreau87b09662015-04-03 00:22:06 +02006071 /* process_stream() will take care of the error */
Willy Tarreauf003d372012-11-26 13:35:37 +01006072 return 0;
6073 }
6074
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006075 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006076 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02006077 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006078 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006079 else if (txn->flags & TX_NOT_FIRST)
6080 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01006081
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006082 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006083 if (objt_server(s->target)) {
6084 objt_server(s->target)->counters.failed_resp++;
6085 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006086 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006087
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006088 channel_auto_close(rep);
Christopher Fauletd7c91962015-04-30 11:48:27 +02006089 rep->analysers &= AN_FLT_END;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006090 txn->status = 502;
Willy Tarreau350f4872014-11-28 14:42:25 +01006091 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006092 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006093 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01006094
Willy Tarreaue7dff022015-04-03 01:14:29 +02006095 if (!(s->flags & SF_ERR_MASK))
6096 s->flags |= SF_ERR_SRVCL;
6097 if (!(s->flags & SF_FINST_MASK))
6098 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006099 return 0;
6100 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006101
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006102 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006103 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006104 if (msg->err_pos >= 0)
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006105 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01006106 else if (txn->flags & TX_NOT_FIRST)
6107 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02006108
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006109 s->be->be_counters.failed_resp++;
Christopher Fauletd7c91962015-04-30 11:48:27 +02006110 rep->analysers &= AN_FLT_END;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006111 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006112
Willy Tarreaue7dff022015-04-03 01:14:29 +02006113 if (!(s->flags & SF_ERR_MASK))
6114 s->flags |= SF_ERR_CLICL;
6115 if (!(s->flags & SF_FINST_MASK))
6116 s->flags |= SF_FINST_H;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006117
Willy Tarreau87b09662015-04-03 00:22:06 +02006118 /* process_stream() will take care of the error */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006119 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006120 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01006121
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006122 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01006123 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006124 return 0;
6125 }
6126
6127 /* More interesting part now : we know that we have a complete
6128 * response which at least looks like HTTP. We have an indicator
6129 * of each header's length, so we can parse them quickly.
6130 */
6131
6132 if (unlikely(msg->err_pos >= 0))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006133 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, sess->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006134
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006135 /*
6136 * 1: get the status code
6137 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006138 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006139 if (n < 1 || n > 5)
6140 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02006141 /* when the client triggers a 4xx from the server, it's most often due
6142 * to a missing object or permission. These events should be tracked
6143 * because if they happen often, it may indicate a brute force or a
6144 * vulnerability scan.
6145 */
6146 if (n == 4)
Willy Tarreau87b09662015-04-03 00:22:06 +02006147 stream_inc_http_err_ctr(s);
Willy Tarreauda7ff642010-06-23 11:44:09 +02006148
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006149 if (objt_server(s->target))
6150 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006151
Willy Tarreau91852eb2015-05-01 13:26:00 +02006152 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
6153 * exactly one digit "." one digit. This check may be disabled using
6154 * option accept-invalid-http-response.
6155 */
6156 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
6157 if (msg->sl.st.v_l != 8) {
6158 msg->err_pos = 0;
6159 goto hdr_response_bad;
6160 }
6161
6162 if (rep->buf->p[4] != '/' ||
6163 !isdigit((unsigned char)rep->buf->p[5]) ||
6164 rep->buf->p[6] != '.' ||
6165 !isdigit((unsigned char)rep->buf->p[7])) {
6166 msg->err_pos = 4;
6167 goto hdr_response_bad;
6168 }
6169 }
6170
Willy Tarreau5b154472009-12-21 20:11:07 +01006171 /* check if the response is HTTP/1.1 or above */
6172 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006173 ((rep->buf->p[5] > '1') ||
6174 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006175 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01006176
6177 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006178 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 +01006179
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006180 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006181 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006182
Willy Tarreau9b28e032012-10-12 23:49:43 +02006183 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006184
Willy Tarreau39650402010-03-15 19:44:39 +01006185 /* Adjust server's health based on status code. Note: status codes 501
6186 * and 505 are triggered on demand by client request, so we must not
6187 * count them as server failures.
6188 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006189 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006190 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006191 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006192 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006193 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02006194 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01006195
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006196 /*
6197 * 2: check for cacheability.
6198 */
6199
6200 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02006201 case 100:
6202 /*
6203 * We may be facing a 100-continue response, in which case this
6204 * is not the right response, and we're waiting for the next one.
6205 * Let's allow this response to go to the client and wait for the
6206 * next one.
6207 */
6208 hdr_idx_init(&txn->hdr_idx);
6209 msg->next -= channel_forward(rep, msg->next);
6210 msg->msg_state = HTTP_MSG_RPBEFORE;
6211 txn->status = 0;
6212 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet3e344292015-11-24 16:24:13 +01006213 FLT_STRM_CB(s, flt_http_reset(s, msg));
Willy Tarreau628c40c2014-04-24 19:11:26 +02006214 goto next_one;
6215
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006216 case 200:
6217 case 203:
6218 case 206:
6219 case 300:
6220 case 301:
6221 case 410:
6222 /* RFC2616 @13.4:
6223 * "A response received with a status code of
6224 * 200, 203, 206, 300, 301 or 410 MAY be stored
6225 * by a cache (...) unless a cache-control
6226 * directive prohibits caching."
6227 *
6228 * RFC2616 @9.5: POST method :
6229 * "Responses to this method are not cacheable,
6230 * unless the response includes appropriate
6231 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006232 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006233 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006234 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006235 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6236 break;
6237 default:
6238 break;
6239 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006240
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006241 /*
6242 * 3: we may need to capture headers
6243 */
6244 s->logs.logwait &= ~LW_RESP;
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006245 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006246 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaucb7dd012015-04-03 22:16:32 +02006247 s->res_cap, sess->fe->rsp_cap);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006248
Willy Tarreau557f1992015-05-01 10:05:17 +02006249 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6250 * by RFC7230#3.3.3 :
6251 *
6252 * The length of a message body is determined by one of the following
6253 * (in order of precedence):
6254 *
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006255 * 1. Any 2xx (Successful) response to a CONNECT request implies that
6256 * the connection will become a tunnel immediately after the empty
6257 * line that concludes the header fields. A client MUST ignore
6258 * any Content-Length or Transfer-Encoding header fields received
6259 * in such a message. Any 101 response (Switching Protocols) is
6260 * managed in the same manner.
6261 *
6262 * 2. Any response to a HEAD request and any response with a 1xx
Willy Tarreau557f1992015-05-01 10:05:17 +02006263 * (Informational), 204 (No Content), or 304 (Not Modified) status
6264 * code is always terminated by the first empty line after the
6265 * header fields, regardless of the header fields present in the
6266 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006267 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006268 * 3. If a Transfer-Encoding header field is present and the chunked
6269 * transfer coding (Section 4.1) is the final encoding, the message
6270 * body length is determined by reading and decoding the chunked
6271 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006272 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006273 * If a Transfer-Encoding header field is present in a response and
6274 * the chunked transfer coding is not the final encoding, the
6275 * message body length is determined by reading the connection until
6276 * it is closed by the server. If a Transfer-Encoding header field
6277 * is present in a request and the chunked transfer coding is not
6278 * the final encoding, the message body length cannot be determined
6279 * reliably; the server MUST respond with the 400 (Bad Request)
6280 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006281 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006282 * If a message is received with both a Transfer-Encoding and a
6283 * Content-Length header field, the Transfer-Encoding overrides the
6284 * Content-Length. Such a message might indicate an attempt to
6285 * perform request smuggling (Section 9.5) or response splitting
6286 * (Section 9.4) and ought to be handled as an error. A sender MUST
6287 * remove the received Content-Length field prior to forwarding such
6288 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006289 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006290 * 4. If a message is received without Transfer-Encoding and with
6291 * either multiple Content-Length header fields having differing
6292 * field-values or a single Content-Length header field having an
6293 * invalid value, then the message framing is invalid and the
6294 * recipient MUST treat it as an unrecoverable error. If this is a
6295 * request message, the server MUST respond with a 400 (Bad Request)
6296 * status code and then close the connection. If this is a response
6297 * message received by a proxy, the proxy MUST close the connection
6298 * to the server, discard the received response, and send a 502 (Bad
6299 * Gateway) response to the client. If this is a response message
6300 * received by a user agent, the user agent MUST close the
6301 * connection to the server and discard the received response.
6302 *
6303 * 5. If a valid Content-Length header field is present without
6304 * Transfer-Encoding, its decimal value defines the expected message
6305 * body length in octets. If the sender closes the connection or
6306 * the recipient times out before the indicated number of octets are
6307 * received, the recipient MUST consider the message to be
6308 * incomplete and close the connection.
6309 *
6310 * 6. If this is a request message and none of the above are true, then
6311 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006312 *
Willy Tarreau557f1992015-05-01 10:05:17 +02006313 * 7. Otherwise, this is a response message without a declared message
6314 * body length, so the message body length is determined by the
6315 * number of octets received prior to the server closing the
6316 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006317 */
6318
6319 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006320 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006321 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006322 * FIXME: should we parse anyway and return an error on chunked encoding ?
6323 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006324 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6325 txn->status == 101)) {
6326 /* Either we've established an explicit tunnel, or we're
6327 * switching the protocol. In both cases, we're very unlikely
6328 * to understand the next protocols. We have to switch to tunnel
6329 * mode, so that we transfer the request and responses then let
6330 * this protocol pass unmodified. When we later implement specific
6331 * parsers for such protocols, we'll want to check the Upgrade
6332 * header which contains information about that protocol for
6333 * responses with status 101 (eg: see RFC2817 about TLS).
6334 */
6335 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6336 msg->flags |= HTTP_MSGF_XFER_LEN;
6337 goto end;
6338 }
6339
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006340 if (txn->meth == HTTP_METH_HEAD ||
6341 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006342 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006343 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006344 goto skip_content_length;
6345 }
6346
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006347 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006348 ctx.idx = 0;
Willy Tarreau4979d5c2015-05-01 10:06:30 +02006349 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006350 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006351 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6352 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006353 /* bad transfer-encoding (chunked followed by something else) */
6354 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006355 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006356 break;
6357 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006358 }
6359
Willy Tarreau1c913912015-04-30 10:57:51 +02006360 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006361 ctx.idx = 0;
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006362 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreau1c913912015-04-30 10:57:51 +02006363 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6364 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6365 }
Willy Tarreaub4d0c032015-05-01 10:25:45 +02006366 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006367 signed long long cl;
6368
Willy Tarreauad14f752011-09-02 20:33:27 +02006369 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006370 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006371 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006372 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006373
Willy Tarreauad14f752011-09-02 20:33:27 +02006374 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006375 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006376 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006377 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006378
Willy Tarreauad14f752011-09-02 20:33:27 +02006379 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006380 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006381 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006382 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006383
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006384 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006385 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006386 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006387 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006388
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006389 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006390 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006391 }
6392
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006393 skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006394 /* Now we have to check if we need to modify the Connection header.
6395 * This is more difficult on the response than it is on the request,
6396 * because we can have two different HTTP versions and we don't know
6397 * how the client will interprete a response. For instance, let's say
6398 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6399 * HTTP/1.1 response without any header. Maybe it will bound itself to
6400 * HTTP/1.0 because it only knows about it, and will consider the lack
6401 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6402 * the lack of header as a keep-alive. Thus we will use two flags
6403 * indicating how a request MAY be understood by the client. In case
6404 * of multiple possibilities, we'll fix the header to be explicit. If
6405 * ambiguous cases such as both close and keepalive are seen, then we
6406 * will fall back to explicit close. Note that we won't take risks with
6407 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006408 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006409 */
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006410 if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6411 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
6412 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6413 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006414 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006415
Willy Tarreau70dffda2014-01-30 03:07:23 +01006416 /* this situation happens when combining pretend-keepalive with httpclose. */
6417 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006418 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006419 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006420 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6421
Willy Tarreau60466522010-01-18 19:08:45 +01006422 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006423 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006424 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6425 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006426
Willy Tarreau60466522010-01-18 19:08:45 +01006427 /* now adjust header transformations depending on current state */
6428 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6429 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6430 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006431 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006432 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006433 }
Willy Tarreau60466522010-01-18 19:08:45 +01006434 else { /* SCL / KAL */
6435 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006436 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006437 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006438 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006439
Willy Tarreau60466522010-01-18 19:08:45 +01006440 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006441 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006442
Willy Tarreau60466522010-01-18 19:08:45 +01006443 /* Some keep-alive responses are converted to Server-close if
6444 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006445 */
Willy Tarreau60466522010-01-18 19:08:45 +01006446 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6447 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006448 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006449 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006450 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006451 }
6452
Christopher Fauletd1cd2092016-11-28 10:14:03 +01006453 end:
Willy Tarreau7959a552013-09-23 16:44:27 +02006454 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006455 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006456
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006457 /* end of job, return OK */
6458 rep->analysers &= ~an_bit;
6459 rep->analyse_exp = TICK_ETERNITY;
6460 channel_auto_close(rep);
6461 return 1;
6462
6463 abort_keep_alive:
6464 /* A keep-alive request to the server failed on a network error.
6465 * The client is required to retry. We need to close without returning
6466 * any other information so that the client retries.
6467 */
6468 txn->status = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +02006469 rep->analysers &= AN_FLT_END;
6470 s->req.analysers &= AN_FLT_END;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006471 channel_auto_close(rep);
6472 s->logs.logwait = 0;
6473 s->logs.level = 0;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006474 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau319f7452015-01-14 20:32:59 +01006475 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006476 http_reply_and_close(s, txn->status, NULL);
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006477 return 0;
6478}
6479
6480/* This function performs all the processing enabled for the current response.
6481 * It normally returns 1 unless it wants to break. It relies on buffers flags,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006482 * and updates s->res.analysers. It might make sense to explode it into several
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006483 * other functions. It works like process_request (see indications above).
6484 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006485int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006486{
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006487 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006488 struct http_txn *txn = s->txn;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006489 struct http_msg *msg = &txn->rsp;
6490 struct proxy *cur_proxy;
6491 struct cond_wordlist *wl;
Thierry FOURNIER9e2ef992015-02-25 13:51:19 +01006492 enum rule_result ret = HTTP_RULE_RES_CONT;
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006493
Willy Tarreau87b09662015-04-03 00:22:06 +02006494 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 +02006495 now_ms, __FUNCTION__,
6496 s,
6497 rep,
6498 rep->rex, rep->wex,
6499 rep->flags,
6500 rep->buf->i,
6501 rep->analysers);
6502
6503 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6504 return 0;
6505
Willy Tarreau70730dd2014-04-24 18:06:27 +02006506 /* The stats applet needs to adjust the Connection header but we don't
6507 * apply any filter there.
6508 */
Willy Tarreau612adb82015-03-10 15:25:54 +01006509 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
6510 rep->analysers &= ~an_bit;
6511 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02006512 goto skip_filters;
Willy Tarreau612adb82015-03-10 15:25:54 +01006513 }
Willy Tarreau70730dd2014-04-24 18:06:27 +02006514
Willy Tarreau58975672014-04-24 21:13:57 +02006515 /*
6516 * We will have to evaluate the filters.
6517 * As opposed to version 1.2, now they will be evaluated in the
6518 * filters order and not in the header order. This means that
6519 * each filter has to be validated among all headers.
6520 *
6521 * Filters are tried with ->be first, then with ->fe if it is
6522 * different from ->be.
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006523 *
6524 * Maybe we are in resume condiion. In this case I choose the
6525 * "struct proxy" which contains the rule list matching the resume
6526 * pointer. If none of theses "struct proxy" match, I initialise
6527 * the process with the first one.
6528 *
6529 * In fact, I check only correspondance betwwen the current list
6530 * pointer and the ->fe rule list. If it doesn't match, I initialize
6531 * the loop with the ->be.
Willy Tarreau58975672014-04-24 21:13:57 +02006532 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006533 if (s->current_rule_list == &sess->fe->http_res_rules)
6534 cur_proxy = sess->fe;
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006535 else
6536 cur_proxy = s->be;
Willy Tarreau58975672014-04-24 21:13:57 +02006537 while (1) {
6538 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006539
Willy Tarreau58975672014-04-24 21:13:57 +02006540 /* evaluate http-response rules */
Willy Tarreau51d861a2015-05-22 17:30:48 +02006541 if (ret == HTTP_RULE_RES_CONT) {
Willy Tarreau987e3fb2015-04-04 01:09:08 +02006542 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02006543
Willy Tarreau51d861a2015-05-22 17:30:48 +02006544 if (ret == HTTP_RULE_RES_BADREQ)
6545 goto return_srv_prx_502;
6546
6547 if (ret == HTTP_RULE_RES_DONE) {
6548 rep->analysers &= ~an_bit;
6549 rep->analyse_exp = TICK_ETERNITY;
6550 return 1;
6551 }
6552 }
6553
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006554 /* we need to be called again. */
6555 if (ret == HTTP_RULE_RES_YIELD) {
6556 channel_dont_close(rep);
6557 return 0;
6558 }
6559
Willy Tarreau58975672014-04-24 21:13:57 +02006560 /* try headers filters */
6561 if (rule_set->rsp_exp != NULL) {
6562 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6563 return_bad_resp:
6564 if (objt_server(s->target)) {
6565 objt_server(s->target)->counters.failed_resp++;
6566 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006567 }
Willy Tarreau58975672014-04-24 21:13:57 +02006568 s->be->be_counters.failed_resp++;
6569 return_srv_prx_502:
Christopher Fauletd7c91962015-04-30 11:48:27 +02006570 rep->analysers &= AN_FLT_END;
Willy Tarreau58975672014-04-24 21:13:57 +02006571 txn->status = 502;
6572 s->logs.t_data = -1; /* was not a valid response */
Willy Tarreau350f4872014-11-28 14:42:25 +01006573 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau319f7452015-01-14 20:32:59 +01006574 channel_truncate(rep);
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006575 http_reply_and_close(s, txn->status, http_error_message(s, HTTP_ERR_502));
Willy Tarreaue7dff022015-04-03 01:14:29 +02006576 if (!(s->flags & SF_ERR_MASK))
6577 s->flags |= SF_ERR_PRXCOND;
6578 if (!(s->flags & SF_FINST_MASK))
6579 s->flags |= SF_FINST_H;
Willy Tarreau58975672014-04-24 21:13:57 +02006580 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006581 }
Willy Tarreau58975672014-04-24 21:13:57 +02006582 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006583
Willy Tarreau58975672014-04-24 21:13:57 +02006584 /* has the response been denied ? */
6585 if (txn->flags & TX_SVDENY) {
6586 if (objt_server(s->target))
6587 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006588
Willy Tarreau58975672014-04-24 21:13:57 +02006589 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006590 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006591 if (sess->listener->counters)
6592 sess->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006593
Willy Tarreau58975672014-04-24 21:13:57 +02006594 goto return_srv_prx_502;
6595 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006596
Willy Tarreau58975672014-04-24 21:13:57 +02006597 /* add response headers from the rule sets in the same order */
6598 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006599 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006600 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006601 if (wl->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02006602 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreau58975672014-04-24 21:13:57 +02006603 ret = acl_pass(ret);
6604 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6605 ret = !ret;
6606 if (!ret)
6607 continue;
6608 }
6609 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6610 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006611 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006612
Willy Tarreau58975672014-04-24 21:13:57 +02006613 /* check whether we're already working on the frontend */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006614 if (cur_proxy == sess->fe)
Willy Tarreau58975672014-04-24 21:13:57 +02006615 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006616 cur_proxy = sess->fe;
Willy Tarreau58975672014-04-24 21:13:57 +02006617 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006618
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01006619 /* After this point, this anayzer can't return yield, so we can
6620 * remove the bit corresponding to this analyzer from the list.
6621 *
6622 * Note that the intermediate returns and goto found previously
6623 * reset the analyzers.
6624 */
6625 rep->analysers &= ~an_bit;
6626 rep->analyse_exp = TICK_ETERNITY;
6627
Willy Tarreau58975672014-04-24 21:13:57 +02006628 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006629 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006630 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006631
Willy Tarreau58975672014-04-24 21:13:57 +02006632 /*
6633 * Now check for a server cookie.
6634 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02006635 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Willy Tarreau58975672014-04-24 21:13:57 +02006636 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006637
Willy Tarreau58975672014-04-24 21:13:57 +02006638 /*
6639 * Check for cache-control or pragma headers if required.
6640 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006641 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 +02006642 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006643
Willy Tarreau58975672014-04-24 21:13:57 +02006644 /*
6645 * Add server cookie in the response if needed
6646 */
6647 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6648 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006649 (!(s->flags & SF_DIRECT) ||
Willy Tarreau58975672014-04-24 21:13:57 +02006650 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6651 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6652 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6653 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6654 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02006655 !(s->flags & SF_IGNORE_PRST)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006656 /* the server is known, it's not the one the client requested, or the
6657 * cookie's last seen date needs to be refreshed. We have to
6658 * insert a set-cookie here, except if we want to insert only on POST
6659 * requests and this one isn't. Note that servers which don't have cookies
6660 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006661 */
Willy Tarreau58975672014-04-24 21:13:57 +02006662 if (!objt_server(s->target)->cookie) {
6663 chunk_printf(&trash,
6664 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6665 s->be->cookie_name);
6666 }
6667 else {
6668 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006669
Willy Tarreau58975672014-04-24 21:13:57 +02006670 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6671 /* emit last_date, which is mandatory */
6672 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6673 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6674 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006675
Willy Tarreau58975672014-04-24 21:13:57 +02006676 if (s->be->cookie_maxlife) {
6677 /* emit first_date, which is either the original one or
6678 * the current date.
6679 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006680 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006681 s30tob64(txn->cookie_first_date ?
6682 txn->cookie_first_date >> 2 :
6683 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006684 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006685 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006686 }
Willy Tarreau58975672014-04-24 21:13:57 +02006687 chunk_appendf(&trash, "; path=/");
6688 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006689
Willy Tarreau58975672014-04-24 21:13:57 +02006690 if (s->be->cookie_domain)
6691 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006692
Willy Tarreau58975672014-04-24 21:13:57 +02006693 if (s->be->ck_opts & PR_CK_HTTPONLY)
6694 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006695
Willy Tarreau58975672014-04-24 21:13:57 +02006696 if (s->be->ck_opts & PR_CK_SECURE)
6697 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006698
Willy Tarreau58975672014-04-24 21:13:57 +02006699 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6700 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006701
Willy Tarreau58975672014-04-24 21:13:57 +02006702 txn->flags &= ~TX_SCK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02006703 if (objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
Willy Tarreau58975672014-04-24 21:13:57 +02006704 /* the server did not change, only the date was updated */
6705 txn->flags |= TX_SCK_UPDATED;
6706 else
6707 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006708
Willy Tarreau58975672014-04-24 21:13:57 +02006709 /* Here, we will tell an eventual cache on the client side that we don't
6710 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6711 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6712 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006713 */
Willy Tarreau58975672014-04-24 21:13:57 +02006714 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006715
Willy Tarreau58975672014-04-24 21:13:57 +02006716 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006717
Willy Tarreau58975672014-04-24 21:13:57 +02006718 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6719 "Cache-control: private", 22) < 0))
6720 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006721 }
Willy Tarreau58975672014-04-24 21:13:57 +02006722 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006723
Willy Tarreau58975672014-04-24 21:13:57 +02006724 /*
6725 * Check if result will be cacheable with a cookie.
6726 * We'll block the response if security checks have caught
6727 * nasty things such as a cacheable cookie.
6728 */
6729 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6730 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6731 (s->be->options & PR_O_CHK_CACHE)) {
6732 /* we're in presence of a cacheable response containing
6733 * a set-cookie header. We'll block it as requested by
6734 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006735 */
Willy Tarreau58975672014-04-24 21:13:57 +02006736 if (objt_server(s->target))
6737 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006738
Willy Tarreau58975672014-04-24 21:13:57 +02006739 s->be->be_counters.denied_resp++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006740 sess->fe->fe_counters.denied_resp++;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02006741 if (sess->listener->counters)
6742 sess->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006743
Willy Tarreau58975672014-04-24 21:13:57 +02006744 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6745 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6746 send_log(s->be, LOG_ALERT,
6747 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6748 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6749 goto return_srv_prx_502;
6750 }
Willy Tarreau03945942009-12-22 16:50:27 +01006751
Willy Tarreau70730dd2014-04-24 18:06:27 +02006752 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006753 /*
6754 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6755 * If an "Upgrade" token is found, the header is left untouched in order
6756 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006757 * if anything but "Upgrade" is present in the Connection header. We don't
6758 * want to touch any 101 response either since it's switching to another
6759 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006760 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006761 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006762 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006763 ((sess->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
Willy Tarreau58975672014-04-24 21:13:57 +02006764 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6765 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006766
Willy Tarreau58975672014-04-24 21:13:57 +02006767 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6768 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6769 /* we want a keep-alive response here. Keep-alive header
6770 * required if either side is not 1.1.
6771 */
6772 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6773 want_flags |= TX_CON_KAL_SET;
6774 }
6775 else {
6776 /* we want a close response here. Close header required if
6777 * the server is 1.1, regardless of the client.
6778 */
6779 if (msg->flags & HTTP_MSGF_VER_11)
6780 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006781 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006782
Willy Tarreau58975672014-04-24 21:13:57 +02006783 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6784 http_change_connection_header(txn, msg, want_flags);
6785 }
6786
6787 skip_header_mangling:
Christopher Faulet3e344292015-11-24 16:24:13 +01006788 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_FILTERS(s) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006789 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
6790 rep->analysers &= ~AN_FLT_XFER_DATA;
Willy Tarreau58975672014-04-24 21:13:57 +02006791 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Christopher Fauletd7c91962015-04-30 11:48:27 +02006792 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006793
Willy Tarreau58975672014-04-24 21:13:57 +02006794 /* if the user wants to log as soon as possible, without counting
6795 * bytes from the server, then this is the right moment. We have
6796 * to temporarily assign bytes_out to log what we currently have.
6797 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006798 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau58975672014-04-24 21:13:57 +02006799 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6800 s->logs.bytes_out = txn->rsp.eoh;
6801 s->do_log(s);
6802 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006803 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006804 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006805}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006806
Willy Tarreaud98cf932009-12-27 22:54:55 +01006807/* This function is an analyser which forwards response body (including chunk
6808 * sizes if any). It is called as soon as we must forward, even if we forward
6809 * zero byte. The only situation where it must not be called is when we're in
6810 * tunnel mode and we want to forward till the close. It's used both to forward
6811 * remaining data and to resync after end of body. It expects the msg_state to
6812 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
Willy Tarreau87b09662015-04-03 00:22:06 +02006813 * read more data, or 1 once we can go on with next request or end the stream.
Willy Tarreaud3510212014-04-21 11:24:13 +02006814 *
6815 * It is capable of compressing response data both in content-length mode and
6816 * in chunked mode. The state machines follows different flows depending on
6817 * whether content-length and chunked modes are used, since there are no
6818 * trailers in content-length :
6819 *
6820 * chk-mode cl-mode
6821 * ,----- BODY -----.
6822 * / \
6823 * V size > 0 V chk-mode
6824 * .--> SIZE -------------> DATA -------------> CRLF
6825 * | | size == 0 | last byte |
6826 * | v final crlf v inspected |
6827 * | TRAILERS -----------> DONE |
6828 * | |
6829 * `----------------------------------------------'
6830 *
6831 * Compression only happens in the DATA state, and must be flushed in final
6832 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6833 * is performed at once on final states for all bytes parsed, or when leaving
6834 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006835 */
Willy Tarreau87b09662015-04-03 00:22:06 +02006836int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006837{
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006838 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02006839 struct http_txn *txn = s->txn;
6840 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet3e344292015-11-24 16:24:13 +01006841 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006842
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006843 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6844 return 0;
6845
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006846 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006847 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Christopher Fauletd7c91962015-04-30 11:48:27 +02006848 !s->req.analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006849 /* Output closed while we were sending data. We must abort and
6850 * wake the other side up.
6851 */
6852 msg->msg_state = HTTP_MSG_ERROR;
6853 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006854 return 1;
6855 }
6856
Willy Tarreau4fe41902010-06-07 22:27:41 +02006857 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006858 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006859
Christopher Fauletd7c91962015-04-30 11:48:27 +02006860 if (msg->msg_state == HTTP_MSG_BODY) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006861 msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK)
6862 ? HTTP_MSG_CHUNK_SIZE
6863 : HTTP_MSG_DATA);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006864 }
6865
Willy Tarreauefdf0942014-04-24 20:08:57 +02006866 if (res->to_forward) {
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006867 /* We can't process the buffer's contents yet */
Willy Tarreauefdf0942014-04-24 20:08:57 +02006868 res->flags |= CF_WAKE_WRITE;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006869 goto missing_data_or_waiting;
Willy Tarreauefdf0942014-04-24 20:08:57 +02006870 }
6871
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006872 if (msg->msg_state < HTTP_MSG_DONE) {
6873 ret = ((msg->flags & HTTP_MSGF_TE_CHNK)
6874 ? http_msg_forward_chunked_body(s, msg)
6875 : http_msg_forward_body(s, msg));
6876 if (!ret)
6877 goto missing_data_or_waiting;
6878 if (ret < 0)
6879 goto return_bad_res;
6880 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02006881
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006882 /* other states, DONE...TUNNEL */
6883 /* for keep-alive we don't want to forward closes on DONE */
6884 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6885 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
6886 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006887
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006888 ret = msg->msg_state;
6889 if (http_resync_states(s)) {
6890 /* some state changes occurred, maybe the analyser was disabled
6891 * too. */
6892 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
6893 if (res->flags & CF_SHUTW) {
6894 /* response errors are most likely due to the
6895 * client aborting the transfer. */
6896 goto aborted_xfer;
Willy Tarreau5523b322009-12-29 12:05:52 +01006897 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006898 if (msg->err_pos >= 0)
6899 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, strm_fe(s));
6900 goto return_bad_res;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006901 }
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006902 return 1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006903 }
Willy Tarreauf51d03c2016-05-02 15:25:15 +02006904 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006905
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006906 missing_data_or_waiting:
Willy Tarreauf003d372012-11-26 13:35:37 +01006907 if (res->flags & CF_SHUTW)
6908 goto aborted_xfer;
6909
6910 /* stop waiting for data if the input is closed before the end. If the
6911 * client side was already closed, it means that the client has aborted,
6912 * so we don't want to count this as a server abort. Otherwise it's a
6913 * server abort.
6914 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006915 if (res->flags & CF_SHUTR) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006916 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006917 goto aborted_xfer;
Christopher Fauleta46bbd82015-06-19 09:00:58 +02006918 /* If we have some pending data, we continue the processing */
6919 if (!buffer_pending(res->buf)) {
6920 if (!(s->flags & SF_ERR_MASK))
6921 s->flags |= SF_ERR_SRVCL;
6922 s->be->be_counters.srv_aborts++;
6923 if (objt_server(s->target))
6924 objt_server(s->target)->counters.srv_aborts++;
6925 goto return_bad_res_stats_ok;
6926 }
Willy Tarreau40dba092010-03-04 18:14:51 +01006927 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006928
Willy Tarreau40dba092010-03-04 18:14:51 +01006929 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006930 if (!s->req.analysers)
Willy Tarreau610ecce2010-01-04 21:15:02 +01006931 goto return_bad_res;
6932
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006933 /* When TE: chunked is used, we need to get there again to parse
6934 * remaining chunks even if the server has closed, so we don't want to
6935 * set CF_DONTCLOSE. Similarly, if the body length is undefined, if
6936 * keep-alive is set on the client side or if there are filters
6937 * registered on the stream, we don't want to forward a close
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006938 */
Christopher Faulet92d36382015-11-05 13:35:03 +01006939 if ((msg->flags & HTTP_MSGF_TE_CHNK) || !msg->body_len ||
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006940 HAS_FILTERS(s) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006941 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6942 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006943 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006944
Willy Tarreau5c620922011-05-11 19:56:11 +02006945 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006946 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006947 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006948 * modes are already handled by the stream sock layer. We must not do
6949 * this in content-length mode because it could present the MSG_MORE
6950 * flag with the last block of forwarded data, which would cause an
6951 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006952 */
Christopher Faulet92d36382015-11-05 13:35:03 +01006953 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006954 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006955
Willy Tarreau87b09662015-04-03 00:22:06 +02006956 /* the stream handler will take care of timeouts and errors */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006957 return 0;
6958
Willy Tarreau40dba092010-03-04 18:14:51 +01006959 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006960 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006961 if (objt_server(s->target))
6962 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006963
6964 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01006965 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006966 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006967 http_reply_and_close(s, txn->status, NULL);
Christopher Fauletd7c91962015-04-30 11:48:27 +02006968 res->analysers &= AN_FLT_END;
6969 s->req.analysers &= AN_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006970 if (objt_server(s->target))
6971 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006972
Willy Tarreaue7dff022015-04-03 01:14:29 +02006973 if (!(s->flags & SF_ERR_MASK))
6974 s->flags |= SF_ERR_PRXCOND;
6975 if (!(s->flags & SF_FINST_MASK))
6976 s->flags |= SF_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006977 return 0;
6978
6979 aborted_xfer:
6980 txn->rsp.msg_state = HTTP_MSG_ERROR;
6981 /* don't send any error message as we're in the body */
Christopher Fauleta94e5a52015-12-09 15:55:06 +01006982 http_reply_and_close(s, txn->status, NULL);
Christopher Fauletd7c91962015-04-30 11:48:27 +02006983 res->analysers &= AN_FLT_END;
6984 s->req.analysers &= AN_FLT_END; /* we're in data phase, we want to abort both directions */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006985
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02006986 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006987 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006988 if (objt_server(s->target))
6989 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006990
Willy Tarreaue7dff022015-04-03 01:14:29 +02006991 if (!(s->flags & SF_ERR_MASK))
6992 s->flags |= SF_ERR_CLICL;
6993 if (!(s->flags & SF_FINST_MASK))
6994 s->flags |= SF_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006995 return 0;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01006996}
6997
6998
6999static inline int
7000http_msg_forward_body(struct stream *s, struct http_msg *msg)
7001{
7002 struct channel *chn = msg->chn;
7003 int ret;
7004
7005 /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */
7006
7007 if (msg->msg_state == HTTP_MSG_ENDING)
7008 goto ending;
7009
7010 /* Neither content-length, nor transfer-encoding was found, so we must
7011 * read the body until the server connection is closed. In that case, we
7012 * eat data as they come. Of course, this happens for response only. */
7013 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
7014 unsigned long long len = (chn->buf->i - msg->next);
7015 msg->chunk_len += len;
7016 msg->body_len += len;
7017 }
Christopher Fauletda02e172015-12-04 09:25:05 +01007018 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
7019 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
7020 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007021 msg->next += ret;
7022 msg->chunk_len -= ret;
7023 if (msg->chunk_len) {
7024 /* input empty or output full */
7025 if (chn->buf->i > msg->next)
7026 chn->flags |= CF_WAKE_WRITE;
7027 goto missing_data_or_waiting;
7028 }
7029
Christopher Faulet32359572016-11-29 14:49:54 +01007030 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && !(chn->flags & CF_SHUTR) &&
7031 HAS_DATA_FILTERS(s, chn)) {
7032 /* The server still sending data that should be filtered */
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007033 goto missing_data_or_waiting;
7034 }
7035 msg->msg_state = HTTP_MSG_ENDING;
7036
7037 ending:
7038 /* we may have some pending data starting at res->buf->p such as a last
7039 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01007040 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
7041 /* default_ret */ msg->next,
7042 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007043 b_adv(chn->buf, ret);
7044 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007045 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
7046 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02007047 if (msg->next)
7048 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007049
Christopher Fauletda02e172015-12-04 09:25:05 +01007050 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
7051 /* default_ret */ 1,
7052 /* on_error */ goto error,
7053 /* on_wait */ goto waiting);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007054 msg->msg_state = HTTP_MSG_DONE;
7055 return 1;
7056
7057 missing_data_or_waiting:
7058 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01007059 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
7060 /* default_ret */ msg->next,
7061 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007062 b_adv(chn->buf, ret);
7063 msg->next -= ret;
7064 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
7065 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01007066 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007067 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02007068 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007069 return 0;
7070 error:
7071 return -1;
7072}
7073
7074static inline int
7075http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg)
7076{
7077 struct channel *chn = msg->chn;
7078 int ret;
7079
7080 /* Here we have the guarantee to be in one of the following state:
7081 * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF,
7082 * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */
7083
7084 switch_states:
7085 switch (msg->msg_state) {
7086 case HTTP_MSG_DATA:
Christopher Fauletda02e172015-12-04 09:25:05 +01007087 ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg),
7088 /* default_ret */ MIN(msg->chunk_len, chn->buf->i - msg->next),
7089 /* on_error */ goto error);
7090 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007091 msg->chunk_len -= ret;
7092 if (msg->chunk_len) {
7093 /* input empty or output full */
7094 if (chn->buf->i > msg->next)
7095 chn->flags |= CF_WAKE_WRITE;
7096 goto missing_data_or_waiting;
7097 }
7098
7099 /* nothing left to forward for this chunk*/
7100 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
7101 /* fall through for HTTP_MSG_CHUNK_CRLF */
7102
7103 case HTTP_MSG_CHUNK_CRLF:
7104 /* we want the CRLF after the data */
7105 ret = http_skip_chunk_crlf(msg);
7106 if (ret == 0)
7107 goto missing_data_or_waiting;
7108 if (ret < 0)
7109 goto chunk_parsing_error;
Christopher Faulet113f7de2015-12-14 14:52:13 +01007110 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007111 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
7112 /* fall through for HTTP_MSG_CHUNK_SIZE */
7113
7114 case HTTP_MSG_CHUNK_SIZE:
7115 /* read the chunk size and assign it to ->chunk_len,
7116 * then set ->next to point to the body and switch to
7117 * DATA or TRAILERS state.
7118 */
7119 ret = http_parse_chunk_size(msg);
7120 if (ret == 0)
7121 goto missing_data_or_waiting;
7122 if (ret < 0)
7123 goto chunk_parsing_error;
Christopher Faulet113f7de2015-12-14 14:52:13 +01007124 msg->next += ret;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007125 if (msg->chunk_len) {
7126 msg->msg_state = HTTP_MSG_DATA;
7127 goto switch_states;
7128 }
7129 msg->msg_state = HTTP_MSG_TRAILERS;
7130 /* fall through for HTTP_MSG_TRAILERS */
7131
7132 case HTTP_MSG_TRAILERS:
7133 ret = http_forward_trailers(msg);
7134 if (ret < 0)
7135 goto chunk_parsing_error;
Christopher Fauletda02e172015-12-04 09:25:05 +01007136 FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg),
7137 /* default_ret */ 1,
7138 /* on_error */ goto error);
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007139 msg->next += msg->sol;
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007140 if (!ret)
7141 goto missing_data_or_waiting;
7142 break;
7143
7144 case HTTP_MSG_ENDING:
7145 goto ending;
7146
7147 default:
7148 /* This should no happen in this function */
7149 goto error;
7150 }
7151
7152 msg->msg_state = HTTP_MSG_ENDING;
7153 ending:
7154 /* we may have some pending data starting at res->buf->p such as a last
7155 * chunk of data or trailers. */
Christopher Fauletda02e172015-12-04 09:25:05 +01007156 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007157 /* default_ret */ msg->next,
7158 /* on_error */ goto error);
7159 b_adv(chn->buf, ret);
7160 msg->next -= ret;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007161 if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0))
7162 msg->sov -= ret;
Christopher Fauleta9300a32016-06-28 15:54:44 +02007163 if (msg->next)
7164 goto waiting;
Willy Tarreau9962f8f2016-06-28 11:52:08 +02007165
Christopher Fauletda02e172015-12-04 09:25:05 +01007166 FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007167 /* default_ret */ 1,
7168 /* on_error */ goto error,
7169 /* on_wait */ goto waiting);
7170 msg->msg_state = HTTP_MSG_DONE;
7171 return 1;
7172
7173 missing_data_or_waiting:
7174 /* we may have some pending data starting at chn->buf->p */
Christopher Fauletda02e172015-12-04 09:25:05 +01007175 ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next),
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007176 /* default_ret */ msg->next,
7177 /* on_error */ goto error);
7178 b_adv(chn->buf, ret);
7179 msg->next -= ret;
7180 if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)
7181 msg->sov -= ret;
Christopher Faulet75e2eb62015-12-15 10:41:47 +01007182 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007183 msg->chunk_len -= channel_forward(chn, msg->chunk_len);
Christopher Fauleta9300a32016-06-28 15:54:44 +02007184 waiting:
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007185 return 0;
7186
7187 chunk_parsing_error:
7188 if (msg->err_pos >= 0) {
7189 if (chn->flags & CF_ISRESP)
7190 http_capture_bad_message(&s->be->invalid_rep, s, msg,
7191 msg->msg_state, strm_fe(s));
7192 else
7193 http_capture_bad_message(&strm_fe(s)->invalid_req, s,
7194 msg, msg->msg_state, s->be);
7195 }
7196 error:
7197 return -1;
Willy Tarreaud98cf932009-12-27 22:54:55 +01007198}
7199
Christopher Fauletdbe34eb2015-12-02 10:01:17 +01007200
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007201/* Iterate the same filter through all request headers.
7202 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007203 * Since it can manage the switch to another backend, it updates the per-proxy
7204 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007205 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007206int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007207{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007208 char *cur_ptr, *cur_end, *cur_next;
7209 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007210 struct http_txn *txn = s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007211 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007212 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01007213
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007214 last_hdr = 0;
7215
Willy Tarreau9b28e032012-10-12 23:49:43 +02007216 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007217 old_idx = 0;
7218
7219 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007220 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007221 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007222 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007223 (exp->action == ACT_ALLOW ||
7224 exp->action == ACT_DENY ||
7225 exp->action == ACT_TARPIT))
7226 return 0;
7227
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007228 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007229 if (!cur_idx)
7230 break;
7231
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007232 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007233 cur_ptr = cur_next;
7234 cur_end = cur_ptr + cur_hdr->len;
7235 cur_next = cur_end + cur_hdr->cr + 1;
7236
7237 /* Now we have one header between cur_ptr and cur_end,
7238 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007239 */
7240
Willy Tarreau15a53a42015-01-21 13:39:42 +01007241 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007242 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007243 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007244 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007245 last_hdr = 1;
7246 break;
7247
7248 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007249 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007250 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007251 break;
7252
7253 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007254 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007255 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007256 break;
7257
7258 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007259 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7260 if (trash.len < 0)
7261 return -1;
7262
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007263 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007264 /* FIXME: if the user adds a newline in the replacement, the
7265 * index will not be recalculated for now, and the new line
7266 * will not be counted as a new header.
7267 */
7268
7269 cur_end += delta;
7270 cur_next += delta;
7271 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007272 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007273 break;
7274
7275 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007276 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007277 cur_next += delta;
7278
Willy Tarreaufa355d42009-11-29 18:12:29 +01007279 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007280 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7281 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007282 cur_hdr->len = 0;
7283 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007284 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007285 break;
7286
7287 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007288 }
7289
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007290 /* keep the link from this header to next one in case of later
7291 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007292 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007293 old_idx = cur_idx;
7294 }
7295 return 0;
7296}
7297
7298
7299/* Apply the filter to the request line.
7300 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7301 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007302 * Since it can manage the switch to another backend, it updates the per-proxy
7303 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007304 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007305int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007306{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007307 char *cur_ptr, *cur_end;
7308 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007309 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007310 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007311
Willy Tarreau3d300592007-03-18 18:34:41 +01007312 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007313 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007314 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007315 (exp->action == ACT_ALLOW ||
7316 exp->action == ACT_DENY ||
7317 exp->action == ACT_TARPIT))
7318 return 0;
7319 else if (exp->action == ACT_REMOVE)
7320 return 0;
7321
7322 done = 0;
7323
Willy Tarreau9b28e032012-10-12 23:49:43 +02007324 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007325 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007326
7327 /* Now we have the request line between cur_ptr and cur_end */
7328
Willy Tarreau15a53a42015-01-21 13:39:42 +01007329 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007330 switch (exp->action) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007331 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007332 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007333 done = 1;
7334 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007335
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007336 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007337 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007338 done = 1;
7339 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007340
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007341 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007342 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007343 done = 1;
7344 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007345
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007346 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007347 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7348 if (trash.len < 0)
7349 return -1;
7350
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007351 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007352 /* FIXME: if the user adds a newline in the replacement, the
7353 * index will not be recalculated for now, and the new line
7354 * will not be counted as a new header.
7355 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007356
Willy Tarreaufa355d42009-11-29 18:12:29 +01007357 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007358 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007359 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007360 HTTP_MSG_RQMETH,
7361 cur_ptr, cur_end + 1,
7362 NULL, NULL);
7363 if (unlikely(!cur_end))
7364 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007365
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007366 /* we have a full request and we know that we have either a CR
7367 * or an LF at <ptr>.
7368 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007369 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7370 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007371 /* there is no point trying this regex on headers */
7372 return 1;
7373 }
7374 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007375 return done;
7376}
Willy Tarreau97de6242006-12-27 17:18:38 +01007377
Willy Tarreau58f10d72006-12-04 02:26:12 +01007378
Willy Tarreau58f10d72006-12-04 02:26:12 +01007379
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007380/*
Willy Tarreau87b09662015-04-03 00:22:06 +02007381 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007382 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007383 * unparsable request. Since it can manage the switch to another backend, it
7384 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007385 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007386int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007387{
Willy Tarreau192252e2015-04-04 01:47:55 +02007388 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007389 struct http_txn *txn = s->txn;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007390 struct hdr_exp *exp;
7391
7392 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007393 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007394
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007395 /*
7396 * The interleaving of transformations and verdicts
7397 * makes it difficult to decide to continue or stop
7398 * the evaluation.
7399 */
7400
Willy Tarreau6c123b12010-01-28 20:22:06 +01007401 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7402 break;
7403
Willy Tarreau3d300592007-03-18 18:34:41 +01007404 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007405 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007406 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007407 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007408
7409 /* if this filter had a condition, evaluate it now and skip to
7410 * next filter if the condition does not match.
7411 */
7412 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02007413 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007414 ret = acl_pass(ret);
7415 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7416 ret = !ret;
7417
7418 if (!ret)
7419 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007420 }
7421
7422 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007423 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007424 if (unlikely(ret < 0))
7425 return -1;
7426
7427 if (likely(ret == 0)) {
7428 /* The filter did not match the request, it can be
7429 * iterated through all headers.
7430 */
Willy Tarreau34d4c3c2015-01-30 20:58:58 +01007431 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7432 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007433 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007434 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007435 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007436}
7437
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007438
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007439/* Find the end of a cookie value contained between <s> and <e>. It works the
7440 * same way as with headers above except that the semi-colon also ends a token.
7441 * See RFC2965 for more information. Note that it requires a valid header to
7442 * return a valid result.
7443 */
7444char *find_cookie_value_end(char *s, const char *e)
7445{
7446 int quoted, qdpair;
7447
7448 quoted = qdpair = 0;
7449 for (; s < e; s++) {
7450 if (qdpair) qdpair = 0;
7451 else if (quoted) {
7452 if (*s == '\\') qdpair = 1;
7453 else if (*s == '"') quoted = 0;
7454 }
7455 else if (*s == '"') quoted = 1;
7456 else if (*s == ',' || *s == ';') return s;
7457 }
7458 return s;
7459}
7460
7461/* Delete a value in a header between delimiters <from> and <next> in buffer
7462 * <buf>. The number of characters displaced is returned, and the pointer to
7463 * the first delimiter is updated if required. The function tries as much as
7464 * possible to respect the following principles :
7465 * - replace <from> delimiter by the <next> one unless <from> points to a
7466 * colon, in which case <next> is simply removed
7467 * - set exactly one space character after the new first delimiter, unless
7468 * there are not enough characters in the block being moved to do so.
7469 * - remove unneeded spaces before the previous delimiter and after the new
7470 * one.
7471 *
7472 * It is the caller's responsibility to ensure that :
7473 * - <from> points to a valid delimiter or the colon ;
7474 * - <next> points to a valid delimiter or the final CR/LF ;
7475 * - there are non-space chars before <from> ;
7476 * - there is a CR/LF at or after <next>.
7477 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007478int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007479{
7480 char *prev = *from;
7481
7482 if (*prev == ':') {
7483 /* We're removing the first value, preserve the colon and add a
7484 * space if possible.
7485 */
Willy Tarreau2235b262016-11-05 15:50:20 +01007486 if (!HTTP_IS_CRLF(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007487 next++;
7488 prev++;
7489 if (prev < next)
7490 *prev++ = ' ';
7491
Willy Tarreau2235b262016-11-05 15:50:20 +01007492 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007493 next++;
7494 } else {
7495 /* Remove useless spaces before the old delimiter. */
Willy Tarreau2235b262016-11-05 15:50:20 +01007496 while (HTTP_IS_SPHT(*(prev-1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007497 prev--;
7498 *from = prev;
7499
7500 /* copy the delimiter and if possible a space if we're
7501 * not at the end of the line.
7502 */
Willy Tarreau2235b262016-11-05 15:50:20 +01007503 if (!HTTP_IS_CRLF(*next)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007504 *prev++ = *next++;
7505 if (prev + 1 < next)
7506 *prev++ = ' ';
Willy Tarreau2235b262016-11-05 15:50:20 +01007507 while (HTTP_IS_SPHT(*next))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007508 next++;
7509 }
7510 }
7511 return buffer_replace2(buf, prev, next, NULL, 0);
7512}
7513
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007514/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007515 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007516 * desirable to call it only when needed. This code is quite complex because
7517 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7518 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007519 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007520void manage_client_side_cookies(struct stream *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007521{
Willy Tarreaueee5b512015-04-03 23:46:31 +02007522 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007523 struct session *sess = s->sess;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007524 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007525 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007526 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7527 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007528
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007529 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007530 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007531 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007532
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007533 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007534 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007535 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007536
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007537 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007538 hdr_beg = hdr_next;
7539 hdr_end = hdr_beg + cur_hdr->len;
7540 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007541
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007542 /* We have one full header between hdr_beg and hdr_end, and the
7543 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007544 * "Cookie:" headers.
7545 */
7546
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007547 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007548 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007549 old_idx = cur_idx;
7550 continue;
7551 }
7552
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007553 del_from = NULL; /* nothing to be deleted */
7554 preserve_hdr = 0; /* assume we may kill the whole header */
7555
Willy Tarreau58f10d72006-12-04 02:26:12 +01007556 /* Now look for cookies. Conforming to RFC2109, we have to support
7557 * attributes whose name begin with a '$', and associate them with
7558 * the right cookie, if we want to delete this cookie.
7559 * So there are 3 cases for each cookie read :
7560 * 1) it's a special attribute, beginning with a '$' : ignore it.
7561 * 2) it's a server id cookie that we *MAY* want to delete : save
7562 * some pointers on it (last semi-colon, beginning of cookie...)
7563 * 3) it's an application cookie : we *MAY* have to delete a previous
7564 * "special" cookie.
7565 * At the end of loop, if a "special" cookie remains, we may have to
7566 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007567 * *MUST* delete it.
7568 *
7569 * Note: RFC2965 is unclear about the processing of spaces around
7570 * the equal sign in the ATTR=VALUE form. A careful inspection of
7571 * the RFC explicitly allows spaces before it, and not within the
7572 * tokens (attrs or values). An inspection of RFC2109 allows that
7573 * too but section 10.1.3 lets one think that spaces may be allowed
7574 * after the equal sign too, resulting in some (rare) buggy
7575 * implementations trying to do that. So let's do what servers do.
7576 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7577 * allowed quoted strings in values, with any possible character
7578 * after a backslash, including control chars and delimitors, which
7579 * causes parsing to become ambiguous. Browsers also allow spaces
7580 * within values even without quotes.
7581 *
7582 * We have to keep multiple pointers in order to support cookie
7583 * removal at the beginning, middle or end of header without
7584 * corrupting the header. All of these headers are valid :
7585 *
7586 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7587 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7588 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7589 * | | | | | | | | |
7590 * | | | | | | | | hdr_end <--+
7591 * | | | | | | | +--> next
7592 * | | | | | | +----> val_end
7593 * | | | | | +-----------> val_beg
7594 * | | | | +--------------> equal
7595 * | | | +----------------> att_end
7596 * | | +---------------------> att_beg
7597 * | +--------------------------> prev
7598 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007599 */
7600
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007601 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7602 /* Iterate through all cookies on this line */
7603
7604 /* find att_beg */
7605 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007606 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007607 att_beg++;
7608
7609 /* find att_end : this is the first character after the last non
7610 * space before the equal. It may be equal to hdr_end.
7611 */
7612 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007613
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007614 while (equal < hdr_end) {
7615 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007616 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01007617 if (HTTP_IS_SPHT(*equal++))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007618 continue;
7619 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007620 }
7621
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007622 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7623 * is between <att_beg> and <equal>, both may be identical.
7624 */
7625
7626 /* look for end of cookie if there is an equal sign */
7627 if (equal < hdr_end && *equal == '=') {
7628 /* look for the beginning of the value */
7629 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01007630 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007631 val_beg++;
7632
7633 /* find the end of the value, respecting quotes */
7634 next = find_cookie_value_end(val_beg, hdr_end);
7635
7636 /* make val_end point to the first white space or delimitor after the value */
7637 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01007638 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007639 val_end--;
7640 } else {
7641 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007642 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007643
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007644 /* We have nothing to do with attributes beginning with '$'. However,
7645 * they will automatically be removed if a header before them is removed,
7646 * since they're supposed to be linked together.
7647 */
7648 if (*att_beg == '$')
7649 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007650
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007651 /* Ignore cookies with no equal sign */
7652 if (equal == next) {
7653 /* This is not our cookie, so we must preserve it. But if we already
7654 * scheduled another cookie for removal, we cannot remove the
7655 * complete header, but we can remove the previous block itself.
7656 */
7657 preserve_hdr = 1;
7658 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007659 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007660 val_end += delta;
7661 next += delta;
7662 hdr_end += delta;
7663 hdr_next += delta;
7664 cur_hdr->len += delta;
7665 http_msg_move_end(&txn->req, delta);
7666 prev = del_from;
7667 del_from = NULL;
7668 }
7669 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007670 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007671
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007672 /* if there are spaces around the equal sign, we need to
7673 * strip them otherwise we'll get trouble for cookie captures,
7674 * or even for rewrites. Since this happens extremely rarely,
7675 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007676 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007677 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7678 int stripped_before = 0;
7679 int stripped_after = 0;
7680
7681 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007682 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007683 equal += stripped_before;
7684 val_beg += stripped_before;
7685 }
7686
7687 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007688 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007689 val_beg += stripped_after;
7690 stripped_before += stripped_after;
7691 }
7692
7693 val_end += stripped_before;
7694 next += stripped_before;
7695 hdr_end += stripped_before;
7696 hdr_next += stripped_before;
7697 cur_hdr->len += stripped_before;
7698 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007699 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007700 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007701
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007702 /* First, let's see if we want to capture this cookie. We check
7703 * that we don't already have a client side cookie, because we
7704 * can only capture one. Also as an optimisation, we ignore
7705 * cookies shorter than the declared name.
7706 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007707 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7708 (val_end - att_beg >= sess->fe->capture_namelen) &&
7709 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007710 int log_len = val_end - att_beg;
7711
7712 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7713 Alert("HTTP logging : out of memory.\n");
7714 } else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02007715 if (log_len > sess->fe->capture_len)
7716 log_len = sess->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007717 memcpy(txn->cli_cookie, att_beg, log_len);
7718 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007719 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007720 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007721
Willy Tarreaubca99692010-10-06 19:25:55 +02007722 /* Persistence cookies in passive, rewrite or insert mode have the
7723 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007724 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007725 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007726 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007727 * For cookies in prefix mode, the form is :
7728 *
7729 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007730 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007731 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7732 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7733 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007734 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007735
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007736 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7737 * have the server ID between val_beg and delim, and the original cookie between
7738 * delim+1 and val_end. Otherwise, delim==val_end :
7739 *
7740 * Cookie: NAME=SRV; # in all but prefix modes
7741 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7742 * | || || | |+-> next
7743 * | || || | +--> val_end
7744 * | || || +---------> delim
7745 * | || |+------------> val_beg
7746 * | || +-------------> att_end = equal
7747 * | |+-----------------> att_beg
7748 * | +------------------> prev
7749 * +-------------------------> hdr_beg
7750 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007751
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007752 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007753 for (delim = val_beg; delim < val_end; delim++)
7754 if (*delim == COOKIE_DELIM)
7755 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007756 } else {
7757 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007758 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007759 /* Now check if the cookie contains a date field, which would
7760 * appear after a vertical bar ('|') just after the server name
7761 * and before the delimiter.
7762 */
7763 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7764 if (vbar1) {
7765 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007766 * right is the last seen date. It is a base64 encoded
7767 * 30-bit value representing the UNIX date since the
7768 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007769 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007770 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007771 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007772 if (val_end - vbar1 >= 5) {
7773 val = b64tos30(vbar1);
7774 if (val > 0)
7775 txn->cookie_last_date = val << 2;
7776 }
7777 /* look for a second vertical bar */
7778 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7779 if (vbar1 && (val_end - vbar1 > 5)) {
7780 val = b64tos30(vbar1 + 1);
7781 if (val > 0)
7782 txn->cookie_first_date = val << 2;
7783 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007784 }
7785 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007786
Willy Tarreauf64d1412010-10-07 20:06:11 +02007787 /* if the cookie has an expiration date and the proxy wants to check
7788 * it, then we do that now. We first check if the cookie is too old,
7789 * then only if it has expired. We detect strict overflow because the
7790 * time resolution here is not great (4 seconds). Cookies with dates
7791 * in the future are ignored if their offset is beyond one day. This
7792 * allows an admin to fix timezone issues without expiring everyone
7793 * and at the same time avoids keeping unwanted side effects for too
7794 * long.
7795 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007796 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7797 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007798 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007799 txn->flags &= ~TX_CK_MASK;
7800 txn->flags |= TX_CK_OLD;
7801 delim = val_beg; // let's pretend we have not found the cookie
7802 txn->cookie_first_date = 0;
7803 txn->cookie_last_date = 0;
7804 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007805 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7806 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007807 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007808 txn->flags &= ~TX_CK_MASK;
7809 txn->flags |= TX_CK_EXPIRED;
7810 delim = val_beg; // let's pretend we have not found the cookie
7811 txn->cookie_first_date = 0;
7812 txn->cookie_last_date = 0;
7813 }
7814
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007815 /* Here, we'll look for the first running server which supports the cookie.
7816 * This allows to share a same cookie between several servers, for example
7817 * to dedicate backup servers to specific servers only.
7818 * However, to prevent clients from sticking to cookie-less backup server
7819 * when they have incidentely learned an empty cookie, we simply ignore
7820 * empty cookies and mark them as invalid.
7821 * The same behaviour is applied when persistence must be ignored.
7822 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02007823 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007824 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007825
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007826 while (srv) {
7827 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7828 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007829 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007830 (s->be->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02007831 (s->flags & SF_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007832 /* we found the server and we can use it */
7833 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007834 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007835 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007836 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007837 break;
7838 } else {
7839 /* we found a server, but it's down,
7840 * mark it as such and go on in case
7841 * another one is available.
7842 */
7843 txn->flags &= ~TX_CK_MASK;
7844 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007845 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007846 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007847 srv = srv->next;
7848 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007849
Willy Tarreauf64d1412010-10-07 20:06:11 +02007850 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007851 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007852 txn->flags &= ~TX_CK_MASK;
Willy Tarreaue7dff022015-04-03 01:14:29 +02007853 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007854 txn->flags |= TX_CK_UNUSED;
7855 else
7856 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007857 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007858
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007859 /* depending on the cookie mode, we may have to either :
7860 * - delete the complete cookie if we're in insert+indirect mode, so that
7861 * the server never sees it ;
7862 * - remove the server id from the cookie value, and tag the cookie as an
7863 * application cookie so that it does not get accidentely removed later,
7864 * if we're in cookie prefix mode
7865 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007866 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007867 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007868
Willy Tarreau9b28e032012-10-12 23:49:43 +02007869 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007870 val_end += delta;
7871 next += delta;
7872 hdr_end += delta;
7873 hdr_next += delta;
7874 cur_hdr->len += delta;
7875 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007876
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007877 del_from = NULL;
7878 preserve_hdr = 1; /* we want to keep this cookie */
7879 }
7880 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007881 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007882 del_from = prev;
7883 }
7884 } else {
7885 /* This is not our cookie, so we must preserve it. But if we already
7886 * scheduled another cookie for removal, we cannot remove the
7887 * complete header, but we can remove the previous block itself.
7888 */
7889 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007890
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007891 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007892 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007893 if (att_beg >= del_from)
7894 att_beg += delta;
7895 if (att_end >= del_from)
7896 att_end += delta;
7897 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007898 val_end += delta;
7899 next += delta;
7900 hdr_end += delta;
7901 hdr_next += delta;
7902 cur_hdr->len += delta;
7903 http_msg_move_end(&txn->req, delta);
7904 prev = del_from;
7905 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007906 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007907 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007908
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007909 /* continue with next cookie on this header line */
7910 att_beg = next;
7911 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007912
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007913 /* There are no more cookies on this line.
7914 * We may still have one (or several) marked for deletion at the
7915 * end of the line. We must do this now in two ways :
7916 * - if some cookies must be preserved, we only delete from the
7917 * mark to the end of line ;
7918 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007919 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007920 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007921 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007922 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007923 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007924 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007925 cur_hdr->len += delta;
7926 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007927 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007928
7929 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007930 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7931 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007932 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007933 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007934 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007935 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007936 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007937 }
7938
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007939 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007940 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007941 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007942}
7943
7944
Willy Tarreaua15645d2007-03-18 16:22:39 +01007945/* Iterate the same filter through all response headers contained in <rtr>.
7946 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7947 */
Willy Tarreau87b09662015-04-03 00:22:06 +02007948int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007949{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007950 char *cur_ptr, *cur_end, *cur_next;
7951 int cur_idx, old_idx, last_hdr;
Willy Tarreaueee5b512015-04-03 23:46:31 +02007952 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007953 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007954 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007955
7956 last_hdr = 0;
7957
Willy Tarreau9b28e032012-10-12 23:49:43 +02007958 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007959 old_idx = 0;
7960
7961 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007962 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007963 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007964 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007965 (exp->action == ACT_ALLOW ||
7966 exp->action == ACT_DENY))
7967 return 0;
7968
7969 cur_idx = txn->hdr_idx.v[old_idx].next;
7970 if (!cur_idx)
7971 break;
7972
7973 cur_hdr = &txn->hdr_idx.v[cur_idx];
7974 cur_ptr = cur_next;
7975 cur_end = cur_ptr + cur_hdr->len;
7976 cur_next = cur_end + cur_hdr->cr + 1;
7977
7978 /* Now we have one header between cur_ptr and cur_end,
7979 * and the next header starts at cur_next.
7980 */
7981
Willy Tarreau15a53a42015-01-21 13:39:42 +01007982 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007983 switch (exp->action) {
7984 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007985 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007986 last_hdr = 1;
7987 break;
7988
7989 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007990 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007991 last_hdr = 1;
7992 break;
7993
7994 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007995 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7996 if (trash.len < 0)
7997 return -1;
7998
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007999 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008000 /* FIXME: if the user adds a newline in the replacement, the
8001 * index will not be recalculated for now, and the new line
8002 * will not be counted as a new header.
8003 */
8004
8005 cur_end += delta;
8006 cur_next += delta;
8007 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008008 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008009 break;
8010
8011 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02008012 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008013 cur_next += delta;
8014
Willy Tarreaufa355d42009-11-29 18:12:29 +01008015 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008016 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8017 txn->hdr_idx.used--;
8018 cur_hdr->len = 0;
8019 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01008020 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008021 break;
8022
8023 }
8024 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008025
8026 /* keep the link from this header to next one in case of later
8027 * removal of next header.
8028 */
8029 old_idx = cur_idx;
8030 }
8031 return 0;
8032}
8033
8034
8035/* Apply the filter to the status line in the response buffer <rtr>.
8036 * Returns 0 if nothing has been done, 1 if the filter has been applied,
8037 * or -1 if a replacement resulted in an invalid status line.
8038 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008039int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008040{
Willy Tarreaua15645d2007-03-18 16:22:39 +01008041 char *cur_ptr, *cur_end;
8042 int done;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008043 struct http_txn *txn = s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008044 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008045
8046
Willy Tarreau3d300592007-03-18 18:34:41 +01008047 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008048 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01008049 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008050 (exp->action == ACT_ALLOW ||
8051 exp->action == ACT_DENY))
8052 return 0;
8053 else if (exp->action == ACT_REMOVE)
8054 return 0;
8055
8056 done = 0;
8057
Willy Tarreau9b28e032012-10-12 23:49:43 +02008058 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008059 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008060
8061 /* Now we have the status line between cur_ptr and cur_end */
8062
Willy Tarreau15a53a42015-01-21 13:39:42 +01008063 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008064 switch (exp->action) {
8065 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01008066 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008067 done = 1;
8068 break;
8069
8070 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01008071 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008072 done = 1;
8073 break;
8074
8075 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06008076 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
8077 if (trash.len < 0)
8078 return -1;
8079
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008080 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008081 /* FIXME: if the user adds a newline in the replacement, the
8082 * index will not be recalculated for now, and the new line
8083 * will not be counted as a new header.
8084 */
8085
Willy Tarreaufa355d42009-11-29 18:12:29 +01008086 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008087 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008088 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02008089 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01008090 cur_ptr, cur_end + 1,
8091 NULL, NULL);
8092 if (unlikely(!cur_end))
8093 return -1;
8094
8095 /* we have a full respnse and we know that we have either a CR
8096 * or an LF at <ptr>.
8097 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008098 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02008099 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01008100 /* there is no point trying this regex on headers */
8101 return 1;
8102 }
8103 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008104 return done;
8105}
8106
8107
8108
8109/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008110 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008111 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
8112 * unparsable response.
8113 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008114int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008115{
Willy Tarreau192252e2015-04-04 01:47:55 +02008116 struct session *sess = s->sess;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008117 struct http_txn *txn = s->txn;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008118 struct hdr_exp *exp;
8119
8120 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008121 int ret;
8122
8123 /*
8124 * The interleaving of transformations and verdicts
8125 * makes it difficult to decide to continue or stop
8126 * the evaluation.
8127 */
8128
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008129 if (txn->flags & TX_SVDENY)
8130 break;
8131
Willy Tarreau3d300592007-03-18 18:34:41 +01008132 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01008133 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
8134 exp->action == ACT_PASS)) {
8135 exp = exp->next;
8136 continue;
8137 }
8138
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008139 /* if this filter had a condition, evaluate it now and skip to
8140 * next filter if the condition does not match.
8141 */
8142 if (exp->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02008143 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008144 ret = acl_pass(ret);
8145 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
8146 ret = !ret;
8147 if (!ret)
8148 continue;
8149 }
8150
Willy Tarreaua15645d2007-03-18 16:22:39 +01008151 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01008152 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008153 if (unlikely(ret < 0))
8154 return -1;
8155
8156 if (likely(ret == 0)) {
8157 /* The filter did not match the response, it can be
8158 * iterated through all headers.
8159 */
Sasha Pachevc6002042014-05-26 12:33:48 -06008160 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
8161 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008162 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008163 }
8164 return 0;
8165}
8166
8167
Willy Tarreaua15645d2007-03-18 16:22:39 +01008168/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01008169 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02008170 * desirable to call it only when needed. This function is also used when we
8171 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01008172 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008173void manage_server_side_cookies(struct stream *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008174{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008175 struct http_txn *txn = s->txn;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008176 struct session *sess = s->sess;
Willy Tarreau827aee92011-03-10 16:55:02 +01008177 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008178 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008179 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008180 char *hdr_beg, *hdr_end, *hdr_next;
8181 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008182
Willy Tarreaua15645d2007-03-18 16:22:39 +01008183 /* Iterate through the headers.
8184 * we start with the start line.
8185 */
8186 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008187 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008188
8189 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8190 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008191 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008192
8193 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008194 hdr_beg = hdr_next;
8195 hdr_end = hdr_beg + cur_hdr->len;
8196 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008197
Willy Tarreau24581ba2010-08-31 22:39:35 +02008198 /* We have one full header between hdr_beg and hdr_end, and the
8199 * next header starts at hdr_next. We're only interested in
8200 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008201 */
8202
Willy Tarreau24581ba2010-08-31 22:39:35 +02008203 is_cookie2 = 0;
8204 prev = hdr_beg + 10;
8205 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008206 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008207 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8208 if (!val) {
8209 old_idx = cur_idx;
8210 continue;
8211 }
8212 is_cookie2 = 1;
8213 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008214 }
8215
Willy Tarreau24581ba2010-08-31 22:39:35 +02008216 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8217 * <prev> points to the colon.
8218 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008219 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008220
Willy Tarreau24581ba2010-08-31 22:39:35 +02008221 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8222 * check-cache is enabled) and we are not interested in checking
8223 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008224 */
Willy Tarreau53a09d52015-08-10 18:59:40 +02008225 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008226 return;
8227
Willy Tarreau24581ba2010-08-31 22:39:35 +02008228 /* OK so now we know we have to process this response cookie.
8229 * The format of the Set-Cookie header is slightly different
8230 * from the format of the Cookie header in that it does not
8231 * support the comma as a cookie delimiter (thus the header
8232 * cannot be folded) because the Expires attribute described in
8233 * the original Netscape's spec may contain an unquoted date
8234 * with a comma inside. We have to live with this because
8235 * many browsers don't support Max-Age and some browsers don't
8236 * support quoted strings. However the Set-Cookie2 header is
8237 * clean.
8238 *
8239 * We have to keep multiple pointers in order to support cookie
8240 * removal at the beginning, middle or end of header without
8241 * corrupting the header (in case of set-cookie2). A special
8242 * pointer, <scav> points to the beginning of the set-cookie-av
8243 * fields after the first semi-colon. The <next> pointer points
8244 * either to the end of line (set-cookie) or next unquoted comma
8245 * (set-cookie2). All of these headers are valid :
8246 *
8247 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8248 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8249 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8250 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8251 * | | | | | | | | | |
8252 * | | | | | | | | +-> next hdr_end <--+
8253 * | | | | | | | +------------> scav
8254 * | | | | | | +--------------> val_end
8255 * | | | | | +--------------------> val_beg
8256 * | | | | +----------------------> equal
8257 * | | | +------------------------> att_end
8258 * | | +----------------------------> att_beg
8259 * | +------------------------------> prev
8260 * +-----------------------------------------> hdr_beg
8261 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008262
Willy Tarreau24581ba2010-08-31 22:39:35 +02008263 for (; prev < hdr_end; prev = next) {
8264 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008265
Willy Tarreau24581ba2010-08-31 22:39:35 +02008266 /* find att_beg */
8267 att_beg = prev + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01008268 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008269 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008270
Willy Tarreau24581ba2010-08-31 22:39:35 +02008271 /* find att_end : this is the first character after the last non
8272 * space before the equal. It may be equal to hdr_end.
8273 */
8274 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008275
Willy Tarreau24581ba2010-08-31 22:39:35 +02008276 while (equal < hdr_end) {
8277 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8278 break;
Willy Tarreau2235b262016-11-05 15:50:20 +01008279 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008280 continue;
8281 att_end = equal;
8282 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008283
Willy Tarreau24581ba2010-08-31 22:39:35 +02008284 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8285 * is between <att_beg> and <equal>, both may be identical.
8286 */
8287
8288 /* look for end of cookie if there is an equal sign */
8289 if (equal < hdr_end && *equal == '=') {
8290 /* look for the beginning of the value */
8291 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +01008292 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008293 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008294
Willy Tarreau24581ba2010-08-31 22:39:35 +02008295 /* find the end of the value, respecting quotes */
8296 next = find_cookie_value_end(val_beg, hdr_end);
8297
8298 /* make val_end point to the first white space or delimitor after the value */
8299 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +01008300 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau24581ba2010-08-31 22:39:35 +02008301 val_end--;
8302 } else {
8303 /* <equal> points to next comma, semi-colon or EOL */
8304 val_beg = val_end = next = equal;
8305 }
8306
8307 if (next < hdr_end) {
8308 /* Set-Cookie2 supports multiple cookies, and <next> points to
8309 * a colon or semi-colon before the end. So skip all attr-value
8310 * pairs and look for the next comma. For Set-Cookie, since
8311 * commas are permitted in values, skip to the end.
8312 */
8313 if (is_cookie2)
8314 next = find_hdr_value_end(next, hdr_end);
8315 else
8316 next = hdr_end;
8317 }
8318
8319 /* Now everything is as on the diagram above */
8320
8321 /* Ignore cookies with no equal sign */
8322 if (equal == val_end)
8323 continue;
8324
8325 /* If there are spaces around the equal sign, we need to
8326 * strip them otherwise we'll get trouble for cookie captures,
8327 * or even for rewrites. Since this happens extremely rarely,
8328 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008329 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008330 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8331 int stripped_before = 0;
8332 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008333
Willy Tarreau24581ba2010-08-31 22:39:35 +02008334 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008335 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008336 equal += stripped_before;
8337 val_beg += stripped_before;
8338 }
8339
8340 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008341 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008342 val_beg += stripped_after;
8343 stripped_before += stripped_after;
8344 }
8345
8346 val_end += stripped_before;
8347 next += stripped_before;
8348 hdr_end += stripped_before;
8349 hdr_next += stripped_before;
8350 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008351 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008352 }
8353
8354 /* First, let's see if we want to capture this cookie. We check
8355 * that we don't already have a server side cookie, because we
8356 * can only capture one. Also as an optimisation, we ignore
8357 * cookies shorter than the declared name.
8358 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008359 if (sess->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008360 txn->srv_cookie == NULL &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008361 (val_end - att_beg >= sess->fe->capture_namelen) &&
8362 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008363 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008364 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008365 Alert("HTTP logging : out of memory.\n");
8366 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008367 else {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008368 if (log_len > sess->fe->capture_len)
8369 log_len = sess->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008370 memcpy(txn->srv_cookie, att_beg, log_len);
8371 txn->srv_cookie[log_len] = 0;
8372 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008373 }
8374
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008375 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008376 /* now check if we need to process it for persistence */
Willy Tarreaue7dff022015-04-03 01:14:29 +02008377 if (!(s->flags & SF_IGNORE_PRST) &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008378 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8379 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008380 /* assume passive cookie by default */
8381 txn->flags &= ~TX_SCK_MASK;
8382 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008383
8384 /* If the cookie is in insert mode on a known server, we'll delete
8385 * this occurrence because we'll insert another one later.
8386 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008387 * a direct access.
8388 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008389 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008390 /* The "preserve" flag was set, we don't want to touch the
8391 * server's cookie.
8392 */
8393 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008394 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02008395 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008396 /* this cookie must be deleted */
8397 if (*prev == ':' && next == hdr_end) {
8398 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008399 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008400 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8401 txn->hdr_idx.used--;
8402 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008403 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008404 hdr_next += delta;
8405 http_msg_move_end(&txn->rsp, delta);
8406 /* note: while both invalid now, <next> and <hdr_end>
8407 * are still equal, so the for() will stop as expected.
8408 */
8409 } else {
8410 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008411 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008412 next = prev;
8413 hdr_end += delta;
8414 hdr_next += delta;
8415 cur_hdr->len += delta;
8416 http_msg_move_end(&txn->rsp, delta);
8417 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008418 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008419 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008420 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008421 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008422 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008423 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008424 * with this server since we know it.
8425 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008426 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008427 next += delta;
8428 hdr_end += delta;
8429 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008430 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008431 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008432
Willy Tarreauf1348312010-10-07 15:54:11 +02008433 txn->flags &= ~TX_SCK_MASK;
8434 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008435 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008436 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008437 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008438 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008439 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008440 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008441 next += delta;
8442 hdr_end += delta;
8443 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008444 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008445 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008446
Willy Tarreau827aee92011-03-10 16:55:02 +01008447 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008448 txn->flags &= ~TX_SCK_MASK;
8449 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008450 }
8451 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008452 /* that's done for this cookie, check the next one on the same
8453 * line when next != hdr_end (only if is_cookie2).
8454 */
8455 }
8456 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008457 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008458 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008459}
8460
8461
Willy Tarreaua15645d2007-03-18 16:22:39 +01008462/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008463 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008464 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008465void check_response_for_cacheability(struct stream *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008466{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008467 struct http_txn *txn = s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008468 char *p1, *p2;
8469
8470 char *cur_ptr, *cur_end, *cur_next;
8471 int cur_idx;
8472
Willy Tarreau5df51872007-11-25 16:20:08 +01008473 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008474 return;
8475
8476 /* Iterate through the headers.
8477 * we start with the start line.
8478 */
8479 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008480 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008481
8482 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8483 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008484 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008485
8486 cur_hdr = &txn->hdr_idx.v[cur_idx];
8487 cur_ptr = cur_next;
8488 cur_end = cur_ptr + cur_hdr->len;
8489 cur_next = cur_end + cur_hdr->cr + 1;
8490
8491 /* We have one full header between cur_ptr and cur_end, and the
8492 * next header starts at cur_next. We're only interested in
8493 * "Cookie:" headers.
8494 */
8495
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008496 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8497 if (val) {
8498 if ((cur_end - (cur_ptr + val) >= 8) &&
8499 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8500 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8501 return;
8502 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008503 }
8504
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008505 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8506 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008507 continue;
8508
8509 /* OK, right now we know we have a cache-control header at cur_ptr */
8510
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008511 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008512
8513 if (p1 >= cur_end) /* no more info */
8514 continue;
8515
8516 /* p1 is at the beginning of the value */
8517 p2 = p1;
8518
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008519 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008520 p2++;
8521
8522 /* we have a complete value between p1 and p2 */
8523 if (p2 < cur_end && *p2 == '=') {
8524 /* we have something of the form no-cache="set-cookie" */
8525 if ((cur_end - p1 >= 21) &&
8526 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8527 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008528 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008529 continue;
8530 }
8531
8532 /* OK, so we know that either p2 points to the end of string or to a comma */
8533 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008534 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008535 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8536 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8537 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008538 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008539 return;
8540 }
8541
8542 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008543 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008544 continue;
8545 }
8546 }
8547}
8548
Willy Tarreau58f10d72006-12-04 02:26:12 +01008549
Willy Tarreaub2513902006-12-17 14:52:38 +01008550/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008551 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008552 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008553 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008554 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008555 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008556 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008557 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008558 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008559int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008560{
8561 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008562 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008563 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008564
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008565 if (!uri_auth)
8566 return 0;
8567
Cyril Bonté70be45d2010-10-12 00:14:35 +02008568 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008569 return 0;
8570
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008571 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008572 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008573 return 0;
8574
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008575 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008576 return 0;
8577
Willy Tarreaub2513902006-12-17 14:52:38 +01008578 return 1;
8579}
8580
Willy Tarreau4076a152009-04-02 15:18:36 +02008581/*
8582 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008583 * By default it tries to report the error position as msg->err_pos. However if
8584 * this one is not set, it will then report msg->next, which is the last known
8585 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008586 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008587 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008588void http_capture_bad_message(struct error_snapshot *es, struct stream *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008589 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008590 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008591{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008592 struct session *sess = strm_sess(s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008593 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008594 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008595
Willy Tarreauf3764b72016-03-31 13:45:10 +02008596 es->len = MIN(chn->buf->i, global.tune.bufsize);
Willy Tarreau9b28e032012-10-12 23:49:43 +02008597 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008598 len1 = MIN(len1, es->len);
8599 len2 = es->len - len1; /* remaining data if buffer wraps */
8600
Willy Tarreauf3764b72016-03-31 13:45:10 +02008601 if (!es->buf)
8602 es->buf = malloc(global.tune.bufsize);
8603
8604 if (es->buf) {
8605 memcpy(es->buf, chn->buf->p, len1);
8606 if (len2)
8607 memcpy(es->buf + len1, chn->buf->data, len2);
8608 }
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008609
Willy Tarreau4076a152009-04-02 15:18:36 +02008610 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008611 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008612 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008613 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008614
Willy Tarreau4076a152009-04-02 15:18:36 +02008615 es->when = date; // user-visible date
8616 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008617 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008618 es->oe = other_end;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008619 if (objt_conn(sess->origin))
8620 es->src = __objt_conn(sess->origin)->addr.from;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008621 else
8622 memset(&es->src, 0, sizeof(es->src));
8623
Willy Tarreau078272e2010-12-12 12:46:33 +01008624 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008625 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008626 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008627 es->s_flags = s->flags;
Willy Tarreaueee5b512015-04-03 23:46:31 +02008628 es->t_flags = s->txn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008629 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008630 es->b_out = chn->buf->o;
8631 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008632 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008633 es->m_clen = msg->chunk_len;
8634 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008635}
Willy Tarreaub2513902006-12-17 14:52:38 +01008636
Willy Tarreau294c4732011-12-16 21:35:50 +01008637/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8638 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8639 * performed over the whole headers. Otherwise it must contain a valid header
8640 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8641 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8642 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8643 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008644 * -1. The value fetch stops at commas, so this function is suited for use with
8645 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008646 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008647 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008648unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008649 struct hdr_idx *idx, int occ,
8650 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008651{
Willy Tarreau294c4732011-12-16 21:35:50 +01008652 struct hdr_ctx local_ctx;
8653 char *ptr_hist[MAX_HDR_HISTORY];
8654 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008655 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008656 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008657
Willy Tarreau294c4732011-12-16 21:35:50 +01008658 if (!ctx) {
8659 local_ctx.idx = 0;
8660 ctx = &local_ctx;
8661 }
8662
Willy Tarreaubce70882009-09-07 11:51:47 +02008663 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008664 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008665 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008666 occ--;
8667 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008668 *vptr = ctx->line + ctx->val;
8669 *vlen = ctx->vlen;
8670 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008671 }
8672 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008673 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008674 }
8675
8676 /* negative occurrence, we scan all the list then walk back */
8677 if (-occ > MAX_HDR_HISTORY)
8678 return 0;
8679
Willy Tarreau294c4732011-12-16 21:35:50 +01008680 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008681 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008682 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8683 len_hist[hist_ptr] = ctx->vlen;
8684 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008685 hist_ptr = 0;
8686 found++;
8687 }
8688 if (-occ > found)
8689 return 0;
8690 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008691 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8692 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8693 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008694 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008695 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008696 if (hist_ptr >= MAX_HDR_HISTORY)
8697 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008698 *vptr = ptr_hist[hist_ptr];
8699 *vlen = len_hist[hist_ptr];
8700 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008701}
8702
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008703/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8704 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8705 * performed over the whole headers. Otherwise it must contain a valid header
8706 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8707 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8708 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8709 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8710 * -1. This function differs from http_get_hdr() in that it only returns full
8711 * line header values and does not stop at commas.
8712 * The return value is 0 if nothing was found, or non-zero otherwise.
8713 */
8714unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8715 struct hdr_idx *idx, int occ,
8716 struct hdr_ctx *ctx, char **vptr, int *vlen)
8717{
8718 struct hdr_ctx local_ctx;
8719 char *ptr_hist[MAX_HDR_HISTORY];
8720 int len_hist[MAX_HDR_HISTORY];
8721 unsigned int hist_ptr;
8722 int found;
8723
8724 if (!ctx) {
8725 local_ctx.idx = 0;
8726 ctx = &local_ctx;
8727 }
8728
8729 if (occ >= 0) {
8730 /* search from the beginning */
8731 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8732 occ--;
8733 if (occ <= 0) {
8734 *vptr = ctx->line + ctx->val;
8735 *vlen = ctx->vlen;
8736 return 1;
8737 }
8738 }
8739 return 0;
8740 }
8741
8742 /* negative occurrence, we scan all the list then walk back */
8743 if (-occ > MAX_HDR_HISTORY)
8744 return 0;
8745
8746 found = hist_ptr = 0;
8747 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8748 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8749 len_hist[hist_ptr] = ctx->vlen;
8750 if (++hist_ptr >= MAX_HDR_HISTORY)
8751 hist_ptr = 0;
8752 found++;
8753 }
8754 if (-occ > found)
8755 return 0;
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008756
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008757 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008758 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8759 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8760 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008761 */
Nenad Merdanovic69ad4b92016-03-29 13:14:30 +02008762 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008763 if (hist_ptr >= MAX_HDR_HISTORY)
8764 hist_ptr -= MAX_HDR_HISTORY;
8765 *vptr = ptr_hist[hist_ptr];
8766 *vlen = len_hist[hist_ptr];
8767 return 1;
8768}
8769
Willy Tarreaubaaee002006-06-26 02:48:02 +02008770/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008771 * Print a debug line with a header. Always stop at the first CR or LF char,
8772 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8773 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008774 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008775void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008776{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008777 struct session *sess = strm_sess(s);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008778 int max;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008779
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008780 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008781 dir,
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02008782 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->t.sock.fd : -1,
Willy Tarreau350f4872014-11-28 14:42:25 +01008783 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 +02008784
8785 for (max = 0; start + max < end; max++)
8786 if (start[max] == '\r' || start[max] == '\n')
8787 break;
8788
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008789 UBOUND(max, trash.size - trash.len - 3);
8790 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8791 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008792 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008793}
8794
Willy Tarreaueee5b512015-04-03 23:46:31 +02008795
8796/* Allocate a new HTTP transaction for stream <s> unless there is one already.
8797 * The hdr_idx is allocated as well. In case of allocation failure, everything
8798 * allocated is freed and NULL is returned. Otherwise the new transaction is
8799 * assigned to the stream and returned.
8800 */
8801struct http_txn *http_alloc_txn(struct stream *s)
8802{
8803 struct http_txn *txn = s->txn;
8804
8805 if (txn)
8806 return txn;
8807
8808 txn = pool_alloc2(pool2_http_txn);
8809 if (!txn)
8810 return txn;
8811
8812 txn->hdr_idx.size = global.tune.max_http_hdr;
8813 txn->hdr_idx.v = pool_alloc2(pool2_hdr_idx);
8814 if (!txn->hdr_idx.v) {
8815 pool_free2(pool2_http_txn, txn);
8816 return NULL;
8817 }
8818
8819 s->txn = txn;
8820 return txn;
8821}
8822
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008823void http_txn_reset_req(struct http_txn *txn)
8824{
8825 txn->req.flags = 0;
8826 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
8827 txn->req.next = 0;
8828 txn->req.chunk_len = 0LL;
8829 txn->req.body_len = 0LL;
8830 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8831}
8832
8833void http_txn_reset_res(struct http_txn *txn)
8834{
8835 txn->rsp.flags = 0;
8836 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
8837 txn->rsp.next = 0;
8838 txn->rsp.chunk_len = 0LL;
8839 txn->rsp.body_len = 0LL;
8840 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
8841}
8842
Willy Tarreau0937bc42009-12-22 15:03:09 +01008843/*
Willy Tarreau87b09662015-04-03 00:22:06 +02008844 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
Willy Tarreau0937bc42009-12-22 15:03:09 +01008845 * the required fields are properly allocated and that we only need to (re)init
8846 * them. This should be used before processing any new request.
8847 */
Willy Tarreau87b09662015-04-03 00:22:06 +02008848void http_init_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008849{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008850 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008851 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008852
8853 txn->flags = 0;
8854 txn->status = -1;
8855
Willy Tarreauf64d1412010-10-07 20:06:11 +02008856 txn->cookie_first_date = 0;
8857 txn->cookie_last_date = 0;
8858
Willy Tarreaueee5b512015-04-03 23:46:31 +02008859 txn->srv_cookie = NULL;
8860 txn->cli_cookie = NULL;
8861 txn->uri = NULL;
8862
Thierry FOURNIERfd50f0b2015-09-25 18:53:18 +02008863 http_txn_reset_req(txn);
8864 http_txn_reset_res(txn);
8865
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008866 txn->req.chn = &s->req;
8867 txn->rsp.chn = &s->res;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008868
8869 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008870
8871 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8872 if (fe->options2 & PR_O2_REQBUG_OK)
8873 txn->req.err_pos = -1; /* let buggy requests pass */
8874
Willy Tarreau0937bc42009-12-22 15:03:09 +01008875 if (txn->hdr_idx.v)
8876 hdr_idx_init(&txn->hdr_idx);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02008877
8878 vars_init(&s->vars_txn, SCOPE_TXN);
8879 vars_init(&s->vars_reqres, SCOPE_REQ);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008880}
8881
8882/* to be used at the end of a transaction */
Willy Tarreau87b09662015-04-03 00:22:06 +02008883void http_end_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008884{
Willy Tarreaueee5b512015-04-03 23:46:31 +02008885 struct http_txn *txn = s->txn;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008886 struct proxy *fe = strm_fe(s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008887
8888 /* these ones will have been dynamically allocated */
8889 pool_free2(pool2_requri, txn->uri);
8890 pool_free2(pool2_capture, txn->cli_cookie);
8891 pool_free2(pool2_capture, txn->srv_cookie);
William Lallemanda73203e2012-03-12 12:48:57 +01008892 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008893
William Lallemanda73203e2012-03-12 12:48:57 +01008894 s->unique_id = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008895 txn->uri = NULL;
8896 txn->srv_cookie = NULL;
8897 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008898
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008899 if (s->req_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008900 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008901 for (h = fe->req_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008902 pool_free2(h->pool, s->req_cap[h->index]);
8903 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008904 }
8905
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008906 if (s->res_cap) {
Willy Tarreau46023632010-01-07 22:51:47 +01008907 struct cap_hdr *h;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02008908 for (h = fe->rsp_cap; h; h = h->next)
Willy Tarreaucb7dd012015-04-03 22:16:32 +02008909 pool_free2(h->pool, s->res_cap[h->index]);
8910 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
Willy Tarreau46023632010-01-07 22:51:47 +01008911 }
8912
Willy Tarreau6204cd92016-03-10 16:33:04 +01008913 vars_prune(&s->vars_txn, s->sess, s);
8914 vars_prune(&s->vars_reqres, s->sess, s);
Willy Tarreau0937bc42009-12-22 15:03:09 +01008915}
8916
8917/* to be used at the end of a transaction to prepare a new one */
Willy Tarreau87b09662015-04-03 00:22:06 +02008918void http_reset_txn(struct stream *s)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008919{
8920 http_end_txn(s);
8921 http_init_txn(s);
8922
Thierry FOURNIERbc4c1ac2015-02-25 13:36:14 +01008923 /* reinitialise the current rule list pointer to NULL. We are sure that
8924 * any rulelist match the NULL pointer.
8925 */
8926 s->current_rule_list = NULL;
8927
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008928 s->be = strm_fe(s);
8929 s->logs.logwait = strm_fe(s)->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008930 s->logs.level = 0;
Willy Tarreau87b09662015-04-03 00:22:06 +02008931 stream_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008932 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008933 /* re-init store persistence */
8934 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008935 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008936
Willy Tarreau0937bc42009-12-22 15:03:09 +01008937 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008938
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008939 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008940
Willy Tarreau739cfba2010-01-25 23:11:14 +01008941 /* We must trim any excess data from the response buffer, because we
8942 * may have blocked an invalid response from a server that we don't
8943 * want to accidentely forward once we disable the analysers, nor do
8944 * we want those data to come along with next response. A typical
8945 * example of such data would be from a buggy server responding to
8946 * a HEAD with some data, or sending more than the advertised
8947 * content-length.
8948 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008949 if (unlikely(s->res.buf->i))
8950 s->res.buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008951
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008952 s->req.rto = strm_fe(s)->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008953 s->req.wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008954
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008955 s->res.rto = TICK_ETERNITY;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02008956 s->res.wto = strm_fe(s)->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008957
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01008958 s->req.rex = TICK_ETERNITY;
8959 s->req.wex = TICK_ETERNITY;
8960 s->req.analyse_exp = TICK_ETERNITY;
8961 s->res.rex = TICK_ETERNITY;
8962 s->res.wex = TICK_ETERNITY;
8963 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008964}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008965
Sasha Pachev218f0642014-06-16 12:05:59 -06008966void free_http_res_rules(struct list *r)
8967{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008968 struct act_rule *tr, *pr;
Sasha Pachev218f0642014-06-16 12:05:59 -06008969
8970 list_for_each_entry_safe(pr, tr, r, list) {
8971 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008972 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008973 free(pr);
8974 }
8975}
8976
8977void free_http_req_rules(struct list *r)
8978{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008979 struct act_rule *tr, *pr;
Willy Tarreauff011f22011-01-06 17:51:27 +01008980
8981 list_for_each_entry_safe(pr, tr, r, list) {
8982 LIST_DEL(&pr->list);
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02008983 if (pr->action == ACT_HTTP_REQ_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008984 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008985
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008986 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008987 free(pr);
8988 }
8989}
8990
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008991/* parse an "http-request" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008992struct act_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreauff011f22011-01-06 17:51:27 +01008993{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02008994 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02008995 struct action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008996 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008997 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008998
Vincent Bernat02779b62016-04-03 13:48:43 +02008999 rule = calloc(1, sizeof(*rule));
Willy Tarreauff011f22011-01-06 17:51:27 +01009000 if (!rule) {
9001 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009002 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009003 }
9004
CJ Ess108b1dd2015-04-07 12:03:37 -04009005 rule->deny_status = HTTP_ERR_403;
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009006 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009007 rule->action = ACT_ACTION_ALLOW;
Willy Tarreauff011f22011-01-06 17:51:27 +01009008 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02009009 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
CJ Ess108b1dd2015-04-07 12:03:37 -04009010 int code;
9011 int hc;
9012
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009013 rule->action = ACT_ACTION_DENY;
Willy Tarreauff011f22011-01-06 17:51:27 +01009014 cur_arg = 1;
CJ Ess108b1dd2015-04-07 12:03:37 -04009015 if (strcmp(args[cur_arg], "deny_status") == 0) {
9016 cur_arg++;
9017 if (!args[cur_arg]) {
9018 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing status code.\n",
9019 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9020 goto out_err;
9021 }
9022
9023 code = atol(args[cur_arg]);
9024 cur_arg++;
9025 for (hc = 0; hc < HTTP_ERR_SIZE; hc++) {
9026 if (http_err_codes[hc] == code) {
9027 rule->deny_status = hc;
9028 break;
9029 }
9030 }
9031
9032 if (hc >= HTTP_ERR_SIZE) {
9033 Warning("parsing [%s:%d] : status code %d not handled, using default code 403.\n",
9034 file, linenum, code);
9035 }
9036 }
Willy Tarreauccbcc372012-12-27 12:37:57 +01009037 } else if (!strcmp(args[0], "tarpit")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009038 rule->action = ACT_HTTP_REQ_TARPIT;
Willy Tarreauccbcc372012-12-27 12:37:57 +01009039 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01009040 } else if (!strcmp(args[0], "auth")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009041 rule->action = ACT_HTTP_REQ_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01009042 cur_arg = 1;
9043
9044 while(*args[cur_arg]) {
9045 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009046 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01009047 cur_arg+=2;
9048 continue;
9049 } else
9050 break;
9051 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009052 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009053 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009054 cur_arg = 1;
9055
9056 if (!*args[cur_arg] ||
9057 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9058 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
9059 file, linenum, args[0]);
9060 goto out_err;
9061 }
9062 rule->arg.nice = atoi(args[cur_arg]);
9063 if (rule->arg.nice < -1024)
9064 rule->arg.nice = -1024;
9065 else if (rule->arg.nice > 1024)
9066 rule->arg.nice = 1024;
9067 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009068 } else if (!strcmp(args[0], "set-tos")) {
9069#ifdef IP_TOS
9070 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009071 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009072 cur_arg = 1;
9073
9074 if (!*args[cur_arg] ||
9075 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9076 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9077 file, linenum, args[0]);
9078 goto out_err;
9079 }
9080
9081 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9082 if (err && *err != '\0') {
9083 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9084 file, linenum, err, args[0]);
9085 goto out_err;
9086 }
9087 cur_arg++;
9088#else
9089 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9090 goto out_err;
9091#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009092 } else if (!strcmp(args[0], "set-mark")) {
9093#ifdef SO_MARK
9094 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009095 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009096 cur_arg = 1;
9097
9098 if (!*args[cur_arg] ||
9099 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9100 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
9101 file, linenum, args[0]);
9102 goto out_err;
9103 }
9104
9105 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9106 if (err && *err != '\0') {
9107 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
9108 file, linenum, err, args[0]);
9109 goto out_err;
9110 }
9111 cur_arg++;
9112 global.last_checks |= LSTCHK_NETADM;
9113#else
9114 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9115 goto out_err;
9116#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009117 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009118 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009119 cur_arg = 1;
9120
9121 if (!*args[cur_arg] ||
9122 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9123 bad_log_level:
9124 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9125 file, linenum, args[0]);
9126 goto out_err;
9127 }
9128 if (strcmp(args[cur_arg], "silent") == 0)
9129 rule->arg.loglevel = -1;
9130 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9131 goto bad_log_level;
9132 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009133 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009134 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009135 cur_arg = 1;
9136
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009137 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9138 (*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 +01009139 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9140 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009141 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009142 }
9143
9144 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9145 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9146 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009147
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009148 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009149 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009150 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 +01009151 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9152 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9153 file, linenum, args[0], error);
9154 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009155 goto out_err;
9156 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009157 free(proxy->conf.lfs_file);
9158 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9159 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009160 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009161 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009162 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009163 cur_arg = 1;
9164
9165 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02009166 (*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 -06009167 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9168 file, linenum, args[0]);
9169 goto out_err;
9170 }
9171
9172 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9173 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9174 LIST_INIT(&rule->arg.hdr_add.fmt);
9175
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009176 error = NULL;
9177 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9178 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9179 args[cur_arg + 1], error);
9180 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009181 goto out_err;
9182 }
9183
9184 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009185 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009186 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 +01009187 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9188 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9189 file, linenum, args[0], error);
9190 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009191 goto out_err;
9192 }
Sasha Pachev218f0642014-06-16 12:05:59 -06009193
9194 free(proxy->conf.lfs_file);
9195 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9196 proxy->conf.lfs_line = proxy->conf.args.line;
9197 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009198 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009199 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009200 cur_arg = 1;
9201
9202 if (!*args[cur_arg] ||
9203 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9204 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9205 file, linenum, args[0]);
9206 goto out_err;
9207 }
9208
9209 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9210 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9211
9212 proxy->conf.args.ctx = ARGC_HRQ;
9213 free(proxy->conf.lfs_file);
9214 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9215 proxy->conf.lfs_line = proxy->conf.args.line;
9216 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009217 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9218 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009219 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009220 struct sample_expr *expr;
9221 unsigned int where;
9222 char *err = NULL;
9223
9224 cur_arg = 1;
9225 proxy->conf.args.ctx = ARGC_TRK;
9226
9227 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9228 if (!expr) {
9229 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9230 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9231 free(err);
9232 goto out_err;
9233 }
9234
9235 where = 0;
9236 if (proxy->cap & PR_CAP_FE)
9237 where |= SMP_VAL_FE_HRQ_HDR;
9238 if (proxy->cap & PR_CAP_BE)
9239 where |= SMP_VAL_BE_HRQ_HDR;
9240
9241 if (!(expr->fetch->val & where)) {
9242 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9243 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9244 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9245 args[cur_arg-1], sample_src_names(expr->fetch->use));
9246 free(expr);
9247 goto out_err;
9248 }
9249
9250 if (strcmp(args[cur_arg], "table") == 0) {
9251 cur_arg++;
9252 if (!args[cur_arg]) {
9253 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9254 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9255 free(expr);
9256 goto out_err;
9257 }
9258 /* we copy the table name for now, it will be resolved later */
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009259 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
Willy Tarreau09448f72014-06-25 18:12:15 +02009260 cur_arg++;
9261 }
Thierry FOURNIER5ec63e02015-08-04 09:09:48 +02009262 rule->arg.trk_ctr.expr = expr;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009263 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009264 } else if (strcmp(args[0], "redirect") == 0) {
9265 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009266 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009267
Willy Tarreaube4653b2015-05-28 15:26:58 +02009268 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 0)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009269 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9270 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9271 goto out_err;
9272 }
9273
9274 /* this redirect rule might already contain a parsed condition which
9275 * we'll pass to the http-request rule.
9276 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009277 rule->action = ACT_HTTP_REDIR;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009278 rule->arg.redir = redir;
9279 rule->cond = redir->cond;
9280 redir->cond = NULL;
9281 cur_arg = 2;
9282 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009283 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9284 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009285 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009286 /*
9287 * '+ 8' for 'add-acl('
9288 * '- 9' for 'add-acl(' + trailing ')'
9289 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009290 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009291
9292 cur_arg = 1;
9293
9294 if (!*args[cur_arg] ||
9295 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9296 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9297 file, linenum, args[0]);
9298 goto out_err;
9299 }
9300
9301 LIST_INIT(&rule->arg.map.key);
9302 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009303 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009304 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009305 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9306 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9307 file, linenum, args[0], error);
9308 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009309 goto out_err;
9310 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009311 free(proxy->conf.lfs_file);
9312 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9313 proxy->conf.lfs_line = proxy->conf.args.line;
9314 cur_arg += 1;
9315 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9316 /* http-request del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009317 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009318 /*
9319 * '+ 8' for 'del-acl('
9320 * '- 9' for 'del-acl(' + trailing ')'
9321 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009322 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009323
9324 cur_arg = 1;
9325
9326 if (!*args[cur_arg] ||
9327 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9328 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9329 file, linenum, args[0]);
9330 goto out_err;
9331 }
9332
9333 LIST_INIT(&rule->arg.map.key);
9334 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009335 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009336 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009337 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9338 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9339 file, linenum, args[0], error);
9340 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009341 goto out_err;
9342 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009343 free(proxy->conf.lfs_file);
9344 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9345 proxy->conf.lfs_line = proxy->conf.args.line;
9346 cur_arg += 1;
9347 } else if (strncmp(args[0], "del-map", 7) == 0) {
9348 /* http-request del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009349 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009350 /*
9351 * '+ 8' for 'del-map('
9352 * '- 9' for 'del-map(' + trailing ')'
9353 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009354 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009355
9356 cur_arg = 1;
9357
9358 if (!*args[cur_arg] ||
9359 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9360 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9361 file, linenum, args[0]);
9362 goto out_err;
9363 }
9364
9365 LIST_INIT(&rule->arg.map.key);
9366 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009367 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009368 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009369 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9370 Alert("parsing [%s:%d]: 'http-request %s': %s.\n",
9371 file, linenum, args[0], error);
9372 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009373 goto out_err;
9374 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009375 free(proxy->conf.lfs_file);
9376 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9377 proxy->conf.lfs_line = proxy->conf.args.line;
9378 cur_arg += 1;
9379 } else if (strncmp(args[0], "set-map", 7) == 0) {
9380 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009381 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009382 /*
9383 * '+ 8' for 'set-map('
9384 * '- 9' for 'set-map(' + trailing ')'
9385 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009386 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009387
9388 cur_arg = 1;
9389
9390 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9391 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9392 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9393 file, linenum, args[0]);
9394 goto out_err;
9395 }
9396
9397 LIST_INIT(&rule->arg.map.key);
9398 LIST_INIT(&rule->arg.map.value);
9399 proxy->conf.args.ctx = ARGC_HRQ;
9400
9401 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009402 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009403 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009404 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9405 Alert("parsing [%s:%d]: 'http-request %s' key: %s.\n",
9406 file, linenum, args[0], error);
9407 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009408 goto out_err;
9409 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009410
9411 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009412 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009413 if (!parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009414 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR, &error)) {
9415 Alert("parsing [%s:%d]: 'http-request %s' pattern: %s.\n",
9416 file, linenum, args[0], error);
9417 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009418 goto out_err;
9419 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009420 free(proxy->conf.lfs_file);
9421 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9422 proxy->conf.lfs_line = proxy->conf.args.line;
9423
9424 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009425 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9426 char *errmsg = NULL;
9427 cur_arg = 1;
9428 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009429 rule->from = ACT_F_HTTP_REQ;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009430 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009431 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009432 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9433 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9434 free(errmsg);
9435 goto out_err;
9436 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009437 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009438 action_build_list(&http_req_keywords.list, &trash);
9439 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', "
9440 "'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009441 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02009442 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009443 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009444 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009445 }
9446
9447 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9448 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009449 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009450
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009451 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9452 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9453 file, linenum, args[0], errmsg);
9454 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009455 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009456 }
9457 rule->cond = cond;
9458 }
9459 else if (*args[cur_arg]) {
9460 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9461 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9462 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009463 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009464 }
9465
9466 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009467 out_err:
9468 free(rule);
9469 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009470}
9471
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009472/* parse an "http-respose" rule */
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009473struct act_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009474{
Thierry FOURNIERa28a9422015-08-04 19:35:46 +02009475 struct act_rule *rule;
Thierry FOURNIER36481b82015-08-19 09:01:53 +02009476 struct action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009477 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009478 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009479
9480 rule = calloc(1, sizeof(*rule));
9481 if (!rule) {
9482 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9483 goto out_err;
9484 }
9485
9486 if (!strcmp(args[0], "allow")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009487 rule->action = ACT_ACTION_ALLOW;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009488 cur_arg = 1;
9489 } else if (!strcmp(args[0], "deny")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009490 rule->action = ACT_ACTION_DENY;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009491 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009492 } else if (!strcmp(args[0], "set-nice")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009493 rule->action = ACT_HTTP_SET_NICE;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009494 cur_arg = 1;
9495
9496 if (!*args[cur_arg] ||
9497 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9498 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9499 file, linenum, args[0]);
9500 goto out_err;
9501 }
9502 rule->arg.nice = atoi(args[cur_arg]);
9503 if (rule->arg.nice < -1024)
9504 rule->arg.nice = -1024;
9505 else if (rule->arg.nice > 1024)
9506 rule->arg.nice = 1024;
9507 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009508 } else if (!strcmp(args[0], "set-tos")) {
9509#ifdef IP_TOS
9510 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009511 rule->action = ACT_HTTP_SET_TOS;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009512 cur_arg = 1;
9513
9514 if (!*args[cur_arg] ||
9515 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9516 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9517 file, linenum, args[0]);
9518 goto out_err;
9519 }
9520
9521 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9522 if (err && *err != '\0') {
9523 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9524 file, linenum, err, args[0]);
9525 goto out_err;
9526 }
9527 cur_arg++;
9528#else
9529 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9530 goto out_err;
9531#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009532 } else if (!strcmp(args[0], "set-mark")) {
9533#ifdef SO_MARK
9534 char *err;
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009535 rule->action = ACT_HTTP_SET_MARK;
Willy Tarreau51347ed2013-06-11 19:34:13 +02009536 cur_arg = 1;
9537
9538 if (!*args[cur_arg] ||
9539 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9540 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9541 file, linenum, args[0]);
9542 goto out_err;
9543 }
9544
9545 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9546 if (err && *err != '\0') {
9547 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9548 file, linenum, err, args[0]);
9549 goto out_err;
9550 }
9551 cur_arg++;
9552 global.last_checks |= LSTCHK_NETADM;
9553#else
9554 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9555 goto out_err;
9556#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009557 } else if (!strcmp(args[0], "set-log-level")) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009558 rule->action = ACT_HTTP_SET_LOGL;
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009559 cur_arg = 1;
9560
9561 if (!*args[cur_arg] ||
9562 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9563 bad_log_level:
9564 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9565 file, linenum, args[0]);
9566 goto out_err;
9567 }
9568 if (strcmp(args[cur_arg], "silent") == 0)
9569 rule->arg.loglevel = -1;
Ruoshan Huangdd016782016-06-15 22:16:03 +08009570 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009571 goto bad_log_level;
9572 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009573 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009574 rule->action = *args[0] == 'a' ? ACT_HTTP_ADD_HDR : ACT_HTTP_SET_HDR;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009575 cur_arg = 1;
9576
9577 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9578 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9579 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9580 file, linenum, args[0]);
9581 goto out_err;
9582 }
9583
9584 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9585 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9586 LIST_INIT(&rule->arg.hdr_add.fmt);
9587
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009588 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009589 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009590 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 +01009591 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9592 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9593 file, linenum, args[0], error);
9594 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009595 goto out_err;
9596 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009597 free(proxy->conf.lfs_file);
9598 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9599 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009600 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009601 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009602 rule->action = args[0][8] == 'h' ? ACT_HTTP_REPLACE_HDR : ACT_HTTP_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009603 cur_arg = 1;
9604
9605 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009606 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9607 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009608 file, linenum, args[0]);
9609 goto out_err;
9610 }
9611
9612 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9613 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9614 LIST_INIT(&rule->arg.hdr_add.fmt);
9615
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009616 error = NULL;
9617 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9618 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9619 args[cur_arg + 1], error);
9620 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009621 goto out_err;
9622 }
9623
9624 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009625 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009626 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 +01009627 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9628 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9629 file, linenum, args[0], error);
9630 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009631 goto out_err;
9632 }
Sasha Pachev218f0642014-06-16 12:05:59 -06009633
9634 free(proxy->conf.lfs_file);
9635 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9636 proxy->conf.lfs_line = proxy->conf.args.line;
9637 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009638 } else if (strcmp(args[0], "del-header") == 0) {
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009639 rule->action = ACT_HTTP_DEL_HDR;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009640 cur_arg = 1;
9641
9642 if (!*args[cur_arg] ||
9643 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9644 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9645 file, linenum, args[0]);
9646 goto out_err;
9647 }
9648
9649 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9650 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9651
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009652 proxy->conf.args.ctx = ARGC_HRS;
9653 free(proxy->conf.lfs_file);
9654 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9655 proxy->conf.lfs_line = proxy->conf.args.line;
9656 cur_arg += 1;
9657 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9658 /* http-request add-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009659 rule->action = ACT_HTTP_ADD_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009660 /*
9661 * '+ 8' for 'add-acl('
9662 * '- 9' for 'add-acl(' + trailing ')'
9663 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009664 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009665
9666 cur_arg = 1;
9667
9668 if (!*args[cur_arg] ||
9669 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9670 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9671 file, linenum, args[0]);
9672 goto out_err;
9673 }
9674
9675 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009676 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009677 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009678 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009679 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9680 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9681 file, linenum, args[0], error);
9682 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009683 goto out_err;
9684 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009685 free(proxy->conf.lfs_file);
9686 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9687 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009688
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009689 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009690 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9691 /* http-response del-acl(<reference (acl name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009692 rule->action = ACT_HTTP_DEL_ACL;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009693 /*
9694 * '+ 8' for 'del-acl('
9695 * '- 9' for 'del-acl(' + trailing ')'
9696 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009697 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009698
9699 cur_arg = 1;
9700
9701 if (!*args[cur_arg] ||
9702 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9703 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9704 file, linenum, args[0]);
9705 goto out_err;
9706 }
9707
9708 LIST_INIT(&rule->arg.map.key);
9709 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009710 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009711 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009712 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9713 Alert("parsing [%s:%d]: 'http-response %s': %s.\n",
9714 file, linenum, args[0], error);
9715 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009716 goto out_err;
9717 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009718 free(proxy->conf.lfs_file);
9719 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9720 proxy->conf.lfs_line = proxy->conf.args.line;
9721 cur_arg += 1;
9722 } else if (strncmp(args[0], "del-map", 7) == 0) {
9723 /* http-response del-map(<reference (map name)>) <key pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009724 rule->action = ACT_HTTP_DEL_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009725 /*
9726 * '+ 8' for 'del-map('
9727 * '- 9' for 'del-map(' + trailing ')'
9728 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009729 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009730
9731 cur_arg = 1;
9732
9733 if (!*args[cur_arg] ||
9734 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9735 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9736 file, linenum, args[0]);
9737 goto out_err;
9738 }
9739
9740 LIST_INIT(&rule->arg.map.key);
9741 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009742 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009743 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009744 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9745 Alert("parsing [%s:%d]: 'http-response %s' %s.\n",
9746 file, linenum, args[0], error);
9747 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009748 goto out_err;
9749 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009750 free(proxy->conf.lfs_file);
9751 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9752 proxy->conf.lfs_line = proxy->conf.args.line;
9753 cur_arg += 1;
9754 } else if (strncmp(args[0], "set-map", 7) == 0) {
9755 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009756 rule->action = ACT_HTTP_SET_MAP;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009757 /*
9758 * '+ 8' for 'set-map('
9759 * '- 9' for 'set-map(' + trailing ')'
9760 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009761 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009762
9763 cur_arg = 1;
9764
9765 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9766 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9767 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9768 file, linenum, args[0]);
9769 goto out_err;
9770 }
9771
9772 LIST_INIT(&rule->arg.map.key);
9773 LIST_INIT(&rule->arg.map.value);
9774
9775 proxy->conf.args.ctx = ARGC_HRS;
9776
9777 /* key pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009778 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009779 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009780 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9781 Alert("parsing [%s:%d]: 'http-response %s' name: %s.\n",
9782 file, linenum, args[0], error);
9783 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009784 goto out_err;
9785 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009786
9787 /* value pattern */
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009788 error = NULL;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009789 if (!parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +01009790 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR, &error)) {
9791 Alert("parsing [%s:%d]: 'http-response %s' value: %s.\n",
9792 file, linenum, args[0], error);
9793 free(error);
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +01009794 goto out_err;
9795 }
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009796
9797 free(proxy->conf.lfs_file);
9798 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9799 proxy->conf.lfs_line = proxy->conf.args.line;
9800
9801 cur_arg += 2;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009802 } else if (strcmp(args[0], "redirect") == 0) {
9803 struct redirect_rule *redir;
9804 char *errmsg = NULL;
9805
9806 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1, 1)) == NULL) {
9807 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9808 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9809 goto out_err;
9810 }
9811
9812 /* this redirect rule might already contain a parsed condition which
9813 * we'll pass to the http-request rule.
9814 */
Thierry FOURNIER0ea5c7f2015-08-05 19:05:19 +02009815 rule->action = ACT_HTTP_REDIR;
Willy Tarreau51d861a2015-05-22 17:30:48 +02009816 rule->arg.redir = redir;
9817 rule->cond = redir->cond;
9818 redir->cond = NULL;
9819 cur_arg = 2;
9820 return rule;
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009821 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9822 args[0][9] == '\0' && args[0][8] >= '0' &&
9823 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
9824 struct sample_expr *expr;
9825 unsigned int where;
9826 char *err = NULL;
9827
9828 cur_arg = 1;
9829 proxy->conf.args.ctx = ARGC_TRK;
9830
9831 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9832 if (!expr) {
9833 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9834 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9835 free(err);
9836 goto out_err;
9837 }
9838
9839 where = 0;
9840 if (proxy->cap & PR_CAP_FE)
9841 where |= SMP_VAL_FE_HRS_HDR;
9842 if (proxy->cap & PR_CAP_BE)
9843 where |= SMP_VAL_BE_HRS_HDR;
9844
9845 if (!(expr->fetch->val & where)) {
9846 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule :"
9847 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9848 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9849 args[cur_arg-1], sample_src_names(expr->fetch->use));
9850 free(expr);
9851 goto out_err;
9852 }
9853
9854 if (strcmp(args[cur_arg], "table") == 0) {
9855 cur_arg++;
9856 if (!args[cur_arg]) {
9857 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : missing table name.\n",
9858 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9859 free(expr);
9860 goto out_err;
9861 }
9862 /* we copy the table name for now, it will be resolved later */
9863 rule->arg.trk_ctr.table.n = strdup(args[cur_arg]);
9864 cur_arg++;
9865 }
9866 rule->arg.trk_ctr.expr = expr;
9867 rule->action = ACT_ACTION_TRK_SC0 + args[0][8] - '0';
William Lallemand73025dd2014-04-24 14:38:37 +02009868 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9869 char *errmsg = NULL;
9870 cur_arg = 1;
9871 /* try in the module list */
Thierry FOURNIER5563e4b2015-08-14 19:20:07 +02009872 rule->from = ACT_F_HTTP_RES;
Thierry FOURNIER85c6c972015-09-22 19:14:35 +02009873 rule->kw = custom;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02009874 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) == ACT_RET_PRS_ERR) {
William Lallemand73025dd2014-04-24 14:38:37 +02009875 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9876 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9877 free(errmsg);
9878 goto out_err;
9879 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009880 } else {
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009881 action_build_list(&http_res_keywords.list, &trash);
9882 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', "
9883 "'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', "
Ruoshan Huange4edc6b2016-07-14 15:07:45 +08009884 "'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', 'track-sc*'"
William Lallemand2e785f22016-05-25 01:48:42 +02009885 "%s%s, but got '%s'%s.\n",
Thierry FOURNIERab95e652015-10-02 08:24:51 +02009886 file, linenum, *trash.str ? ", " : "", trash.str, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009887 goto out_err;
9888 }
9889
9890 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9891 struct acl_cond *cond;
9892 char *errmsg = NULL;
9893
9894 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9895 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9896 file, linenum, args[0], errmsg);
9897 free(errmsg);
9898 goto out_err;
9899 }
9900 rule->cond = cond;
9901 }
9902 else if (*args[cur_arg]) {
9903 Alert("parsing [%s:%d]: 'http-response %s' expects"
9904 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9905 file, linenum, args[0], args[cur_arg]);
9906 goto out_err;
9907 }
9908
9909 return rule;
9910 out_err:
9911 free(rule);
9912 return NULL;
9913}
9914
Willy Tarreau4baae242012-12-27 12:00:31 +01009915/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009916 * with <err> filled with the error message. If <use_fmt> is not null, builds a
Willy Tarreaube4653b2015-05-28 15:26:58 +02009917 * dynamic log-format rule instead of a static string. Parameter <dir> indicates
9918 * the direction of the rule, and equals 0 for request, non-zero for responses.
Willy Tarreau4baae242012-12-27 12:00:31 +01009919 */
9920struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Willy Tarreaube4653b2015-05-28 15:26:58 +02009921 const char **args, char **errmsg, int use_fmt, int dir)
Willy Tarreau4baae242012-12-27 12:00:31 +01009922{
9923 struct redirect_rule *rule;
9924 int cur_arg;
9925 int type = REDIRECT_TYPE_NONE;
9926 int code = 302;
9927 const char *destination = NULL;
9928 const char *cookie = NULL;
9929 int cookie_set = 0;
9930 unsigned int flags = REDIRECT_FLAG_NONE;
9931 struct acl_cond *cond = NULL;
9932
9933 cur_arg = 0;
9934 while (*(args[cur_arg])) {
9935 if (strcmp(args[cur_arg], "location") == 0) {
9936 if (!*args[cur_arg + 1])
9937 goto missing_arg;
9938
9939 type = REDIRECT_TYPE_LOCATION;
9940 cur_arg++;
9941 destination = args[cur_arg];
9942 }
9943 else if (strcmp(args[cur_arg], "prefix") == 0) {
9944 if (!*args[cur_arg + 1])
9945 goto missing_arg;
Willy Tarreau4baae242012-12-27 12:00:31 +01009946 type = REDIRECT_TYPE_PREFIX;
9947 cur_arg++;
9948 destination = args[cur_arg];
9949 }
9950 else if (strcmp(args[cur_arg], "scheme") == 0) {
9951 if (!*args[cur_arg + 1])
9952 goto missing_arg;
9953
9954 type = REDIRECT_TYPE_SCHEME;
9955 cur_arg++;
9956 destination = args[cur_arg];
9957 }
9958 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9959 if (!*args[cur_arg + 1])
9960 goto missing_arg;
9961
9962 cur_arg++;
9963 cookie = args[cur_arg];
9964 cookie_set = 1;
9965 }
9966 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9967 if (!*args[cur_arg + 1])
9968 goto missing_arg;
9969
9970 cur_arg++;
9971 cookie = args[cur_arg];
9972 cookie_set = 0;
9973 }
9974 else if (strcmp(args[cur_arg], "code") == 0) {
9975 if (!*args[cur_arg + 1])
9976 goto missing_arg;
9977
9978 cur_arg++;
9979 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009980 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009981 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009982 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009983 args[cur_arg - 1], args[cur_arg]);
9984 return NULL;
9985 }
9986 }
9987 else if (!strcmp(args[cur_arg],"drop-query")) {
9988 flags |= REDIRECT_FLAG_DROP_QS;
9989 }
9990 else if (!strcmp(args[cur_arg],"append-slash")) {
9991 flags |= REDIRECT_FLAG_APPEND_SLASH;
9992 }
9993 else if (strcmp(args[cur_arg], "if") == 0 ||
9994 strcmp(args[cur_arg], "unless") == 0) {
9995 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9996 if (!cond) {
9997 memprintf(errmsg, "error in condition: %s", *errmsg);
9998 return NULL;
9999 }
10000 break;
10001 }
10002 else {
10003 memprintf(errmsg,
10004 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
10005 args[cur_arg]);
10006 return NULL;
10007 }
10008 cur_arg++;
10009 }
10010
10011 if (type == REDIRECT_TYPE_NONE) {
10012 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
10013 return NULL;
10014 }
10015
Willy Tarreaube4653b2015-05-28 15:26:58 +020010016 if (dir && type != REDIRECT_TYPE_LOCATION) {
10017 memprintf(errmsg, "response only supports redirect type 'location'");
10018 return NULL;
10019 }
10020
Vincent Bernat3c2f2f22016-04-03 13:48:42 +020010021 rule = calloc(1, sizeof(*rule));
Willy Tarreau4baae242012-12-27 12:00:31 +010010022 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +010010023 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010024
10025 if (!use_fmt) {
10026 /* old-style static redirect rule */
10027 rule->rdr_str = strdup(destination);
10028 rule->rdr_len = strlen(destination);
10029 }
10030 else {
10031 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010032
10033 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
10034 * if prefix == "/", we don't want to add anything, otherwise it
10035 * makes it hard for the user to configure a self-redirection.
10036 */
Godbachd9722032014-12-18 15:44:58 +080010037 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010038 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010010039 if (!parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
10040 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 +010010041 : (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
10042 errmsg)) {
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010010043 return NULL;
10044 }
Willy Tarreau59ad1a22014-01-29 14:39:58 +010010045 free(curproxy->conf.lfs_file);
10046 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
10047 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +010010048 }
10049 }
10050
Willy Tarreau4baae242012-12-27 12:00:31 +010010051 if (cookie) {
10052 /* depending on cookie_set, either we want to set the cookie, or to clear it.
10053 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
10054 */
10055 rule->cookie_len = strlen(cookie);
10056 if (cookie_set) {
10057 rule->cookie_str = malloc(rule->cookie_len + 10);
10058 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10059 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
10060 rule->cookie_len += 9;
10061 } else {
10062 rule->cookie_str = malloc(rule->cookie_len + 21);
10063 memcpy(rule->cookie_str, cookie, rule->cookie_len);
10064 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
10065 rule->cookie_len += 20;
10066 }
10067 }
10068 rule->type = type;
10069 rule->code = code;
10070 rule->flags = flags;
10071 LIST_INIT(&rule->list);
10072 return rule;
10073
10074 missing_arg:
10075 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
10076 return NULL;
10077}
10078
Willy Tarreau8797c062007-05-07 00:55:35 +020010079/************************************************************************/
10080/* The code below is dedicated to ACL parsing and matching */
10081/************************************************************************/
10082
10083
Willy Tarreau14174bc2012-04-16 14:34:04 +020010084/* This function ensures that the prerequisites for an L7 fetch are ready,
10085 * which means that a request or response is ready. If some data is missing,
10086 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +020010087 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
10088 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010089 *
10090 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +020010091 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
10092 * decide whether or not an HTTP message is present ;
10093 * 0 if the requested data cannot be fetched or if it is certain that
10094 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010095 * 1 if an HTTP message is ready
10096 */
James Rosewell91a41cb2015-09-18 17:11:16 +010010097int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010098 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +020010099{
Willy Tarreau192252e2015-04-04 01:47:55 +020010100 struct http_txn *txn;
10101 struct http_msg *msg;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010102
Willy Tarreaube508f12016-03-10 11:47:01 +010010103 /* Note: it is possible that <s> is NULL when called before stream
10104 * initialization (eg: tcp-request connection), so this function is the
10105 * one responsible for guarding against this case for all HTTP users.
Willy Tarreau14174bc2012-04-16 14:34:04 +020010106 */
Willy Tarreau192252e2015-04-04 01:47:55 +020010107 if (!s)
10108 return 0;
Willy Tarreaube508f12016-03-10 11:47:01 +010010109
Thierry FOURNIERed08d6a2015-09-24 08:40:18 +020010110 if (!s->txn) {
10111 if (unlikely(!http_alloc_txn(s)))
10112 return 0; /* not enough memory */
10113 http_init_txn(s);
10114 }
Willy Tarreau192252e2015-04-04 01:47:55 +020010115 txn = s->txn;
Willy Tarreau192252e2015-04-04 01:47:55 +020010116 msg = &txn->req;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010117
10118 /* Check for a dependency on a request */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010119 smp->data.type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010120
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010121 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010122 /* If the buffer does not leave enough free space at the end,
10123 * we must first realign it.
10124 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010125 if (s->req.buf->p > s->req.buf->data &&
10126 s->req.buf->i + s->req.buf->p > s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)
10127 buffer_slow_realign(s->req.buf);
Willy Tarreauaae75e32013-03-29 12:31:49 +010010128
Willy Tarreau14174bc2012-04-16 14:34:04 +020010129 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +020010130 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +020010131 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010132
10133 /* Try to decode HTTP request */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010134 if (likely(msg->next < s->req.buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +020010135 http_msg_analyzer(msg, &txn->hdr_idx);
10136
10137 /* Still no valid request ? */
10138 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +020010139 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010140 buffer_full(s->req.buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +020010141 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010142 }
10143 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +020010144 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010145 return 0;
10146 }
10147
10148 /* OK we just got a valid HTTP request. We have some minor
10149 * preparation to perform so that further checks can rely
10150 * on HTTP tests.
10151 */
Willy Tarreauaae75e32013-03-29 12:31:49 +010010152
10153 /* If the request was parsed but was too large, we must absolutely
10154 * return an error so that it is not processed. At the moment this
10155 * cannot happen, but if the parsers are to change in the future,
10156 * we want this check to be maintained.
10157 */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010010158 if (unlikely(s->req.buf->i + s->req.buf->p >
10159 s->req.buf->data + s->req.buf->size - global.tune.maxrewrite)) {
Willy Tarreauaae75e32013-03-29 12:31:49 +010010160 msg->msg_state = HTTP_MSG_ERROR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010161 smp->data.u.sint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +010010162 return 1;
10163 }
10164
Willy Tarreau9b28e032012-10-12 23:49:43 +020010165 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +020010166 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
Willy Tarreaue7dff022015-04-03 01:14:29 +020010167 s->flags |= SF_REDIRECTABLE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010168
Willy Tarreau506d0502013-07-06 13:29:24 +020010169 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
10170 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010171 }
10172
Willy Tarreau506d0502013-07-06 13:29:24 +020010173 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +020010174 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +020010175 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010176
10177 /* otherwise everything's ready for the request */
10178 }
Willy Tarreau24e32d82012-04-23 23:55:44 +020010179 else {
10180 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +020010181 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
10182 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010183 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +020010184 }
Willy Tarreau14174bc2012-04-16 14:34:04 +020010185 }
10186
10187 /* everything's OK */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010188 smp->data.u.sint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +020010189 return 1;
10190}
Willy Tarreau8797c062007-05-07 00:55:35 +020010191
Willy Tarreau8797c062007-05-07 00:55:35 +020010192/* 1. Check on METHOD
10193 * We use the pre-parsed method if it is known, and store its number as an
10194 * integer. If it is unknown, we use the pointer and the length.
10195 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010196static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +020010197{
10198 int len, meth;
10199
Thierry FOURNIER580c32c2014-01-24 10:58:12 +010010200 len = strlen(text);
10201 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +020010202
10203 pattern->val.i = meth;
10204 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +020010205 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +020010206 pattern->len = len;
10207 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010208 else {
10209 pattern->ptr.str = NULL;
10210 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010211 }
Willy Tarreau8797c062007-05-07 00:55:35 +020010212 return 1;
10213}
10214
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010215/* This function fetches the method of current HTTP request and stores
10216 * it in the global pattern struct as a chunk. There are two possibilities :
10217 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
10218 * in <len> and <ptr> is NULL ;
10219 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
10220 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +010010221 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010222 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010223static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010224smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010225{
10226 int meth;
Willy Tarreaube508f12016-03-10 11:47:01 +010010227 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010228
Willy Tarreau24e32d82012-04-23 23:55:44 +020010229 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010230
Willy Tarreaube508f12016-03-10 11:47:01 +010010231 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010232 meth = txn->meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010233 smp->data.type = SMP_T_METH;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010234 smp->data.u.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +020010235 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +020010236 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
10237 /* ensure the indexes are not affected */
10238 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010239 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010240 smp->data.u.meth.str.len = txn->req.sl.rq.m_l;
10241 smp->data.u.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010242 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010243 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010244 return 1;
10245}
10246
Willy Tarreau8e5e9552011-12-16 15:38:49 +010010247/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010248static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +020010249{
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010250 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010251 struct pattern_list *lst;
10252 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010253
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010254 list_for_each_entry(lst, &expr->patterns, list) {
10255 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +020010256
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010257 /* well-known method */
10258 if (pattern->val.i != HTTP_METH_OTHER) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010259 if (smp->data.u.meth.meth == pattern->val.i)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010260 return pattern;
10261 else
10262 continue;
10263 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +020010264
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010265 /* Other method, we must compare the strings */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010266 if (pattern->len != smp->data.u.meth.str.len)
Thierry FOURNIER5338eea2013-12-16 14:22:13 +010010267 continue;
10268
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +020010269 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010270 if ((icase && strncasecmp(pattern->ptr.str, smp->data.u.meth.str.str, smp->data.u.meth.str.len) == 0) ||
10271 (!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 +010010272 return pattern;
10273 }
10274 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +020010275}
10276
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010277static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010278smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010279{
Willy Tarreaube508f12016-03-10 11:47:01 +010010280 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010281 char *ptr;
10282 int len;
10283
Willy Tarreauc0239e02012-04-16 14:42:55 +020010284 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010285
Willy Tarreaube508f12016-03-10 11:47:01 +010010286 txn = smp->strm->txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010287 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010288 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +020010289
10290 while ((len-- > 0) && (*ptr++ != '/'));
10291 if (len <= 0)
10292 return 0;
10293
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010294 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010295 smp->data.u.str.str = ptr;
10296 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010297
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010298 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010299 return 1;
10300}
10301
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010302static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010303smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010304{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010305 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010306 char *ptr;
10307 int len;
10308
Willy Tarreauc0239e02012-04-16 14:42:55 +020010309 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010310
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010311 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010312 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10313 return 0;
10314
Willy Tarreau8797c062007-05-07 00:55:35 +020010315 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010316 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010317
10318 while ((len-- > 0) && (*ptr++ != '/'));
10319 if (len <= 0)
10320 return 0;
10321
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010322 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010323 smp->data.u.str.str = ptr;
10324 smp->data.u.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010325
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010326 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010327 return 1;
10328}
10329
10330/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010331static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010332smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010333{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010334 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010335 char *ptr;
10336 int len;
10337
Willy Tarreauc0239e02012-04-16 14:42:55 +020010338 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010339
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010340 txn = smp->strm->txn;
Willy Tarreauf26b2522012-12-14 08:33:14 +010010341 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10342 return 0;
10343
Willy Tarreau8797c062007-05-07 00:55:35 +020010344 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010345 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010346
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010347 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010348 smp->data.u.sint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010349 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010350 return 1;
10351}
10352
Thierry Fournierf4011dd2016-03-29 17:23:51 +020010353static int
10354smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
10355{
10356 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
10357 return 0;
10358
10359 if (!smp->strm->unique_id) {
10360 if ((smp->strm->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
10361 return 0;
10362 smp->strm->unique_id[0] = '\0';
10363 }
10364 smp->data.u.str.len = build_logline(smp->strm, smp->strm->unique_id,
10365 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
10366
10367 smp->data.type = SMP_T_STR;
10368 smp->data.u.str.str = smp->strm->unique_id;
10369 smp->flags = SMP_F_CONST;
10370 return 1;
10371}
10372
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010373/* returns the longest available part of the body. This requires that the body
10374 * has been waited for using http-buffer-request.
10375 */
10376static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010377smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010378{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010379 struct http_msg *msg;
10380 unsigned long len;
10381 unsigned long block1;
10382 char *body;
10383 struct chunk *temp;
10384
10385 CHECK_HTTP_MESSAGE_FIRST();
10386
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010387 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010388 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010389 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010390 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010391
10392 len = http_body_bytes(msg);
10393 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
10394
10395 block1 = len;
10396 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
10397 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
10398
10399 if (block1 == len) {
10400 /* buffer is not wrapped (or empty) */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010401 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010402 smp->data.u.str.str = body;
10403 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010404 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
10405 }
10406 else {
10407 /* buffer is wrapped, we need to defragment it */
10408 temp = get_trash_chunk();
10409 memcpy(temp->str, body, block1);
10410 memcpy(temp->str + block1, msg->chn->buf->data, len - block1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010411 smp->data.type = SMP_T_BIN;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010412 smp->data.u.str.str = temp->str;
10413 smp->data.u.str.len = len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010414 smp->flags = SMP_F_VOL_TEST;
10415 }
10416 return 1;
10417}
10418
10419
10420/* returns the available length of the body. This requires that the body
10421 * has been waited for using http-buffer-request.
10422 */
10423static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010424smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010425{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010426 struct http_msg *msg;
10427
10428 CHECK_HTTP_MESSAGE_FIRST();
10429
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010430 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010431 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010432 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010433 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010434
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010435 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010436 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010437
10438 smp->flags = SMP_F_VOL_TEST;
10439 return 1;
10440}
10441
10442
10443/* returns the advertised length of the body, or the advertised size of the
10444 * chunks available in the buffer. This requires that the body has been waited
10445 * for using http-buffer-request.
10446 */
10447static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010448smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010449{
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010450 struct http_msg *msg;
10451
10452 CHECK_HTTP_MESSAGE_FIRST();
10453
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010454 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010010455 msg = &smp->strm->txn->req;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010456 else
Willy Tarreaube508f12016-03-10 11:47:01 +010010457 msg = &smp->strm->txn->rsp;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010458
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010459 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010460 smp->data.u.sint = msg->body_len;
Willy Tarreaua5910cc2015-05-02 00:46:08 +020010461
10462 smp->flags = SMP_F_VOL_TEST;
10463 return 1;
10464}
10465
10466
Willy Tarreau8797c062007-05-07 00:55:35 +020010467/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010468static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010469smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8797c062007-05-07 00:55:35 +020010470{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010471 struct http_txn *txn;
Willy Tarreau8797c062007-05-07 00:55:35 +020010472
Willy Tarreauc0239e02012-04-16 14:42:55 +020010473 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010474
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010475 txn = smp->strm->txn;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010476 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010477 smp->data.u.str.len = txn->req.sl.rq.u_l;
10478 smp->data.u.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010479 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010480 return 1;
10481}
10482
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010483static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010484smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010485{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010486 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010487 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010488
Willy Tarreauc0239e02012-04-16 14:42:55 +020010489 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010490
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010491 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010492 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 +020010493 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010494 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010495
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010496 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010497 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010498 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010499 return 1;
10500}
10501
10502static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010503smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010504{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010505 struct http_txn *txn;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010506 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010507
Willy Tarreauc0239e02012-04-16 14:42:55 +020010508 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010509
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010510 txn = smp->strm->txn;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010511 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 +020010512 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10513 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010514
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010515 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010516 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010517 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010518 return 1;
10519}
10520
Willy Tarreau185b5c42012-04-26 15:11:51 +020010521/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10522 * Accepts an optional argument of type string containing the header field name,
10523 * and an optional argument of type signed or unsigned integer to request an
10524 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010525 * headers are considered from the first one. It does not stop on commas and
10526 * returns full lines instead (useful for User-Agent or Date for example).
10527 */
10528static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010529smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010530{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010531 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010532 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010533 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010534 int occ = 0;
10535 const char *name_str = NULL;
10536 int name_len = 0;
10537
10538 if (!ctx) {
10539 /* first call */
10540 ctx = &static_hdr_ctx;
10541 ctx->idx = 0;
10542 smp->ctx.a[0] = ctx;
10543 }
10544
10545 if (args) {
10546 if (args[0].type != ARGT_STR)
10547 return 0;
10548 name_str = args[0].data.str.str;
10549 name_len = args[0].data.str.len;
10550
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010551 if (args[1].type == ARGT_SINT)
10552 occ = args[1].data.sint;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010553 }
10554
10555 CHECK_HTTP_MESSAGE_FIRST();
10556
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010557 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010558 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 +020010559
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010560 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10561 /* search for header from the beginning */
10562 ctx->idx = 0;
10563
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010564 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010565 /* no explicit occurrence and single fetch => last header by default */
10566 occ = -1;
10567
10568 if (!occ)
10569 /* prepare to report multiple occurrences for ACL fetches */
10570 smp->flags |= SMP_F_NOT_LAST;
10571
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010572 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010573 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010574 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 +020010575 return 1;
10576
10577 smp->flags &= ~SMP_F_NOT_LAST;
10578 return 0;
10579}
10580
10581/* 6. Check on HTTP header count. The number of occurrences is returned.
10582 * Accepts exactly 1 argument of type string. It does not stop on commas and
10583 * returns full lines instead (useful for User-Agent or Date for example).
10584 */
10585static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010586smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010587{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010588 struct hdr_idx *idx;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010589 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010590 const struct http_msg *msg;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010591 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010592 const char *name = NULL;
10593 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010594
Willy Tarreau601a4d12015-04-01 19:16:09 +020010595 if (args && args->type == ARGT_STR) {
10596 name = args->data.str.str;
10597 len = args->data.str.len;
10598 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010599
10600 CHECK_HTTP_MESSAGE_FIRST();
10601
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010602 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010603 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 +020010604
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010605 ctx.idx = 0;
10606 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010607 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010608 cnt++;
10609
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010610 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010611 smp->data.u.sint = cnt;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010612 smp->flags = SMP_F_VOL_HDR;
10613 return 1;
10614}
10615
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010616static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010617smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010618{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010619 struct hdr_idx *idx;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010620 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010621 const struct http_msg *msg;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010622 struct chunk *temp;
10623 char del = ',';
10624
10625 if (args && args->type == ARGT_STR)
10626 del = *args[0].data.str.str;
10627
10628 CHECK_HTTP_MESSAGE_FIRST();
10629
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010630 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010631 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 +020010632
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010633 temp = get_trash_chunk();
10634
10635 ctx.idx = 0;
10636 while (http_find_next_header(msg->chn->buf->p, idx, &ctx)) {
10637 if (temp->len)
10638 temp->str[temp->len++] = del;
10639 memcpy(temp->str + temp->len, ctx.line, ctx.del);
10640 temp->len += ctx.del;
10641 }
10642
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010643 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010644 smp->data.u.str.str = temp->str;
10645 smp->data.u.str.len = temp->len;
Willy Tarreaueb27ec72015-02-20 13:55:29 +010010646 smp->flags = SMP_F_VOL_HDR;
10647 return 1;
10648}
10649
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010650/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10651 * Accepts an optional argument of type string containing the header field name,
10652 * and an optional argument of type signed or unsigned integer to request an
10653 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010654 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010655 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010656static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010657smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010658{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010659 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010660 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau15e91e12015-04-04 00:52:09 +020010661 const struct http_msg *msg;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010662 int occ = 0;
10663 const char *name_str = NULL;
10664 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010665
Willy Tarreaua890d072013-04-02 12:01:06 +020010666 if (!ctx) {
10667 /* first call */
10668 ctx = &static_hdr_ctx;
10669 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010670 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010671 }
10672
Willy Tarreau185b5c42012-04-26 15:11:51 +020010673 if (args) {
10674 if (args[0].type != ARGT_STR)
10675 return 0;
10676 name_str = args[0].data.str.str;
10677 name_len = args[0].data.str.len;
10678
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020010679 if (args[1].type == ARGT_SINT)
10680 occ = args[1].data.sint;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010681 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010682
Willy Tarreaue333ec92012-04-16 16:26:40 +020010683 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010684
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010685 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010686 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010687
Willy Tarreau185b5c42012-04-26 15:11:51 +020010688 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010689 /* search for header from the beginning */
10690 ctx->idx = 0;
10691
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010692 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau185b5c42012-04-26 15:11:51 +020010693 /* no explicit occurrence and single fetch => last header by default */
10694 occ = -1;
10695
10696 if (!occ)
10697 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010698 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010699
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010700 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010701 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010702 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 +020010703 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010704
Willy Tarreau37406352012-04-23 16:16:37 +020010705 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010706 return 0;
10707}
10708
Willy Tarreauc11416f2007-06-17 16:58:38 +020010709/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010710 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010711 */
10712static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010713smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010714{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010715 struct hdr_idx *idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010716 struct hdr_ctx ctx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020010717 const struct http_msg *msg;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010718 int cnt;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010719 const char *name = NULL;
10720 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010721
Willy Tarreau601a4d12015-04-01 19:16:09 +020010722 if (args && args->type == ARGT_STR) {
10723 name = args->data.str.str;
10724 len = args->data.str.len;
10725 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010726
Willy Tarreaue333ec92012-04-16 16:26:40 +020010727 CHECK_HTTP_MESSAGE_FIRST();
10728
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010729 idx = &smp->strm->txn->hdr_idx;
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010730 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 +020010731
Willy Tarreau33a7e692007-06-10 19:45:56 +020010732 ctx.idx = 0;
10733 cnt = 0;
Willy Tarreau601a4d12015-04-01 19:16:09 +020010734 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010735 cnt++;
10736
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010737 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010738 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010739 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010740 return 1;
10741}
10742
Willy Tarreau185b5c42012-04-26 15:11:51 +020010743/* Fetch an HTTP header's integer value. The integer value is returned. It
10744 * takes a mandatory argument of type string and an optional one of type int
10745 * to designate a specific occurrence. It returns an unsigned integer, which
10746 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010747 */
10748static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010749smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010750{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010751 int ret = smp_fetch_hdr(args, smp, kw, private);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010752
Willy Tarreauf853c462012-04-23 18:53:56 +020010753 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010754 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010755 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreauf853c462012-04-23 18:53:56 +020010756 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010757
Willy Tarreaud53e2422012-04-16 17:21:11 +020010758 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010759}
10760
Cyril Bonté69fa9922012-10-25 00:01:06 +020010761/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10762 * and an optional one of type int to designate a specific occurrence.
10763 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010764 */
10765static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010766smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau106f9792009-09-19 07:54:16 +020010767{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010768 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010769
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010770 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010771 if (url2ipv4((char *)smp->data.u.str.str, &smp->data.u.ipv4)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010772 smp->data.type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010773 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010774 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010775 struct chunk *temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010776 if (smp->data.u.str.len < temp->size - 1) {
10777 memcpy(temp->str, smp->data.u.str.str, smp->data.u.str.len);
10778 temp->str[smp->data.u.str.len] = '\0';
10779 if (inet_pton(AF_INET6, temp->str, &smp->data.u.ipv6)) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010780 smp->data.type = SMP_T_IPV6;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010781 break;
10782 }
10783 }
10784 }
10785
Willy Tarreaud53e2422012-04-16 17:21:11 +020010786 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010787 if (!(smp->flags & SMP_F_NOT_LAST))
10788 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010789 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010790 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010791}
10792
Willy Tarreau737b0c12007-06-10 21:28:46 +020010793/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10794 * the first '/' after the possible hostname, and ends before the possible '?'.
10795 */
10796static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010797smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010798{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010799 struct http_txn *txn;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010800 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010801
Willy Tarreauc0239e02012-04-16 14:42:55 +020010802 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010803
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010804 txn = smp->strm->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010805 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010806 ptr = http_get_path(txn);
10807 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010808 return 0;
10809
10810 /* OK, we got the '/' ! */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010811 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010812 smp->data.u.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010813
10814 while (ptr < end && *ptr != '?')
10815 ptr++;
10816
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010817 smp->data.u.str.len = ptr - smp->data.u.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010818 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010819 return 1;
10820}
10821
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010822/* This produces a concatenation of the first occurrence of the Host header
10823 * followed by the path component if it begins with a slash ('/'). This means
10824 * that '*' will not be added, resulting in exactly the first Host entry.
10825 * If no Host header is found, then the path is returned as-is. The returned
10826 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010827 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010828 */
10829static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010830smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010831{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010832 struct http_txn *txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010833 char *ptr, *end, *beg;
10834 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010835 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010836
10837 CHECK_HTTP_MESSAGE_FIRST();
10838
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010839 txn = smp->strm->txn;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010840 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010841 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010842 return smp_fetch_path(args, smp, kw, private);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010843
10844 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010845 temp = get_trash_chunk();
10846 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010847 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010848 smp->data.u.str.str = temp->str;
10849 smp->data.u.str.len = ctx.vlen;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010850
10851 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010852 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010853 beg = http_get_path(txn);
10854 if (!beg)
10855 beg = end;
10856
10857 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10858
10859 if (beg < ptr && *beg == '/') {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010860 memcpy(smp->data.u.str.str + smp->data.u.str.len, beg, ptr - beg);
10861 smp->data.u.str.len += ptr - beg;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010862 }
10863
10864 smp->flags = SMP_F_VOL_1ST;
10865 return 1;
10866}
10867
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010868/* This produces a 32-bit hash of the concatenation of the first occurrence of
10869 * the Host header followed by the path component if it begins with a slash ('/').
10870 * This means that '*' will not be added, resulting in exactly the first Host
10871 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010872 * is hashed using the path hash followed by a full avalanche hash and provides a
10873 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010874 * high-traffic sites without having to store whole paths.
10875 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010876int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010877smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010878{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010879 struct http_txn *txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010880 struct hdr_ctx ctx;
10881 unsigned int hash = 0;
10882 char *ptr, *beg, *end;
10883 int len;
10884
10885 CHECK_HTTP_MESSAGE_FIRST();
10886
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010887 txn = smp->strm->txn;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010888 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010889 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010890 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10891 ptr = ctx.line + ctx.val;
10892 len = ctx.vlen;
10893 while (len--)
10894 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10895 }
10896
10897 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010898 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010899 beg = http_get_path(txn);
10900 if (!beg)
10901 beg = end;
10902
10903 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10904
10905 if (beg < ptr && *beg == '/') {
10906 while (beg < ptr)
10907 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10908 }
10909 hash = full_hash(hash);
10910
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010911 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010912 smp->data.u.sint = hash;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010913 smp->flags = SMP_F_VOL_1ST;
10914 return 1;
10915}
10916
Willy Tarreau4a550602012-12-09 14:53:32 +010010917/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010918 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10919 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10920 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010921 * that in environments where IPv6 is insignificant, truncating the output to
10922 * 8 bytes would still work.
10923 */
10924static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010925smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a550602012-12-09 14:53:32 +010010926{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010927 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010928 struct connection *cli_conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010929
10930 if (!cli_conn)
10931 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010932
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010933 if (!smp_fetch_base32(args, smp, kw, private))
Willy Tarreau4a550602012-12-09 14:53:32 +010010934 return 0;
10935
Willy Tarreau47ca5452012-12-23 20:22:19 +010010936 temp = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010937 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010938 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010939
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010940 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010941 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010942 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010943 temp->len += 4;
10944 break;
10945 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010946 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010947 temp->len += 16;
10948 break;
10949 default:
10950 return 0;
10951 }
10952
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010953 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010954 smp->data.type = SMP_T_BIN;
Willy Tarreau4a550602012-12-09 14:53:32 +010010955 return 1;
10956}
10957
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010958/* Extracts the query string, which comes after the question mark '?'. If no
10959 * question mark is found, nothing is returned. Otherwise it returns a sample
10960 * of type string carrying the whole query string.
10961 */
10962static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010963smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010964{
Willy Tarreau15e91e12015-04-04 00:52:09 +020010965 struct http_txn *txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010966 char *ptr, *end;
10967
10968 CHECK_HTTP_MESSAGE_FIRST();
10969
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020010970 txn = smp->strm->txn;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010971 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
10972 end = ptr + txn->req.sl.rq.u_l;
10973
10974 /* look up the '?' */
10975 do {
10976 if (ptr == end)
10977 return 0;
10978 } while (*ptr++ != '?');
10979
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010980 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010981 smp->data.u.str.str = ptr;
10982 smp->data.u.str.len = end - ptr;
Willy Tarreau49ad95c2015-01-19 15:06:26 +010010983 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
10984 return 1;
10985}
10986
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010987static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020010988smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010989{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010990 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10991 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10992 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010993
Willy Tarreau24e32d82012-04-23 23:55:44 +020010994 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010995
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020010996 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020010997 smp->data.u.sint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010998 return 1;
10999}
11000
Willy Tarreau7f18e522010-10-22 20:04:13 +020011001/* return a valid test if the current request is the first one on the connection */
11002static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011003smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7f18e522010-10-22 20:04:13 +020011004{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011005 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011006 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020011007 return 1;
11008}
11009
Willy Tarreau34db1082012-04-19 17:16:54 +020011010/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011011static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011012smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011013{
11014
Willy Tarreau24e32d82012-04-23 23:55:44 +020011015 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011016 return 0;
11017
Willy Tarreauc0239e02012-04-16 14:42:55 +020011018 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011019
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011020 if (!get_http_auth(smp->strm))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011021 return 0;
11022
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011023 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011024 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011025 smp->strm->txn->auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010011026 return 1;
11027}
Willy Tarreau8797c062007-05-07 00:55:35 +020011028
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011029/* Accepts exactly 1 argument of type userlist */
11030static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011031smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011032{
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011033 if (!args || args->type != ARGT_USR)
11034 return 0;
11035
11036 CHECK_HTTP_MESSAGE_FIRST();
11037
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011038 if (!get_http_auth(smp->strm))
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011039 return 0;
11040
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011041 /* if the user does not belong to the userlist or has a wrong password,
11042 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010011043 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011044 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011045 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
11046 smp->strm->txn->auth.pass))
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010011047 return 0;
11048
11049 /* pat_match_auth() will need the user list */
11050 smp->ctx.a[0] = args->data.usr;
11051
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011052 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011053 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011054 smp->data.u.str.str = smp->strm->txn->auth.user;
11055 smp->data.u.str.len = strlen(smp->strm->txn->auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020011056
11057 return 1;
11058}
11059
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011060/* Try to find the next occurrence of a cookie name in a cookie header value.
11061 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
11062 * the cookie value is returned into *value and *value_l, and the function
11063 * returns a pointer to the next pointer to search from if the value was found.
11064 * Otherwise if the cookie was not found, NULL is returned and neither value
11065 * nor value_l are touched. The input <hdr> string should first point to the
11066 * header's value, and the <hdr_end> pointer must point to the first character
11067 * not part of the value. <list> must be non-zero if value may represent a list
11068 * of values (cookie headers). This makes it faster to abort parsing when no
11069 * list is expected.
11070 */
David Carlier4686f792015-09-25 14:10:50 +010011071char *
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011072extract_cookie_value(char *hdr, const char *hdr_end,
11073 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020011074 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011075{
11076 char *equal, *att_end, *att_beg, *val_beg, *val_end;
11077 char *next;
11078
11079 /* we search at least a cookie name followed by an equal, and more
11080 * generally something like this :
11081 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
11082 */
11083 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
11084 /* Iterate through all cookies on this line */
11085
Willy Tarreau2235b262016-11-05 15:50:20 +010011086 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011087 att_beg++;
11088
11089 /* find att_end : this is the first character after the last non
11090 * space before the equal. It may be equal to hdr_end.
11091 */
11092 equal = att_end = att_beg;
11093
11094 while (equal < hdr_end) {
11095 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
11096 break;
Willy Tarreau2235b262016-11-05 15:50:20 +010011097 if (HTTP_IS_SPHT(*equal++))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011098 continue;
11099 att_end = equal;
11100 }
11101
11102 /* here, <equal> points to '=', a delimitor or the end. <att_end>
11103 * is between <att_beg> and <equal>, both may be identical.
11104 */
11105
11106 /* look for end of cookie if there is an equal sign */
11107 if (equal < hdr_end && *equal == '=') {
11108 /* look for the beginning of the value */
11109 val_beg = equal + 1;
Willy Tarreau2235b262016-11-05 15:50:20 +010011110 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011111 val_beg++;
11112
11113 /* find the end of the value, respecting quotes */
11114 next = find_cookie_value_end(val_beg, hdr_end);
11115
11116 /* make val_end point to the first white space or delimitor after the value */
11117 val_end = next;
Willy Tarreau2235b262016-11-05 15:50:20 +010011118 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011119 val_end--;
11120 } else {
11121 val_beg = val_end = next = equal;
11122 }
11123
11124 /* We have nothing to do with attributes beginning with '$'. However,
11125 * they will automatically be removed if a header before them is removed,
11126 * since they're supposed to be linked together.
11127 */
11128 if (*att_beg == '$')
11129 continue;
11130
11131 /* Ignore cookies with no equal sign */
11132 if (equal == next)
11133 continue;
11134
11135 /* Now we have the cookie name between att_beg and att_end, and
11136 * its value between val_beg and val_end.
11137 */
11138
11139 if (att_end - att_beg == cookie_name_l &&
11140 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
11141 /* let's return this value and indicate where to go on from */
11142 *value = val_beg;
11143 *value_l = val_end - val_beg;
11144 return next + 1;
11145 }
11146
11147 /* Set-Cookie headers only have the name in the first attr=value part */
11148 if (!list)
11149 break;
11150 }
11151
11152 return NULL;
11153}
11154
William Lallemanda43ba4e2014-01-28 18:14:25 +010011155/* Fetch a captured HTTP request header. The index is the position of
11156 * the "capture" option in the configuration file
11157 */
11158static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011159smp_fetch_capture_header_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011160{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011161 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011162 int idx;
11163
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011164 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011165 return 0;
11166
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011167 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011168
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011169 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 +010011170 return 0;
11171
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011172 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011173 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011174 smp->data.u.str.str = smp->strm->req_cap[idx];
11175 smp->data.u.str.len = strlen(smp->strm->req_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011176
11177 return 1;
11178}
11179
11180/* Fetch a captured HTTP response header. The index is the position of
11181 * the "capture" option in the configuration file
11182 */
11183static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011184smp_fetch_capture_header_res(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011185{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011186 struct proxy *fe = strm_fe(smp->strm);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011187 int idx;
11188
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011189 if (!args || args->type != ARGT_SINT)
William Lallemanda43ba4e2014-01-28 18:14:25 +010011190 return 0;
11191
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020011192 idx = args->data.sint;
William Lallemanda43ba4e2014-01-28 18:14:25 +010011193
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011194 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 +010011195 return 0;
11196
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011197 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011198 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011199 smp->data.u.str.str = smp->strm->res_cap[idx];
11200 smp->data.u.str.len = strlen(smp->strm->res_cap[idx]);
William Lallemanda43ba4e2014-01-28 18:14:25 +010011201
11202 return 1;
11203}
11204
William Lallemand65ad6e12014-01-31 15:08:02 +010011205/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
11206static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011207smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011208{
11209 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011210 struct http_txn *txn = smp->strm->txn;
William Lallemand96a77852014-02-05 00:30:02 +010011211 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011212
Willy Tarreau15e91e12015-04-04 00:52:09 +020011213 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011214 return 0;
11215
William Lallemand96a77852014-02-05 00:30:02 +010011216 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010011217
William Lallemand96a77852014-02-05 00:30:02 +010011218 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11219 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011220
William Lallemand96a77852014-02-05 00:30:02 +010011221 temp = get_trash_chunk();
11222 temp->str = txn->uri;
11223 temp->len = ptr - txn->uri;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011224 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011225 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011226 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011227
11228 return 1;
11229
11230}
11231
11232/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
11233static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011234smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
William Lallemand65ad6e12014-01-31 15:08:02 +010011235{
11236 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011237 struct http_txn *txn = smp->strm->txn;
William Lallemand65ad6e12014-01-31 15:08:02 +010011238 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010011239
Willy Tarreau15e91e12015-04-04 00:52:09 +020011240 if (!txn || !txn->uri)
William Lallemand65ad6e12014-01-31 15:08:02 +010011241 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010011242
William Lallemand65ad6e12014-01-31 15:08:02 +010011243 ptr = txn->uri;
11244
11245 while (*ptr != ' ' && *ptr != '\0') /* find first space */
11246 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010011247
William Lallemand65ad6e12014-01-31 15:08:02 +010011248 if (!*ptr)
11249 return 0;
11250
11251 ptr++; /* skip the space */
11252
11253 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010011254 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010011255 if (!ptr)
11256 return 0;
11257 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
11258 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010011259
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011260 smp->data.u.str = *temp;
11261 smp->data.u.str.len = ptr - temp->str;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011262 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011263 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010011264
11265 return 1;
11266}
11267
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011268/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
11269 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11270 */
11271static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011272smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011273{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011274 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011275
Willy Tarreau15e91e12015-04-04 00:52:09 +020011276 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011277 return 0;
11278
11279 if (txn->req.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011280 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011281 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011282 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011283
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011284 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011285 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011286 smp->flags = SMP_F_CONST;
11287 return 1;
11288
11289}
11290
11291/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
11292 * as a string (either "HTTP/1.0" or "HTTP/1.1").
11293 */
11294static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011295smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011296{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011297 struct http_txn *txn = smp->strm->txn;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011298
Willy Tarreau15e91e12015-04-04 00:52:09 +020011299 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011300 return 0;
11301
11302 if (txn->rsp.flags & HTTP_MSGF_VER_11)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011303 smp->data.u.str.str = "HTTP/1.1";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011304 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011305 smp->data.u.str.str = "HTTP/1.0";
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011306
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011307 smp->data.u.str.len = 8;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011308 smp->data.type = SMP_T_STR;
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020011309 smp->flags = SMP_F_CONST;
11310 return 1;
11311
11312}
11313
William Lallemand65ad6e12014-01-31 15:08:02 +010011314
Willy Tarreaue333ec92012-04-16 16:26:40 +020011315/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020011316 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020011317 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020011318 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020011319 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020011320 * Accepts exactly 1 argument of type string. If the input options indicate
11321 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020011322 * The returned sample is of type CSTR. Can be used to parse cookies in other
11323 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011324 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011325int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011326{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011327 struct http_txn *txn;
11328 struct hdr_idx *idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020011329 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020011330 const struct http_msg *msg;
11331 const char *hdr_name;
11332 int hdr_name_len;
11333 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020011334 int occ = 0;
11335 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011336
Willy Tarreau24e32d82012-04-23 23:55:44 +020011337 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011338 return 0;
11339
Willy Tarreaua890d072013-04-02 12:01:06 +020011340 if (!ctx) {
11341 /* first call */
11342 ctx = &static_hdr_ctx;
11343 ctx->idx = 0;
11344 smp->ctx.a[2] = ctx;
11345 }
11346
Willy Tarreaue333ec92012-04-16 16:26:40 +020011347 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011348
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011349 txn = smp->strm->txn;
11350 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011351
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011352 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011353 msg = &txn->req;
11354 hdr_name = "Cookie";
11355 hdr_name_len = 6;
11356 } else {
11357 msg = &txn->rsp;
11358 hdr_name = "Set-Cookie";
11359 hdr_name_len = 10;
11360 }
11361
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011362 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
Willy Tarreau28376d62012-04-26 21:26:10 +020011363 /* no explicit occurrence and single fetch => last cookie by default */
11364 occ = -1;
11365
11366 /* OK so basically here, either we want only one value and it's the
11367 * last one, or we want to iterate over all of them and we fetch the
11368 * next one.
11369 */
11370
Willy Tarreau9b28e032012-10-12 23:49:43 +020011371 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020011372 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011373 /* search for the header from the beginning, we must first initialize
11374 * the search parameters.
11375 */
Willy Tarreau37406352012-04-23 16:16:37 +020011376 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011377 ctx->idx = 0;
11378 }
11379
Willy Tarreau28376d62012-04-26 21:26:10 +020011380 smp->flags |= SMP_F_VOL_HDR;
11381
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011382 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020011383 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
11384 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011385 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
11386 goto out;
11387
Willy Tarreau24e32d82012-04-23 23:55:44 +020011388 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011389 continue;
11390
Willy Tarreau37406352012-04-23 16:16:37 +020011391 smp->ctx.a[0] = ctx->line + ctx->val;
11392 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011393 }
11394
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011395 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011396 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020011397 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020011398 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011399 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011400 &smp->data.u.str.str,
11401 &smp->data.u.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020011402 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020011403 found = 1;
11404 if (occ >= 0) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011405 /* one value was returned into smp->data.u.str.{str,len} */
Willy Tarreau28376d62012-04-26 21:26:10 +020011406 smp->flags |= SMP_F_NOT_LAST;
11407 return 1;
11408 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011409 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011410 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011411 }
Willy Tarreau28376d62012-04-26 21:26:10 +020011412 /* all cookie headers and values were scanned. If we're looking for the
11413 * last occurrence, we may return it now.
11414 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011415 out:
Willy Tarreau37406352012-04-23 16:16:37 +020011416 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020011417 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011418}
11419
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011420/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020011421 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010011422 * multiple cookies may be parsed on the same line. The returned sample is of
11423 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011424 */
11425static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011426smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011427{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011428 struct http_txn *txn;
11429 struct hdr_idx *idx;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011430 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011431 const struct http_msg *msg;
11432 const char *hdr_name;
11433 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011434 int cnt;
11435 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020011436 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011437
Willy Tarreau24e32d82012-04-23 23:55:44 +020011438 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020011439 return 0;
11440
Willy Tarreaue333ec92012-04-16 16:26:40 +020011441 CHECK_HTTP_MESSAGE_FIRST();
11442
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011443 txn = smp->strm->txn;
11444 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau15e91e12015-04-04 00:52:09 +020011445
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011446 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020011447 msg = &txn->req;
11448 hdr_name = "Cookie";
11449 hdr_name_len = 6;
11450 } else {
11451 msg = &txn->rsp;
11452 hdr_name = "Set-Cookie";
11453 hdr_name_len = 10;
11454 }
11455
Willy Tarreau9b28e032012-10-12 23:49:43 +020011456 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020011457 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011458 ctx.idx = 0;
11459 cnt = 0;
11460
11461 while (1) {
11462 /* Note: val_beg == NULL every time we need to fetch a new header */
11463 if (!val_beg) {
11464 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
11465 break;
11466
Willy Tarreau24e32d82012-04-23 23:55:44 +020011467 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011468 continue;
11469
11470 val_beg = ctx.line + ctx.val;
11471 val_end = val_beg + ctx.vlen;
11472 }
11473
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011474 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011475 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011476 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020011477 args->data.str.str, args->data.str.len,
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011478 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011479 &smp->data.u.str.str,
11480 &smp->data.u.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011481 cnt++;
11482 }
11483 }
11484
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011485 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011486 smp->data.u.sint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011487 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011488 return 1;
11489}
11490
Willy Tarreau51539362012-05-08 12:46:28 +020011491/* Fetch an cookie's integer value. The integer value is returned. It
11492 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11493 */
11494static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011495smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau51539362012-05-08 12:46:28 +020011496{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011497 int ret = smp_fetch_cookie(args, smp, kw, private);
Willy Tarreau51539362012-05-08 12:46:28 +020011498
11499 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011500 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011501 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreau51539362012-05-08 12:46:28 +020011502 }
11503
11504 return ret;
11505}
11506
Willy Tarreau8797c062007-05-07 00:55:35 +020011507/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011508/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011509/************************************************************************/
11510
David Cournapeau16023ee2010-12-23 20:55:41 +090011511/*
11512 * Given a path string and its length, find the position of beginning of the
11513 * query string. Returns NULL if no query string is found in the path.
11514 *
11515 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11516 *
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011517 * find_query_string(path, n, '?') points to "yo=mama;ye=daddy" string.
David Cournapeau16023ee2010-12-23 20:55:41 +090011518 */
bedis4c75cca2012-10-05 08:38:24 +020011519static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011520{
11521 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011522
bedis4c75cca2012-10-05 08:38:24 +020011523 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011524 return p ? p + 1 : NULL;
11525}
11526
bedis4c75cca2012-10-05 08:38:24 +020011527static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011528{
bedis4c75cca2012-10-05 08:38:24 +020011529 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011530}
11531
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011532/* after increasing a pointer value, it can exceed the first buffer
11533 * size. This function transform the value of <ptr> according with
11534 * the expected position. <chunks> is an array of the one or two
11535 * avalaible chunks. The first value is the start of the first chunk,
11536 * the second value if the end+1 of the first chunks. The third value
11537 * is NULL or the start of the second chunk and the fourth value is
11538 * the end+1 of the second chunk. The function returns 1 if does a
11539 * wrap, else returns 0.
11540 */
11541static inline int fix_pointer_if_wrap(const char **chunks, const char **ptr)
11542{
11543 if (*ptr < chunks[1])
11544 return 0;
11545 if (!chunks[2])
11546 return 0;
11547 *ptr = chunks[2] + ( *ptr - chunks[1] );
11548 return 1;
11549}
11550
David Cournapeau16023ee2010-12-23 20:55:41 +090011551/*
11552 * Given a url parameter, find the starting position of the first occurence,
11553 * or NULL if the parameter is not found.
11554 *
11555 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11556 * the function will return query_string+8.
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011557 *
Willy Tarreauf6625822015-12-27 14:51:01 +010011558 * Warning: this function returns a pointer that can point to the first chunk
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011559 * or the second chunk. The caller must be check the position before using the
11560 * result.
David Cournapeau16023ee2010-12-23 20:55:41 +090011561 */
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011562static const char *
11563find_url_param_pos(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011564 const char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011565 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011566{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011567 const char *pos, *last, *equal;
11568 const char **bufs = chunks;
11569 int l1, l2;
David Cournapeau16023ee2010-12-23 20:55:41 +090011570
David Cournapeau16023ee2010-12-23 20:55:41 +090011571
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011572 pos = bufs[0];
11573 last = bufs[1];
Willy Tarreauf6625822015-12-27 14:51:01 +010011574 while (pos < last) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011575 /* Check the equal. */
11576 equal = pos + url_param_name_l;
11577 if (fix_pointer_if_wrap(chunks, &equal)) {
11578 if (equal >= chunks[3])
11579 return NULL;
11580 } else {
11581 if (equal >= chunks[1])
11582 return NULL;
11583 }
11584 if (*equal == '=') {
11585 if (pos + url_param_name_l > last) {
11586 /* process wrap case, we detect a wrap. In this case, the
11587 * comparison is performed in two parts.
11588 */
11589
11590 /* This is the end, we dont have any other chunk. */
11591 if (bufs != chunks || !bufs[2])
11592 return NULL;
11593
11594 /* Compute the length of each part of the comparison. */
11595 l1 = last - pos;
11596 l2 = url_param_name_l - l1;
11597
11598 /* The second buffer is too short to contain the compared string. */
11599 if (bufs[2] + l2 > bufs[3])
11600 return NULL;
11601
11602 if (memcmp(pos, url_param_name, l1) == 0 &&
11603 memcmp(bufs[2], url_param_name+l1, l2) == 0)
11604 return pos;
11605
11606 /* Perform wrapping and jump the string who fail the comparison. */
11607 bufs += 2;
11608 pos = bufs[0] + l2;
11609 last = bufs[1];
11610
11611 } else {
11612 /* process a simple comparison. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011613 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11614 return pos;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011615 pos += url_param_name_l + 1;
11616 if (fix_pointer_if_wrap(chunks, &pos))
11617 last = bufs[2];
11618 }
11619 }
11620
11621 while (1) {
11622 /* Look for the next delimiter. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011623 while (pos < last && !is_param_delimiter(*pos, delim))
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011624 pos++;
11625 if (pos < last)
11626 break;
11627 /* process buffer wrapping. */
11628 if (bufs != chunks || !bufs[2])
11629 return NULL;
11630 bufs += 2;
11631 pos = bufs[0];
11632 last = bufs[1];
David Cournapeau16023ee2010-12-23 20:55:41 +090011633 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011634 pos++;
11635 }
11636 return NULL;
11637}
11638
11639/*
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011640 * Given a url parameter name and a query string, find the next value.
11641 * An empty url_param_name matches the first available parameter.
11642 * If the parameter is found, 1 is returned and *vstart / *vend are updated to
11643 * respectively provide a pointer to the value and its end.
11644 * Otherwise, 0 is returned and vstart/vend are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090011645 */
11646static int
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011647find_next_url_param(const char **chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011648 const char* url_param_name, size_t url_param_name_l,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011649 const char **vstart, const char **vend, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011650{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011651 const char *arg_start, *qs_end;
11652 const char *value_start, *value_end;
David Cournapeau16023ee2010-12-23 20:55:41 +090011653
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011654 arg_start = chunks[0];
11655 qs_end = chunks[1];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011656 if (url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011657 /* Looks for an argument name. */
11658 arg_start = find_url_param_pos(chunks,
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011659 url_param_name, url_param_name_l,
11660 delim);
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011661 /* Check for wrapping. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011662 if (arg_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011663 qs_end = chunks[3];
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011664 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011665 if (!arg_start)
11666 return 0;
11667
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011668 if (!url_param_name_l) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011669 while (1) {
11670 /* looks for the first argument. */
11671 value_start = memchr(arg_start, '=', qs_end - arg_start);
11672 if (!value_start) {
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011673 /* Check for wrapping. */
11674 if (arg_start >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011675 arg_start < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011676 chunks[2]) {
11677 arg_start = chunks[2];
11678 qs_end = chunks[3];
11679 continue;
11680 }
11681 return 0;
11682 }
11683 break;
11684 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011685 value_start++;
11686 }
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011687 else {
11688 /* Jump the argument length. */
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011689 value_start = arg_start + url_param_name_l + 1;
11690
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011691 /* Check for pointer wrapping. */
11692 if (fix_pointer_if_wrap(chunks, &value_start)) {
11693 /* Update the end pointer. */
11694 qs_end = chunks[3];
11695
11696 /* Check for overflow. */
Willy Tarreauf6625822015-12-27 14:51:01 +010011697 if (value_start >= qs_end)
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011698 return 0;
11699 }
11700 }
11701
David Cournapeau16023ee2010-12-23 20:55:41 +090011702 value_end = value_start;
11703
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011704 while (1) {
11705 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
11706 value_end++;
11707 if (value_end < qs_end)
11708 break;
11709 /* process buffer wrapping. */
11710 if (value_end >= chunks[0] &&
Willy Tarreauf6625822015-12-27 14:51:01 +010011711 value_end < chunks[1] &&
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011712 chunks[2]) {
11713 value_end = chunks[2];
11714 qs_end = chunks[3];
11715 continue;
11716 }
11717 break;
11718 }
David Cournapeau16023ee2010-12-23 20:55:41 +090011719
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011720 *vstart = value_start;
11721 *vend = value_end;
Cyril Bontéce1ef4d2015-11-26 21:39:56 +010011722 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011723}
11724
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011725/* This scans a URL-encoded query string. It takes an optionally wrapping
11726 * string whose first contigous chunk has its beginning in ctx->a[0] and end
11727 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
11728 * pointers are updated for next iteration before leaving.
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011729 */
David Cournapeau16023ee2010-12-23 20:55:41 +090011730static int
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011731smp_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 +090011732{
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011733 const char *vstart, *vend;
11734 struct chunk *temp;
11735 const char **chunks = (const char **)smp->ctx.a;
bedis4c75cca2012-10-05 08:38:24 +020011736
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011737 if (!find_next_url_param(chunks,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011738 name, name_len,
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011739 &vstart, &vend,
Thierry FOURNIER0948d412015-05-20 15:22:37 +020011740 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011741 return 0;
11742
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011743 /* Create sample. If the value is contiguous, return the pointer as CONST,
11744 * if the value is wrapped, copy-it in a buffer.
11745 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011746 smp->data.type = SMP_T_STR;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011747 if (chunks[2] &&
11748 vstart >= chunks[0] && vstart <= chunks[1] &&
11749 vend >= chunks[2] && vend <= chunks[3]) {
11750 /* Wrapped case. */
11751 temp = get_trash_chunk();
11752 memcpy(temp->str, vstart, chunks[1] - vstart);
11753 memcpy(temp->str + ( chunks[1] - vstart ), chunks[2], vend - chunks[2]);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011754 smp->data.u.str.str = temp->str;
11755 smp->data.u.str.len = ( chunks[1] - vstart ) + ( vend - chunks[2] );
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011756 } else {
11757 /* Contiguous case. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011758 smp->data.u.str.str = (char *)vstart;
11759 smp->data.u.str.len = vend - vstart;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011760 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
11761 }
11762
11763 /* Update context, check wrapping. */
11764 chunks[0] = vend;
11765 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
11766 chunks[1] = chunks[3];
11767 chunks[2] = NULL;
11768 }
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011769
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011770 if (chunks[0] < chunks[1])
Willy Tarreau1ede1da2015-05-07 16:06:18 +020011771 smp->flags |= SMP_F_NOT_LAST;
11772
David Cournapeau16023ee2010-12-23 20:55:41 +090011773 return 1;
11774}
11775
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011776/* This function iterates over each parameter of the query string. It uses
11777 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011778 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
11779 * An optional parameter name is passed in args[0], otherwise any parameter is
11780 * considered. It supports an optional delimiter argument for the beginning of
11781 * the string in args[1], which defaults to "?".
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011782 */
11783static int
11784smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11785{
11786 struct http_msg *msg;
11787 char delim = '?';
11788 const char *name;
11789 int name_len;
11790
Dragan Dosen26f77e52015-05-25 10:02:11 +020011791 if (!args ||
11792 (args[0].type && args[0].type != ARGT_STR) ||
11793 (args[1].type && args[1].type != ARGT_STR))
11794 return 0;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011795
Dragan Dosen26f77e52015-05-25 10:02:11 +020011796 name = "";
11797 name_len = 0;
11798 if (args->type == ARGT_STR) {
11799 name = args->data.str.str;
11800 name_len = args->data.str.len;
11801 }
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011802
Dragan Dosen26f77e52015-05-25 10:02:11 +020011803 if (args[1].type)
11804 delim = *args[1].data.str.str;
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011805
Dragan Dosen26f77e52015-05-25 10:02:11 +020011806 if (!smp->ctx.a[0]) { // first call, find the query string
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011807 CHECK_HTTP_MESSAGE_FIRST();
11808
11809 msg = &smp->strm->txn->req;
11810
11811 smp->ctx.a[0] = find_param_list(msg->chn->buf->p + msg->sl.rq.u,
11812 msg->sl.rq.u_l, delim);
11813 if (!smp->ctx.a[0])
11814 return 0;
11815
11816 smp->ctx.a[1] = msg->chn->buf->p + msg->sl.rq.u + msg->sl.rq.u_l;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011817
11818 /* Assume that the context is filled with NULL pointer
11819 * before the first call.
11820 * smp->ctx.a[2] = NULL;
11821 * smp->ctx.a[3] = NULL;
11822 */
Thierry FOURNIER4fdc74c2015-05-19 14:46:23 +020011823 }
11824
11825 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
11826}
11827
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011828/* This function iterates over each parameter of the body. This requires
11829 * that the body has been waited for using http-buffer-request. It uses
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011830 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
11831 * contigous part of the body, and optionally ctx->a[2..3] to reference the
11832 * optional second part if the body wraps at the end of the buffer. An optional
11833 * parameter name is passed in args[0], otherwise any parameter is considered.
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011834 */
11835static int
11836smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
11837{
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011838 struct http_msg *msg;
11839 unsigned long len;
11840 unsigned long block1;
11841 char *body;
11842 const char *name;
11843 int name_len;
11844
11845 if (!args || (args[0].type && args[0].type != ARGT_STR))
11846 return 0;
11847
11848 name = "";
11849 name_len = 0;
11850 if (args[0].type == ARGT_STR) {
11851 name = args[0].data.str.str;
11852 name_len = args[0].data.str.len;
11853 }
11854
11855 if (!smp->ctx.a[0]) { // first call, find the query string
11856 CHECK_HTTP_MESSAGE_FIRST();
11857
11858 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
Willy Tarreaube508f12016-03-10 11:47:01 +010011859 msg = &smp->strm->txn->req;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011860 else
Willy Tarreaube508f12016-03-10 11:47:01 +010011861 msg = &smp->strm->txn->rsp;
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011862
11863 len = http_body_bytes(msg);
11864 body = b_ptr(msg->chn->buf, -http_data_rewind(msg));
11865
11866 block1 = len;
11867 if (block1 > msg->chn->buf->data + msg->chn->buf->size - body)
11868 block1 = msg->chn->buf->data + msg->chn->buf->size - body;
11869
11870 if (block1 == len) {
11871 /* buffer is not wrapped (or empty) */
11872 smp->ctx.a[0] = body;
11873 smp->ctx.a[1] = body + len;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011874
11875 /* Assume that the context is filled with NULL pointer
11876 * before the first call.
11877 * smp->ctx.a[2] = NULL;
11878 * smp->ctx.a[3] = NULL;
11879 */
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011880 }
11881 else {
11882 /* buffer is wrapped, we need to defragment it */
11883 smp->ctx.a[0] = body;
11884 smp->ctx.a[1] = body + block1;
Thierry FOURNIER8be451c2015-05-20 15:28:12 +020011885 smp->ctx.a[2] = msg->chn->buf->data;
11886 smp->ctx.a[3] = msg->chn->buf->data + ( len - block1 );
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020011887 }
11888 }
11889 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
11890}
11891
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011892/* Return the signed integer value for the specified url parameter (see url_param
11893 * above).
11894 */
11895static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011896smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011897{
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011898 int ret = smp_fetch_url_param(args, smp, kw, private);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011899
11900 if (ret > 0) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011901 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011902 smp->data.u.sint = strl2ic(smp->data.u.str.str, smp->data.u.str.len);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011903 }
11904
11905 return ret;
11906}
11907
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011908/* This produces a 32-bit hash of the concatenation of the first occurrence of
11909 * the Host header followed by the path component if it begins with a slash ('/').
11910 * This means that '*' will not be added, resulting in exactly the first Host
11911 * entry. If no Host header is found, then the path is used. The resulting value
11912 * is hashed using the url hash followed by a full avalanche hash and provides a
11913 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11914 * high-traffic sites without having to store whole paths.
11915 * this differs from the base32 functions in that it includes the url parameters
11916 * as well as the path
11917 */
11918static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011919smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011920{
Willy Tarreau15e91e12015-04-04 00:52:09 +020011921 struct http_txn *txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011922 struct hdr_ctx ctx;
11923 unsigned int hash = 0;
11924 char *ptr, *beg, *end;
11925 int len;
11926
11927 CHECK_HTTP_MESSAGE_FIRST();
11928
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011929 txn = smp->strm->txn;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011930 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011931 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011932 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11933 ptr = ctx.line + ctx.val;
11934 len = ctx.vlen;
11935 while (len--)
11936 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11937 }
11938
11939 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011940 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 +000011941 beg = http_get_path(txn);
11942 if (!beg)
11943 beg = end;
11944
11945 for (ptr = beg; ptr < end ; ptr++);
11946
11947 if (beg < ptr && *beg == '/') {
11948 while (beg < ptr)
11949 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11950 }
11951 hash = full_hash(hash);
11952
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011953 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011954 smp->data.u.sint = hash;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011955 smp->flags = SMP_F_VOL_1ST;
11956 return 1;
11957}
11958
11959/* This concatenates the source address with the 32-bit hash of the Host and
11960 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11961 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11962 * on the source address length. The URL hash is stored before the address so
11963 * that in environments where IPv6 is insignificant, truncating the output to
11964 * 8 bytes would still work.
11965 */
11966static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011967smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011968{
11969 struct chunk *temp;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020011970 struct connection *cli_conn = objt_conn(smp->sess->origin);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011971
Dragan Dosendb5af612016-06-16 11:23:01 +020011972 if (!cli_conn)
11973 return 0;
11974
Thierry FOURNIER0786d052015-05-11 15:42:45 +020011975 if (!smp_fetch_url32(args, smp, kw, private))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011976 return 0;
11977
11978 temp = get_trash_chunk();
Dragan Dosene5f41332016-06-16 11:08:08 +020011979 *(unsigned int *)temp->str = htonl(smp->data.u.sint);
11980 temp->len += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011981
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011982 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011983 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011984 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011985 temp->len += 4;
11986 break;
11987 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011988 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011989 temp->len += 16;
11990 break;
11991 default:
11992 return 0;
11993 }
11994
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020011995 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020011996 smp->data.type = SMP_T_BIN;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011997 return 1;
11998}
11999
Willy Tarreau185b5c42012-04-26 15:11:51 +020012000/* This function is used to validate the arguments passed to any "hdr" fetch
12001 * keyword. These keywords support an optional positive or negative occurrence
12002 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
12003 * is assumed that the types are already the correct ones. Returns 0 on error,
12004 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
12005 * error message in case of error, that the caller is responsible for freeing.
12006 * The initial location must either be freeable or NULL.
12007 */
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010012008int val_hdr(struct arg *arg, char **err_msg)
Willy Tarreau185b5c42012-04-26 15:11:51 +020012009{
12010 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020012011 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020012012 return 0;
12013 }
12014 return 1;
12015}
12016
Willy Tarreau276fae92013-07-25 14:36:01 +020012017/* takes an UINT value on input supposed to represent the time since EPOCH,
12018 * adds an optional offset found in args[0] and emits a string representing
12019 * the date in RFC-1123/5322 format.
12020 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012021static int sample_conv_http_date(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau276fae92013-07-25 14:36:01 +020012022{
Cyril Bontéf78d8962016-01-22 19:40:28 +010012023 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020012024 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
12025 struct chunk *temp;
12026 struct tm *tm;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012027 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012028 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Willy Tarreau276fae92013-07-25 14:36:01 +020012029
12030 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012031 if (args && (args[0].type == ARGT_SINT))
Willy Tarreau276fae92013-07-25 14:36:01 +020012032 curr_date += args[0].data.sint;
12033
12034 tm = gmtime(&curr_date);
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +020012035 if (!tm)
12036 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020012037
12038 temp = get_trash_chunk();
12039 temp->len = snprintf(temp->str, temp->size - temp->len,
12040 "%s, %02d %s %04d %02d:%02d:%02d GMT",
12041 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
12042 tm->tm_hour, tm->tm_min, tm->tm_sec);
12043
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012044 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +020012045 smp->data.type = SMP_T_STR;
Willy Tarreau276fae92013-07-25 14:36:01 +020012046 return 1;
12047}
12048
Thierry FOURNIERad903512014-04-11 17:51:01 +020012049/* Match language range with language tag. RFC2616 14.4:
12050 *
12051 * A language-range matches a language-tag if it exactly equals
12052 * the tag, or if it exactly equals a prefix of the tag such
12053 * that the first tag character following the prefix is "-".
12054 *
12055 * Return 1 if the strings match, else return 0.
12056 */
12057static inline int language_range_match(const char *range, int range_len,
12058 const char *tag, int tag_len)
12059{
12060 const char *end = range + range_len;
12061 const char *tend = tag + tag_len;
12062 while (range < end) {
12063 if (*range == '-' && tag == tend)
12064 return 1;
12065 if (*range != *tag || tag == tend)
12066 return 0;
12067 range++;
12068 tag++;
12069 }
12070 /* Return true only if the last char of the tag is matched. */
12071 return tag == tend;
12072}
12073
12074/* Arguments: The list of expected value, the number of parts returned and the separator */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012075static int sample_conv_q_prefered(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIERad903512014-04-11 17:51:01 +020012076{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012077 const char *al = smp->data.u.str.str;
12078 const char *end = al + smp->data.u.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012079 const char *token;
12080 int toklen;
12081 int qvalue;
12082 const char *str;
12083 const char *w;
12084 int best_q = 0;
12085
12086 /* Set the constant to the sample, because the output of the
12087 * function will be peek in the constant configuration string.
12088 */
12089 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012090 smp->data.u.str.size = 0;
12091 smp->data.u.str.str = "";
12092 smp->data.u.str.len = 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012093
12094 /* Parse the accept language */
12095 while (1) {
12096
12097 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012098 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012099 al++;
12100 if (al >= end)
12101 break;
12102
12103 /* Start of the fisrt word. */
12104 token = al;
12105
12106 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012107 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012108 al++;
12109 if (al == token)
12110 goto expect_comma;
12111
12112 /* Length of the token. */
12113 toklen = al - token;
12114 qvalue = 1000;
12115
12116 /* Check if the token exists in the list. If the token not exists,
12117 * jump to the next token.
12118 */
12119 str = args[0].data.str.str;
12120 w = str;
12121 while (1) {
12122 if (*str == ';' || *str == '\0') {
12123 if (language_range_match(token, toklen, w, str-w))
12124 goto look_for_q;
12125 if (*str == '\0')
12126 goto expect_comma;
12127 w = str + 1;
12128 }
12129 str++;
12130 }
12131 goto expect_comma;
12132
12133look_for_q:
12134
12135 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012136 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012137 al++;
12138 if (al >= end)
12139 goto process_value;
12140
12141 /* If ',' is found, process the result */
12142 if (*al == ',')
12143 goto process_value;
12144
12145 /* If the character is different from ';', look
12146 * for the end of the header part in best effort.
12147 */
12148 if (*al != ';')
12149 goto expect_comma;
12150
12151 /* Assumes that the char is ';', now expect "q=". */
12152 al++;
12153
12154 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012155 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012156 al++;
12157 if (al >= end)
12158 goto process_value;
12159
12160 /* Expect 'q'. If no 'q', continue in best effort */
12161 if (*al != 'q')
12162 goto process_value;
12163 al++;
12164
12165 /* Jump spaces, process value 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 /* Expect '='. If no '=', continue in best effort */
12172 if (*al != '=')
12173 goto process_value;
12174 al++;
12175
12176 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020012177 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020012178 al++;
12179 if (al >= end)
12180 goto process_value;
12181
12182 /* Parse the q value. */
12183 qvalue = parse_qvalue(al, &al);
12184
12185process_value:
12186
12187 /* If the new q value is the best q value, then store the associated
12188 * language in the response. If qvalue is the biggest value (1000),
12189 * break the process.
12190 */
12191 if (qvalue > best_q) {
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012192 smp->data.u.str.str = (char *)w;
12193 smp->data.u.str.len = str - w;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012194 if (qvalue >= 1000)
12195 break;
12196 best_q = qvalue;
12197 }
12198
12199expect_comma:
12200
12201 /* Expect comma or end. If the end is detected, quit the loop. */
12202 while (al < end && *al != ',')
12203 al++;
12204 if (al >= end)
12205 break;
12206
12207 /* Comma is found, jump it and restart the analyzer. */
12208 al++;
12209 }
12210
12211 /* Set default value if required. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012212 if (smp->data.u.str.len == 0 && args[1].type == ARGT_STR) {
12213 smp->data.u.str.str = args[1].data.str.str;
12214 smp->data.u.str.len = args[1].data.str.len;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012215 }
12216
12217 /* Return true only if a matching language was found. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012218 return smp->data.u.str.len != 0;
Thierry FOURNIERad903512014-04-11 17:51:01 +020012219}
12220
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012221/* This fetch url-decode any input string. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +020012222static int sample_conv_url_dec(const struct arg *args, struct sample *smp, void *private)
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012223{
12224 /* If the constant flag is set or if not size is avalaible at
12225 * the end of the buffer, copy the string in other buffer
12226 * before decoding.
12227 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012228 if (smp->flags & SMP_F_CONST || smp->data.u.str.size <= smp->data.u.str.len) {
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012229 struct chunk *str = get_trash_chunk();
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012230 memcpy(str->str, smp->data.u.str.str, smp->data.u.str.len);
12231 smp->data.u.str.str = str->str;
12232 smp->data.u.str.size = str->size;
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012233 smp->flags &= ~SMP_F_CONST;
12234 }
12235
12236 /* Add final \0 required by url_decode(), and convert the input string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012237 smp->data.u.str.str[smp->data.u.str.len] = '\0';
12238 smp->data.u.str.len = url_decode(smp->data.u.str.str);
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020012239 return 1;
12240}
12241
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012242static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void *private)
12243{
12244 struct proxy *fe = strm_fe(smp->strm);
12245 int idx, i;
12246 struct cap_hdr *hdr;
12247 int len;
12248
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012249 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012250 return 0;
12251
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012252 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012253
12254 /* Check the availibity of the capture id. */
12255 if (idx > fe->nb_req_cap - 1)
12256 return 0;
12257
12258 /* Look for the original configuration. */
12259 for (hdr = fe->req_cap, i = fe->nb_req_cap - 1;
12260 hdr != NULL && i != idx ;
12261 i--, hdr = hdr->next);
12262 if (!hdr)
12263 return 0;
12264
12265 /* check for the memory allocation */
12266 if (smp->strm->req_cap[hdr->index] == NULL)
12267 smp->strm->req_cap[hdr->index] = pool_alloc2(hdr->pool);
12268 if (smp->strm->req_cap[hdr->index] == NULL)
12269 return 0;
12270
12271 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012272 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012273 if (len > hdr->len)
12274 len = hdr->len;
12275
12276 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012277 memcpy(smp->strm->req_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012278 smp->strm->req_cap[idx][len] = '\0';
12279
12280 return 1;
12281}
12282
12283static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void *private)
12284{
12285 struct proxy *fe = strm_fe(smp->strm);
12286 int idx, i;
12287 struct cap_hdr *hdr;
12288 int len;
12289
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012290 if (!args || args->type != ARGT_SINT)
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012291 return 0;
12292
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020012293 idx = args->data.sint;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012294
12295 /* Check the availibity of the capture id. */
12296 if (idx > fe->nb_rsp_cap - 1)
12297 return 0;
12298
12299 /* Look for the original configuration. */
12300 for (hdr = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
12301 hdr != NULL && i != idx ;
12302 i--, hdr = hdr->next);
12303 if (!hdr)
12304 return 0;
12305
12306 /* check for the memory allocation */
12307 if (smp->strm->res_cap[hdr->index] == NULL)
12308 smp->strm->res_cap[hdr->index] = pool_alloc2(hdr->pool);
12309 if (smp->strm->res_cap[hdr->index] == NULL)
12310 return 0;
12311
12312 /* Check length. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012313 len = smp->data.u.str.len;
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012314 if (len > hdr->len)
12315 len = hdr->len;
12316
12317 /* Capture input data. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012318 memcpy(smp->strm->res_cap[idx], smp->data.u.str.str, len);
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020012319 smp->strm->res_cap[idx][len] = '\0';
12320
12321 return 1;
12322}
12323
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012324/* This function executes one of the set-{method,path,query,uri} actions. It
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012325 * takes the string from the variable 'replace' with length 'len', then modifies
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012326 * the relevant part of the request line accordingly. Then it updates various
12327 * pointers to the next elements which were moved, and the total buffer length.
12328 * It finds the action to be performed in p[2], previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012329 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
12330 * error, though this can be revisited when this code is finally exploited.
12331 *
12332 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
12333 * query string and 3 to replace uri.
12334 *
12335 * In query string case, the mark question '?' must be set at the start of the
12336 * string by the caller, event if the replacement query string is empty.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012337 */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012338int http_replace_req_line(int action, const char *replace, int len,
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012339 struct proxy *px, struct stream *s)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012340{
Willy Tarreau987e3fb2015-04-04 01:09:08 +020012341 struct http_txn *txn = s->txn;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012342 char *cur_ptr, *cur_end;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012343 int offset = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012344 int delta;
12345
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012346 switch (action) {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012347 case 0: // method
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012348 cur_ptr = s->req.buf->p;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012349 cur_end = cur_ptr + txn->req.sl.rq.m_l;
12350
12351 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012352 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012353 txn->req.sl.rq.m_l += delta;
12354 txn->req.sl.rq.u += delta;
12355 txn->req.sl.rq.v += delta;
12356 break;
12357
12358 case 1: // path
12359 cur_ptr = http_get_path(txn);
12360 if (!cur_ptr)
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012361 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012362
12363 cur_end = cur_ptr;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012364 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 +010012365 cur_end++;
12366
12367 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012368 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012369 txn->req.sl.rq.u_l += delta;
12370 txn->req.sl.rq.v += delta;
12371 break;
12372
12373 case 2: // query
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012374 offset = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012375 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012376 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12377 while (cur_ptr < cur_end && *cur_ptr != '?')
12378 cur_ptr++;
12379
12380 /* skip the question mark or indicate that we must insert it
12381 * (but only if the format string is not empty then).
12382 */
12383 if (cur_ptr < cur_end)
12384 cur_ptr++;
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012385 else if (len > 1)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012386 offset = 0;
12387
12388 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012389 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012390 txn->req.sl.rq.u_l += delta;
12391 txn->req.sl.rq.v += delta;
12392 break;
12393
12394 case 3: // uri
Willy Tarreau22ec1ea2014-11-27 20:45:39 +010012395 cur_ptr = s->req.buf->p + txn->req.sl.rq.u;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012396 cur_end = cur_ptr + txn->req.sl.rq.u_l;
12397
12398 /* adjust req line offsets and lengths */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012399 delta = len - offset - (cur_end - cur_ptr);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012400 txn->req.sl.rq.u_l += delta;
12401 txn->req.sl.rq.v += delta;
12402 break;
12403
12404 default:
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012405 return -1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012406 }
12407
12408 /* commit changes and adjust end of message */
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012409 delta = buffer_replace2(s->req.buf, cur_ptr, cur_end, replace + offset, len - offset);
Thierry FOURNIER7f6192c2015-04-26 18:01:40 +020012410 txn->req.sl.rq.l += delta;
12411 txn->hdr_idx.v[0].len += delta;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012412 http_msg_move_end(&txn->req, delta);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012413 return 0;
12414}
12415
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012416/* This function replace the HTTP status code and the associated message. The
12417 * variable <status> contains the new status code. This function never fails.
12418 */
12419void http_set_status(unsigned int status, struct stream *s)
12420{
12421 struct http_txn *txn = s->txn;
12422 char *cur_ptr, *cur_end;
12423 int delta;
12424 char *res;
12425 int c_l;
12426 const char *msg;
12427 int msg_len;
12428
12429 chunk_reset(&trash);
12430
12431 res = ultoa_o(status, trash.str, trash.size);
12432 c_l = res - trash.str;
12433
12434 trash.str[c_l] = ' ';
12435 trash.len = c_l + 1;
12436
12437 msg = get_reason(status);
12438 msg_len = strlen(msg);
12439
12440 strncpy(&trash.str[trash.len], msg, trash.size - trash.len);
12441 trash.len += msg_len;
12442
12443 cur_ptr = s->res.buf->p + txn->rsp.sl.st.c;
12444 cur_end = s->res.buf->p + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l;
12445
12446 /* commit changes and adjust message */
12447 delta = buffer_replace2(s->res.buf, cur_ptr, cur_end, trash.str, trash.len);
12448
12449 /* adjust res line offsets and lengths */
12450 txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l;
12451 txn->rsp.sl.st.c_l = c_l;
12452 txn->rsp.sl.st.r_l = msg_len;
12453
12454 delta = trash.len - (cur_end - cur_ptr);
12455 txn->rsp.sl.st.l += delta;
12456 txn->hdr_idx.v[0].len += delta;
12457 http_msg_move_end(&txn->rsp, delta);
12458}
12459
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012460/* This function executes one of the set-{method,path,query,uri} actions. It
12461 * builds a string in the trash from the specified format string. It finds
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012462 * the action to be performed in <http.action>, previously filled by function
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012463 * parse_set_req_line(). The replacement action is excuted by the function
Thierry FOURNIER3f4bc652015-08-26 16:23:34 +020012464 * http_action_set_req_line(). It always returns ACT_RET_CONT. If an error
12465 * occurs the action is canceled, but the rule processing continue.
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012466 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012467enum act_return http_action_set_req_line(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012468 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012469{
12470 chunk_reset(&trash);
12471
12472 /* If we have to create a query string, prepare a '?'. */
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012473 if (rule->arg.http.action == 2)
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012474 trash.str[trash.len++] = '?';
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012475 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.http.logfmt);
Thierry FOURNIERb77aece2015-03-14 13:55:46 +010012476
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012477 http_replace_req_line(rule->arg.http.action, trash.str, trash.len, px, s);
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012478 return ACT_RET_CONT;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012479}
12480
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012481/* This function is just a compliant action wrapper for "set-status". */
12482enum act_return action_http_set_status(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012483 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012484{
12485 http_set_status(rule->arg.status.code, s);
12486 return ACT_RET_CONT;
12487}
12488
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012489/* parse an http-request action among :
12490 * set-method
12491 * set-path
12492 * set-query
12493 * set-uri
12494 *
12495 * All of them accept a single argument of type string representing a log-format.
12496 * The resulting rule makes use of arg->act.p[0..1] to store the log-format list
12497 * 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 +020012498 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012499 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012500enum act_parse_ret parse_set_req_line(const char **args, int *orig_arg, struct proxy *px,
12501 struct act_rule *rule, char **err)
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012502{
12503 int cur_arg = *orig_arg;
12504
Thierry FOURNIER42148732015-09-02 17:17:33 +020012505 rule->action = ACT_CUSTOM;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012506
12507 switch (args[0][4]) {
12508 case 'm' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012509 rule->arg.http.action = 0;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012510 rule->action_ptr = http_action_set_req_line;
12511 break;
12512 case 'p' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012513 rule->arg.http.action = 1;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012514 rule->action_ptr = http_action_set_req_line;
12515 break;
12516 case 'q' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012517 rule->arg.http.action = 2;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012518 rule->action_ptr = http_action_set_req_line;
12519 break;
12520 case 'u' :
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012521 rule->arg.http.action = 3;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012522 rule->action_ptr = http_action_set_req_line;
12523 break;
12524 default:
12525 memprintf(err, "internal error: unhandled action '%s'", args[0]);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012526 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012527 }
12528
12529 if (!*args[cur_arg] ||
12530 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
12531 memprintf(err, "expects exactly 1 argument <format>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012532 return ACT_RET_PRS_ERR;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012533 }
12534
Thierry FOURNIER8855a922015-07-31 08:54:25 +020012535 LIST_INIT(&rule->arg.http.logfmt);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012536 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIER / OZON.IO59fd5112016-11-22 23:50:02 +010012537 if (!parse_logformat_string(args[cur_arg], proxy, &rule->arg.http.logfmt, LOG_OPT_HTTP,
Thierry FOURNIER / OZON.IO8a4e4422016-11-23 00:41:28 +010012538 (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 +010012539 return ACT_RET_PRS_ERR;
12540 }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012541
12542 (*orig_arg)++;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012543 return ACT_RET_PRS_OK;
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010012544}
12545
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012546/* parse set-status action:
12547 * This action accepts a single argument of type int representing
12548 * an http status code. It returns ACT_RET_PRS_OK on success,
12549 * ACT_RET_PRS_ERR on error.
12550 */
12551enum act_parse_ret parse_http_set_status(const char **args, int *orig_arg, struct proxy *px,
12552 struct act_rule *rule, char **err)
12553{
12554 char *error;
12555
Thierry FOURNIER42148732015-09-02 17:17:33 +020012556 rule->action = ACT_CUSTOM;
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020012557 rule->action_ptr = action_http_set_status;
12558
12559 /* Check if an argument is available */
12560 if (!*args[*orig_arg]) {
12561 memprintf(err, "expects exactly 1 argument <status>");
12562 return ACT_RET_PRS_ERR;
12563 }
12564
12565 /* convert status code as integer */
12566 rule->arg.status.code = strtol(args[*orig_arg], &error, 10);
12567 if (*error != '\0' || rule->arg.status.code < 100 || rule->arg.status.code > 999) {
12568 memprintf(err, "expects an integer status code between 100 and 999");
12569 return ACT_RET_PRS_ERR;
12570 }
12571
12572 (*orig_arg)++;
12573 return ACT_RET_PRS_OK;
12574}
12575
Willy Tarreaua9083d02015-05-08 15:27:59 +020012576/* This function executes the "capture" action. It executes a fetch expression,
12577 * turns the result into a string and puts it in a capture slot. It always
12578 * returns 1. If an error occurs the action is cancelled, but the rule
12579 * processing continues.
12580 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012581enum act_return http_action_req_capture(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012582 struct session *sess, struct stream *s, int flags)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012583{
Willy Tarreaua9083d02015-05-08 15:27:59 +020012584 struct sample *key;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012585 struct cap_hdr *h = rule->arg.cap.hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012586 char **cap = s->req_cap;
12587 int len;
12588
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012589 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 +020012590 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012591 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012592
12593 if (cap[h->index] == NULL)
12594 cap[h->index] = pool_alloc2(h->pool);
12595
12596 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012597 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012598
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012599 len = key->data.u.str.len;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012600 if (len > h->len)
12601 len = h->len;
12602
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012603 memcpy(cap[h->index], key->data.u.str.str, len);
Willy Tarreaua9083d02015-05-08 15:27:59 +020012604 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012605 return ACT_RET_CONT;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012606}
12607
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012608/* This function executes the "capture" action and store the result in a
12609 * capture slot if exists. It executes a fetch expression, turns the result
12610 * into a string and puts it in a capture slot. It always returns 1. If an
12611 * error occurs the action is cancelled, but the rule processing continues.
12612 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012613enum act_return http_action_req_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012614 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012615{
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012616 struct sample *key;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012617 struct cap_hdr *h;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012618 char **cap = s->req_cap;
12619 struct proxy *fe = strm_fe(s);
12620 int len;
12621 int i;
12622
12623 /* Look for the original configuration. */
12624 for (h = fe->req_cap, i = fe->nb_req_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012625 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012626 i--, h = h->next);
12627 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012628 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012629
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012630 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 +020012631 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012632 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012633
12634 if (cap[h->index] == NULL)
12635 cap[h->index] = pool_alloc2(h->pool);
12636
12637 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012638 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012639
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012640 len = key->data.u.str.len;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012641 if (len > h->len)
12642 len = h->len;
12643
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012644 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012645 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012646 return ACT_RET_CONT;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012647}
12648
Willy Tarreaua9083d02015-05-08 15:27:59 +020012649/* parse an "http-request capture" action. It takes a single argument which is
12650 * a sample fetch expression. It stores the expression into arg->act.p[0] and
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012651 * the allocated hdr_cap struct or the preallocated "id" into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012652 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Willy Tarreaua9083d02015-05-08 15:27:59 +020012653 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012654enum act_parse_ret parse_http_req_capture(const char **args, int *orig_arg, struct proxy *px,
12655 struct act_rule *rule, char **err)
Willy Tarreaua9083d02015-05-08 15:27:59 +020012656{
12657 struct sample_expr *expr;
12658 struct cap_hdr *hdr;
12659 int cur_arg;
Willy Tarreau3986ac12015-05-08 16:13:42 +020012660 int len = 0;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012661
12662 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12663 if (strcmp(args[cur_arg], "if") == 0 ||
12664 strcmp(args[cur_arg], "unless") == 0)
12665 break;
12666
12667 if (cur_arg < *orig_arg + 3) {
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012668 memprintf(err, "expects <expression> [ 'len' <length> | id <idx> ]");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012669 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012670 }
12671
Willy Tarreaua9083d02015-05-08 15:27:59 +020012672 cur_arg = *orig_arg;
12673 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12674 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012675 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012676
12677 if (!(expr->fetch->val & SMP_VAL_FE_HRQ_HDR)) {
12678 memprintf(err,
12679 "fetch method '%s' extracts information from '%s', none of which is available here",
12680 args[cur_arg-1], sample_src_names(expr->fetch->use));
12681 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012682 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012683 }
12684
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012685 if (!args[cur_arg] || !*args[cur_arg]) {
12686 memprintf(err, "expects 'len or 'id'");
12687 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012688 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012689 }
12690
Willy Tarreaua9083d02015-05-08 15:27:59 +020012691 if (strcmp(args[cur_arg], "len") == 0) {
12692 cur_arg++;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012693
12694 if (!(px->cap & PR_CAP_FE)) {
12695 memprintf(err, "proxy '%s' has no frontend capability", px->id);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012696 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012697 }
12698
12699 proxy->conf.args.ctx = ARGC_CAP;
12700
Willy Tarreaua9083d02015-05-08 15:27:59 +020012701 if (!args[cur_arg]) {
12702 memprintf(err, "missing length value");
12703 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012704 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012705 }
12706 /* we copy the table name for now, it will be resolved later */
12707 len = atoi(args[cur_arg]);
12708 if (len <= 0) {
12709 memprintf(err, "length must be > 0");
12710 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012711 return ACT_RET_PRS_ERR;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012712 }
12713 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012714
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012715 if (!len) {
12716 memprintf(err, "a positive 'len' argument is mandatory");
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
Vincent Bernat02779b62016-04-03 13:48:43 +020012721 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012722 hdr->next = px->req_cap;
12723 hdr->name = NULL; /* not a header capture */
12724 hdr->namelen = 0;
12725 hdr->len = len;
12726 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
12727 hdr->index = px->nb_req_cap++;
12728
12729 px->req_cap = hdr;
12730 px->to_log |= LW_REQHDR;
12731
Thierry FOURNIER42148732015-09-02 17:17:33 +020012732 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012733 rule->action_ptr = http_action_req_capture;
Thierry FOURNIER32b15002015-07-31 08:56:16 +020012734 rule->arg.cap.expr = expr;
12735 rule->arg.cap.hdr = hdr;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012736 }
12737
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012738 else if (strcmp(args[cur_arg], "id") == 0) {
12739 int id;
12740 char *error;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012741
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012742 cur_arg++;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012743
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012744 if (!args[cur_arg]) {
12745 memprintf(err, "missing id value");
12746 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012747 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012748 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012749
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012750 id = strtol(args[cur_arg], &error, 10);
12751 if (*error != '\0') {
12752 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12753 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012754 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012755 }
12756 cur_arg++;
12757
12758 proxy->conf.args.ctx = ARGC_CAP;
12759
Thierry FOURNIER42148732015-09-02 17:17:33 +020012760 rule->action = ACT_CUSTOM;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012761 rule->action_ptr = http_action_req_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012762 rule->arg.capid.expr = expr;
12763 rule->arg.capid.idx = id;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012764 }
12765
12766 else {
12767 memprintf(err, "expects 'len' or 'id', found '%s'", args[cur_arg]);
12768 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012769 return ACT_RET_PRS_ERR;
Thierry FOURNIER82bf70d2015-05-26 17:58:29 +020012770 }
Willy Tarreaua9083d02015-05-08 15:27:59 +020012771
12772 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012773 return ACT_RET_PRS_OK;
Willy Tarreaua9083d02015-05-08 15:27:59 +020012774}
12775
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012776/* This function executes the "capture" action and store the result in a
12777 * capture slot if exists. It executes a fetch expression, turns the result
12778 * into a string and puts it in a capture slot. It always returns 1. If an
12779 * error occurs the action is cancelled, but the rule processing continues.
12780 */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012781enum act_return http_action_res_capture_by_id(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +020012782 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012783{
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012784 struct sample *key;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012785 struct cap_hdr *h;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012786 char **cap = s->res_cap;
12787 struct proxy *fe = strm_fe(s);
12788 int len;
12789 int i;
12790
12791 /* Look for the original configuration. */
12792 for (h = fe->rsp_cap, i = fe->nb_rsp_cap - 1;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012793 h != NULL && i != rule->arg.capid.idx ;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012794 i--, h = h->next);
12795 if (!h)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012796 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012797
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012798 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 +020012799 if (!key)
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012800 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012801
12802 if (cap[h->index] == NULL)
12803 cap[h->index] = pool_alloc2(h->pool);
12804
12805 if (cap[h->index] == NULL) /* no more capture memory */
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012806 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012807
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012808 len = key->data.u.str.len;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012809 if (len > h->len)
12810 len = h->len;
12811
Thierry FOURNIER136f9d32015-08-19 09:07:19 +020012812 memcpy(cap[h->index], key->data.u.str.str, len);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012813 cap[h->index][len] = 0;
Thierry FOURNIER24ff6c62015-08-06 08:52:53 +020012814 return ACT_RET_CONT;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012815}
12816
12817/* parse an "http-response capture" action. It takes a single argument which is
12818 * a sample fetch expression. It stores the expression into arg->act.p[0] and
12819 * the allocated hdr_cap struct od the preallocated id into arg->act.p[1].
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012820 * It returns ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error.
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012821 */
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012822enum act_parse_ret parse_http_res_capture(const char **args, int *orig_arg, struct proxy *px,
12823 struct act_rule *rule, char **err)
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012824{
12825 struct sample_expr *expr;
12826 int cur_arg;
12827 int id;
12828 char *error;
12829
12830 for (cur_arg = *orig_arg; cur_arg < *orig_arg + 3 && *args[cur_arg]; cur_arg++)
12831 if (strcmp(args[cur_arg], "if") == 0 ||
12832 strcmp(args[cur_arg], "unless") == 0)
12833 break;
12834
12835 if (cur_arg < *orig_arg + 3) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012836 memprintf(err, "expects <expression> id <idx>");
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012837 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012838 }
12839
12840 cur_arg = *orig_arg;
12841 expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args);
12842 if (!expr)
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012843 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012844
12845 if (!(expr->fetch->val & SMP_VAL_FE_HRS_HDR)) {
12846 memprintf(err,
12847 "fetch method '%s' extracts information from '%s', none of which is available here",
12848 args[cur_arg-1], sample_src_names(expr->fetch->use));
12849 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012850 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012851 }
12852
12853 if (!args[cur_arg] || !*args[cur_arg]) {
Willy Tarreau29bdb1c2016-06-24 15:36:34 +020012854 memprintf(err, "expects 'id'");
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012855 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012856 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012857 }
12858
12859 if (strcmp(args[cur_arg], "id") != 0) {
12860 memprintf(err, "expects 'id', found '%s'", args[cur_arg]);
12861 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012862 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012863 }
12864
12865 cur_arg++;
12866
12867 if (!args[cur_arg]) {
12868 memprintf(err, "missing id value");
12869 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012870 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012871 }
12872
12873 id = strtol(args[cur_arg], &error, 10);
12874 if (*error != '\0') {
12875 memprintf(err, "cannot parse id '%s'", args[cur_arg]);
12876 free(expr);
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012877 return ACT_RET_PRS_ERR;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012878 }
12879 cur_arg++;
12880
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012881 proxy->conf.args.ctx = ARGC_CAP;
12882
Thierry FOURNIER42148732015-09-02 17:17:33 +020012883 rule->action = ACT_CUSTOM;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012884 rule->action_ptr = http_action_res_capture_by_id;
Thierry FOURNIERe2097972015-07-31 08:56:35 +020012885 rule->arg.capid.expr = expr;
12886 rule->arg.capid.idx = id;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012887
12888 *orig_arg = cur_arg;
Thierry FOURNIERafa80492015-08-19 09:04:15 +020012889 return ACT_RET_PRS_OK;
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020012890}
12891
William Lallemand73025dd2014-04-24 14:38:37 +020012892/*
12893 * Return the struct http_req_action_kw associated to a keyword.
12894 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012895struct action_kw *action_http_req_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012896{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012897 return action_lookup(&http_req_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012898}
12899
12900/*
12901 * Return the struct http_res_action_kw associated to a keyword.
12902 */
Thierry FOURNIER36481b82015-08-19 09:01:53 +020012903struct action_kw *action_http_res_custom(const char *kw)
William Lallemand73025dd2014-04-24 14:38:37 +020012904{
Thierry FOURNIER322a1242015-08-19 09:07:47 +020012905 return action_lookup(&http_res_keywords.list, kw);
William Lallemand73025dd2014-04-24 14:38:37 +020012906}
12907
Willy Tarreau12207b32016-11-22 19:48:51 +010012908
12909/* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop
12910 * now.
12911 */
12912static int cli_parse_show_errors(char **args, struct appctx *appctx, void *private)
12913{
12914 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
12915 return 1;
12916
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012917 if (*args[2]) {
12918 struct proxy *px;
12919
12920 px = proxy_find_by_name(args[2], 0, 0);
12921 if (px)
12922 appctx->ctx.errors.iid = px->uuid;
12923 else
12924 appctx->ctx.errors.iid = atoi(args[2]);
12925
12926 if (!appctx->ctx.errors.iid) {
12927 appctx->ctx.cli.msg = "No such proxy.\n";
12928 appctx->st0 = CLI_ST_PRINT;
12929 return 1;
12930 }
12931 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012932 else
Willy Tarreau234ba2d2016-11-25 08:39:10 +010012933 appctx->ctx.errors.iid = -1; // dump all proxies
12934
Willy Tarreau35069f82016-11-25 09:16:37 +010012935 appctx->ctx.errors.flag = 0;
12936 if (strcmp(args[3], "request") == 0)
12937 appctx->ctx.errors.flag |= 4; // ignore response
12938 else if (strcmp(args[3], "response") == 0)
12939 appctx->ctx.errors.flag |= 2; // ignore request
Willy Tarreau12207b32016-11-22 19:48:51 +010012940 appctx->ctx.errors.px = NULL;
Willy Tarreau12207b32016-11-22 19:48:51 +010012941 return 0;
12942}
12943
12944/* This function dumps all captured errors onto the stream interface's
12945 * read buffer. It returns 0 if the output buffer is full and it needs
12946 * to be called again, otherwise non-zero.
12947 */
12948static int cli_io_handler_show_errors(struct appctx *appctx)
12949{
12950 struct stream_interface *si = appctx->owner;
12951 extern const char *monthname[12];
12952
12953 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
12954 return 1;
12955
12956 chunk_reset(&trash);
12957
12958 if (!appctx->ctx.errors.px) {
12959 /* the function had not been called yet, let's prepare the
12960 * buffer for a response.
12961 */
12962 struct tm tm;
12963
12964 get_localtime(date.tv_sec, &tm);
12965 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
12966 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
12967 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
12968 error_snapshot_id);
12969
12970 if (bi_putchk(si_ic(si), &trash) == -1) {
12971 /* Socket buffer full. Let's try again later from the same point */
12972 si_applet_cant_put(si);
12973 return 0;
12974 }
12975
12976 appctx->ctx.errors.px = proxy;
Willy Tarreau12207b32016-11-22 19:48:51 +010012977 appctx->ctx.errors.bol = 0;
12978 appctx->ctx.errors.ptr = -1;
12979 }
12980
12981 /* we have two inner loops here, one for the proxy, the other one for
12982 * the buffer.
12983 */
12984 while (appctx->ctx.errors.px) {
12985 struct error_snapshot *es;
12986
Willy Tarreau35069f82016-11-25 09:16:37 +010012987 if ((appctx->ctx.errors.flag & 1) == 0) {
Willy Tarreau12207b32016-11-22 19:48:51 +010012988 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau35069f82016-11-25 09:16:37 +010012989 if (appctx->ctx.errors.flag & 2) // skip req
12990 goto next;
12991 }
12992 else {
Willy Tarreau12207b32016-11-22 19:48:51 +010012993 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau35069f82016-11-25 09:16:37 +010012994 if (appctx->ctx.errors.flag & 4) // skip resp
12995 goto next;
12996 }
Willy Tarreau12207b32016-11-22 19:48:51 +010012997
12998 if (!es->when.tv_sec)
12999 goto next;
13000
13001 if (appctx->ctx.errors.iid >= 0 &&
13002 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
13003 es->oe->uuid != appctx->ctx.errors.iid)
13004 goto next;
13005
13006 if (appctx->ctx.errors.ptr < 0) {
13007 /* just print headers now */
13008
13009 char pn[INET6_ADDRSTRLEN];
13010 struct tm tm;
13011 int port;
13012
13013 get_localtime(es->when.tv_sec, &tm);
13014 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
13015 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
13016 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
13017
13018 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
13019 case AF_INET:
13020 case AF_INET6:
13021 port = get_host_port(&es->src);
13022 break;
13023 default:
13024 port = 0;
13025 }
13026
Willy Tarreau35069f82016-11-25 09:16:37 +010013027 switch (appctx->ctx.errors.flag & 1) {
Willy Tarreau12207b32016-11-22 19:48:51 +010013028 case 0:
13029 chunk_appendf(&trash,
13030 " frontend %s (#%d): invalid request\n"
13031 " backend %s (#%d)",
13032 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
13033 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
13034 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
13035 break;
13036 case 1:
13037 chunk_appendf(&trash,
13038 " backend %s (#%d): invalid response\n"
13039 " frontend %s (#%d)",
13040 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
13041 es->oe->id, es->oe->uuid);
13042 break;
13043 }
13044
13045 chunk_appendf(&trash,
13046 ", server %s (#%d), event #%u\n"
13047 " src %s:%d, session #%d, session flags 0x%08x\n"
13048 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
13049 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
13050 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
13051 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
13052 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
13053 es->ev_id,
13054 pn, port, es->sid, es->s_flags,
13055 es->state, es->m_flags, es->t_flags,
13056 es->m_clen, es->m_blen,
13057 es->b_flags, es->b_out, es->b_tot,
13058 es->len, es->b_wrap, es->pos);
13059
13060 if (bi_putchk(si_ic(si), &trash) == -1) {
13061 /* Socket buffer full. Let's try again later from the same point */
13062 si_applet_cant_put(si);
13063 return 0;
13064 }
13065 appctx->ctx.errors.ptr = 0;
13066 appctx->ctx.errors.sid = es->sid;
13067 }
13068
13069 if (appctx->ctx.errors.sid != es->sid) {
13070 /* the snapshot changed while we were dumping it */
13071 chunk_appendf(&trash,
13072 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
13073 if (bi_putchk(si_ic(si), &trash) == -1) {
13074 si_applet_cant_put(si);
13075 return 0;
13076 }
13077 goto next;
13078 }
13079
13080 /* OK, ptr >= 0, so we have to dump the current line */
13081 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
13082 int newptr;
13083 int newline;
13084
13085 newline = appctx->ctx.errors.bol;
13086 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
13087 if (newptr == appctx->ctx.errors.ptr)
13088 return 0;
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 = newptr;
13096 appctx->ctx.errors.bol = newline;
13097 };
13098 next:
13099 appctx->ctx.errors.bol = 0;
13100 appctx->ctx.errors.ptr = -1;
Willy Tarreau35069f82016-11-25 09:16:37 +010013101 appctx->ctx.errors.flag ^= 1;
13102 if (!(appctx->ctx.errors.flag & 1))
Willy Tarreau12207b32016-11-22 19:48:51 +010013103 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau12207b32016-11-22 19:48:51 +010013104 }
13105
13106 /* dump complete */
13107 return 1;
13108}
13109
13110/* register cli keywords */
13111static struct cli_kw_list cli_kws = {{ },{
13112 { { "show", "errors", NULL },
13113 "show errors : report last request and response errors for each proxy",
13114 cli_parse_show_errors, cli_io_handler_show_errors, NULL,
13115 },
13116 {{},}
13117}};
13118
Willy Tarreau4a568972010-05-12 08:08:50 +020013119/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013120/* All supported ACL keywords must be declared here. */
13121/************************************************************************/
13122
13123/* Note: must not be declared <const> as its list will be overwritten.
13124 * Please take care of keeping this list alphabetically sorted.
13125 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020013126static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013127 { "base", "base", PAT_MATCH_STR },
13128 { "base_beg", "base", PAT_MATCH_BEG },
13129 { "base_dir", "base", PAT_MATCH_DIR },
13130 { "base_dom", "base", PAT_MATCH_DOM },
13131 { "base_end", "base", PAT_MATCH_END },
13132 { "base_len", "base", PAT_MATCH_LEN },
13133 { "base_reg", "base", PAT_MATCH_REG },
13134 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020013135
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013136 { "cook", "req.cook", PAT_MATCH_STR },
13137 { "cook_beg", "req.cook", PAT_MATCH_BEG },
13138 { "cook_dir", "req.cook", PAT_MATCH_DIR },
13139 { "cook_dom", "req.cook", PAT_MATCH_DOM },
13140 { "cook_end", "req.cook", PAT_MATCH_END },
13141 { "cook_len", "req.cook", PAT_MATCH_LEN },
13142 { "cook_reg", "req.cook", PAT_MATCH_REG },
13143 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013144
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013145 { "hdr", "req.hdr", PAT_MATCH_STR },
13146 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
13147 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
13148 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
13149 { "hdr_end", "req.hdr", PAT_MATCH_END },
13150 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
13151 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
13152 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013153
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013154 /* these two declarations uses strings with list storage (in place
13155 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
13156 * and delete functions are relative to the list management. The parse
13157 * and match method are related to the corresponding fetch methods. This
13158 * is very particular ACL declaration mode.
13159 */
13160 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
13161 { "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 +020013162
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013163 { "path", "path", PAT_MATCH_STR },
13164 { "path_beg", "path", PAT_MATCH_BEG },
13165 { "path_dir", "path", PAT_MATCH_DIR },
13166 { "path_dom", "path", PAT_MATCH_DOM },
13167 { "path_end", "path", PAT_MATCH_END },
13168 { "path_len", "path", PAT_MATCH_LEN },
13169 { "path_reg", "path", PAT_MATCH_REG },
13170 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013171
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013172 { "req_ver", "req.ver", PAT_MATCH_STR },
13173 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013174
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013175 { "scook", "res.cook", PAT_MATCH_STR },
13176 { "scook_beg", "res.cook", PAT_MATCH_BEG },
13177 { "scook_dir", "res.cook", PAT_MATCH_DIR },
13178 { "scook_dom", "res.cook", PAT_MATCH_DOM },
13179 { "scook_end", "res.cook", PAT_MATCH_END },
13180 { "scook_len", "res.cook", PAT_MATCH_LEN },
13181 { "scook_reg", "res.cook", PAT_MATCH_REG },
13182 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013183
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013184 { "shdr", "res.hdr", PAT_MATCH_STR },
13185 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
13186 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
13187 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
13188 { "shdr_end", "res.hdr", PAT_MATCH_END },
13189 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
13190 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
13191 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013192
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013193 { "url", "url", PAT_MATCH_STR },
13194 { "url_beg", "url", PAT_MATCH_BEG },
13195 { "url_dir", "url", PAT_MATCH_DIR },
13196 { "url_dom", "url", PAT_MATCH_DOM },
13197 { "url_end", "url", PAT_MATCH_END },
13198 { "url_len", "url", PAT_MATCH_LEN },
13199 { "url_reg", "url", PAT_MATCH_REG },
13200 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013201
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010013202 { "urlp", "urlp", PAT_MATCH_STR },
13203 { "urlp_beg", "urlp", PAT_MATCH_BEG },
13204 { "urlp_dir", "urlp", PAT_MATCH_DIR },
13205 { "urlp_dom", "urlp", PAT_MATCH_DOM },
13206 { "urlp_end", "urlp", PAT_MATCH_END },
13207 { "urlp_len", "urlp", PAT_MATCH_LEN },
13208 { "urlp_reg", "urlp", PAT_MATCH_REG },
13209 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013210
Willy Tarreau8ed669b2013-01-11 15:49:37 +010013211 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020013212}};
13213
13214/************************************************************************/
13215/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020013216/************************************************************************/
13217/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020013218static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013219 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013220 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013221 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
13222
Willy Tarreau87b09662015-04-03 00:22:06 +020013223 /* capture are allocated and are permanent in the stream */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013224 { "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 +020013225
13226 /* retrieve these captures from the HTTP logs */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013227 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
13228 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
13229 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020013230
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013231 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
13232 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010013233
Willy Tarreau409bcde2013-01-08 00:31:00 +010013234 /* cookie is valid in both directions (eg: for "stick ...") but cook*
13235 * are only here to match the ACL's name, are request-only and are used
13236 * for ACL compatibility only.
13237 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013238 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13239 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013240 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
13241 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013242
13243 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
13244 * only here to match the ACL's name, are request-only and are used for
13245 * ACL compatibility only.
13246 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013247 { "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 +020013248 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013249 { "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 +020013250 { "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 +010013251
Willy Tarreau0a0daec2013-04-02 22:44:58 +020013252 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013253 { "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 +010013254 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010013255 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013256 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau49ad95c2015-01-19 15:06:26 +010013257 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013258
13259 /* HTTP protocol on the request path */
13260 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013261 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013262
13263 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013264 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
13265 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013266
Willy Tarreaua5910cc2015-05-02 00:46:08 +020013267 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013268 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
13269 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIERe28c4992015-05-19 14:45:09 +020013270 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreaua5910cc2015-05-02 00:46:08 +020013271
Willy Tarreau18ed2562013-01-14 15:56:36 +010013272 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013273 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
13274 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013275
Willy Tarreau18ed2562013-01-14 15:56:36 +010013276 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013277 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013278 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
13279 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013280
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013281 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013282 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013283 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013284 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013285 { "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 +010013286 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013287 { "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 +010013288
13289 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013290 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013291 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13292 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013293
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013294 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013295 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013296 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013297 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010013298 { "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 +010013299 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013300 { "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 +010013301
Willy Tarreau409bcde2013-01-08 00:31:00 +010013302 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013303 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013304 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
13305 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013306 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010013307
13308 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013309 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013310 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013311 { "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 +020013312 { "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 +010013313
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013314 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
Thierry Fournier0e00dca2016-04-07 15:47:40 +020013315 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010013316 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013317 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000013318 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010013319 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013320 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Willy Tarreau1ede1da2015-05-07 16:06:18 +020013321 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
13322 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013323 { "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 +010013324 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020013325}};
13326
Willy Tarreau8797c062007-05-07 00:55:35 +020013327
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013328/************************************************************************/
13329/* All supported converter keywords must be declared here. */
13330/************************************************************************/
Willy Tarreau276fae92013-07-25 14:36:01 +020013331/* Note: must not be declared <const> as its list will be overwritten */
13332static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020013333 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_T_STR},
Thierry FOURNIERad903512014-04-11 17:51:01 +020013334 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020013335 { "capture-req", smp_conv_req_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
13336 { "capture-res", smp_conv_res_capture, ARG1(1,SINT), NULL, SMP_T_STR, SMP_T_STR},
Thierry FOURNIER82ff3c92015-05-07 15:46:20 +020013337 { "url_dec", sample_conv_url_dec, 0, NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020013338 { NULL, NULL, 0, 0, 0 },
13339}};
13340
Thierry FOURNIER35ab2752015-05-28 13:22:03 +020013341
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013342/************************************************************************/
13343/* All supported http-request action keywords must be declared here. */
13344/************************************************************************/
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013345struct action_kw_list http_req_actions = {
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013346 .kw = {
Willy Tarreaua9083d02015-05-08 15:27:59 +020013347 { "capture", parse_http_req_capture },
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013348 { "set-method", parse_set_req_line },
13349 { "set-path", parse_set_req_line },
13350 { "set-query", parse_set_req_line },
13351 { "set-uri", parse_set_req_line },
Willy Tarreaucb703b02015-04-03 09:52:01 +020013352 { NULL, NULL }
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013353 }
13354};
13355
Thierry FOURNIER36481b82015-08-19 09:01:53 +020013356struct action_kw_list http_res_actions = {
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013357 .kw = {
13358 { "capture", parse_http_res_capture },
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +020013359 { "set-status", parse_http_set_status },
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013360 { NULL, NULL }
13361 }
13362};
13363
Willy Tarreau8797c062007-05-07 00:55:35 +020013364__attribute__((constructor))
13365static void __http_protocol_init(void)
13366{
13367 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020013368 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020013369 sample_register_convs(&sample_conv_kws);
Willy Tarreaua0dc23f2015-01-22 20:46:11 +010013370 http_req_keywords_register(&http_req_actions);
Thierry FOURNIERe80fada2015-05-26 18:06:31 +020013371 http_res_keywords_register(&http_res_actions);
Willy Tarreau12207b32016-11-22 19:48:51 +010013372 cli_register_kw(&cli_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020013373}
13374
13375
Willy Tarreau58f10d72006-12-04 02:26:12 +010013376/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020013377 * Local variables:
13378 * c-indent-level: 8
13379 * c-basic-offset: 8
13380 * End:
13381 */